UTI: Add URL support to "News"
[csit.git] / resources / tools / dash / app / pal / news / layout.py
index 2f66ce5..527710f 100644 (file)
@@ -27,7 +27,10 @@ from yaml import load, FullLoader, YAMLError
 from copy import deepcopy
 
 from ..data.data import Data
-from ..data.utils import classify_anomalies
+from ..utils.constants import Constants as C
+from ..utils.utils import classify_anomalies, show_tooltip, gen_new_url
+from ..utils.url_processing import url_decode
+from ..data.data import Data
 from .tables import table_news
 
 
@@ -35,12 +38,6 @@ class Layout:
     """The layout of the dash app and the callbacks.
     """
 
-    # The default job displayed when the page is loaded first time.
-    DEFAULT_JOB = "csit-vpp-perf-mrr-daily-master-2n-icx"
-
-    # Time period for regressions and progressions.
-    TIME_PERIOD = 21  # [days]
-
     def __init__(self, app: Flask, html_layout_file: str, data_spec_file: str,
         tooltip_file: str) -> None:
         """Initialization:
@@ -73,7 +70,7 @@ class Layout:
         data_stats, data_mrr, data_ndrpdr = Data(
             data_spec_file=self._data_spec_file,
             debug=True
-        ).read_stats(days=self.TIME_PERIOD)
+        ).read_stats(days=C.NEWS_TIME_PERIOD)
 
         df_tst_info = pd.concat([data_mrr, data_ndrpdr], ignore_index=True)
 
@@ -95,7 +92,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.NEWS_DEFAULT_JOB)
 
         # Pre-process the data:
 
@@ -393,43 +390,6 @@ class Layout:
                 lst_job[1], lst_job[3], lst_job[4]))
         }
 
-    def _show_tooltip(self, id: str, title: str,
-            clipboard_id: str=None) -> list:
-        """Generate list of elements to display a text (e.g. a title) with a
-        tooltip and optionaly with Copy&Paste icon and the clipboard
-        functionality enabled.
-
-        :param id: Tooltip ID.
-        :param title: A text for which the tooltip will be displayed.
-        :param clipboard_id: If defined, a Copy&Paste icon is displayed and the
-            clipboard functionality is enabled.
-        :type id: str
-        :type title: str
-        :type clipboard_id: str
-        :returns: List of elements to display a text with a tooltip and
-            optionaly with Copy&Paste icon.
-        :rtype: list
-        """
-
-        return [
-            dcc.Clipboard(target_id=clipboard_id, title="Copy URL") \
-                if clipboard_id else str(),
-            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):
         """Top level method which generated the web page.
 
@@ -449,6 +409,7 @@ class Layout:
                 id="div-main",
                 children=[
                     dcc.Store(id="control-panel"),
+                    dcc.Location(id="url", refresh=False),
                     dbc.Row(
                         id="row-navbar",
                         class_name="g-0",
@@ -533,6 +494,33 @@ class Layout:
                     id="row-table-failed",
                     class_name="g-0 p-2",
                     children=self._default_tab_failed
+                ),
+                dbc.Row(
+                    class_name="g-0 p-2",
+                    align="center",
+                    justify="start",
+                    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=""
+                                )
+                            ]
+                        )
+                    ]
                 )
             ],
             width=9,
@@ -556,7 +544,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(
@@ -574,7 +562,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(
@@ -590,7 +578,7 @@ class Layout:
                             children=[
                                 dbc.Label(
                                     class_name="p-0",
-                                    children=self._show_tooltip(
+                                    children=show_tooltip(self._tooltips,
                                         "help-cadence", "Cadence"),
                                 ),
                                 dbc.RadioItems(
@@ -606,7 +594,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(
@@ -681,6 +669,7 @@ class Layout:
         @app.callback(
             Output("control-panel", "data"),  # Store
             Output("row-table-failed", "children"),
+            Output("input-url", "value"),
             Output("ri-ttypes", "options"),
             Output("ri-cadences", "options"),
             Output("dd-tbeds", "options"),
@@ -694,14 +683,22 @@ class Layout:
             Input("ri-ttypes", "value"),
             Input("ri-cadences", "value"),
             Input("dd-tbeds", "value"),
+            Input("url", "href")
         )
         def _update_ctrl_panel(cp_data: dict, dut:str, ttype: str, cadence:str,
-                tbed: str) -> tuple:
+                tbed: str, href: str) -> tuple:
             """
             """
 
             ctrl_panel = self.ControlPanel(cp_data, self.default)
 
+            # 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":
                 ttype_opts = self._generate_options(self._get_ttypes(dut))
@@ -750,6 +747,21 @@ class Layout:
                 ctrl_panel.set({
                     "dd-tbeds-value": tbed
                 })
+            elif trigger_id == "url":
+                # TODO: Add verification
+                if url_params:
+                    new_job = url_params.get("job", list())[0]
+                    if new_job:
+                        job_params = self._set_job_params(new_job)
+                        ctrl_panel = self.ControlPanel(None, job_params)
+                else:
+                    ctrl_panel = self.ControlPanel(cp_data, self.default)
+                    job = self._get_job(
+                        ctrl_panel.get("ri-duts-value"),
+                        ctrl_panel.get("ri-ttypes-value"),
+                        ctrl_panel.get("ri-cadences-value"),
+                        ctrl_panel.get("dd-tbeds-value")
+                    )
 
             job = self._get_job(
                 ctrl_panel.get("ri-duts-value"),
@@ -762,7 +774,8 @@ class Layout:
 
             ret_val = [
                 ctrl_panel.panel,
-                tab_failed
+                tab_failed,
+                gen_new_url(parsed_url, {"job": job})
             ]
             ret_val.extend(ctrl_panel.values())
             return ret_val