vigigraph / vigigraph / lib / graphs.py @ 54de53e3
History | View | Annotate | Download (4.97 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""Functions for graphs"""
|
3 |
|
4 |
from tg import config |
5 |
|
6 |
import string |
7 |
import urllib |
8 |
import urllib2 |
9 |
from pylons.i18n import ugettext as _ |
10 |
|
11 |
from time import gmtime, strftime |
12 |
from datetime import datetime |
13 |
|
14 |
|
15 |
def graphsList(**kwargs): |
16 |
"""
|
17 |
Page liste des graphes
|
18 |
|
19 |
@param **kwargs : arguments nommes
|
20 |
@type **kwargs : dict
|
21 |
|
22 |
"""
|
23 |
graphslist = [] |
24 |
|
25 |
if kwargs is not None: |
26 |
format = "%d-%m-%Y %H:%M"
|
27 |
for key in kwargs: |
28 |
# titre
|
29 |
title = "Inconnu"
|
30 |
graph = ""
|
31 |
server = ""
|
32 |
# recherche arguments (apres ?) -> cle1=valeur1&cle2=valeur2&...
|
33 |
lca = kwargs[key].split("?")
|
34 |
if len(lca) == 2: |
35 |
# analyse de chacun des arguments -> cle=valeur
|
36 |
largs = lca[1].split("&") |
37 |
for arg in largs: |
38 |
larg = arg.split("=")
|
39 |
if len(larg) == 2: |
40 |
if larg[0] == "server": |
41 |
server = larg[1]
|
42 |
elif larg[0] == "graphtemplate": |
43 |
graph = larg[1]
|
44 |
elif larg[0] == "start": |
45 |
start = larg[1]
|
46 |
elif larg[0] == "duration": |
47 |
duration = larg[1]
|
48 |
if graph != "" or server != "": |
49 |
title = "'%s' Graph for host %s" % \
|
50 |
(urllib.unquote_plus(graph), server) |
51 |
graph = {} |
52 |
graph['title'] = title
|
53 |
v = int(start)
|
54 |
graph['sts'] = _(strftime(format, gmtime(v)))
|
55 |
v = int(start) + int(duration) |
56 |
graph['ets'] = _(strftime(format, gmtime(v)))
|
57 |
graph['src'] = urllib2.unquote(kwargs[key])
|
58 |
graphslist.append(graph) |
59 |
|
60 |
return graphslist
|
61 |
|
62 |
def tempoDelayRefresh(): |
63 |
"""
|
64 |
Lecture de la temporisation pour le rafraichissement automatique
|
65 |
dans le fichier de configuration development.ini
|
66 |
(valeur par defaut = 30000)
|
67 |
|
68 |
@return : valeur de temporisation (en millisecondes)
|
69 |
@rtype : C{str}
|
70 |
"""
|
71 |
|
72 |
delay = config.get('delay_refresh')
|
73 |
delay = string.strip(delay) |
74 |
|
75 |
b_evaluate = False
|
76 |
if delay == '': |
77 |
b_evaluate = True
|
78 |
else:
|
79 |
if delay.isalnum():
|
80 |
delay_l = int(delay)
|
81 |
b_evaluate = (delay_l <= 0)
|
82 |
else:
|
83 |
b_evaluate = True
|
84 |
|
85 |
if b_evaluate:
|
86 |
delay = '30000'
|
87 |
|
88 |
return delay
|
89 |
|
90 |
def getExportFileName(host, indicator_f, start, end): |
91 |
"""
|
92 |
Determination nom fichier pour export
|
93 |
-> <hote>_<indicator_f>_<date_heure_debut>_<date_heure_fin>
|
94 |
avec format <date_heure_...> = AAMMJJ-hhmmss
|
95 |
|
96 |
@param host : hôte
|
97 |
@type host : C{str}
|
98 |
@param graph : graphe
|
99 |
@type graph : C{str}
|
100 |
@param indicator_f : indicateur graphe ( nom du graphe ou d un des indicateurs)
|
101 |
@type indicator_f : C{str}
|
102 |
@param start : date-heure de debut des donnees
|
103 |
@type start : C{str}
|
104 |
|
105 |
@return : nom du fichier
|
106 |
@rtype : C{str}
|
107 |
"""
|
108 |
|
109 |
# plage temps sous forme texte
|
110 |
format = '%Y%m%d-%H%M%S'
|
111 |
|
112 |
dt = datetime.utcfromtimestamp(int(start))
|
113 |
str_start = dt.strftime(format) |
114 |
|
115 |
dt = datetime.utcfromtimestamp(int(end))
|
116 |
str_end = dt.strftime(format) |
117 |
|
118 |
# nom fichier
|
119 |
filename = '%s_%s_%s_%s' % (host, indicator_f, str_start, str_end)
|
120 |
|
121 |
# remplacement caracteres particuliers
|
122 |
lc = [' ', '|', '/', '\\', ':', '?', '*', '<', '>', '"'] |
123 |
for c in lc: |
124 |
filename = filename.replace(c, "_")
|
125 |
|
126 |
# extension
|
127 |
filename += ".csv"
|
128 |
|
129 |
return filename
|
130 |
|
131 |
def setExportFile(writer, dict_values, dict_indicators, sep_value): |
132 |
"""
|
133 |
Ecriture des donnees sous forme texte dans le fichier d export
|
134 |
(les donnees se rapportent aux indicateurs passes en parametre)
|
135 |
|
136 |
@param writer : gestion csv pour lecture/ecriture
|
137 |
@type writer : csv.DictWriter (voir Python)
|
138 |
@param dict_values : valeurs
|
139 |
@type dict_values : dict
|
140 |
@param dict_indicators : indicateurs
|
141 |
@type dict_indicators : dict
|
142 |
@param sep_value : separateur partie entiere - partie decimale
|
143 |
@type sep_value : C{str}
|
144 |
|
145 |
@return : resultat generation (pas de valeurs -> false, sinon true)
|
146 |
@rtype : booleen
|
147 |
"""
|
148 |
|
149 |
# format pour valeur temps
|
150 |
format = '%Y/%m/%d %H:%M:%S'
|
151 |
|
152 |
result = (writer is not None) |
153 |
result &= (dict_values is not None or dict_values != "{}") |
154 |
if result:
|
155 |
# parcours valeurs
|
156 |
for key_tv in dict_values: |
157 |
tv = dict_values[key_tv] |
158 |
|
159 |
# generation ligne
|
160 |
dict_data = {} |
161 |
for key_i in dict_indicators: |
162 |
iv = dict_indicators[key_i] |
163 |
v = str(tv[key_i])
|
164 |
|
165 |
# temps sous forme texte
|
166 |
if iv == 'TimeStamp': |
167 |
dt = datetime.utcfromtimestamp(int(v))
|
168 |
v = dt.strftime(format) |
169 |
|
170 |
# separateur dans valeur -> remplacement . par ,
|
171 |
v = v.replace(".", sep_value)
|
172 |
|
173 |
dict_data[iv] = v |
174 |
|
175 |
writer.writerow(dict_data) |
176 |
|
177 |
return result
|