UTI: Add URL support to "News"
[csit.git] / resources / tools / dash / app / pal / stats / layout.py
index 8b4de84..2e74fb8 100644 (file)
@@ -28,8 +28,10 @@ from yaml import load, FullLoader, YAMLError
 from datetime import datetime, timedelta
 from copy import deepcopy
 
+from ..utils.constants import Constants as C
+from ..utils.utils import show_tooltip, gen_new_url
+from ..utils.url_processing import url_decode
 from ..data.data import Data
-from ..data.url_processing import url_decode, url_encode
 from .graphs import graph_statistics, select_data
 
 
@@ -37,9 +39,7 @@ class Layout:
     """
     """
 
-    DEFAULT_JOB = "csit-vpp-perf-mrr-daily-master-2n-icx"
-
-    def __init__(self, app: Flask, html_layout_file: str, spec_file: str,
+    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 +48,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
@@ -90,7 +89,7 @@ class Layout:
             job_info["tbed"].append("-".join(lst_job[-2:]))
         self.df_job_info = pd.DataFrame.from_dict(job_info)
 
-        self._default = self._set_job_params(self.DEFAULT_JOB)
+        self._default = self._set_job_params(C.STATS_DEFAULT_JOB)
 
         tst_info = {
             "job": list(),
@@ -99,10 +98,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 +112,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,26 +262,6 @@ class Layout:
                 lst_job[1], lst_job[3], lst_job[4]))
         }
 
-    def _show_tooltip(self, id: str, title: str) -> list:
-        """
-        """
-        return [
-            f"{title} ",
-            dbc.Badge(
-                id=id,
-                children="?",
-                pill=True,
-                color="white",
-                text_color="info",
-                class_name="border ms-1",
-            ),
-            dbc.Tooltip(
-                children=self._tooltips.get(id, str()),
-                target=id,
-                placement="auto"
-            )
-        ]
-
     def add_content(self):
         """
         """
@@ -291,7 +280,7 @@ class Layout:
                     ),
                     dcc.Loading(
                         dbc.Offcanvas(
-                            class_name="w-25",
+                            class_name="w-50",
                             id="offcanvas-metadata",
                             title="Detailed Information",
                             placement="end",
@@ -397,24 +386,38 @@ class Layout:
                                 dcc.Loading(children=[
                                     dbc.Button(
                                         id="btn-download-data",
-                                        children=self._show_tooltip(
+                                        children=show_tooltip(self._tooltips,
                                             "help-download", "Download Data"),
                                         class_name="me-1",
                                         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=C.URL_STYLE,
+                                            children=show_tooltip(
+                                                self._tooltips,
+                                                "help-url", "URL",
+                                                "input-url"
+                                            )
+                                        ),
+                                        dbc.Input(
+                                            id="input-url",
+                                            readonly=True,
+                                            type="url",
+                                            style=C.URL_STYLE,
+                                            value=""
+                                        )
+                                    ]
+                                )
                             ]
                         )
                     ]
@@ -438,7 +441,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="p-0",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-dut", "Device under Test")
                                 ),
                                 dbc.Row(
@@ -456,7 +459,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="p-0",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-ttype", "Test Type"),
                                 ),
                                 dbc.RadioItems(
@@ -472,7 +475,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="p-0",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-cadence", "Cadence"),
                                 ),
                                 dbc.RadioItems(
@@ -488,7 +491,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="p-0",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-tbed", "Test Bed"),
                                 ),
                                 dbc.Select(
@@ -514,7 +517,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="gy-1",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-time-period", "Time Period"),
                                 ),
                                 dcc.DatePickerRange(
@@ -590,7 +593,7 @@ class Layout:
             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"),
@@ -607,7 +610,6 @@ class Layout:
             Input("dpr-period", "start_date"),
             Input("dpr-period", "end_date"),
             Input("url", "href")
-            # prevent_initial_call=True
         )
         def _update_ctrl_panel(cp_data: dict, dut:str, ttype: str, cadence:str,
                 tbed: str, start: str, end: str, href: str) -> tuple:
@@ -707,32 +709,18 @@ class Layout:
             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,
-                [  # URL
-                    dcc.Clipboard(
-                        target_id="card-url",
-                        title="Copy URL",
-                        style={"display": "inline-block"}
-                    ),
-                    new_url
-                ]
+                gen_new_url(
+                    parsed_url,
+                    {
+                        "job": job,
+                        "start": start,
+                        "end": end
+                    }
+                )
             ]
             ret_val.extend(ctrl_panel.values())
             return ret_val
@@ -791,6 +779,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",
@@ -815,7 +823,7 @@ class Layout:
                                                 ),
                                                 x.split(": ")[1]
                                             ]
-                                        ) for x in graph_data.split("<br>")
+                                        ) for x in lst_graph_data
                                     ],
                                     flush=True),
                                 ]
@@ -823,6 +831,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