feat(uti): Form layout
[csit.git] / resources / tools / dash / app / pal / trending / layout.py
index f3ce21f..ac866cf 100644 (file)
@@ -19,12 +19,13 @@ import dash_bootstrap_components as dbc
 
 from dash import dcc
 from dash import html
-from dash import callback_context, no_update
+from dash import callback_context, no_update, ALL
 from dash import Input, Output, State
 from dash.exceptions import PreventUpdate
 from yaml import load, FullLoader, YAMLError
 from datetime import datetime, timedelta
 from copy import deepcopy
+from json import loads, JSONDecodeError
 
 from ..data.data import Data
 from .graphs import graph_trending, graph_hdrh_latency, \
@@ -35,20 +36,22 @@ class Layout:
     """
     """
 
-    NO_GRAPH = {"data": [], "layout": {}, "frames": []}
+    STYLE_DISABLED = {"display": "none"}
+    STYLE_ENABLED = {"display": "inherit"}
 
     CL_ALL_DISABLED = [{
         "label": "All",
         "value": "all",
         "disabled": True
     }]
-
     CL_ALL_ENABLED = [{
         "label": "All",
         "value": "all",
         "disabled": False
     }]
 
+    PLACEHOLDER = html.Nobr("")
+
     def __init__(self, app, html_layout_file, spec_file, graph_layout_file,
         data_spec_file):
         """
@@ -65,12 +68,12 @@ class Layout:
         data_mrr = Data(
             data_spec_file=self._data_spec_file,
             debug=True
-        ).read_trending_mrr(days=5)
+        ).read_trending_mrr(days=180)
 
         data_ndrpdr = Data(
             data_spec_file=self._data_spec_file,
             debug=True
-        ).read_trending_ndrpdr(days=14)
+        ).read_trending_ndrpdr(days=180)
 
         self._data = pd.concat([data_mrr, data_ndrpdr], ignore_index=True)
 
@@ -152,6 +155,7 @@ class Layout:
                     ),
                     dcc.Loading(
                         dbc.Offcanvas(
+                            class_name="w-50",
                             id="offcanvas-metadata",
                             title="Throughput And Latency",
                             placement="end",
@@ -164,7 +168,7 @@ class Layout:
                     ),
                     dbc.Row(
                         id="row-main",
-                        class_name="g-0 p-2",
+                        class_name="g-0",
                         children=[
                             dcc.Store(
                                 id="selected-tests"
@@ -220,7 +224,6 @@ class Layout:
             id="col-controls",
             children=[
                 self._add_ctrl_panel(),
-                self._add_ctrl_shown()
             ],
         )
 
@@ -234,31 +237,21 @@ class Layout:
                     id="row-graph-tput",
                     class_name="g-0 p-2",
                     children=[
-                        dcc.Loading(
-                            dcc.Graph(id="graph-tput")
-                        )
+                        self.PLACEHOLDER
                     ]
                 ),
                 dbc.Row(  # Latency
                     id="row-graph-lat",
                     class_name="g-0 p-2",
                     children=[
-                        dcc.Loading(
-                            dcc.Graph(id="graph-latency")
-                        )
+                        self.PLACEHOLDER
                     ]
                 ),
                 dbc.Row(  # Download
-                    id="div-download",
-                    class_name="g-0",
+                    id="row-btn-download",
+                    class_name="g-0 p-2",
                     children=[
-                        dcc.Loading(children=[
-                            dbc.Button(
-                                id="btn-download-data",
-                                children=["Download Data"]
-                            ),
-                            dcc.Download(id="download-data")
-                        ])
+                        self.PLACEHOLDER
                     ]
                 )
             ],
@@ -270,115 +263,173 @@ class Layout:
         """
         return dbc.Row(
             id="row-ctrl-panel",
-            class_name="g-0",
+            class_name="g-0 p-2",
             children=[
-                dbc.Label("Physical Test Bed Topology, NIC and Driver"),
-                dbc.Select(
-                    id="dd-ctrl-phy",
-                    className="p-2",
-                    placeholder="Select a Physical Test Bed Topology...",
-                    options=[
-                        {"label": k, "value": k} for k in self.spec_tbs.keys()
-                    ],
-                    size="sm",
+                dbc.Row(
+                    class_name="g-0",
+                    children=[
+                        dbc.InputGroup(
+                            [
+                                dbc.InputGroupText("Infra"),
+                                dbc.Select(
+                                    id="dd-ctrl-phy",
+                                    placeholder="Select a Physical Test Bed Topology...",
+                                    options=[
+                                        {"label": k, "value": k} for k in self.spec_tbs.keys()
+                                    ],
+                                ),
+                            ],
+                            class_name="mb-3",
+                            size="sm",
+                        ),
+                    ]
                 ),
-                dbc.Label("Area"),
-                dbc.Select(
-                    id="dd-ctrl-area",
-                    className="p-2",
-                    placeholder="Select an Area...",
-                    disabled=True,
-                    size="sm",
+                dbc.Row(
+                    class_name="g-0",
+                    children=[
+                        dbc.InputGroup(
+                            [
+                                dbc.InputGroupText("Area"),
+                                dbc.Select(
+                                    id="dd-ctrl-area",
+                                    placeholder="Select an Area...",
+                                    disabled=True,
+                                ),
+                            ],
+                            class_name="mb-3",
+                            size="sm",
+                        ),
+                    ]
                 ),
-                dbc.Label("Test"),
-                dbc.Select(
-                    id="dd-ctrl-test",
-                    className="p-2",
-                    placeholder="Select a Test...",
-                    disabled=True,
-                    size="sm",
+                dbc.Row(
+                    class_name="g-0",
+                    children=[
+                        dbc.InputGroup(
+                            [
+                                dbc.InputGroupText("Test"),
+                                dbc.Select(
+                                    id="dd-ctrl-test",
+                                    placeholder="Select a Test...",
+                                    disabled=True,
+                                ),
+                            ],
+                            class_name="mb-3",
+                            size="sm",
+                        ),
+                    ]
                 ),
                 dbc.Row(
                     id="row-ctrl-core",
-                    class_name="g-0",
+                    class_name="gy-1",
                     children=[
-                        dbc.Label("Number of Cores"),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-core-all",
-                                options=self.CL_ALL_DISABLED,
-                                inline=True,
-                                switch=False
-                            ),
-                        ], width=3),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-core",
-                                inline=True,
-                                switch=False
-                            )
-                        ])
+                        dbc.Label(
+                            "Number of Cores",
+                            class_name="p-0"
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-core-all",
+                                    options=self.CL_ALL_DISABLED,
+                                    inline=False,
+                                    switch=False
+                                )
+                            ],
+                            width=3
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-core",
+                                    inline=True,
+                                    switch=False
+                                )
+                            ]
+                        )
                     ]
                 ),
                 dbc.Row(
                     id="row-ctrl-framesize",
-                    class_name="g-0",
+                    class_name="gy-1",
                     children=[
-                        dbc.Label("Frame Size"),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-framesize-all",
-                                options=self.CL_ALL_DISABLED,
-                                inline=True,
-                                switch=False
-                            ),
-                        ], width=3),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-framesize",
-                                inline=True,
-                                switch=False
-                            )
-                        ])
+                        dbc.Label(
+                            "Frame Size",
+                            class_name="p-0"
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-framesize-all",
+                                    options=self.CL_ALL_DISABLED,
+                                    inline=True,
+                                    switch=False
+                                ),
+                            ],
+                            width=3
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-framesize",
+                                    inline=True,
+                                    switch=False
+                                )
+                            ]
+                        )
                     ]
                 ),
                 dbc.Row(
                     id="row-ctrl-testtype",
-                    class_name="g-0",
+                    class_name="gy-1",
                     children=[
-                        dbc.Label("Test Type"),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-testtype-all",
-                                options=self.CL_ALL_DISABLED,
-                                inline=True,
-                                switch=False
-                            ),
-                        ], width=3),
-                        dbc.Col([
-                            dbc.Checklist(
-                                id="cl-ctrl-testtype",
-                                inline=True,
-                                switch=False
-                            )
-                        ])
+                        dbc.Label(
+                            "Test Type",
+                            class_name="p-0"
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-testtype-all",
+                                    options=self.CL_ALL_DISABLED,
+                                    inline=True,
+                                    switch=False
+                                ),
+                            ],
+                            width=3
+                        ),
+                        dbc.Col(
+                            children=[
+                                dbc.Checklist(
+                                    id="cl-ctrl-testtype",
+                                    inline=True,
+                                    switch=False
+                                )
+                            ]
+                        )
                     ]
                 ),
                 dbc.Row(
-                    class_name="g-0",
+                    class_name="gy-1 p-0",
                     children=[
-                        dbc.Button(
-                            id="btn-ctrl-add",
-                            children="Add",
-                            disabled=True
+                        dbc.ButtonGroup(
+                            [
+                                dbc.Button(
+                                    id="btn-ctrl-add",
+                                    children="Add Selected",
+                                    class_name="me-1",
+                                    color="info"
+                                )
+                            ],
+                            size="md",
                         )
                     ]
                 ),
                 dbc.Row(
-                    class_name="g-0",
+                    class_name="gy-1",
                     children=[
                         dcc.DatePickerRange(
                             id="dpr-period",
+                            className="d-flex justify-content-center",
                             min_date_allowed=\
                                 datetime.utcnow()-timedelta(days=180),
                             max_date_allowed=datetime.utcnow(),
@@ -388,57 +439,51 @@ class Layout:
                             display_format="D MMMM YY"
                         )
                     ]
-                )
-            ]
-        )
-
-    def _add_ctrl_shown(self) -> dbc.Row:
-        """
-        """
-        return dbc.Row(
-            id="div-ctrl-shown",
-            class_name="g-0",
-            children=[
+                ),
                 dbc.Row(
-                    class_name="g-0",
+                    id="row-card-sel-tests",
+                    class_name="gy-1",
+                    style=self.STYLE_DISABLED,
                     children=[
-                        dbc.Label("Selected tests"),
+                        dbc.Label(
+                            "Selected tests",
+                            class_name="p-0"
+                        ),
                         dbc.Checklist(
+                            class_name="overflow-auto",
                             id="cl-selected",
                             options=[],
-                            inline=False
+                            inline=False,
+                            style={"max-height": "12em"},
                         )
-                    ]
+                    ],
                 ),
                 dbc.Row(
-                    class_name="g-0",
+                    id="row-btns-sel-tests",
+                    style=self.STYLE_DISABLED,
                     children=[
                         dbc.ButtonGroup(
-                            [
-                                dbc.Button(
-                                    id="btn-sel-remove-all",
-                                    children="Remove All",
-                                    color="secondary",
-                                    disabled=False
-                                ),
+                            class_name="gy-2",
+                            children=[
                                 dbc.Button(
                                     id="btn-sel-remove",
                                     children="Remove Selected",
-                                    color="secondary",
+                                    class_name="w-100 me-1",
+                                    color="info",
                                     disabled=False
                                 ),
                                 dbc.Button(
-                                    id="btn-sel-display",
-                                    children="Display",
-                                    color="secondary",
+                                    id="btn-sel-remove-all",
+                                    children="Remove All",
+                                    class_name="w-100 me-1",
+                                    color="info",
                                     disabled=False
-                                )
+                                ),
                             ],
                             size="md",
-                            class_name="me-1",
-                        ),
+                        )
                     ]
-                )
+                ),
             ]
         )
 
@@ -526,11 +571,56 @@ class Layout:
 
     def callbacks(self, app):
 
+        def _generate_plotting_arrea(args: tuple) -> tuple:
+            """
+            """
+
+            (fig_tput, fig_lat) = args
+
+            row_fig_tput = self.PLACEHOLDER
+            row_fig_lat = self.PLACEHOLDER
+            row_btn_dwnld = self.PLACEHOLDER
+
+            if fig_tput:
+                row_fig_tput = [
+                    dcc.Loading(
+                        dcc.Graph(
+                            id={"type": "graph", "index": "tput"},
+                            figure=fig_tput
+                        )
+                    ),
+                ]
+                row_btn_dwnld = [
+                    dcc.Loading(children=[
+                        dbc.Button(
+                            id="btn-download-data",
+                            children=["Download Data"],
+                            class_name="me-1",
+                            color="info"
+                        ),
+                        dcc.Download(id="download-data")
+                    ]),
+                ]
+            if fig_lat:
+                row_fig_lat = [
+                    dcc.Loading(
+                        dcc.Graph(
+                            id={"type": "graph", "index": "lat"},
+                            figure=fig_lat
+                        )
+                    )
+                ]
+
+            return row_fig_tput, row_fig_lat, row_btn_dwnld
+
         @app.callback(
             Output("control-panel", "data"),  # Store
             Output("selected-tests", "data"),  # Store
-            Output("graph-tput", "figure"),
-            Output("graph-latency", "figure"),
+            Output("row-graph-tput", "children"),
+            Output("row-graph-lat", "children"),
+            Output("row-btn-download", "children"),
+            Output("row-card-sel-tests", "style"),
+            Output("row-btns-sel-tests", "style"),
             Output("dd-ctrl-phy", "value"),
             Output("dd-ctrl-area", "options"),
             Output("dd-ctrl-area", "disabled"),
@@ -567,7 +657,6 @@ class Layout:
             Input("btn-ctrl-add", "n_clicks"),
             Input("dpr-period", "start_date"),
             Input("dpr-period", "end_date"),
-            Input("btn-sel-display", "n_clicks"),
             Input("btn-sel-remove", "n_clicks"),
             Input("btn-sel-remove-all", "n_clicks"),
         )
@@ -575,7 +664,7 @@ class Layout:
             dd_phy: str, dd_area: str, dd_test: str, cl_core: list,
             cl_core_all: list, cl_framesize: list, cl_framesize_all: list,
             cl_testtype: list, cl_testtype_all: list, btn_add: int,
-            d_start: str, d_end: str, btn_display: int, btn_remove: int,
+            d_start: str, d_end: str, btn_remove: int,
             btn_remove_all: int) -> tuple:
             """
             """
@@ -584,8 +673,11 @@ class Layout:
                 int(d_start[8:10]))
             d_end = datetime(int(d_end[0:4]), int(d_end[5:7]), int(d_end[8:10]))
 
-            fig_tput = no_update
-            fig_lat = no_update
+            row_fig_tput = no_update
+            row_fig_lat = no_update
+            row_btn_dwnld = no_update
+            row_card_sel_tests = no_update
+            row_btns_sel_tests = no_update
 
             ctrl_panel = self.ControlPanel(cp_data)
 
@@ -786,21 +878,32 @@ class Layout:
                                         "core": core.lower(),
                                         "testtype": ttype.lower()
                                     })
+                    row_card_sel_tests = self.STYLE_ENABLED
+                    row_btns_sel_tests = self.STYLE_ENABLED
                     ctrl_panel.set(ctrl_panel.defaults)
                     ctrl_panel.set({
                         "cl-selected-options": self._list_tests(store_sel)
                     })
-            elif trigger_id in ("btn-sel-display", "dpr-period"):
-                _ = btn_display
-                fig_tput, fig_lat = graph_trending(
-                    self.data, store_sel, self.layout, d_start, d_end
-                )
-                fig_tput = fig_tput if fig_tput else self.NO_GRAPH
-                fig_lat = fig_lat if fig_lat else self.NO_GRAPH
+                    row_fig_tput, row_fig_lat, row_btn_dwnld = \
+                    _generate_plotting_arrea(
+                        graph_trending(
+                            self.data, store_sel, self.layout, d_start, d_end
+                        )
+                    )
+            elif trigger_id == "dpr-period":
+                row_fig_tput, row_fig_lat, row_btn_dwnld = \
+                    _generate_plotting_arrea(
+                        graph_trending(
+                            self.data, store_sel, self.layout, d_start, d_end
+                        )
+                    )
             elif trigger_id == "btn-sel-remove-all":
                 _ = btn_remove_all
-                fig_tput = self.NO_GRAPH
-                fig_lat = self.NO_GRAPH
+                row_fig_tput = self.PLACEHOLDER
+                row_fig_lat = self.PLACEHOLDER
+                row_btn_dwnld = self.PLACEHOLDER
+                row_card_sel_tests = self.STYLE_DISABLED
+                row_btns_sel_tests = self.STYLE_DISABLED
                 store_sel = list()
                 ctrl_panel.set({
                         "cl-selected-options": list()
@@ -814,23 +917,31 @@ class Layout:
                             new_store_sel.append(item)
                     store_sel = new_store_sel
                 if store_sel:
-                    fig_tput, fig_lat = graph_trending(
-                        self.data, store_sel, self.layout, d_start, d_end
+                    row_fig_tput, row_fig_lat, row_btn_dwnld = \
+                    _generate_plotting_arrea(
+                        graph_trending(
+                            self.data, store_sel, self.layout, d_start, d_end
+                        )
                     )
-                    fig_tput = fig_tput if fig_tput else self.NO_GRAPH
-                    fig_lat = fig_lat if fig_lat else self.NO_GRAPH
                     ctrl_panel.set({
                         "cl-selected-options": self._list_tests(store_sel)
                     })
                 else:
-                    fig_tput = self.NO_GRAPH
-                    fig_lat = self.NO_GRAPH
+                    row_fig_tput = self.PLACEHOLDER
+                    row_fig_lat = self.PLACEHOLDER
+                    row_btn_dwnld = self.PLACEHOLDER
+                    row_card_sel_tests = self.STYLE_DISABLED
+                    row_btns_sel_tests = self.STYLE_DISABLED
                     store_sel = list()
                     ctrl_panel.set({
                             "cl-selected-options": list()
                     })
 
-            ret_val = [ctrl_panel.panel, store_sel, fig_tput, fig_lat]
+            ret_val = [
+                ctrl_panel.panel, store_sel,
+                row_fig_tput, row_fig_lat, row_btn_dwnld,
+                row_card_sel_tests, row_btns_sel_tests
+            ]
             ret_val.extend(ctrl_panel.values())
             return ret_val
 
@@ -838,35 +949,53 @@ class Layout:
             Output("metadata-tput-lat", "children"),
             Output("metadata-hdrh-graph", "children"),
             Output("offcanvas-metadata", "is_open"),
-            Input("graph-tput", "clickData"),
-            Input("graph-latency", "clickData")
+            Input({"type": "graph", "index": ALL}, "clickData"),
+            prevent_initial_call=True
         )
-        def _show_metadata_from_graphs(
-            tput_data: dict, lat_data: dict) -> tuple:
+        def _show_metadata_from_graphs(graph_data: dict) -> tuple:
             """
             """
-            if not (tput_data or lat_data):
+            try:
+                trigger_id = loads(
+                    callback_context.triggered[0]["prop_id"].split(".")[0]
+                )["index"]
+                idx = 0 if trigger_id == "tput" else 1
+                graph_data = graph_data[idx]["points"][0]
+            except (JSONDecodeError, IndexError, KeyError, ValueError,
+                    TypeError):
                 raise PreventUpdate
 
             metadata = no_update
             graph = list()
 
-            trigger_id = callback_context.triggered[0]["prop_id"].split(".")[0]
-            if trigger_id == "graph-tput":
+            children = [
+                dbc.ListGroupItem(
+                    [dbc.Badge(x.split(":")[0]), x.split(": ")[1]]
+                ) for x in graph_data.get("text", "").split("<br>")
+            ]
+            if trigger_id == "tput":
                 title = "Throughput"
-                txt = tput_data["points"][0]["text"].replace("<br>", "\n")
-            elif trigger_id == "graph-latency":
+            elif trigger_id == "lat":
                 title = "Latency"
-                txt = lat_data["points"][0]["text"].replace("<br>", "\n")
-                hdrh_data = lat_data["points"][0].get("customdata", None)
+                hdrh_data = graph_data.get("customdata", None)
                 if hdrh_data:
-                    graph = [dcc.Graph(
-                        id="hdrh-latency-graph",
-                        figure=graph_hdrh_latency(hdrh_data, self.layout)
-                    ), ]
-
+                    graph = [dbc.Card(
+                        class_name="gy-2 p-0",
+                        children=[
+                            dbc.CardHeader(hdrh_data.pop("name")),
+                            dbc.CardBody(children=[
+                                dcc.Graph(
+                                    id="hdrh-latency-graph",
+                                    figure=graph_hdrh_latency(
+                                        hdrh_data, self.layout
+                                    )
+                                )
+                            ])
+                        ])
+                    ]
             metadata = [
                 dbc.Card(
+                    class_name="gy-2 p-0",
                     children=[
                         dbc.CardHeader(children=[
                             dcc.Clipboard(
@@ -878,7 +1007,8 @@ class Layout:
                         ]),
                         dbc.CardBody(
                             id="tput-lat-metadata",
-                            children=[txt]
+                            class_name="p-0",
+                            children=[dbc.ListGroup(children, flush=True), ]
                         )
                     ]
                 )
@@ -899,6 +1029,9 @@ class Layout:
             if not n_clicks:
                 raise PreventUpdate
 
+            if not store_sel:
+                raise PreventUpdate
+
             df = pd.DataFrame()
             for itm in store_sel:
                 sel_data = select_trending_data(self.data, itm)