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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Generation of Continuous Performance Trending and Analysis.
21 import plotly.offline as ploff
22 import plotly.graph_objs as plgo
23 import plotly.exceptions as plerr
27 from collections import OrderedDict
28 from utils import split_outliers, archive_input_data, execute_command
31 # Command to build the html format of the report
32 HTML_BUILDER = 'sphinx-build -v -c conf_cpta -a ' \
35 '-D version="Generated on {date}" ' \
39 # .css file for the html format of the report
40 THEME_OVERRIDES = """/* override table width restrictions */
42 max-width: 1200px !important;
46 COLORS = ["SkyBlue", "Olive", "Purple", "Coral", "Indigo", "Pink",
47 "Chocolate", "Brown", "Magenta", "Cyan", "Orange", "Black",
48 "Violet", "Blue", "Yellow"]
51 def generate_cpta(spec, data):
52 """Generate all formats and versions of the Continuous Performance Trending
55 :param spec: Specification read from the specification file.
56 :param data: Full data set.
57 :type spec: Specification
61 logging.info("Generating the Continuous Performance Trending and Analysis "
64 ret_code = _generate_all_charts(spec, data)
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]"])
72 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE]"], "w") as \
74 css_file.write(THEME_OVERRIDES)
76 with open(spec.environment["paths"]["DIR[CSS_PATCH_FILE2]"], "w") as \
78 css_file.write(THEME_OVERRIDES)
80 archive_input_data(spec)
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 ...
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
99 :type fill_missing: bool
100 :type use_first: bool
101 :returns: Reduced data.
105 first_idx = min(in_data.keys())
106 last_idx = max(in_data.keys())
111 data_dict[first_idx] = in_data[first_idx]
112 while idx >= first_idx:
113 data = in_data.get(idx, None)
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
125 while flag_l or flag_h:
126 if idx + inc > idx_high:
129 idx_lst.append(idx + inc)
130 if idx - inc < idx_low:
133 idx_lst.append(idx - inc)
137 if i in in_data.keys():
138 data_dict[i] = in_data[i]
141 data_dict[idx] = data
144 return OrderedDict(sorted(data_dict.items(), key=lambda t: t[0]))
147 def _evaluate_results(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 trimmed moving median - 3 * stdev
152 - normal: between trimmed moving median - 3 * stdev and median + 3 * stdev
153 - progress: more than trimmed moving median + 3 * stdev
154 where stdev is trimmed moving standard deviation.
156 :param trimmed_data: Full data set with the outliers replaced by nan.
157 :param window: Window size used to calculate moving average and moving stdev.
158 :type trimmed_data: pandas.Series
160 :returns: Evaluated results.
164 if len(trimmed_data) > 2:
165 win_size = trimmed_data.size if trimmed_data.size < window else window
167 tmm = trimmed_data.rolling(window=win_size, min_periods=2).median()
168 tmstd = trimmed_data.rolling(window=win_size, min_periods=2).std()
171 for build_nr, value in trimmed_data.iteritems():
176 or np.isnan(tmm[build_nr])
177 or np.isnan(tmstd[build_nr])):
179 elif value < (tmm[build_nr] - 3 * tmstd[build_nr]):
181 elif value > (tmm[build_nr] + 3 * tmstd[build_nr]):
188 tmm = np.median(trimmed_data)
189 tmstd = np.std(trimmed_data)
190 if trimmed_data.values[-1] < (tmm - 3 * tmstd):
192 elif (tmm - 3 * tmstd) <= trimmed_data.values[-1] <= (
202 def _generate_trending_traces(in_data, build_info, period, moving_win_size=10,
203 fill_missing=True, use_first=False,
204 show_trend_line=True, name="", color=""):
205 """Generate the trending traces:
207 - trimmed moving median (trending line)
208 - outliers, regress, progress
210 :param in_data: Full data set.
211 :param build_info: Information about the builds.
212 :param period: Sampling period.
213 :param moving_win_size: Window size.
214 :param fill_missing: If the chosen sample is missing in the full set, its
215 nearest neighbour is used.
216 :param use_first: Use the first sample even though it is not chosen.
217 :param show_trend_line: Show moving median (trending plot).
218 :param name: Name of the plot
219 :param color: Name of the color for the plot.
220 :type in_data: OrderedDict
221 :type build_info: dict
223 :type moving_win_size: int
224 :type fill_missing: bool
225 :type use_first: bool
226 :type show_trend_line: bool
229 :returns: Generated traces (list) and the evaluated result (float).
230 :rtype: tuple(traces, result)
234 in_data = _select_data(in_data, period,
235 fill_missing=fill_missing,
238 data_x = list(in_data.keys())
239 data_y = list(in_data.values())
243 hover_text.append("vpp-build: {0}".
244 format(build_info[str(idx)][1].split("~")[-1]))
246 data_pd = pd.Series(data_y, index=data_x)
248 t_data, outliers = split_outliers(data_pd, outlier_const=1.5,
249 window=moving_win_size)
250 results = _evaluate_results(t_data, window=moving_win_size)
252 anomalies = pd.Series()
253 anomalies_res = list()
254 for idx, item in enumerate(in_data.items()):
255 item_pd = pd.Series([item[1], ], index=[item[0], ])
256 if item[0] in outliers.keys():
257 anomalies = anomalies.append(item_pd)
258 anomalies_res.append(0.0)
259 elif results[idx] in (0.33, 1.0):
260 anomalies = anomalies.append(item_pd)
261 anomalies_res.append(results[idx])
262 anomalies_res.extend([0.0, 0.33, 0.66, 1.0])
265 color_scale = [[0.00, "grey"],
274 trace_samples = plgo.Scatter(
281 name="{name}-thput".format(name=name),
288 hoverinfo="x+y+text+name"
290 traces = [trace_samples, ]
292 trace_anomalies = plgo.Scatter(
299 name="{name}-anomalies".format(name=name),
302 "symbol": "circle-open",
303 "color": anomalies_res,
304 "colorscale": color_scale,
312 "title": "Circles Marking Data Classification",
313 "titleside": 'right',
318 "tickvals": [0.125, 0.375, 0.625, 0.875],
319 "ticktext": ["Outlier", "Regression", "Normal", "Progression"],
327 traces.append(trace_anomalies)
330 data_trend = t_data.rolling(window=moving_win_size,
331 min_periods=2).median()
332 trace_trend = plgo.Scatter(
334 y=data_trend.tolist(),
341 name='{name}-trend'.format(name=name)
343 traces.append(trace_trend)
345 return traces, results[-1]
348 def _generate_chart(traces, layout, file_name):
349 """Generates the whole chart using pre-generated traces.
351 :param traces: Traces for the chart.
352 :param layout: Layout of the chart.
353 :param file_name: File name for the generated chart.
360 logging.info(" Writing the file '{0}' ...".format(file_name))
361 plpl = plgo.Figure(data=traces, layout=layout)
363 ploff.plot(plpl, show_link=False, auto_open=False, filename=file_name)
364 except plerr.PlotlyEmptyDataError:
365 logging.warning(" No data for the plot. Skipped.")
368 def _generate_all_charts(spec, input_data):
369 """Generate all charts specified in the specification file.
371 :param spec: Specification.
372 :param input_data: Full data set.
373 :type spec: Specification
374 :type input_data: InputData
377 job_name = spec.cpta["data"].keys()[0]
380 for build in spec.input["builds"][job_name]:
381 status = build["status"]
382 if status != "failed" and status != "not found":
383 builds_lst.append(str(build["build"]))
385 # Get "build ID": "date" dict:
386 build_info = OrderedDict()
387 for build in builds_lst:
389 build_info[build] = (
390 input_data.metadata(job_name, build)["generated"][:14],
391 input_data.metadata(job_name, build)["version"]
394 build_info[build] = ("", "")
395 logging.info("{}: {}, {}".format(build,
396 build_info[build][0],
397 build_info[build][1]))
401 header = "Build Number:," + ",".join(builds_lst) + '\n'
402 csv_table.append(header)
403 build_dates = [x[0] for x in build_info.values()]
404 header = "Build Date:," + ",".join(build_dates) + '\n'
405 csv_table.append(header)
406 vpp_versions = [x[1] for x in build_info.values()]
407 header = "VPP Version:," + ",".join(vpp_versions) + '\n'
408 csv_table.append(header)
411 for chart in spec.cpta["plots"]:
412 logging.info(" Generating the chart '{0}' ...".
413 format(chart.get("title", "")))
416 data = input_data.filter_data(chart, continue_on_error=True)
418 logging.error("No data.")
423 for idx, build in job.items():
424 for test_name, test in build.items():
425 if chart_data.get(test_name, None) is None:
426 chart_data[test_name] = OrderedDict()
428 chart_data[test_name][int(idx)] = \
429 test["result"]["throughput"]
430 except (KeyError, TypeError):
433 # Add items to the csv table:
434 for tst_name, tst_data in chart_data.items():
436 for build in builds_lst:
437 item = tst_data.get(int(build), '')
438 tst_lst.append(str(item))
439 # tst_lst.append(str(item) if item else '')
440 csv_table.append("{0},".format(tst_name) + ",".join(tst_lst) + '\n')
442 for period in chart["periods"]:
445 win_size = 14 if period == 1 else 5 if period < 20 else 3
447 for test_name, test_data in chart_data.items():
449 logging.warning("No data for the test '{0}'".
452 test_name = test_name.split('.')[-1]
453 trace, result = _generate_trending_traces(
455 build_info=build_info,
457 moving_win_size=win_size,
460 name='-'.join(test_name.split('-')[3:-1]),
463 results.append(result)
466 # Generate the chart:
467 chart["layout"]["xaxis"]["title"] = \
468 chart["layout"]["xaxis"]["title"].format(job=job_name)
469 _generate_chart(traces,
471 file_name="{0}-{1}-{2}{3}".format(
472 spec.cpta["output-file"],
473 chart["output-file-name"],
475 spec.cpta["output-file-type"]))
477 logging.info(" Done.")
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)
485 with open("{0}.csv".format(file_name), 'rb') as csv_file:
486 csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
488 for row in csv_content:
489 if txt_table is None:
490 txt_table = prettytable.PrettyTable(row)
493 for idx, item in enumerate(row):
495 row[idx] = str(round(float(item) / 1000000, 2))
499 txt_table.add_row(row)
500 except Exception as err:
501 logging.warning("Error occurred while generating TXT table:"
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))
514 if item == 0.66 and result == "PASS":
516 elif item == 0.33 or item == 0.0:
519 logging.info("Partial results: {0}".format(results))
520 logging.info("Result: {0}".format(result))