feat(uti): Add iterative data
[csit.git] / resources / tools / dash / app / pal / stats / layout.py
index d265145..5c3758b 100644 (file)
@@ -15,7 +15,6 @@
 """
 
 import logging
-import urllib
 import pandas as pd
 import dash_bootstrap_components as dbc
 
@@ -30,6 +29,7 @@ from datetime import datetime, timedelta
 from copy import deepcopy
 
 from ..data.data import Data
+from ..data.url_processing import url_decode, url_encode
 from .graphs import graph_statistics, select_data
 
 
@@ -39,7 +39,13 @@ class Layout:
 
     DEFAULT_JOB = "csit-vpp-perf-mrr-daily-master-2n-icx"
 
-    def __init__(self, app: Flask, html_layout_file: str, spec_file: str,
+    URL_STYLE = {
+        "background-color": "#d2ebf5",
+        "border-color": "#bce1f1",
+        "color": "#135d7c"
+    }
+
+    def __init__(self, app: Flask, html_layout_file: str,
         graph_layout_file: str, data_spec_file: str, tooltip_file: str,
         time_period: int=None) -> None:
         """
@@ -48,7 +54,6 @@ class Layout:
         # Inputs
         self._app = app
         self._html_layout_file = html_layout_file
-        self._spec_file = spec_file
         self._graph_layout_file = graph_layout_file
         self._data_spec_file = data_spec_file
         self._tooltip_file = tooltip_file
@@ -99,10 +104,10 @@ class Layout:
             "dut_version": list(),
             "hosts": list(),
             "passed": list(),
-            "failed": list()
+            "failed": list(),
+            "lst_failed": list()
         }
         for job in jobs:
-            # TODO: Add list of failed tests for each build
             df_job = df_tst_info.loc[(df_tst_info["job"] == job)]
             builds = df_job["build"].unique()
             for build in builds:
@@ -113,15 +118,25 @@ class Layout:
                 tst_info["dut_version"].append(df_build["dut_version"].iloc[-1])
                 tst_info["hosts"].append(df_build["hosts"].iloc[-1])
                 try:
-                    passed = df_build.value_counts(subset='passed')[True]
+                    passed = df_build.value_counts(subset="passed")[True]
                 except KeyError:
                     passed = 0
                 try:
-                    failed = df_build.value_counts(subset='passed')[False]
+                    failed = df_build.value_counts(subset="passed")[False]
+                    failed_tests = df_build.loc[(df_build["passed"] == False)]\
+                        ["test_id"].to_list()
+                    l_failed = list()
+                    for tst in failed_tests:
+                        lst_tst = tst.split(".")
+                        suite = lst_tst[-2].replace("2n1l-", "").\
+                            replace("1n1l-", "").replace("2n-", "")
+                        l_failed.append(f"{suite.split('-')[0]}-{lst_tst[-1]}")
                 except KeyError:
                     failed = 0
+                    l_failed = list()
                 tst_info["passed"].append(passed)
                 tst_info["failed"].append(failed)
+                tst_info["lst_failed"].append(sorted(l_failed))
 
         self._data = data_stats.merge(pd.DataFrame.from_dict(tst_info))
 
@@ -253,10 +268,13 @@ class Layout:
                 lst_job[1], lst_job[3], lst_job[4]))
         }
 
-    def _show_tooltip(self, id: str, title: str) -> list:
+    def _show_tooltip(self, id: str, title: str,
+            clipboard_id: str=None) -> list:
         """
         """
         return [
+            dcc.Clipboard(target_id=clipboard_id, title="Copy URL") \
+                if clipboard_id else str(),
             f"{title} ",
             dbc.Badge(
                 id=id,
@@ -291,7 +309,7 @@ class Layout:
                     ),
                     dcc.Loading(
                         dbc.Offcanvas(
-                            class_name="w-25",
+                            class_name="w-50",
                             id="offcanvas-metadata",
                             title="Detailed Information",
                             placement="end",
@@ -403,18 +421,29 @@ class Layout:
                                         color="info"
                                     ),
                                     dcc.Download(id="download-data")
-                                ]),
+                                ])
                             ]
                         ),
                         dbc.Col(  # Show URL
                             width=10,
                             children=[
-                                dbc.Card(
-                                    id="card-url",
-                                    body=True,
-                                    class_name="gy-2 p-0",
-                                    children=[]
-                                ),
+                                dbc.InputGroup(
+                                    class_name="me-1",
+                                    children=[
+                                        dbc.InputGroupText(
+                                            style=self.URL_STYLE,
+                                            children=self._show_tooltip(
+                                                "help-url", "URL", "input-url")
+                                        ),
+                                        dbc.Input(
+                                            id="input-url",
+                                            readonly=True,
+                                            type="url",
+                                            style=self.URL_STYLE,
+                                            value=""
+                                        )
+                                    ]
+                                )
                             ]
                         )
                     ]
@@ -578,17 +607,19 @@ class Layout:
 
     @staticmethod
     def _generate_options(opts: list) -> list:
-        """
-        """
         return [{"label": i, "value": i} for i in opts]
 
+    @staticmethod
+    def _get_date(s_date: str) -> datetime:
+        return datetime(int(s_date[0:4]), int(s_date[5:7]), int(s_date[8:10]))
+
     def callbacks(self, app):
 
         @app.callback(
             Output("control-panel", "data"),  # Store
             Output("graph-passed", "figure"),
             Output("graph-duration", "figure"),
-            Output("card-url", "children"),
+            Output("input-url", "value"),
             Output("ri-ttypes", "options"),
             Output("ri-cadences", "options"),
             Output("dd-tbeds", "options"),
@@ -614,12 +645,15 @@ class Layout:
 
             ctrl_panel = self.ControlPanel(cp_data, self.default)
 
-            start = datetime(int(start[0:4]), int(start[5:7]), int(start[8:10]))
-            end = datetime(int(end[0:4]), int(end[5:7]), int(end[8:10]))
+            start = self._get_date(start)
+            end = self._get_date(end)
 
-            parsed_url = urllib.parse.urlparse(href)
-            url = f"{parsed_url.netloc}{parsed_url.path}"
-            url_params = urllib.parse.parse_qs(parsed_url.fragment)
+            # Parse the url:
+            parsed_url = url_decode(href)
+            if parsed_url:
+                url_params = parsed_url["params"]
+            else:
+                url_params = None
 
             trigger_id = callback_context.triggered[0]["prop_id"].split(".")[0]
             if trigger_id == "ri-duts":
@@ -678,12 +712,8 @@ class Layout:
                     new_start = url_params.get("start", list())[0]
                     new_end = url_params.get("end", list())[0]
                     if new_job and new_start and new_end:
-                        start = datetime(
-                            int(new_start[0:4]), int(new_start[5:7]),
-                            int(new_start[8:10]))
-                        end = datetime(
-                            int(new_end[0:4]), int(new_end[5:7]),
-                            int(new_end[8:10]))
+                        start = self._get_date(new_start)
+                        end = self._get_date(new_end)
                         job_params = self._set_job_params(new_job)
                         ctrl_panel = self.ControlPanel(None, job_params)
                 else:
@@ -701,28 +731,30 @@ class Layout:
                 ctrl_panel.get("ri-cadences-value"),
                 ctrl_panel.get("dd-tbeds-value")
             )
-            url_params = {
-                "job": job,
-                "start": start,
-                "end": end
-            }
 
             ctrl_panel.set({"al-job-children": job})
-            fig_passed, fig_duration = graph_statistics(
-                self.data, job, self.layout, start, end)
+            fig_passed, fig_duration = graph_statistics(self.data, job,
+                self.layout, start, end)
+
+            if parsed_url:
+                new_url = url_encode({
+                    "scheme": parsed_url["scheme"],
+                    "netloc": parsed_url["netloc"],
+                    "path": parsed_url["path"],
+                    "params": {
+                        "job": job,
+                        "start": start,
+                        "end": end
+                    }
+                })
+            else:
+                new_url = str()
 
             ret_val = [
                 ctrl_panel.panel,
                 fig_passed,
                 fig_duration,
-                [
-                    dcc.Clipboard(
-                        target_id="card-url",
-                        title="Copy URL",
-                        style={"display": "inline-block"}
-                    ),
-                    f"{url}#{urllib.parse.urlencode(url_params)}"
-                ]
+                new_url
             ]
             ret_val.extend(ctrl_panel.values())
             return ret_val
@@ -781,6 +813,26 @@ class Layout:
             elif trigger_id == "graph-duration":
                 graph_data = duration_data["points"][0].get("text", "")
             if graph_data:
+                lst_graph_data = graph_data.split("<br>")
+
+                # Prepare list of failed tests:
+                job = str()
+                build = str()
+                for itm in lst_graph_data:
+                    if "csit-ref:" in itm:
+                        job, build = itm.split(" ")[-1].split("/")
+                        break
+                if job and build:
+                    fail_tests = self.data.loc[
+                        (self.data["job"] == job) &
+                        (self.data["build"] == build)
+                    ]["lst_failed"].values[0]
+                    if not fail_tests:
+                        fail_tests = None
+                else:
+                    fail_tests = None
+
+                # Create the content of the offcanvas:
                 metadata = [
                     dbc.Card(
                         class_name="gy-2 p-0",
@@ -805,7 +857,7 @@ class Layout:
                                                 ),
                                                 x.split(": ")[1]
                                             ]
-                                        ) for x in graph_data.split("<br>")
+                                        ) for x in lst_graph_data
                                     ],
                                     flush=True),
                                 ]
@@ -813,6 +865,30 @@ class Layout:
                         ]
                     )
                 ]
+
+                if fail_tests is not None:
+                    metadata.append(
+                        dbc.Card(
+                            class_name="gy-2 p-0",
+                            children=[
+                                dbc.CardHeader(
+                                    f"List of Failed Tests ({len(fail_tests)})"
+                                ),
+                                dbc.CardBody(
+                                    id="failed-tests",
+                                    class_name="p-0",
+                                    children=[dbc.ListGroup(
+                                        children=[
+                                            dbc.ListGroupItem(x) \
+                                                for x in fail_tests
+                                        ],
+                                        flush=True),
+                                    ]
+                                )
+                            ]
+                        )
+                    )
+
                 open_canvas = True
 
             return metadata, open_canvas