eee95d6092a55d5e2f29bf95aa49fd078c8b4493
[csit.git] / resources / tools / presentation / generator_tables.py
1 # Copyright (c) 2020 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 """Algorithms to generate tables.
15 """
16
17
18 import logging
19 import csv
20 import re
21
22 from collections import OrderedDict
23 from xml.etree import ElementTree as ET
24 from datetime import datetime as dt
25 from datetime import timedelta
26 from copy import deepcopy
27
28 import plotly.graph_objects as go
29 import plotly.offline as ploff
30 import pandas as pd
31
32 from numpy import nan, isnan
33 from yaml import load, FullLoader, YAMLError
34
35 from pal_utils import mean, stdev, classify_anomalies, \
36     convert_csv_to_pretty_txt, relative_change_stdev, relative_change
37
38
39 REGEX_NIC = re.compile(r'(\d*ge\dp\d\D*\d*[a-z]*)')
40
41
42 def generate_tables(spec, data):
43     """Generate all tables specified in the specification file.
44
45     :param spec: Specification read from the specification file.
46     :param data: Data to process.
47     :type spec: Specification
48     :type data: InputData
49     """
50
51     generator = {
52         u"table_merged_details": table_merged_details,
53         u"table_soak_vs_ndr": table_soak_vs_ndr,
54         u"table_perf_trending_dash": table_perf_trending_dash,
55         u"table_perf_trending_dash_html": table_perf_trending_dash_html,
56         u"table_last_failed_tests": table_last_failed_tests,
57         u"table_failed_tests": table_failed_tests,
58         u"table_failed_tests_html": table_failed_tests_html,
59         u"table_oper_data_html": table_oper_data_html,
60         u"table_comparison": table_comparison,
61         u"table_weekly_comparison": table_weekly_comparison
62     }
63
64     logging.info(u"Generating the tables ...")
65     for table in spec.tables:
66         try:
67             if table[u"algorithm"] == u"table_weekly_comparison":
68                 table[u"testbeds"] = spec.environment.get(u"testbeds", None)
69             generator[table[u"algorithm"]](table, data)
70         except NameError as err:
71             logging.error(
72                 f"Probably algorithm {table[u'algorithm']} is not defined: "
73                 f"{repr(err)}"
74             )
75     logging.info(u"Done.")
76
77
78 def table_oper_data_html(table, input_data):
79     """Generate the table(s) with algorithm: html_table_oper_data
80     specified in the specification file.
81
82     :param table: Table to generate.
83     :param input_data: Data to process.
84     :type table: pandas.Series
85     :type input_data: InputData
86     """
87
88     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
89     # Transform the data
90     logging.info(
91         f"    Creating the data set for the {table.get(u'type', u'')} "
92         f"{table.get(u'title', u'')}."
93     )
94     data = input_data.filter_data(
95         table,
96         params=[u"name", u"parent", u"show-run", u"type"],
97         continue_on_error=True
98     )
99     if data.empty:
100         return
101     data = input_data.merge_data(data)
102
103     sort_tests = table.get(u"sort", None)
104     if sort_tests:
105         args = dict(
106             inplace=True,
107             ascending=(sort_tests == u"ascending")
108         )
109         data.sort_index(**args)
110
111     suites = input_data.filter_data(
112         table,
113         continue_on_error=True,
114         data_set=u"suites"
115     )
116     if suites.empty:
117         return
118     suites = input_data.merge_data(suites)
119
120     def _generate_html_table(tst_data):
121         """Generate an HTML table with operational data for the given test.
122
123         :param tst_data: Test data to be used to generate the table.
124         :type tst_data: pandas.Series
125         :returns: HTML table with operational data.
126         :rtype: str
127         """
128
129         colors = {
130             u"header": u"#7eade7",
131             u"empty": u"#ffffff",
132             u"body": (u"#e9f1fb", u"#d4e4f7")
133         }
134
135         tbl = ET.Element(u"table", attrib=dict(width=u"100%", border=u"0"))
136
137         trow = ET.SubElement(tbl, u"tr", attrib=dict(bgcolor=colors[u"header"]))
138         thead = ET.SubElement(
139             trow, u"th", attrib=dict(align=u"left", colspan=u"6")
140         )
141         thead.text = tst_data[u"name"]
142
143         trow = ET.SubElement(tbl, u"tr", attrib=dict(bgcolor=colors[u"empty"]))
144         thead = ET.SubElement(
145             trow, u"th", attrib=dict(align=u"left", colspan=u"6")
146         )
147         thead.text = u"\t"
148
149         if tst_data.get(u"show-run", u"No Data") == u"No Data":
150             trow = ET.SubElement(
151                 tbl, u"tr", attrib=dict(bgcolor=colors[u"header"])
152             )
153             tcol = ET.SubElement(
154                 trow, u"td", attrib=dict(align=u"left", colspan=u"6")
155             )
156             tcol.text = u"No Data"
157
158             trow = ET.SubElement(
159                 tbl, u"tr", attrib=dict(bgcolor=colors[u"empty"])
160             )
161             thead = ET.SubElement(
162                 trow, u"th", attrib=dict(align=u"left", colspan=u"6")
163             )
164             font = ET.SubElement(
165                 thead, u"font", attrib=dict(size=u"12px", color=u"#ffffff")
166             )
167             font.text = u"."
168             return str(ET.tostring(tbl, encoding=u"unicode"))
169
170         tbl_hdr = (
171             u"Name",
172             u"Nr of Vectors",
173             u"Nr of Packets",
174             u"Suspends",
175             u"Cycles per Packet",
176             u"Average Vector Size"
177         )
178
179         for dut_data in tst_data[u"show-run"].values():
180             trow = ET.SubElement(
181                 tbl, u"tr", attrib=dict(bgcolor=colors[u"header"])
182             )
183             tcol = ET.SubElement(
184                 trow, u"td", attrib=dict(align=u"left", colspan=u"6")
185             )
186             if dut_data.get(u"threads", None) is None:
187                 tcol.text = u"No Data"
188                 continue
189
190             bold = ET.SubElement(tcol, u"b")
191             bold.text = (
192                 f"Host IP: {dut_data.get(u'host', '')}, "
193                 f"Socket: {dut_data.get(u'socket', '')}"
194             )
195             trow = ET.SubElement(
196                 tbl, u"tr", attrib=dict(bgcolor=colors[u"empty"])
197             )
198             thead = ET.SubElement(
199                 trow, u"th", attrib=dict(align=u"left", colspan=u"6")
200             )
201             thead.text = u"\t"
202
203             for thread_nr, thread in dut_data[u"threads"].items():
204                 trow = ET.SubElement(
205                     tbl, u"tr", attrib=dict(bgcolor=colors[u"header"])
206                 )
207                 tcol = ET.SubElement(
208                     trow, u"td", attrib=dict(align=u"left", colspan=u"6")
209                 )
210                 bold = ET.SubElement(tcol, u"b")
211                 bold.text = u"main" if thread_nr == 0 else f"worker_{thread_nr}"
212                 trow = ET.SubElement(
213                     tbl, u"tr", attrib=dict(bgcolor=colors[u"header"])
214                 )
215                 for idx, col in enumerate(tbl_hdr):
216                     tcol = ET.SubElement(
217                         trow, u"td",
218                         attrib=dict(align=u"right" if idx else u"left")
219                     )
220                     font = ET.SubElement(
221                         tcol, u"font", attrib=dict(size=u"2")
222                     )
223                     bold = ET.SubElement(font, u"b")
224                     bold.text = col
225                 for row_nr, row in enumerate(thread):
226                     trow = ET.SubElement(
227                         tbl, u"tr",
228                         attrib=dict(bgcolor=colors[u"body"][row_nr % 2])
229                     )
230                     for idx, col in enumerate(row):
231                         tcol = ET.SubElement(
232                             trow, u"td",
233                             attrib=dict(align=u"right" if idx else u"left")
234                         )
235                         font = ET.SubElement(
236                             tcol, u"font", attrib=dict(size=u"2")
237                         )
238                         if isinstance(col, float):
239                             font.text = f"{col:.2f}"
240                         else:
241                             font.text = str(col)
242                 trow = ET.SubElement(
243                     tbl, u"tr", attrib=dict(bgcolor=colors[u"empty"])
244                 )
245                 thead = ET.SubElement(
246                     trow, u"th", attrib=dict(align=u"left", colspan=u"6")
247                 )
248                 thead.text = u"\t"
249
250         trow = ET.SubElement(tbl, u"tr", attrib=dict(bgcolor=colors[u"empty"]))
251         thead = ET.SubElement(
252             trow, u"th", attrib=dict(align=u"left", colspan=u"6")
253         )
254         font = ET.SubElement(
255             thead, u"font", attrib=dict(size=u"12px", color=u"#ffffff")
256         )
257         font.text = u"."
258
259         return str(ET.tostring(tbl, encoding=u"unicode"))
260
261     for suite in suites.values:
262         html_table = str()
263         for test_data in data.values:
264             if test_data[u"parent"] not in suite[u"name"]:
265                 continue
266             html_table += _generate_html_table(test_data)
267         if not html_table:
268             continue
269         try:
270             file_name = f"{table[u'output-file']}{suite[u'name']}.rst"
271             with open(f"{file_name}", u'w') as html_file:
272                 logging.info(f"    Writing file: {file_name}")
273                 html_file.write(u".. raw:: html\n\n\t")
274                 html_file.write(html_table)
275                 html_file.write(u"\n\t<p><br><br></p>\n")
276         except KeyError:
277             logging.warning(u"The output file is not defined.")
278             return
279     logging.info(u"  Done.")
280
281
282 def table_merged_details(table, input_data):
283     """Generate the table(s) with algorithm: table_merged_details
284     specified in the specification file.
285
286     :param table: Table to generate.
287     :param input_data: Data to process.
288     :type table: pandas.Series
289     :type input_data: InputData
290     """
291
292     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
293
294     # Transform the data
295     logging.info(
296         f"    Creating the data set for the {table.get(u'type', u'')} "
297         f"{table.get(u'title', u'')}."
298     )
299     data = input_data.filter_data(table, continue_on_error=True)
300     data = input_data.merge_data(data)
301
302     sort_tests = table.get(u"sort", None)
303     if sort_tests:
304         args = dict(
305             inplace=True,
306             ascending=(sort_tests == u"ascending")
307         )
308         data.sort_index(**args)
309
310     suites = input_data.filter_data(
311         table, continue_on_error=True, data_set=u"suites")
312     suites = input_data.merge_data(suites)
313
314     # Prepare the header of the tables
315     header = list()
316     for column in table[u"columns"]:
317         header.append(
318             u'"{0}"'.format(str(column[u"title"]).replace(u'"', u'""'))
319         )
320
321     for suite in suites.values:
322         # Generate data
323         suite_name = suite[u"name"]
324         table_lst = list()
325         for test in data.keys():
326             if data[test][u"parent"] not in suite_name:
327                 continue
328             row_lst = list()
329             for column in table[u"columns"]:
330                 try:
331                     col_data = str(data[test][column[
332                         u"data"].split(u" ")[1]]).replace(u'"', u'""')
333                     # Do not include tests with "Test Failed" in test message
334                     if u"Test Failed" in col_data:
335                         continue
336                     col_data = col_data.replace(
337                         u"No Data", u"Not Captured     "
338                     )
339                     if column[u"data"].split(u" ")[1] in (u"name", ):
340                         if len(col_data) > 30:
341                             col_data_lst = col_data.split(u"-")
342                             half = int(len(col_data_lst) / 2)
343                             col_data = f"{u'-'.join(col_data_lst[:half])}" \
344                                        f"- |br| " \
345                                        f"{u'-'.join(col_data_lst[half:])}"
346                         col_data = f" |prein| {col_data} |preout| "
347                     elif column[u"data"].split(u" ")[1] in (u"msg", ):
348                         # Temporary solution: remove NDR results from message:
349                         if bool(table.get(u'remove-ndr', False)):
350                             try:
351                                 col_data = col_data.split(u" |br| ", 1)[1]
352                             except IndexError:
353                                 pass
354                         col_data = f" |prein| {col_data} |preout| "
355                     elif column[u"data"].split(u" ")[1] in \
356                             (u"conf-history", u"show-run"):
357                         col_data = col_data.replace(u" |br| ", u"", 1)
358                         col_data = f" |prein| {col_data[:-5]} |preout| "
359                     row_lst.append(f'"{col_data}"')
360                 except KeyError:
361                     row_lst.append(u'"Not captured"')
362             if len(row_lst) == len(table[u"columns"]):
363                 table_lst.append(row_lst)
364
365         # Write the data to file
366         if table_lst:
367             separator = u"" if table[u'output-file'].endswith(u"/") else u"_"
368             file_name = f"{table[u'output-file']}{separator}{suite_name}.csv"
369             logging.info(f"      Writing file: {file_name}")
370             with open(file_name, u"wt") as file_handler:
371                 file_handler.write(u",".join(header) + u"\n")
372                 for item in table_lst:
373                     file_handler.write(u",".join(item) + u"\n")
374
375     logging.info(u"  Done.")
376
377
378 def _tpc_modify_test_name(test_name, ignore_nic=False):
379     """Modify a test name by replacing its parts.
380
381     :param test_name: Test name to be modified.
382     :param ignore_nic: If True, NIC is removed from TC name.
383     :type test_name: str
384     :type ignore_nic: bool
385     :returns: Modified test name.
386     :rtype: str
387     """
388     test_name_mod = test_name.\
389         replace(u"-ndrpdrdisc", u""). \
390         replace(u"-ndrpdr", u"").\
391         replace(u"-pdrdisc", u""). \
392         replace(u"-ndrdisc", u"").\
393         replace(u"-pdr", u""). \
394         replace(u"-ndr", u""). \
395         replace(u"1t1c", u"1c").\
396         replace(u"2t1c", u"1c"). \
397         replace(u"2t2c", u"2c").\
398         replace(u"4t2c", u"2c"). \
399         replace(u"4t4c", u"4c").\
400         replace(u"8t4c", u"4c")
401
402     if ignore_nic:
403         return re.sub(REGEX_NIC, u"", test_name_mod)
404     return test_name_mod
405
406
407 def _tpc_modify_displayed_test_name(test_name):
408     """Modify a test name which is displayed in a table by replacing its parts.
409
410     :param test_name: Test name to be modified.
411     :type test_name: str
412     :returns: Modified test name.
413     :rtype: str
414     """
415     return test_name.\
416         replace(u"1t1c", u"1c").\
417         replace(u"2t1c", u"1c"). \
418         replace(u"2t2c", u"2c").\
419         replace(u"4t2c", u"2c"). \
420         replace(u"4t4c", u"4c").\
421         replace(u"8t4c", u"4c")
422
423
424 def _tpc_insert_data(target, src, include_tests):
425     """Insert src data to the target structure.
426
427     :param target: Target structure where the data is placed.
428     :param src: Source data to be placed into the target stucture.
429     :param include_tests: Which results will be included (MRR, NDR, PDR).
430     :type target: list
431     :type src: dict
432     :type include_tests: str
433     """
434     try:
435         if include_tests == u"MRR":
436             target[u"mean"] = src[u"result"][u"receive-rate"]
437             target[u"stdev"] = src[u"result"][u"receive-stdev"]
438         elif include_tests == u"PDR":
439             target[u"data"].append(src[u"throughput"][u"PDR"][u"LOWER"])
440         elif include_tests == u"NDR":
441             target[u"data"].append(src[u"throughput"][u"NDR"][u"LOWER"])
442     except (KeyError, TypeError):
443         pass
444
445
446 def _tpc_generate_html_table(header, data, out_file_name, legend=u"",
447                              footnote=u"", sort_data=True, title=u"",
448                              generate_rst=True):
449     """Generate html table from input data with simple sorting possibility.
450
451     :param header: Table header.
452     :param data: Input data to be included in the table. It is a list of lists.
453         Inner lists are rows in the table. All inner lists must be of the same
454         length. The length of these lists must be the same as the length of the
455         header.
456     :param out_file_name: The name (relative or full path) where the
457         generated html table is written.
458     :param legend: The legend to display below the table.
459     :param footnote: The footnote to display below the table (and legend).
460     :param sort_data: If True the data sorting is enabled.
461     :param title: The table (and file) title.
462     :param generate_rst: If True, wrapping rst file is generated.
463     :type header: list
464     :type data: list of lists
465     :type out_file_name: str
466     :type legend: str
467     :type footnote: str
468     :type sort_data: bool
469     :type title: str
470     :type generate_rst: bool
471     """
472
473     try:
474         idx = header.index(u"Test Case")
475     except ValueError:
476         idx = 0
477     params = {
478         u"align-hdr": (
479             [u"left", u"right"],
480             [u"left", u"left", u"right"],
481             [u"left", u"left", u"left", u"right"]
482         ),
483         u"align-itm": (
484             [u"left", u"right"],
485             [u"left", u"left", u"right"],
486             [u"left", u"left", u"left", u"right"]
487         ),
488         u"width": ([15, 9], [4, 24, 10], [4, 4, 32, 10])
489     }
490
491     df_data = pd.DataFrame(data, columns=header)
492
493     if sort_data:
494         df_sorted = [df_data.sort_values(
495             by=[key, header[idx]], ascending=[True, True]
496             if key != header[idx] else [False, True]) for key in header]
497         df_sorted_rev = [df_data.sort_values(
498             by=[key, header[idx]], ascending=[False, True]
499             if key != header[idx] else [True, True]) for key in header]
500         df_sorted.extend(df_sorted_rev)
501     else:
502         df_sorted = df_data
503
504     fill_color = [[u"#d4e4f7" if idx % 2 else u"#e9f1fb"
505                    for idx in range(len(df_data))]]
506     table_header = dict(
507         values=[f"<b>{item.replace(u',', u',<br>')}</b>" for item in header],
508         fill_color=u"#7eade7",
509         align=params[u"align-hdr"][idx],
510         font=dict(
511             family=u"Courier New",
512             size=12
513         )
514     )
515
516     fig = go.Figure()
517
518     if sort_data:
519         for table in df_sorted:
520             columns = [table.get(col) for col in header]
521             fig.add_trace(
522                 go.Table(
523                     columnwidth=params[u"width"][idx],
524                     header=table_header,
525                     cells=dict(
526                         values=columns,
527                         fill_color=fill_color,
528                         align=params[u"align-itm"][idx],
529                         font=dict(
530                             family=u"Courier New",
531                             size=12
532                         )
533                     )
534                 )
535             )
536
537         buttons = list()
538         menu_items = [f"<b>{itm}</b> (ascending)" for itm in header]
539         menu_items.extend([f"<b>{itm}</b> (descending)" for itm in header])
540         for idx, hdr in enumerate(menu_items):
541             visible = [False, ] * len(menu_items)
542             visible[idx] = True
543             buttons.append(
544                 dict(
545                     label=hdr.replace(u" [Mpps]", u""),
546                     method=u"update",
547                     args=[{u"visible": visible}],
548                 )
549             )
550
551         fig.update_layout(
552             updatemenus=[
553                 go.layout.Updatemenu(
554                     type=u"dropdown",
555                     direction=u"down",
556                     x=0.0,
557                     xanchor=u"left",
558                     y=1.002,
559                     yanchor=u"bottom",
560                     active=len(menu_items) - 1,
561                     buttons=list(buttons)
562                 )
563             ],
564         )
565     else:
566         fig.add_trace(
567             go.Table(
568                 columnwidth=params[u"width"][idx],
569                 header=table_header,
570                 cells=dict(
571                     values=[df_sorted.get(col) for col in header],
572                     fill_color=fill_color,
573                     align=params[u"align-itm"][idx],
574                     font=dict(
575                         family=u"Courier New",
576                         size=12
577                     )
578                 )
579             )
580         )
581
582     ploff.plot(
583         fig,
584         show_link=False,
585         auto_open=False,
586         filename=f"{out_file_name}_in.html"
587     )
588
589     if not generate_rst:
590         return
591
592     file_name = out_file_name.split(u"/")[-1]
593     if u"vpp" in out_file_name:
594         path = u"_tmp/src/vpp_performance_tests/comparisons/"
595     else:
596         path = u"_tmp/src/dpdk_performance_tests/comparisons/"
597     logging.info(f"    Writing the HTML file to {path}{file_name}.rst")
598     with open(f"{path}{file_name}.rst", u"wt") as rst_file:
599         rst_file.write(
600             u"\n"
601             u".. |br| raw:: html\n\n    <br />\n\n\n"
602             u".. |prein| raw:: html\n\n    <pre>\n\n\n"
603             u".. |preout| raw:: html\n\n    </pre>\n\n"
604         )
605         if title:
606             rst_file.write(f"{title}\n")
607             rst_file.write(f"{u'`' * len(title)}\n\n")
608         rst_file.write(
609             u".. raw:: html\n\n"
610             f'    <iframe frameborder="0" scrolling="no" '
611             f'width="1600" height="1200" '
612             f'src="../..{out_file_name.replace(u"_build", u"")}_in.html">'
613             f'</iframe>\n\n'
614         )
615
616         if legend:
617             try:
618                 itm_lst = legend[1:-2].split(u"\n")
619                 rst_file.write(
620                     f"{itm_lst[0]}\n\n- " + u'\n- '.join(itm_lst[1:]) + u"\n\n"
621                 )
622             except IndexError as err:
623                 logging.error(f"Legend cannot be written to html file\n{err}")
624         if footnote:
625             try:
626                 itm_lst = footnote[1:].split(u"\n")
627                 rst_file.write(
628                     f"{itm_lst[0]}\n\n- " + u'\n- '.join(itm_lst[1:]) + u"\n\n"
629                 )
630             except IndexError as err:
631                 logging.error(f"Footnote cannot be written to html file\n{err}")
632
633
634 def table_soak_vs_ndr(table, input_data):
635     """Generate the table(s) with algorithm: table_soak_vs_ndr
636     specified in the specification file.
637
638     :param table: Table to generate.
639     :param input_data: Data to process.
640     :type table: pandas.Series
641     :type input_data: InputData
642     """
643
644     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
645
646     # Transform the data
647     logging.info(
648         f"    Creating the data set for the {table.get(u'type', u'')} "
649         f"{table.get(u'title', u'')}."
650     )
651     data = input_data.filter_data(table, continue_on_error=True)
652
653     # Prepare the header of the table
654     try:
655         header = [
656             u"Test Case",
657             f"Avg({table[u'reference'][u'title']})",
658             f"Stdev({table[u'reference'][u'title']})",
659             f"Avg({table[u'compare'][u'title']})",
660             f"Stdev{table[u'compare'][u'title']})",
661             u"Diff",
662             u"Stdev(Diff)"
663         ]
664         header_str = u";".join(header) + u"\n"
665         legend = (
666             u"\nLegend:\n"
667             f"Avg({table[u'reference'][u'title']}): "
668             f"Mean value of {table[u'reference'][u'title']} [Mpps] computed "
669             f"from a series of runs of the listed tests.\n"
670             f"Stdev({table[u'reference'][u'title']}): "
671             f"Standard deviation value of {table[u'reference'][u'title']} "
672             f"[Mpps] computed from a series of runs of the listed tests.\n"
673             f"Avg({table[u'compare'][u'title']}): "
674             f"Mean value of {table[u'compare'][u'title']} [Mpps] computed from "
675             f"a series of runs of the listed tests.\n"
676             f"Stdev({table[u'compare'][u'title']}): "
677             f"Standard deviation value of {table[u'compare'][u'title']} [Mpps] "
678             f"computed from a series of runs of the listed tests.\n"
679             f"Diff({table[u'reference'][u'title']},"
680             f"{table[u'compare'][u'title']}): "
681             f"Percentage change calculated for mean values.\n"
682             u"Stdev(Diff): "
683             u"Standard deviation of percentage change calculated for mean "
684             u"values."
685         )
686     except (AttributeError, KeyError) as err:
687         logging.error(f"The model is invalid, missing parameter: {repr(err)}")
688         return
689
690     # Create a list of available SOAK test results:
691     tbl_dict = dict()
692     for job, builds in table[u"compare"][u"data"].items():
693         for build in builds:
694             for tst_name, tst_data in data[job][str(build)].items():
695                 if tst_data[u"type"] == u"SOAK":
696                     tst_name_mod = tst_name.replace(u"-soak", u"")
697                     if tbl_dict.get(tst_name_mod, None) is None:
698                         groups = re.search(REGEX_NIC, tst_data[u"parent"])
699                         nic = groups.group(0) if groups else u""
700                         name = (
701                             f"{nic}-"
702                             f"{u'-'.join(tst_data[u'name'].split(u'-')[:-1])}"
703                         )
704                         tbl_dict[tst_name_mod] = {
705                             u"name": name,
706                             u"ref-data": list(),
707                             u"cmp-data": list()
708                         }
709                     try:
710                         tbl_dict[tst_name_mod][u"cmp-data"].append(
711                             tst_data[u"throughput"][u"LOWER"])
712                     except (KeyError, TypeError):
713                         pass
714     tests_lst = tbl_dict.keys()
715
716     # Add corresponding NDR test results:
717     for job, builds in table[u"reference"][u"data"].items():
718         for build in builds:
719             for tst_name, tst_data in data[job][str(build)].items():
720                 tst_name_mod = tst_name.replace(u"-ndrpdr", u"").\
721                     replace(u"-mrr", u"")
722                 if tst_name_mod not in tests_lst:
723                     continue
724                 try:
725                     if tst_data[u"type"] not in (u"NDRPDR", u"MRR", u"BMRR"):
726                         continue
727                     if table[u"include-tests"] == u"MRR":
728                         result = (tst_data[u"result"][u"receive-rate"],
729                                   tst_data[u"result"][u"receive-stdev"])
730                     elif table[u"include-tests"] == u"PDR":
731                         result = \
732                             tst_data[u"throughput"][u"PDR"][u"LOWER"]
733                     elif table[u"include-tests"] == u"NDR":
734                         result = \
735                             tst_data[u"throughput"][u"NDR"][u"LOWER"]
736                     else:
737                         result = None
738                     if result is not None:
739                         tbl_dict[tst_name_mod][u"ref-data"].append(
740                             result)
741                 except (KeyError, TypeError):
742                     continue
743
744     tbl_lst = list()
745     for tst_name in tbl_dict:
746         item = [tbl_dict[tst_name][u"name"], ]
747         data_r = tbl_dict[tst_name][u"ref-data"]
748         if data_r:
749             if table[u"include-tests"] == u"MRR":
750                 data_r_mean = data_r[0][0]
751                 data_r_stdev = data_r[0][1]
752             else:
753                 data_r_mean = mean(data_r)
754                 data_r_stdev = stdev(data_r)
755             item.append(round(data_r_mean / 1e6, 1))
756             item.append(round(data_r_stdev / 1e6, 1))
757         else:
758             data_r_mean = None
759             data_r_stdev = None
760             item.extend([None, None])
761         data_c = tbl_dict[tst_name][u"cmp-data"]
762         if data_c:
763             if table[u"include-tests"] == u"MRR":
764                 data_c_mean = data_c[0][0]
765                 data_c_stdev = data_c[0][1]
766             else:
767                 data_c_mean = mean(data_c)
768                 data_c_stdev = stdev(data_c)
769             item.append(round(data_c_mean / 1e6, 1))
770             item.append(round(data_c_stdev / 1e6, 1))
771         else:
772             data_c_mean = None
773             data_c_stdev = None
774             item.extend([None, None])
775         if data_r_mean is not None and data_c_mean is not None:
776             delta, d_stdev = relative_change_stdev(
777                 data_r_mean, data_c_mean, data_r_stdev, data_c_stdev)
778             try:
779                 item.append(round(delta))
780             except ValueError:
781                 item.append(delta)
782             try:
783                 item.append(round(d_stdev))
784             except ValueError:
785                 item.append(d_stdev)
786             tbl_lst.append(item)
787
788     # Sort the table according to the relative change
789     tbl_lst.sort(key=lambda rel: rel[-1], reverse=True)
790
791     # Generate csv tables:
792     csv_file_name = f"{table[u'output-file']}.csv"
793     with open(csv_file_name, u"wt") as file_handler:
794         file_handler.write(header_str)
795         for test in tbl_lst:
796             file_handler.write(u";".join([str(item) for item in test]) + u"\n")
797
798     convert_csv_to_pretty_txt(
799         csv_file_name, f"{table[u'output-file']}.txt", delimiter=u";"
800     )
801     with open(f"{table[u'output-file']}.txt", u'a') as file_handler:
802         file_handler.write(legend)
803
804     # Generate html table:
805     _tpc_generate_html_table(
806         header,
807         tbl_lst,
808         table[u'output-file'],
809         legend=legend,
810         title=table.get(u"title", u"")
811     )
812
813
814 def table_perf_trending_dash(table, input_data):
815     """Generate the table(s) with algorithm:
816     table_perf_trending_dash
817     specified in the specification file.
818
819     :param table: Table to generate.
820     :param input_data: Data to process.
821     :type table: pandas.Series
822     :type input_data: InputData
823     """
824
825     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
826
827     # Transform the data
828     logging.info(
829         f"    Creating the data set for the {table.get(u'type', u'')} "
830         f"{table.get(u'title', u'')}."
831     )
832     data = input_data.filter_data(table, continue_on_error=True)
833
834     # Prepare the header of the tables
835     header = [
836         u"Test Case",
837         u"Trend [Mpps]",
838         u"Short-Term Change [%]",
839         u"Long-Term Change [%]",
840         u"Regressions [#]",
841         u"Progressions [#]"
842     ]
843     header_str = u",".join(header) + u"\n"
844
845     incl_tests = table.get(u"include-tests", u"MRR")
846
847     # Prepare data to the table:
848     tbl_dict = dict()
849     for job, builds in table[u"data"].items():
850         for build in builds:
851             for tst_name, tst_data in data[job][str(build)].items():
852                 if tst_name.lower() in table.get(u"ignore-list", list()):
853                     continue
854                 if tbl_dict.get(tst_name, None) is None:
855                     groups = re.search(REGEX_NIC, tst_data[u"parent"])
856                     if not groups:
857                         continue
858                     nic = groups.group(0)
859                     tbl_dict[tst_name] = {
860                         u"name": f"{nic}-{tst_data[u'name']}",
861                         u"data": OrderedDict()
862                     }
863                 try:
864                     if incl_tests == u"MRR":
865                         tbl_dict[tst_name][u"data"][str(build)] = \
866                             tst_data[u"result"][u"receive-rate"]
867                     elif incl_tests == u"NDR":
868                         tbl_dict[tst_name][u"data"][str(build)] = \
869                             tst_data[u"throughput"][u"NDR"][u"LOWER"]
870                     elif incl_tests == u"PDR":
871                         tbl_dict[tst_name][u"data"][str(build)] = \
872                             tst_data[u"throughput"][u"PDR"][u"LOWER"]
873                 except (TypeError, KeyError):
874                     pass  # No data in output.xml for this test
875
876     tbl_lst = list()
877     for tst_name in tbl_dict:
878         data_t = tbl_dict[tst_name][u"data"]
879         if len(data_t) < 2:
880             continue
881
882         classification_lst, avgs, _ = classify_anomalies(data_t)
883
884         win_size = min(len(data_t), table[u"window"])
885         long_win_size = min(len(data_t), table[u"long-trend-window"])
886
887         try:
888             max_long_avg = max(
889                 [x for x in avgs[-long_win_size:-win_size]
890                  if not isnan(x)])
891         except ValueError:
892             max_long_avg = nan
893         last_avg = avgs[-1]
894         avg_week_ago = avgs[max(-win_size, -len(avgs))]
895
896         if isnan(last_avg) or isnan(avg_week_ago) or avg_week_ago == 0.0:
897             rel_change_last = nan
898         else:
899             rel_change_last = round(
900                 ((last_avg - avg_week_ago) / avg_week_ago) * 1e2, 2)
901
902         if isnan(max_long_avg) or isnan(last_avg) or max_long_avg == 0.0:
903             rel_change_long = nan
904         else:
905             rel_change_long = round(
906                 ((last_avg - max_long_avg) / max_long_avg) * 1e2, 2)
907
908         if classification_lst:
909             if isnan(rel_change_last) and isnan(rel_change_long):
910                 continue
911             if isnan(last_avg) or isnan(rel_change_last) or \
912                     isnan(rel_change_long):
913                 continue
914             tbl_lst.append(
915                 [tbl_dict[tst_name][u"name"],
916                  round(last_avg / 1e6, 2),
917                  rel_change_last,
918                  rel_change_long,
919                  classification_lst[-win_size+1:].count(u"regression"),
920                  classification_lst[-win_size+1:].count(u"progression")])
921
922     tbl_lst.sort(key=lambda rel: rel[0])
923     tbl_lst.sort(key=lambda rel: rel[3])
924     tbl_lst.sort(key=lambda rel: rel[2])
925
926     tbl_sorted = list()
927     for nrr in range(table[u"window"], -1, -1):
928         tbl_reg = [item for item in tbl_lst if item[4] == nrr]
929         for nrp in range(table[u"window"], -1, -1):
930             tbl_out = [item for item in tbl_reg if item[5] == nrp]
931             tbl_sorted.extend(tbl_out)
932
933     file_name = f"{table[u'output-file']}{table[u'output-file-ext']}"
934
935     logging.info(f"    Writing file: {file_name}")
936     with open(file_name, u"wt") as file_handler:
937         file_handler.write(header_str)
938         for test in tbl_sorted:
939             file_handler.write(u",".join([str(item) for item in test]) + u'\n')
940
941     logging.info(f"    Writing file: {table[u'output-file']}.txt")
942     convert_csv_to_pretty_txt(file_name, f"{table[u'output-file']}.txt")
943
944
945 def _generate_url(testbed, test_name):
946     """Generate URL to a trending plot from the name of the test case.
947
948     :param testbed: The testbed used for testing.
949     :param test_name: The name of the test case.
950     :type testbed: str
951     :type test_name: str
952     :returns: The URL to the plot with the trending data for the given test
953         case.
954     :rtype str
955     """
956
957     if u"x520" in test_name:
958         nic = u"x520"
959     elif u"x710" in test_name:
960         nic = u"x710"
961     elif u"xl710" in test_name:
962         nic = u"xl710"
963     elif u"xxv710" in test_name:
964         nic = u"xxv710"
965     elif u"vic1227" in test_name:
966         nic = u"vic1227"
967     elif u"vic1385" in test_name:
968         nic = u"vic1385"
969     elif u"x553" in test_name:
970         nic = u"x553"
971     elif u"cx556" in test_name or u"cx556a" in test_name:
972         nic = u"cx556a"
973     else:
974         nic = u""
975
976     if u"64b" in test_name:
977         frame_size = u"64b"
978     elif u"78b" in test_name:
979         frame_size = u"78b"
980     elif u"imix" in test_name:
981         frame_size = u"imix"
982     elif u"9000b" in test_name:
983         frame_size = u"9000b"
984     elif u"1518b" in test_name:
985         frame_size = u"1518b"
986     elif u"114b" in test_name:
987         frame_size = u"114b"
988     else:
989         frame_size = u""
990
991     if u"1t1c" in test_name or \
992         (u"-1c-" in test_name and
993          testbed in (u"3n-hsw", u"3n-tsh", u"2n-dnv", u"3n-dnv")):
994         cores = u"1t1c"
995     elif u"2t2c" in test_name or \
996          (u"-2c-" in test_name and
997           testbed in (u"3n-hsw", u"3n-tsh", u"2n-dnv", u"3n-dnv")):
998         cores = u"2t2c"
999     elif u"4t4c" in test_name or \
1000          (u"-4c-" in test_name and
1001           testbed in (u"3n-hsw", u"3n-tsh", u"2n-dnv", u"3n-dnv")):
1002         cores = u"4t4c"
1003     elif u"2t1c" in test_name or \
1004          (u"-1c-" in test_name and
1005           testbed in (u"2n-skx", u"3n-skx", u"2n-clx")):
1006         cores = u"2t1c"
1007     elif u"4t2c" in test_name or \
1008          (u"-2c-" in test_name and
1009           testbed in (u"2n-skx", u"3n-skx", u"2n-clx")):
1010         cores = u"4t2c"
1011     elif u"8t4c" in test_name or \
1012          (u"-4c-" in test_name and
1013           testbed in (u"2n-skx", u"3n-skx", u"2n-clx")):
1014         cores = u"8t4c"
1015     else:
1016         cores = u""
1017
1018     if u"testpmd" in test_name:
1019         driver = u"testpmd"
1020     elif u"l3fwd" in test_name:
1021         driver = u"l3fwd"
1022     elif u"avf" in test_name:
1023         driver = u"avf"
1024     elif u"rdma" in test_name:
1025         driver = u"rdma"
1026     elif u"dnv" in testbed or u"tsh" in testbed:
1027         driver = u"ixgbe"
1028     else:
1029         driver = u"dpdk"
1030
1031     if u"macip-iacl1s" in test_name:
1032         bsf = u"features-macip-iacl1"
1033     elif u"macip-iacl10s" in test_name:
1034         bsf = u"features-macip-iacl01"
1035     elif u"macip-iacl50s" in test_name:
1036         bsf = u"features-macip-iacl50"
1037     elif u"iacl1s" in test_name:
1038         bsf = u"features-iacl1"
1039     elif u"iacl10s" in test_name:
1040         bsf = u"features-iacl10"
1041     elif u"iacl50s" in test_name:
1042         bsf = u"features-iacl50"
1043     elif u"oacl1s" in test_name:
1044         bsf = u"features-oacl1"
1045     elif u"oacl10s" in test_name:
1046         bsf = u"features-oacl10"
1047     elif u"oacl50s" in test_name:
1048         bsf = u"features-oacl50"
1049     elif u"udpsrcscale" in test_name:
1050         bsf = u"features-udp"
1051     elif u"iacl" in test_name:
1052         bsf = u"features"
1053     elif u"policer" in test_name:
1054         bsf = u"features"
1055     elif u"cop" in test_name:
1056         bsf = u"features"
1057     elif u"nat" in test_name:
1058         bsf = u"features"
1059     elif u"macip" in test_name:
1060         bsf = u"features"
1061     elif u"scale" in test_name:
1062         bsf = u"scale"
1063     elif u"base" in test_name:
1064         bsf = u"base"
1065     else:
1066         bsf = u"base"
1067
1068     if u"114b" in test_name and u"vhost" in test_name:
1069         domain = u"vts"
1070     elif u"testpmd" in test_name or u"l3fwd" in test_name:
1071         domain = u"dpdk"
1072     elif u"memif" in test_name:
1073         domain = u"container_memif"
1074     elif u"srv6" in test_name:
1075         domain = u"srv6"
1076     elif u"vhost" in test_name:
1077         domain = u"vhost"
1078         if u"vppl2xc" in test_name:
1079             driver += u"-vpp"
1080         else:
1081             driver += u"-testpmd"
1082         if u"lbvpplacp" in test_name:
1083             bsf += u"-link-bonding"
1084     elif u"ch" in test_name and u"vh" in test_name and u"vm" in test_name:
1085         domain = u"nf_service_density_vnfc"
1086     elif u"ch" in test_name and u"mif" in test_name and u"dcr" in test_name:
1087         domain = u"nf_service_density_cnfc"
1088     elif u"pl" in test_name and u"mif" in test_name and u"dcr" in test_name:
1089         domain = u"nf_service_density_cnfp"
1090     elif u"ipsec" in test_name:
1091         domain = u"ipsec"
1092         if u"sw" in test_name:
1093             bsf += u"-sw"
1094         elif u"hw" in test_name:
1095             bsf += u"-hw"
1096     elif u"ethip4vxlan" in test_name:
1097         domain = u"ip4_tunnels"
1098     elif u"ip4base" in test_name or u"ip4scale" in test_name:
1099         domain = u"ip4"
1100     elif u"ip6base" in test_name or u"ip6scale" in test_name:
1101         domain = u"ip6"
1102     elif u"l2xcbase" in test_name or \
1103             u"l2xcscale" in test_name or \
1104             u"l2bdbasemaclrn" in test_name or \
1105             u"l2bdscale" in test_name or \
1106             u"l2patch" in test_name:
1107         domain = u"l2"
1108     else:
1109         domain = u""
1110
1111     file_name = u"-".join((domain, testbed, nic)) + u".html#"
1112     anchor_name = u"-".join((frame_size, cores, bsf, driver))
1113
1114     return file_name + anchor_name
1115
1116
1117 def table_perf_trending_dash_html(table, input_data):
1118     """Generate the table(s) with algorithm:
1119     table_perf_trending_dash_html specified in the specification
1120     file.
1121
1122     :param table: Table to generate.
1123     :param input_data: Data to process.
1124     :type table: dict
1125     :type input_data: InputData
1126     """
1127
1128     _ = input_data
1129
1130     if not table.get(u"testbed", None):
1131         logging.error(
1132             f"The testbed is not defined for the table "
1133             f"{table.get(u'title', u'')}. Skipping."
1134         )
1135         return
1136
1137     test_type = table.get(u"test-type", u"MRR")
1138     if test_type not in (u"MRR", u"NDR", u"PDR"):
1139         logging.error(
1140             f"Test type {table.get(u'test-type', u'MRR')} is not defined. "
1141             f"Skipping."
1142         )
1143         return
1144
1145     if test_type in (u"NDR", u"PDR"):
1146         lnk_dir = u"../ndrpdr_trending/"
1147         lnk_sufix = f"-{test_type.lower()}"
1148     else:
1149         lnk_dir = u"../trending/"
1150         lnk_sufix = u""
1151
1152     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1153
1154     try:
1155         with open(table[u"input-file"], u'rt') as csv_file:
1156             csv_lst = list(csv.reader(csv_file, delimiter=u',', quotechar=u'"'))
1157     except KeyError:
1158         logging.warning(u"The input file is not defined.")
1159         return
1160     except csv.Error as err:
1161         logging.warning(
1162             f"Not possible to process the file {table[u'input-file']}.\n"
1163             f"{repr(err)}"
1164         )
1165         return
1166
1167     # Table:
1168     dashboard = ET.Element(u"table", attrib=dict(width=u"100%", border=u'0'))
1169
1170     # Table header:
1171     trow = ET.SubElement(dashboard, u"tr", attrib=dict(bgcolor=u"#7eade7"))
1172     for idx, item in enumerate(csv_lst[0]):
1173         alignment = u"left" if idx == 0 else u"center"
1174         thead = ET.SubElement(trow, u"th", attrib=dict(align=alignment))
1175         thead.text = item
1176
1177     # Rows:
1178     colors = {
1179         u"regression": (
1180             u"#ffcccc",
1181             u"#ff9999"
1182         ),
1183         u"progression": (
1184             u"#c6ecc6",
1185             u"#9fdf9f"
1186         ),
1187         u"normal": (
1188             u"#e9f1fb",
1189             u"#d4e4f7"
1190         )
1191     }
1192     for r_idx, row in enumerate(csv_lst[1:]):
1193         if int(row[4]):
1194             color = u"regression"
1195         elif int(row[5]):
1196             color = u"progression"
1197         else:
1198             color = u"normal"
1199         trow = ET.SubElement(
1200             dashboard, u"tr", attrib=dict(bgcolor=colors[color][r_idx % 2])
1201         )
1202
1203         # Columns:
1204         for c_idx, item in enumerate(row):
1205             tdata = ET.SubElement(
1206                 trow,
1207                 u"td",
1208                 attrib=dict(align=u"left" if c_idx == 0 else u"center")
1209             )
1210             # Name:
1211             if c_idx == 0 and table.get(u"add-links", True):
1212                 ref = ET.SubElement(
1213                     tdata,
1214                     u"a",
1215                     attrib=dict(
1216                         href=f"{lnk_dir}"
1217                              f"{_generate_url(table.get(u'testbed', ''), item)}"
1218                              f"{lnk_sufix}"
1219                     )
1220                 )
1221                 ref.text = item
1222             else:
1223                 tdata.text = item
1224     try:
1225         with open(table[u"output-file"], u'w') as html_file:
1226             logging.info(f"    Writing file: {table[u'output-file']}")
1227             html_file.write(u".. raw:: html\n\n\t")
1228             html_file.write(str(ET.tostring(dashboard, encoding=u"unicode")))
1229             html_file.write(u"\n\t<p><br><br></p>\n")
1230     except KeyError:
1231         logging.warning(u"The output file is not defined.")
1232         return
1233
1234
1235 def table_last_failed_tests(table, input_data):
1236     """Generate the table(s) with algorithm: table_last_failed_tests
1237     specified in the specification file.
1238
1239     :param table: Table to generate.
1240     :param input_data: Data to process.
1241     :type table: pandas.Series
1242     :type input_data: InputData
1243     """
1244
1245     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1246
1247     # Transform the data
1248     logging.info(
1249         f"    Creating the data set for the {table.get(u'type', u'')} "
1250         f"{table.get(u'title', u'')}."
1251     )
1252
1253     data = input_data.filter_data(table, continue_on_error=True)
1254
1255     if data is None or data.empty:
1256         logging.warning(
1257             f"    No data for the {table.get(u'type', u'')} "
1258             f"{table.get(u'title', u'')}."
1259         )
1260         return
1261
1262     tbl_list = list()
1263     for job, builds in table[u"data"].items():
1264         for build in builds:
1265             build = str(build)
1266             try:
1267                 version = input_data.metadata(job, build).get(u"version", u"")
1268             except KeyError:
1269                 logging.error(f"Data for {job}: {build} is not present.")
1270                 return
1271             tbl_list.append(build)
1272             tbl_list.append(version)
1273             failed_tests = list()
1274             passed = 0
1275             failed = 0
1276             for tst_data in data[job][build].values:
1277                 if tst_data[u"status"] != u"FAIL":
1278                     passed += 1
1279                     continue
1280                 failed += 1
1281                 groups = re.search(REGEX_NIC, tst_data[u"parent"])
1282                 if not groups:
1283                     continue
1284                 nic = groups.group(0)
1285                 failed_tests.append(f"{nic}-{tst_data[u'name']}")
1286             tbl_list.append(str(passed))
1287             tbl_list.append(str(failed))
1288             tbl_list.extend(failed_tests)
1289
1290     file_name = f"{table[u'output-file']}{table[u'output-file-ext']}"
1291     logging.info(f"    Writing file: {file_name}")
1292     with open(file_name, u"wt") as file_handler:
1293         for test in tbl_list:
1294             file_handler.write(test + u'\n')
1295
1296
1297 def table_failed_tests(table, input_data):
1298     """Generate the table(s) with algorithm: table_failed_tests
1299     specified in the specification file.
1300
1301     :param table: Table to generate.
1302     :param input_data: Data to process.
1303     :type table: pandas.Series
1304     :type input_data: InputData
1305     """
1306
1307     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1308
1309     # Transform the data
1310     logging.info(
1311         f"    Creating the data set for the {table.get(u'type', u'')} "
1312         f"{table.get(u'title', u'')}."
1313     )
1314     data = input_data.filter_data(table, continue_on_error=True)
1315
1316     test_type = u"MRR"
1317     if u"NDRPDR" in table.get(u"filter", list()):
1318         test_type = u"NDRPDR"
1319
1320     # Prepare the header of the tables
1321     header = [
1322         u"Test Case",
1323         u"Failures [#]",
1324         u"Last Failure [Time]",
1325         u"Last Failure [VPP-Build-Id]",
1326         u"Last Failure [CSIT-Job-Build-Id]"
1327     ]
1328
1329     # Generate the data for the table according to the model in the table
1330     # specification
1331
1332     now = dt.utcnow()
1333     timeperiod = timedelta(int(table.get(u"window", 7)))
1334
1335     tbl_dict = dict()
1336     for job, builds in table[u"data"].items():
1337         for build in builds:
1338             build = str(build)
1339             for tst_name, tst_data in data[job][build].items():
1340                 if tst_name.lower() in table.get(u"ignore-list", list()):
1341                     continue
1342                 if tbl_dict.get(tst_name, None) is None:
1343                     groups = re.search(REGEX_NIC, tst_data[u"parent"])
1344                     if not groups:
1345                         continue
1346                     nic = groups.group(0)
1347                     tbl_dict[tst_name] = {
1348                         u"name": f"{nic}-{tst_data[u'name']}",
1349                         u"data": OrderedDict()
1350                     }
1351                 try:
1352                     generated = input_data.metadata(job, build).\
1353                         get(u"generated", u"")
1354                     if not generated:
1355                         continue
1356                     then = dt.strptime(generated, u"%Y%m%d %H:%M")
1357                     if (now - then) <= timeperiod:
1358                         tbl_dict[tst_name][u"data"][build] = (
1359                             tst_data[u"status"],
1360                             generated,
1361                             input_data.metadata(job, build).get(u"version",
1362                                                                 u""),
1363                             build
1364                         )
1365                 except (TypeError, KeyError) as err:
1366                     logging.warning(f"tst_name: {tst_name} - err: {repr(err)}")
1367
1368     max_fails = 0
1369     tbl_lst = list()
1370     for tst_data in tbl_dict.values():
1371         fails_nr = 0
1372         fails_last_date = u""
1373         fails_last_vpp = u""
1374         fails_last_csit = u""
1375         for val in tst_data[u"data"].values():
1376             if val[0] == u"FAIL":
1377                 fails_nr += 1
1378                 fails_last_date = val[1]
1379                 fails_last_vpp = val[2]
1380                 fails_last_csit = val[3]
1381         if fails_nr:
1382             max_fails = fails_nr if fails_nr > max_fails else max_fails
1383             tbl_lst.append([
1384                 tst_data[u"name"],
1385                 fails_nr,
1386                 fails_last_date,
1387                 fails_last_vpp,
1388                 f"{u'mrr-daily' if test_type == u'MRR' else u'ndrpdr-weekly'}"
1389                 f"-build-{fails_last_csit}"
1390             ])
1391
1392     tbl_lst.sort(key=lambda rel: rel[2], reverse=True)
1393     tbl_sorted = list()
1394     for nrf in range(max_fails, -1, -1):
1395         tbl_fails = [item for item in tbl_lst if item[1] == nrf]
1396         tbl_sorted.extend(tbl_fails)
1397
1398     file_name = f"{table[u'output-file']}{table[u'output-file-ext']}"
1399     logging.info(f"    Writing file: {file_name}")
1400     with open(file_name, u"wt") as file_handler:
1401         file_handler.write(u",".join(header) + u"\n")
1402         for test in tbl_sorted:
1403             file_handler.write(u",".join([str(item) for item in test]) + u'\n')
1404
1405     logging.info(f"    Writing file: {table[u'output-file']}.txt")
1406     convert_csv_to_pretty_txt(file_name, f"{table[u'output-file']}.txt")
1407
1408
1409 def table_failed_tests_html(table, input_data):
1410     """Generate the table(s) with algorithm: table_failed_tests_html
1411     specified in the specification file.
1412
1413     :param table: Table to generate.
1414     :param input_data: Data to process.
1415     :type table: pandas.Series
1416     :type input_data: InputData
1417     """
1418
1419     _ = input_data
1420
1421     if not table.get(u"testbed", None):
1422         logging.error(
1423             f"The testbed is not defined for the table "
1424             f"{table.get(u'title', u'')}. Skipping."
1425         )
1426         return
1427
1428     test_type = table.get(u"test-type", u"MRR")
1429     if test_type not in (u"MRR", u"NDR", u"PDR", u"NDRPDR"):
1430         logging.error(
1431             f"Test type {table.get(u'test-type', u'MRR')} is not defined. "
1432             f"Skipping."
1433         )
1434         return
1435
1436     if test_type in (u"NDRPDR", u"NDR", u"PDR"):
1437         lnk_dir = u"../ndrpdr_trending/"
1438         lnk_sufix = u"-pdr"
1439     else:
1440         lnk_dir = u"../trending/"
1441         lnk_sufix = u""
1442
1443     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1444
1445     try:
1446         with open(table[u"input-file"], u'rt') as csv_file:
1447             csv_lst = list(csv.reader(csv_file, delimiter=u',', quotechar=u'"'))
1448     except KeyError:
1449         logging.warning(u"The input file is not defined.")
1450         return
1451     except csv.Error as err:
1452         logging.warning(
1453             f"Not possible to process the file {table[u'input-file']}.\n"
1454             f"{repr(err)}"
1455         )
1456         return
1457
1458     # Table:
1459     failed_tests = ET.Element(u"table", attrib=dict(width=u"100%", border=u'0'))
1460
1461     # Table header:
1462     trow = ET.SubElement(failed_tests, u"tr", attrib=dict(bgcolor=u"#7eade7"))
1463     for idx, item in enumerate(csv_lst[0]):
1464         alignment = u"left" if idx == 0 else u"center"
1465         thead = ET.SubElement(trow, u"th", attrib=dict(align=alignment))
1466         thead.text = item
1467
1468     # Rows:
1469     colors = (u"#e9f1fb", u"#d4e4f7")
1470     for r_idx, row in enumerate(csv_lst[1:]):
1471         background = colors[r_idx % 2]
1472         trow = ET.SubElement(
1473             failed_tests, u"tr", attrib=dict(bgcolor=background)
1474         )
1475
1476         # Columns:
1477         for c_idx, item in enumerate(row):
1478             tdata = ET.SubElement(
1479                 trow,
1480                 u"td",
1481                 attrib=dict(align=u"left" if c_idx == 0 else u"center")
1482             )
1483             # Name:
1484             if c_idx == 0 and table.get(u"add-links", True):
1485                 ref = ET.SubElement(
1486                     tdata,
1487                     u"a",
1488                     attrib=dict(
1489                         href=f"{lnk_dir}"
1490                              f"{_generate_url(table.get(u'testbed', ''), item)}"
1491                              f"{lnk_sufix}"
1492                     )
1493                 )
1494                 ref.text = item
1495             else:
1496                 tdata.text = item
1497     try:
1498         with open(table[u"output-file"], u'w') as html_file:
1499             logging.info(f"    Writing file: {table[u'output-file']}")
1500             html_file.write(u".. raw:: html\n\n\t")
1501             html_file.write(str(ET.tostring(failed_tests, encoding=u"unicode")))
1502             html_file.write(u"\n\t<p><br><br></p>\n")
1503     except KeyError:
1504         logging.warning(u"The output file is not defined.")
1505         return
1506
1507
1508 def table_comparison(table, input_data):
1509     """Generate the table(s) with algorithm: table_comparison
1510     specified in the specification file.
1511
1512     :param table: Table to generate.
1513     :param input_data: Data to process.
1514     :type table: pandas.Series
1515     :type input_data: InputData
1516     """
1517     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1518
1519     # Transform the data
1520     logging.info(
1521         f"    Creating the data set for the {table.get(u'type', u'')} "
1522         f"{table.get(u'title', u'')}."
1523     )
1524
1525     columns = table.get(u"columns", None)
1526     if not columns:
1527         logging.error(
1528             f"No columns specified for {table.get(u'title', u'')}. Skipping."
1529         )
1530         return
1531
1532     cols = list()
1533     for idx, col in enumerate(columns):
1534         if col.get(u"data-set", None) is None:
1535             logging.warning(f"No data for column {col.get(u'title', u'')}")
1536             continue
1537         tag = col.get(u"tag", None)
1538         data = input_data.filter_data(
1539             table,
1540             params=[u"throughput", u"result", u"name", u"parent", u"tags"],
1541             data=col[u"data-set"],
1542             continue_on_error=True
1543         )
1544         col_data = {
1545             u"title": col.get(u"title", f"Column{idx}"),
1546             u"data": dict()
1547         }
1548         for builds in data.values:
1549             for build in builds:
1550                 for tst_name, tst_data in build.items():
1551                     if tag and tag not in tst_data[u"tags"]:
1552                         continue
1553                     tst_name_mod = \
1554                         _tpc_modify_test_name(tst_name, ignore_nic=True).\
1555                         replace(u"2n1l-", u"")
1556                     if col_data[u"data"].get(tst_name_mod, None) is None:
1557                         name = tst_data[u'name'].rsplit(u'-', 1)[0]
1558                         if u"across testbeds" in table[u"title"].lower() or \
1559                                 u"across topologies" in table[u"title"].lower():
1560                             name = _tpc_modify_displayed_test_name(name)
1561                         col_data[u"data"][tst_name_mod] = {
1562                             u"name": name,
1563                             u"replace": True,
1564                             u"data": list(),
1565                             u"mean": None,
1566                             u"stdev": None
1567                         }
1568                     _tpc_insert_data(
1569                         target=col_data[u"data"][tst_name_mod],
1570                         src=tst_data,
1571                         include_tests=table[u"include-tests"]
1572                     )
1573
1574         replacement = col.get(u"data-replacement", None)
1575         if replacement:
1576             rpl_data = input_data.filter_data(
1577                 table,
1578                 params=[u"throughput", u"result", u"name", u"parent", u"tags"],
1579                 data=replacement,
1580                 continue_on_error=True
1581             )
1582             for builds in rpl_data.values:
1583                 for build in builds:
1584                     for tst_name, tst_data in build.items():
1585                         if tag and tag not in tst_data[u"tags"]:
1586                             continue
1587                         tst_name_mod = \
1588                             _tpc_modify_test_name(tst_name, ignore_nic=True).\
1589                             replace(u"2n1l-", u"")
1590                         if col_data[u"data"].get(tst_name_mod, None) is None:
1591                             name = tst_data[u'name'].rsplit(u'-', 1)[0]
1592                             if u"across testbeds" in table[u"title"].lower() \
1593                                     or u"across topologies" in \
1594                                     table[u"title"].lower():
1595                                 name = _tpc_modify_displayed_test_name(name)
1596                             col_data[u"data"][tst_name_mod] = {
1597                                 u"name": name,
1598                                 u"replace": False,
1599                                 u"data": list(),
1600                                 u"mean": None,
1601                                 u"stdev": None
1602                             }
1603                         if col_data[u"data"][tst_name_mod][u"replace"]:
1604                             col_data[u"data"][tst_name_mod][u"replace"] = False
1605                             col_data[u"data"][tst_name_mod][u"data"] = list()
1606                         _tpc_insert_data(
1607                             target=col_data[u"data"][tst_name_mod],
1608                             src=tst_data,
1609                             include_tests=table[u"include-tests"]
1610                         )
1611
1612         if table[u"include-tests"] in (u"NDR", u"PDR"):
1613             for tst_name, tst_data in col_data[u"data"].items():
1614                 if tst_data[u"data"]:
1615                     tst_data[u"mean"] = mean(tst_data[u"data"])
1616                     tst_data[u"stdev"] = stdev(tst_data[u"data"])
1617
1618         cols.append(col_data)
1619
1620     tbl_dict = dict()
1621     for col in cols:
1622         for tst_name, tst_data in col[u"data"].items():
1623             if tbl_dict.get(tst_name, None) is None:
1624                 tbl_dict[tst_name] = {
1625                     "name": tst_data[u"name"]
1626                 }
1627             tbl_dict[tst_name][col[u"title"]] = {
1628                 u"mean": tst_data[u"mean"],
1629                 u"stdev": tst_data[u"stdev"]
1630             }
1631
1632     if not tbl_dict:
1633         logging.warning(f"No data for table {table.get(u'title', u'')}!")
1634         return
1635
1636     tbl_lst = list()
1637     for tst_data in tbl_dict.values():
1638         row = [tst_data[u"name"], ]
1639         for col in cols:
1640             row.append(tst_data.get(col[u"title"], None))
1641         tbl_lst.append(row)
1642
1643     comparisons = table.get(u"comparisons", None)
1644     if comparisons and isinstance(comparisons, list):
1645         for idx, comp in enumerate(comparisons):
1646             try:
1647                 col_ref = int(comp[u"reference"])
1648                 col_cmp = int(comp[u"compare"])
1649             except KeyError:
1650                 logging.warning(u"Comparison: No references defined! Skipping.")
1651                 comparisons.pop(idx)
1652                 continue
1653             if not (0 < col_ref <= len(cols) and
1654                     0 < col_cmp <= len(cols)) or \
1655                     col_ref == col_cmp:
1656                 logging.warning(f"Wrong values of reference={col_ref} "
1657                                 f"and/or compare={col_cmp}. Skipping.")
1658                 comparisons.pop(idx)
1659                 continue
1660
1661     tbl_cmp_lst = list()
1662     if comparisons:
1663         for row in tbl_lst:
1664             new_row = deepcopy(row)
1665             add_to_tbl = False
1666             for comp in comparisons:
1667                 ref_itm = row[int(comp[u"reference"])]
1668                 if ref_itm is None and \
1669                         comp.get(u"reference-alt", None) is not None:
1670                     ref_itm = row[int(comp[u"reference-alt"])]
1671                 cmp_itm = row[int(comp[u"compare"])]
1672                 if ref_itm is not None and cmp_itm is not None and \
1673                         ref_itm[u"mean"] is not None and \
1674                         cmp_itm[u"mean"] is not None and \
1675                         ref_itm[u"stdev"] is not None and \
1676                         cmp_itm[u"stdev"] is not None:
1677                     delta, d_stdev = relative_change_stdev(
1678                         ref_itm[u"mean"], cmp_itm[u"mean"],
1679                         ref_itm[u"stdev"], cmp_itm[u"stdev"]
1680                     )
1681                     new_row.append(
1682                         {
1683                             u"mean": delta * 1e6,
1684                             u"stdev": d_stdev * 1e6
1685                         }
1686                     )
1687                     add_to_tbl = True
1688                 else:
1689                     new_row.append(None)
1690             if add_to_tbl:
1691                 tbl_cmp_lst.append(new_row)
1692
1693     try:
1694         tbl_cmp_lst.sort(key=lambda rel: rel[0], reverse=False)
1695         tbl_cmp_lst.sort(key=lambda rel: rel[-1][u'mean'], reverse=True)
1696     except TypeError as err:
1697         logging.warning(f"Empty data element in table\n{tbl_cmp_lst}\n{err}")
1698
1699     rcas = list()
1700     rca_in = table.get(u"rca", None)
1701     if rca_in and isinstance(rca_in, list):
1702         for idx, itm in enumerate(rca_in):
1703             try:
1704                 with open(itm.get(u"data", u""), u"r") as rca_file:
1705                     rcas.append(
1706                         {
1707                             u"title": itm.get(u"title", f"RCA{idx}"),
1708                             u"data": load(rca_file, Loader=FullLoader)
1709                         }
1710                     )
1711             except (YAMLError, IOError) as err:
1712                 logging.warning(
1713                     f"The RCA file {itm.get(u'data', u'')} does not exist or "
1714                     f"it is corrupted!"
1715                 )
1716                 logging.debug(repr(err))
1717
1718     tbl_for_csv = list()
1719     for line in tbl_cmp_lst:
1720         row = [line[0], ]
1721         for idx, itm in enumerate(line[1:]):
1722             if itm is None or not isinstance(itm, dict) or\
1723                     itm.get(u'mean', None) is None or \
1724                     itm.get(u'stdev', None) is None:
1725                 row.append(u"NT")
1726                 row.append(u"NT")
1727             else:
1728                 row.append(round(float(itm[u'mean']) / 1e6, 3))
1729                 row.append(round(float(itm[u'stdev']) / 1e6, 3))
1730         for rca in rcas:
1731             rca_nr = rca[u"data"].get(row[0], u"-")
1732             row.append(f"[{rca_nr}]" if rca_nr != u"-" else u"-")
1733         tbl_for_csv.append(row)
1734
1735     header_csv = [u"Test Case", ]
1736     for col in cols:
1737         header_csv.append(f"Avg({col[u'title']})")
1738         header_csv.append(f"Stdev({col[u'title']})")
1739     for comp in comparisons:
1740         header_csv.append(
1741             f"Avg({comp.get(u'title', u'')})"
1742         )
1743         header_csv.append(
1744             f"Stdev({comp.get(u'title', u'')})"
1745         )
1746     header_csv.extend([rca[u"title"] for rca in rcas])
1747
1748     legend_lst = table.get(u"legend", None)
1749     if legend_lst is None:
1750         legend = u""
1751     else:
1752         legend = u"\n" + u"\n".join(legend_lst) + u"\n"
1753
1754     footnote = u""
1755     if rcas:
1756         footnote += u"\nRCA:\n"
1757         for rca in rcas:
1758             footnote += rca[u"data"].get(u"footnote", u"")
1759
1760     csv_file_name = f"{table[u'output-file']}-csv.csv"
1761     with open(csv_file_name, u"wt", encoding='utf-8') as file_handler:
1762         file_handler.write(
1763             u",".join([f'"{itm}"' for itm in header_csv]) + u"\n"
1764         )
1765         for test in tbl_for_csv:
1766             file_handler.write(
1767                 u",".join([f'"{item}"' for item in test]) + u"\n"
1768             )
1769         if legend_lst:
1770             for item in legend_lst:
1771                 file_handler.write(f'"{item}"\n')
1772         if footnote:
1773             for itm in footnote.split(u"\n"):
1774                 file_handler.write(f'"{itm}"\n')
1775
1776     tbl_tmp = list()
1777     max_lens = [0, ] * len(tbl_cmp_lst[0])
1778     for line in tbl_cmp_lst:
1779         row = [line[0], ]
1780         for idx, itm in enumerate(line[1:]):
1781             if itm is None or not isinstance(itm, dict) or \
1782                     itm.get(u'mean', None) is None or \
1783                     itm.get(u'stdev', None) is None:
1784                 new_itm = u"NT"
1785             else:
1786                 if idx < len(cols):
1787                     new_itm = (
1788                         f"{round(float(itm[u'mean']) / 1e6, 1)} "
1789                         f"\u00B1{round(float(itm[u'stdev']) / 1e6, 1)}".
1790                         replace(u"nan", u"NaN")
1791                     )
1792                 else:
1793                     new_itm = (
1794                         f"{round(float(itm[u'mean']) / 1e6, 1):+} "
1795                         f"\u00B1{round(float(itm[u'stdev']) / 1e6, 1)}".
1796                         replace(u"nan", u"NaN")
1797                     )
1798             if len(new_itm.rsplit(u" ", 1)[-1]) > max_lens[idx]:
1799                 max_lens[idx] = len(new_itm.rsplit(u" ", 1)[-1])
1800             row.append(new_itm)
1801
1802         tbl_tmp.append(row)
1803
1804     tbl_final = list()
1805     for line in tbl_tmp:
1806         row = [line[0], ]
1807         for idx, itm in enumerate(line[1:]):
1808             if itm in (u"NT", u"NaN"):
1809                 row.append(itm)
1810                 continue
1811             itm_lst = itm.rsplit(u"\u00B1", 1)
1812             itm_lst[-1] = \
1813                 f"{u' ' * (max_lens[idx] - len(itm_lst[-1]))}{itm_lst[-1]}"
1814             row.append(u"\u00B1".join(itm_lst))
1815         for rca in rcas:
1816             rca_nr = rca[u"data"].get(row[0], u"-")
1817             row.append(f"[{rca_nr}]" if rca_nr != u"-" else u"-")
1818
1819         tbl_final.append(row)
1820
1821     header = [u"Test Case", ]
1822     header.extend([col[u"title"] for col in cols])
1823     header.extend([comp.get(u"title", u"") for comp in comparisons])
1824     header.extend([rca[u"title"] for rca in rcas])
1825
1826     # Generate csv tables:
1827     csv_file_name = f"{table[u'output-file']}.csv"
1828     logging.info(f"    Writing the file {csv_file_name}")
1829     with open(csv_file_name, u"wt", encoding='utf-8') as file_handler:
1830         file_handler.write(u";".join(header) + u"\n")
1831         for test in tbl_final:
1832             file_handler.write(u";".join([str(item) for item in test]) + u"\n")
1833
1834     # Generate txt table:
1835     txt_file_name = f"{table[u'output-file']}.txt"
1836     logging.info(f"    Writing the file {txt_file_name}")
1837     convert_csv_to_pretty_txt(csv_file_name, txt_file_name, delimiter=u";")
1838
1839     with open(txt_file_name, u'a', encoding='utf-8') as file_handler:
1840         file_handler.write(legend)
1841         file_handler.write(footnote)
1842
1843     # Generate html table:
1844     _tpc_generate_html_table(
1845         header,
1846         tbl_final,
1847         table[u'output-file'],
1848         legend=legend,
1849         footnote=footnote,
1850         sort_data=False,
1851         title=table.get(u"title", u"")
1852     )
1853
1854
1855 def table_weekly_comparison(table, in_data):
1856     """Generate the table(s) with algorithm: table_weekly_comparison
1857     specified in the specification file.
1858
1859     :param table: Table to generate.
1860     :param in_data: Data to process.
1861     :type table: pandas.Series
1862     :type in_data: InputData
1863     """
1864     logging.info(f"  Generating the table {table.get(u'title', u'')} ...")
1865
1866     # Transform the data
1867     logging.info(
1868         f"    Creating the data set for the {table.get(u'type', u'')} "
1869         f"{table.get(u'title', u'')}."
1870     )
1871
1872     incl_tests = table.get(u"include-tests", None)
1873     if incl_tests not in (u"NDR", u"PDR"):
1874         logging.error(f"Wrong tests to include specified ({incl_tests}).")
1875         return
1876
1877     nr_cols = table.get(u"nr-of-data-columns", None)
1878     if not nr_cols or nr_cols < 2:
1879         logging.error(
1880             f"No columns specified for {table.get(u'title', u'')}. Skipping."
1881         )
1882         return
1883
1884     data = in_data.filter_data(
1885         table,
1886         params=[u"throughput", u"result", u"name", u"parent", u"tags"],
1887         continue_on_error=True
1888     )
1889
1890     header = [
1891         [u"VPP Version", ],
1892         [u"Start Timestamp", ],
1893         [u"CSIT Build", ],
1894         [u"CSIT Testbed", ]
1895     ]
1896     tbl_dict = dict()
1897     idx = 0
1898     tb_tbl = table.get(u"testbeds", None)
1899     for job_name, job_data in data.items():
1900         for build_nr, build in job_data.items():
1901             if idx >= nr_cols:
1902                 break
1903             if build.empty:
1904                 continue
1905
1906             tb_ip = in_data.metadata(job_name, build_nr).get(u"testbed", u"")
1907             if tb_ip and tb_tbl:
1908                 testbed = tb_tbl.get(tb_ip, u"")
1909             else:
1910                 testbed = u""
1911             header[2].insert(1, build_nr)
1912             header[3].insert(1, testbed)
1913             header[1].insert(
1914                 1, in_data.metadata(job_name, build_nr).get(u"generated", u"")
1915             )
1916             header[0].insert(
1917                 1, in_data.metadata(job_name, build_nr).get(u"version", u"")
1918             )
1919
1920             for tst_name, tst_data in build.items():
1921                 tst_name_mod = \
1922                     _tpc_modify_test_name(tst_name).replace(u"2n1l-", u"")
1923                 if not tbl_dict.get(tst_name_mod, None):
1924                     tbl_dict[tst_name_mod] = dict(
1925                         name=tst_data[u'name'].rsplit(u'-', 1)[0],
1926                     )
1927                 try:
1928                     tbl_dict[tst_name_mod][-idx - 1] = \
1929                         tst_data[u"throughput"][incl_tests][u"LOWER"]
1930                 except (TypeError, IndexError, KeyError, ValueError):
1931                     pass
1932             idx += 1
1933
1934     if idx < nr_cols:
1935         logging.error(u"Not enough data to build the table! Skipping")
1936         return
1937
1938     cmp_dict = dict()
1939     for idx, cmp in enumerate(table.get(u"comparisons", list())):
1940         idx_ref = cmp.get(u"reference", None)
1941         idx_cmp = cmp.get(u"compare", None)
1942         if idx_ref is None or idx_cmp is None:
1943             continue
1944         header[0].append(
1945             f"Diff({header[0][idx_ref - idx].split(u'~')[-1]} vs "
1946             f"{header[0][idx_cmp - idx].split(u'~')[-1]})"
1947         )
1948         header[1].append(u"")
1949         header[2].append(u"")
1950         header[3].append(u"")
1951         for tst_name, tst_data in tbl_dict.items():
1952             if not cmp_dict.get(tst_name, None):
1953                 cmp_dict[tst_name] = list()
1954             ref_data = tst_data.get(idx_ref, None)
1955             cmp_data = tst_data.get(idx_cmp, None)
1956             if ref_data is None or cmp_data is None:
1957                 cmp_dict[tst_name].append(float(u'nan'))
1958             else:
1959                 cmp_dict[tst_name].append(
1960                     relative_change(ref_data, cmp_data)
1961                 )
1962
1963     tbl_lst_none = list()
1964     tbl_lst = list()
1965     for tst_name, tst_data in tbl_dict.items():
1966         itm_lst = [tst_data[u"name"], ]
1967         for idx in range(nr_cols):
1968             item = tst_data.get(-idx - 1, None)
1969             if item is None:
1970                 itm_lst.insert(1, None)
1971             else:
1972                 itm_lst.insert(1, round(item / 1e6, 1))
1973         itm_lst.extend(
1974             [
1975                 None if itm is None else round(itm, 1)
1976                 for itm in cmp_dict[tst_name]
1977             ]
1978         )
1979         if str(itm_lst[-1]) == u"nan" or itm_lst[-1] is None:
1980             tbl_lst_none.append(itm_lst)
1981         else:
1982             tbl_lst.append(itm_lst)
1983
1984     tbl_lst_none.sort(key=lambda rel: rel[0], reverse=False)
1985     tbl_lst.sort(key=lambda rel: rel[0], reverse=False)
1986     tbl_lst.sort(key=lambda rel: rel[-1], reverse=False)
1987     tbl_lst.extend(tbl_lst_none)
1988
1989     # Generate csv table:
1990     csv_file_name = f"{table[u'output-file']}.csv"
1991     logging.info(f"    Writing the file {csv_file_name}")
1992     with open(csv_file_name, u"wt", encoding='utf-8') as file_handler:
1993         for hdr in header:
1994             file_handler.write(u",".join(hdr) + u"\n")
1995         for test in tbl_lst:
1996             file_handler.write(u",".join(
1997                 [
1998                     str(item).replace(u"None", u"-").replace(u"nan", u"-").
1999                     replace(u"null", u"-") for item in test
2000                 ]
2001             ) + u"\n")
2002
2003     txt_file_name = f"{table[u'output-file']}.txt"
2004     logging.info(f"    Writing the file {txt_file_name}")
2005     convert_csv_to_pretty_txt(csv_file_name, txt_file_name, delimiter=u",")
2006
2007     # Reorganize header in txt table
2008     txt_table = list()
2009     with open(txt_file_name, u"rt", encoding='utf-8') as file_handler:
2010         for line in file_handler:
2011             txt_table.append(line)
2012     try:
2013         txt_table.insert(5, txt_table.pop(2))
2014         with open(txt_file_name, u"wt", encoding='utf-8') as file_handler:
2015             file_handler.writelines(txt_table)
2016     except IndexError:
2017         pass
2018
2019     # Generate html table:
2020     hdr_html = [
2021         u"<br>".join(row) for row in zip(*header)
2022     ]
2023     _tpc_generate_html_table(
2024         hdr_html,
2025         tbl_lst,
2026         table[u'output-file'],
2027         sort_data=True,
2028         title=table.get(u"title", u""),
2029         generate_rst=False
2030     )