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