CSIT-1041: Trending dashboard
[csit.git] / resources / tools / presentation / generator_CPTA.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Generation of Continuous Performance Trending and Analysis.
15 """
16
17 import datetime
18 import logging
19 import csv
20 import prettytable
21 import plotly.offline as ploff
22 import plotly.graph_objs as plgo
23 import plotly.exceptions as plerr
24 import numpy as np
25 import pandas as pd
26
27 from collections import OrderedDict
28 from utils import find_outliers, archive_input_data, execute_command
29
30
31 # Command to build the html format of the report
32 HTML_BUILDER = 'sphinx-build -v -c conf_cpta -a ' \
33                '-b html -E ' \
34                '-t html ' \
35                '-D version="Generated on {date}" ' \
36                '{working_dir} ' \
37                '{build_dir}/'
38
39 # .css file for the html format of the report
40 THEME_OVERRIDES = """/* override table width restrictions */
41 .wy-nav-content {
42     max-width: 1200px !important;
43 }
44 """
45
46 COLORS = ["SkyBlue", "Olive", "Purple", "Coral", "Indigo", "Pink",
47           "Chocolate", "Brown", "Magenta", "Cyan", "Orange", "Black",
48           "Violet", "Blue", "Yellow"]
49
50
51 def generate_cpta(spec, data):
52     """Generate all formats and versions of the Continuous Performance Trending
53     and Analysis.
54
55     :param spec: Specification read from the specification file.
56     :param data: Full data set.
57     :type spec: Specification
58     :type data: InputData
59     """
60
61     logging.info("Generating the Continuous Performance Trending and Analysis "
62                  "...")
63
64     ret_code = _generate_all_charts(spec, data)
65
66     cmd = HTML_BUILDER.format(
67         date=datetime.date.today().strftime('%d-%b-%Y'),
68         working_dir=spec.environment["paths"]["DIR[WORKING,SRC]"],
69         build_dir=spec.environment["paths"]["DIR[BUILD,HTML]"])
70     execute_command(cmd)
71
72     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
73             css_file:
74         css_file.write(THEME_OVERRIDES)
75
76     with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
77             css_file:
78         css_file.write(THEME_OVERRIDES)
79
80     archive_input_data(spec)
81
82     logging.info("Done.")
83
84     return ret_code
85
86
87 def _select_data(in_data, period, fill_missing=False, use_first=False):
88     """Select the data from the full data set. The selection is done by picking
89     the samples depending on the period: period = 1: All, period = 2: every
90     second sample, period = 3: every third sample ...
91
92     :param in_data: Full set of data.
93     :param period: Sampling period.
94     :param fill_missing: If the chosen sample is missing in the full set, its
95     nearest neighbour is used.
96     :param use_first: Use the first sample even though it is not chosen.
97     :type in_data: OrderedDict
98     :type period: int
99     :type fill_missing: bool
100     :type use_first: bool
101     :returns: Reduced data.
102     :rtype: OrderedDict
103     """
104
105     first_idx = min(in_data.keys())
106     last_idx = max(in_data.keys())
107
108     idx = last_idx
109     data_dict = dict()
110     if use_first:
111         data_dict[first_idx] = in_data[first_idx]
112     while idx >= first_idx:
113         data = in_data.get(idx, None)
114         if data is None:
115             if fill_missing:
116                 threshold = int(round(idx - period / 2)) + 1 - period % 2
117                 idx_low = first_idx if threshold < first_idx else threshold
118                 threshold = int(round(idx + period / 2))
119                 idx_high = last_idx if threshold > last_idx else threshold
120
121                 flag_l = True
122                 flag_h = True
123                 idx_lst = list()
124                 inc = 1
125                 while flag_l or flag_h:
126                     if idx + inc > idx_high:
127                         flag_h = False
128                     else:
129                         idx_lst.append(idx + inc)
130                     if idx - inc < idx_low:
131                         flag_l = False
132                     else:
133                         idx_lst.append(idx - inc)
134                     inc += 1
135
136                 for i in idx_lst:
137                     if i in in_data.keys():
138                         data_dict[i] = in_data[i]
139                         break
140         else:
141             data_dict[idx] = data
142         idx -= period
143
144     return OrderedDict(sorted(data_dict.items(), key=lambda t: t[0]))
145
146
147 def _evaluate_results(in_data, trimmed_data, window=10):
148     """Evaluates if the sample value is regress, normal or progress compared to
149     previous data within the window.
150     We use the intervals defined as:
151     - regress: less than median - 3 * stdev
152     - normal: between median - 3 * stdev and median + 3 * stdev
153     - progress: more than median + 3 * stdev
154
155     :param in_data: Full data set.
156     :param trimmed_data: Full data set without the outliers.
157     :param window: Window size used to calculate moving median and moving stdev.
158     :type in_data: pandas.Series
159     :type trimmed_data: pandas.Series
160     :type window: int
161     :returns: Evaluated results.
162     :rtype: list
163     """
164
165     if len(in_data) > 2:
166         win_size = in_data.size if in_data.size < window else window
167         results = [0.66, ]
168         median = in_data.rolling(window=win_size, min_periods=2).median()
169         stdev_t = trimmed_data.rolling(window=win_size, min_periods=2).std()
170
171         first = True
172         for build_nr, value in in_data.iteritems():
173             if first:
174                 first = False
175                 continue
176             if np.isnan(trimmed_data[build_nr]) \
177                     or np.isnan(median[build_nr]) \
178                     or np.isnan(stdev_t[build_nr]) \
179                     or np.isnan(value):
180                 results.append(0.0)
181             elif value < (median[build_nr] - 3 * stdev_t[build_nr]):
182                 results.append(0.33)
183             elif value > (median[build_nr] + 3 * stdev_t[build_nr]):
184                 results.append(1.0)
185             else:
186                 results.append(0.66)
187     else:
188         results = [0.0, ]
189         try:
190             median = np.median(in_data)
191             stdev = np.std(in_data)
192             if in_data.values[-1] < (median - 3 * stdev):
193                 results.append(0.33)
194             elif (median - 3 * stdev) <= in_data.values[-1] <= (
195                     median + 3 * stdev):
196                 results.append(0.66)
197             else:
198                 results.append(1.0)
199         except TypeError:
200             results.append(None)
201     return results
202
203
204 def _generate_trending_traces(in_data, build_info, period, moving_win_size=10,
205                               fill_missing=True, use_first=False,
206                               show_moving_median=True, name="", color=""):
207     """Generate the trending traces:
208      - samples,
209      - moving median (trending plot)
210      - outliers, regress, progress
211
212     :param in_data: Full data set.
213     :param build_info: Information about the builds.
214     :param period: Sampling period.
215     :param moving_win_size: Window size.
216     :param fill_missing: If the chosen sample is missing in the full set, its
217     nearest neighbour is used.
218     :param use_first: Use the first sample even though it is not chosen.
219     :param show_moving_median: Show moving median (trending plot).
220     :param name: Name of the plot
221     :param color: Name of the color for the plot.
222     :type in_data: OrderedDict
223     :type build_info: dict
224     :type period: int
225     :type moving_win_size: int
226     :type fill_missing: bool
227     :type use_first: bool
228     :type show_moving_median: bool
229     :type name: str
230     :type color: str
231     :returns: Generated traces (list) and the evaluated result (float).
232     :rtype: tuple(traces, result)
233     """
234
235     if period > 1:
236         in_data = _select_data(in_data, period,
237                                fill_missing=fill_missing,
238                                use_first=use_first)
239
240     data_x = [key for key in in_data.keys()]
241     data_y = [val for val in in_data.values()]
242
243     hover_text = list()
244     for idx in data_x:
245         hover_text.append("vpp-build: {0}".
246                           format(build_info[str(idx)][1].split("~")[-1]))
247
248     data_pd = pd.Series(data_y, index=data_x)
249
250     t_data, outliers = find_outliers(data_pd, outlier_const=1.5)
251     results = _evaluate_results(data_pd, t_data, window=moving_win_size)
252
253     anomalies = pd.Series()
254     anomalies_res = list()
255     for idx, item in enumerate(in_data.items()):
256         item_pd = pd.Series([item[1], ], index=[item[0], ])
257         if item[0] in outliers.keys():
258             anomalies = anomalies.append(item_pd)
259             anomalies_res.append(0.0)
260         elif results[idx] in (0.33, 1.0):
261             anomalies = anomalies.append(item_pd)
262             anomalies_res.append(results[idx])
263     anomalies_res.extend([0.0, 0.33, 0.66, 1.0])
264
265     # Create traces
266     color_scale = [[0.00, "grey"],
267                    [0.25, "grey"],
268                    [0.25, "red"],
269                    [0.50, "red"],
270                    [0.50, "white"],
271                    [0.75, "white"],
272                    [0.75, "green"],
273                    [1.00, "green"]]
274
275     trace_samples = plgo.Scatter(
276         x=data_x,
277         y=data_y,
278         mode='markers',
279         line={
280             "width": 1
281         },
282         name="{name}-thput".format(name=name),
283         marker={
284             "size": 5,
285             "color": color,
286             "symbol": "circle",
287         },
288         text=hover_text,
289         hoverinfo="x+y+text+name"
290     )
291     traces = [trace_samples, ]
292
293     trace_anomalies = plgo.Scatter(
294         x=anomalies.keys(),
295         y=anomalies.values,
296         mode='markers',
297         hoverinfo="none",
298         showlegend=False,
299         legendgroup=name,
300         name="{name}: outliers".format(name=name),
301         marker={
302             "size": 15,
303             "symbol": "circle-open",
304             "color": anomalies_res,
305             "colorscale": color_scale,
306             "showscale": True,
307             "line": {
308                 "width": 2
309             },
310             "colorbar": {
311                 "y": 0.5,
312                 "len": 0.8,
313                 "title": "Circles Marking Data Classification",
314                 "titleside": 'right',
315                 "titlefont": {
316                     "size": 14
317                 },
318                 "tickmode": 'array',
319                 "tickvals": [0.125, 0.375, 0.625, 0.875],
320                 "ticktext": ["Outlier", "Regression", "Normal", "Progression"],
321                 "ticks": "",
322                 "ticklen": 0,
323                 "tickangle": -90,
324                 "thickness": 10
325             }
326         }
327     )
328     traces.append(trace_anomalies)
329
330     if show_moving_median:
331         data_mean_y = pd.Series(data_y).rolling(
332             window=moving_win_size, min_periods=2).median()
333         trace_median = plgo.Scatter(
334             x=data_x,
335             y=data_mean_y,
336             mode='lines',
337             line={
338                 "shape": "spline",
339                 "width": 1,
340                 "color": color,
341             },
342             name='{name}-trend'.format(name=name)
343         )
344         traces.append(trace_median)
345
346     return traces, results[-1]
347
348
349 def _generate_chart(traces, layout, file_name):
350     """Generates the whole chart using pre-generated traces.
351
352     :param traces: Traces for the chart.
353     :param layout: Layout of the chart.
354     :param file_name: File name for the generated chart.
355     :type traces: list
356     :type layout: dict
357     :type file_name: str
358     """
359
360     # Create plot
361     logging.info("    Writing the file '{0}' ...".format(file_name))
362     plpl = plgo.Figure(data=traces, layout=layout)
363     try:
364         ploff.plot(plpl, show_link=False, auto_open=False, filename=file_name)
365     except plerr.PlotlyEmptyDataError:
366         logging.warning(" No data for the plot. Skipped.")
367
368
369 def _generate_all_charts(spec, input_data):
370     """Generate all charts specified in the specification file.
371
372     :param spec: Specification.
373     :param input_data: Full data set.
374     :type spec: Specification
375     :type input_data: InputData
376     """
377
378     job_name = spec.cpta["data"].keys()[0]
379
380     builds_lst = list()
381     for build in spec.input["builds"][job_name]:
382         status = build["status"]
383         if status != "failed" and status != "not found":
384             builds_lst.append(str(build["build"]))
385
386     # Get "build ID": "date" dict:
387     build_info = OrderedDict()
388     for build in builds_lst:
389         try:
390             build_info[build] = (
391                 input_data.metadata(job_name, build)["generated"][:14],
392                 input_data.metadata(job_name, build)["version"]
393             )
394         except KeyError:
395             build_info[build] = ("", "")
396         logging.info("{}: {}, {}".format(build,
397                                          build_info[build][0],
398                                          build_info[build][1]))
399
400     # Create the header:
401     csv_table = list()
402     header = "Build Number:," + ",".join(builds_lst) + '\n'
403     csv_table.append(header)
404     build_dates = [x[0] for x in build_info.values()]
405     header = "Build Date:," + ",".join(build_dates) + '\n'
406     csv_table.append(header)
407     vpp_versions = [x[1] for x in build_info.values()]
408     header = "VPP Version:," + ",".join(vpp_versions) + '\n'
409     csv_table.append(header)
410
411     results = list()
412     for chart in spec.cpta["plots"]:
413         logging.info("  Generating the chart '{0}' ...".
414                      format(chart.get("title", "")))
415
416         # Transform the data
417         data = input_data.filter_data(chart, continue_on_error=True)
418         if data is None:
419             logging.error("No data.")
420             return
421
422         chart_data = dict()
423         for job in data:
424             for idx, build in job.items():
425                 for test_name, test in build.items():
426                     if chart_data.get(test_name, None) is None:
427                         chart_data[test_name] = OrderedDict()
428                     try:
429                         chart_data[test_name][int(idx)] = \
430                             test["result"]["throughput"]
431                     except (KeyError, TypeError):
432                         pass
433
434         # Add items to the csv table:
435         for tst_name, tst_data in chart_data.items():
436             tst_lst = list()
437             for build in builds_lst:
438                 item = tst_data.get(int(build), '')
439                 tst_lst.append(str(item) if item else '')
440             csv_table.append("{0},".format(tst_name) + ",".join(tst_lst) + '\n')
441
442         for period in chart["periods"]:
443             # Generate traces:
444             traces = list()
445             win_size = 14 if period == 1 else 5 if period < 20 else 3
446             idx = 0
447             for test_name, test_data in chart_data.items():
448                 if not test_data:
449                     logging.warning("No data for the test '{0}'".
450                                     format(test_name))
451                     continue
452                 test_name = test_name.split('.')[-1]
453                 trace, result = _generate_trending_traces(
454                     test_data,
455                     build_info=build_info,
456                     period=period,
457                     moving_win_size=win_size,
458                     fill_missing=True,
459                     use_first=False,
460                     name='-'.join(test_name.split('-')[3:-1]),
461                     color=COLORS[idx])
462                 traces.extend(trace)
463                 results.append(result)
464                 idx += 1
465
466             # Generate the chart:
467             chart["layout"]["xaxis"]["title"] = \
468                 chart["layout"]["xaxis"]["title"].format(job=job_name)
469             _generate_chart(traces,
470                             chart["layout"],
471                             file_name="{0}-{1}-{2}{3}".format(
472                                 spec.cpta["output-file"],
473                                 chart["output-file-name"],
474                                 period,
475                                 spec.cpta["output-file-type"]))
476
477         logging.info("  Done.")
478
479     # Write the tables:
480     file_name = spec.cpta["output-file"] + "-trending"
481     with open("{0}.csv".format(file_name), 'w') as file_handler:
482         file_handler.writelines(csv_table)
483
484     txt_table = None
485     with open("{0}.csv".format(file_name), 'rb') as csv_file:
486         csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
487         line_nr = 0
488         for row in csv_content:
489             if txt_table is None:
490                 txt_table = prettytable.PrettyTable(row)
491             else:
492                 if line_nr > 1:
493                     for idx, item in enumerate(row):
494                         try:
495                             row[idx] = str(round(float(item) / 1000000, 2))
496                         except ValueError:
497                             pass
498                 try:
499                     txt_table.add_row(row)
500                 except Exception as err:
501                     logging.warning("Error occurred while generating TXT table:"
502                                     "\n{0}".format(err))
503             line_nr += 1
504         txt_table.align["Build Number:"] = "l"
505     with open("{0}.txt".format(file_name), "w") as txt_file:
506         txt_file.write(str(txt_table))
507
508     # Evaluate result:
509     result = "PASS"
510     for item in results:
511         if item is None:
512             result = "FAIL"
513             break
514         if item == 0.66 and result == "PASS":
515             result = "PASS"
516         elif item == 0.33 or item == 0.0:
517             result = "FAIL"
518
519     logging.info("Partial results: {0}".format(results))
520     logging.info("Result: {0}".format(result))
521
522     return result