Coverage for app/charts.py : 62%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from flask import current_app
3class SummaryCharts:
4 #
5 # prepares jsons for samary charts
6 #
8 # status strings from GlobalConstants
9 TESTCASESTATUS_SUCCESS = "OK"
10 TESTCASESTATUS_ERROR = "Failed"
11 TESTCASESTATUS_WAITING = "Paused"
14 def __init__(self, log, min_length=10):
15 self.log = log
16 self.shortest = min_length
18 def get_charts(self):
19 #
20 # summary set of charts
21 #
22 return {
23 'figures': self.figures(),
24 'status': self.status(),
25 'testcases': self.testcases(),
26 'files': self.files(),
27 }
29 def files(self):
30 return [
31 {
32 'title': 'Logfile',
33 'url': '/'.join((
34 'http:/',
35 current_app.config.get('BAANGT_DATAFILE_HOST'),
36 current_app.config.get('BAANGT_DATAFILE_GET'),
37 self.log['id'],
38 current_app.config.get('BAANGT_DATAFILE_LOGFILE'),
39 )),
40 },
41 {
42 'title': 'Results',
43 'url': '/'.join((
44 'http:/',
45 current_app.config.get('BAANGT_DATAFILE_HOST'),
46 current_app.config.get('BAANGT_DATAFILE_GET'),
47 self.log['id'],
48 current_app.config.get('BAANGT_DATAFILE_RESULTS'),
49 )),
50 },
51 ]
53 def figures(self):
54 #
55 # builds json with log statistics
56 #
58 return {
59 'records': self.log['Summary'].get('TestRecords'),
60 'successful': self.log['Summary'].get('Successful'),
61 'error': self.log['Summary'].get('Error'),
62 'paused': self.log['Summary'].get('Paused'),
63 }
65 def status(self):
66 #
67 # status chart
68 #
70 return {
71 'type': 'doughnut',
72 'data': {
73 'datasets': [{
74 'data': [
75 self.log['Summary'].get('Successful'),
76 self.log['Summary'].get('Error'),
77 self.log['Summary'].get('Paused'),
78 ],
79 'backgroundColor': [
80 '#52ff52',
81 '#ff5252',
82 '#bdbdbd',
83 ],
84 }],
85 'labels': [
86 'Passed',
87 'Failed',
88 'Paused'
89 ],
90 },
91 'options': {
92 'legend': {
93 'display': False,
94 },
95 },
96 }
98 def testcases(self):
99 #
100 # builds json for Chart.js: duration of testcases
101 #
103 testcases = []
105 # collect data for every TestCaseSequence
106 for tcs in self.log['TestSequences']:
107 null = max(0, self.shortest - len(tcs['TestCases']))
108 testcases.append({
109 'type': 'bar',
110 'data': {
111 'datasets': [
112 {
113 'data': [self.duration_to_seconds(t['Parameters'].get('Duration')) \
114 if t['Parameters'].get('TestCaseStatus') == self.TESTCASESTATUS_SUCCESS else 0 \
115 for t in tcs['TestCases']] + [None]*null,
116 'backgroundColor': '#52ff52',
117 'label': 'PASSED',
118 },
119 {
120 'data': [self.duration_to_seconds(t['Parameters'].get('Duration')) \
121 if t['Parameters'].get('TestCaseStatus') == self.TESTCASESTATUS_ERROR else 0 \
122 for t in tcs['TestCases']] + [None]*null,
123 'backgroundColor': '#ff5252',
124 'label': 'FAILED',
125 },
126 {
127 'data': [self.duration_to_seconds(t['Parameters'].get('Duration')) \
128 if t['Parameters'].get('TestCaseStatus') == self.TESTCASESTATUS_WAITING else 0 \
129 for t in tcs['TestCases']] + [None]*null,
130 'backgroundColor': '#bdbdbd',
131 'label': 'PAUSED',
132 },
133 ],
134 'labels': [t['Parameters'].get('Duration') for t in tcs['TestCases']] + [None]*null,
135 },
136 'options': {
137 'legend': {
138 'display': False,
139 },
140 'tooltips': {
141 'mode': 'index',
142 'intersect': False,
143 },
144 'scales': {
145 'xAxes': [{
146 'gridLines': {
147 'display': False,
148 'drawBorder': False,
149 },
150 'ticks': {
151 'display': False,
152 },
153 'stacked': True,
154 }],
155 'yAxes': [{
156 'gridLines': {
157 'display': False,
158 'drawBorder': False,
159 },
160 'ticks': {
161 'display': False,
162 },
163 'stacked': True,
164 }],
165 },
166 },
167 })
169 return testcases
172 def duration_to_seconds(self, duration):
173 if duration:
174 without_decimals = duration.split('.')[0]
175 factors = [3600, 60, 1]
177 return sum(t*f for t,f in zip(map(int, without_decimals.split(':')), factors))
179 return 0
182class DashboardCharts:
183 #
184 # prepares jsons for dashboard Charts.js
185 #
187 def __init__(self, logs, length=10):
188 self.logs = logs[-length:]
189 self.null = length - len(self.logs)
192 def get_charts(self):
193 #
194 # dashboard chart set
195 #
197 return {
198 'figures': self.figures(),
199 'status': self.status(),
200 'results': self.results(),
201 'duration': self.duration(),
202 }
204 def figures(self):
205 #
206 # builds json with log statistics
207 #
209 return {
210 'records': self.logs[-1].testrecords,
211 'successful': self.logs[-1].successful,
212 'error': self.logs[-1].error,
213 'paused': self.logs[-1].paused,
214 }
216 def status(self):
217 #
218 # status chart
219 #
221 return {
222 'type': 'doughnut',
223 'data': {
224 'datasets': [{
225 'data': [
226 self.logs[-1].successful,
227 self.logs[-1].error,
228 self.logs[-1].paused,
229 ],
230 'backgroundColor': [
231 '#52ff52',
232 '#ff5252',
233 '#bdbdbd',
234 ],
235 }],
236 'labels': [
237 'Passed',
238 'Failed',
239 'Paused'
240 ],
241 },
242 'options': {
243 'legend': {
244 'display': False,
245 },
246 },
247 }
250 def results(self):
251 #
252 # results chart
253 #
255 return {
256 'type': 'bar',
257 'data': {
258 'datasets': [
259 {
260 'data': [x.successful for x in self.logs] + [None]*self.null,
261 'backgroundColor': '#52ff52',
262 'label': 'Passed',
263 },
264 {
265 'data': [x.error for x in self.logs] + [None]*self.null,
266 'backgroundColor': '#ff5252',
267 'label': 'Failed',
268 },
269 {
270 'data': [x.paused for x in self.logs] + [None]*self.null,
271 'backgroundColor': '#bdbdbd',
272 'label': 'Paused',
273 },
274 ],
275 'labels': [x.created.strftime('%Y-%m-%d %H:%M:%S') for x in self.logs] + [None]*self.null,
276 },
277 'options': {
278 'legend': {
279 'display': False,
280 },
281 'tooltips': {
282 'mode': 'index',
283 'intersect': False,
284 },
285 'scales': {
286 'xAxes': [{
287 'gridLines': {
288 'display': False,
289 'drawBorder': False,
290 },
291 'ticks': {
292 'display': False,
293 },
294 'stacked': True,
295 }],
296 'yAxes': [{
297 'gridLines': {
298 'display': False,
299 'drawBorder': False,
300 },
301 'ticks': {
302 'display': False,
303 },
304 'stacked': True,
305 }],
306 },
307 },
308 }
310 def duration(self):
311 #
312 # duration chart
313 #
315 return {
316 'type': 'line',
317 'data': {
318 'datasets': [{
319 'data': [x.duration for x in self.logs] + [None]*self.null,
320 'fill': False,
321 'borderColor': '#cfd8dc',
322 'borderWidth': 1,
323 'pointBackgroundColor': 'transparent',
324 'pointBorderColor': '#0277bd',
325 'pointRadius': 5,
326 'lineTension': 0,
327 'label': 'Duration',
328 }],
329 'labels': [x.created.strftime('%Y-%m-%d %H:%M:%S') for x in self.logs] + [None]*self.null,
330 },
331 'options': {
332 'legend': {
333 'display': False,
334 },
335 'tooltips': {
336 'mode': 'index',
337 'intersect': False,
338 },
339 'layout': {
340 'padding': {
341 'top': 10,
342 'right': 10,
343 },
344 },
345 'scales': {
346 'xAxes': [{
347 'gridLines': {
348 'display': False,
349 'drawBorder': False,
350 },
351 'ticks': {
352 'display': False,
353 },
354 }],
355 'yAxes': [{
356 'gridLines': {
357 'display': False,
358 'drawBorder': False,
359 },
360 'ticks': {
361 'display': False,
362 },
363 }],
364 },
365 },
366 }