X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=csit.infra.dash%2Fapp%2Fcdash%2Ftrending%2Flayout.py;h=97181e199d315d53cb720e306c4553eb1867dc20;hb=0d6639a38336a3f73e276d81c86ea0d0895e1f40;hp=a8f6b6a054ee9ffaf0e8921010b85257c7b883a2;hpb=430c577e8e8a737cb46e67cbe802e038b8ffd25a;p=csit.git diff --git a/csit.infra.dash/app/cdash/trending/layout.py b/csit.infra.dash/app/cdash/trending/layout.py index a8f6b6a054..97181e199d 100644 --- a/csit.infra.dash/app/cdash/trending/layout.py +++ b/csit.infra.dash/app/cdash/trending/layout.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Cisco and/or its affiliates. +# Copyright (c) 2023 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -27,15 +27,16 @@ from dash import Input, Output, State from dash.exceptions import PreventUpdate from yaml import load, FullLoader, YAMLError from ast import literal_eval +from copy import deepcopy from ..utils.constants import Constants as C from ..utils.control_panel import ControlPanel from ..utils.trigger import Trigger +from ..utils.telemetry_data import TelemetryData from ..utils.utils import show_tooltip, label, sync_checklists, gen_new_url, \ - generate_options, get_list_group_items + generate_options, get_list_group_items, graph_hdrh_latency from ..utils.url_processing import url_decode -from ..data.data import Data -from .graphs import graph_trending, graph_hdrh_latency, select_trending_data +from .graphs import graph_trending, select_trending_data, graph_tm_trending # Control panel partameters and their default values. @@ -71,9 +72,13 @@ class Layout: """The layout of the dash app and the callbacks. """ - def __init__(self, app: Flask, html_layout_file: str, - graph_layout_file: str, data_spec_file: str, tooltip_file: str, - time_period: str=None) -> None: + def __init__(self, + app: Flask, + data_trending: pd.DataFrame, + html_layout_file: str, + graph_layout_file: str, + tooltip_file: str + ) -> None: """Initialization: - save the input parameters, - read and pre-process the data, @@ -82,58 +87,39 @@ class Layout: - read tooltips from the tooltip file. :param app: Flask application running the dash application. + :param data_trending: Pandas dataframe with trending data. :param html_layout_file: Path and name of the file specifying the HTML layout of the dash application. :param graph_layout_file: Path and name of the file with layout of plot.ly graphs. - :param data_spec_file: Path and name of the file specifying the data to - be read from parquets for this application. :param tooltip_file: Path and name of the yaml file specifying the tooltips. - :param time_period: It defines the time period for data read from the - parquets in days from now back to the past. :type app: Flask + :type data_trending: pandas.DataFrame :type html_layout_file: str :type graph_layout_file: str - :type data_spec_file: str :type tooltip_file: str - :type time_period: int """ # Inputs self._app = app + self._data = data_trending self._html_layout_file = html_layout_file self._graph_layout_file = graph_layout_file - self._data_spec_file = data_spec_file self._tooltip_file = tooltip_file - self._time_period = time_period - - # Read the data: - data_mrr = Data( - data_spec_file=self._data_spec_file, - debug=True - ).read_trending_mrr(days=self._time_period) - - data_ndrpdr = Data( - data_spec_file=self._data_spec_file, - debug=True - ).read_trending_ndrpdr(days=self._time_period) - - self._data = pd.concat([data_mrr, data_ndrpdr], ignore_index=True) # Get structure of tests: tbs = dict() - for _, row in self._data[["job", "test_id"]].drop_duplicates().\ - iterrows(): + cols = ["job", "test_id", "test_type", "tg_type"] + for _, row in self._data[cols].drop_duplicates().iterrows(): lst_job = row["job"].split("-") dut = lst_job[1] - ttype = lst_job[3] tbed = "-".join(lst_job[-2:]) lst_test = row["test_id"].split(".") if dut == "dpdk": area = "dpdk" else: - area = "-".join(lst_test[3:-2]) + area = ".".join(lst_test[3:-2]) suite = lst_test[-2].replace("2n1l-", "").replace("1n1l-", "").\ replace("2n-", "") test = lst_test[-1] @@ -172,14 +158,23 @@ class Layout: tbs[dut][infra][area][test]["frame-size"].append( framesize.upper() ) - if ttype == "mrr": + if row["test_type"] == "mrr": if "MRR" not in tbs[dut][infra][area][test]["test-type"]: tbs[dut][infra][area][test]["test-type"].append("MRR") - elif ttype == "ndrpdr": + elif row["test_type"] == "ndrpdr": if "NDR" not in tbs[dut][infra][area][test]["test-type"]: tbs[dut][infra][area][test]["test-type"].extend( ("NDR", "PDR") ) + elif row["test_type"] == "hoststack": + if row["tg_type"] in ("iperf", "vpp"): + if "BPS" not in tbs[dut][infra][area][test]["test-type"]: + tbs[dut][infra][area][test]["test-type"].append("BPS") + elif row["tg_type"] == "ab": + if "CPS" not in tbs[dut][infra][area][test]["test-type"]: + tbs[dut][infra][area][test]["test-type"].extend( + ("CPS", "RPS") + ) self._spec_tbs = tbs # Read from files: @@ -249,6 +244,8 @@ class Layout: id="div-main", className="small", children=[ + dcc.Store(id="store"), + dcc.Location(id="url", refresh=False), dbc.Row( id="row-navbar", class_name="g-0", @@ -256,7 +253,15 @@ class Layout: self._add_navbar() ] ), - dcc.Loading( + dbc.Row( + id="row-main", + class_name="g-0", + children=[ + self._add_ctrl_col(), + self._add_plotting_col() + ] + ), + dbc.Spinner( dbc.Offcanvas( class_name="w-50", id="offcanvas-metadata", @@ -267,18 +272,8 @@ class Layout: dbc.Row(id="metadata-tput-lat"), dbc.Row(id="metadata-hdrh-graph") ] - ) - ), - dbc.Row( - id="row-main", - class_name="g-0", - children=[ - dcc.Store(id="store-selected-tests"), - dcc.Store(id="store-control-panel"), - dcc.Location(id="url", refresh=False), - self._add_ctrl_col(), - self._add_plotting_col() - ] + ), + delay_show=C.SPINNER_DELAY ) ] ) @@ -333,30 +328,6 @@ class Layout: ) ]) - def _add_plotting_col(self) -> dbc.Col: - """Add column with plots and tables. It is placed on the right side. - - :returns: Column with tables. - :rtype: dbc.Col - """ - return dbc.Col( - id="col-plotting-area", - children=[ - dcc.Loading( - children=[ - dbc.Row( - id="plotting-area", - class_name="g-0 p-0", - children=[ - C.PLACEHOLDER - ] - ) - ] - ) - ], - width=9 - ) - def _add_ctrl_panel(self) -> list: """Add control panel. @@ -459,134 +430,164 @@ class Layout: dbc.Row( class_name="g-0 p-1", children=[ - dbc.Label( - children=show_tooltip( - self._tooltips, - "help-framesize", - "Frame Size" - ) - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "frmsize-all"}, - options=C.CL_ALL_DISABLED, - inline=True, - switch=False, - input_class_name="border-info bg-info" + dbc.InputGroup( + [ + dbc.InputGroupText( + children=show_tooltip( + self._tooltips, + "help-framesize", + "Frame Size" + ) + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "frmsize-all" + }, + options=C.CL_ALL_DISABLED, + inline=True, + class_name="ms-2" + ) + ], + width=2 + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "frmsize" + }, + inline=True + ) + ] ) ], - width=3 - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "frmsize"}, - inline=True, - switch=False, - input_class_name="border-info bg-info" - ) - ] + style={"align-items": "center"}, + size="sm" ) ] ), dbc.Row( class_name="g-0 p-1", children=[ - dbc.Label( - children=show_tooltip( - self._tooltips, - "help-cores", - "Number of Cores" - ) - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "core-all"}, - options=C.CL_ALL_DISABLED, - inline=False, - switch=False, - input_class_name="border-info bg-info" + dbc.InputGroup( + [ + dbc.InputGroupText( + children=show_tooltip( + self._tooltips, + "help-cores", + "Number of Cores" + ) + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "core-all" + }, + options=C.CL_ALL_DISABLED, + inline=True, + class_name="ms-2" + ) + ], + width=2 + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "core" + }, + inline=True + ) + ] ) ], - width=3 - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "core"}, - inline=True, - switch=False, - input_class_name="border-info bg-info" - ) - ] + style={"align-items": "center"}, + size="sm" ) ] ), dbc.Row( class_name="g-0 p-1", children=[ - dbc.Label( - children=show_tooltip( - self._tooltips, - "help-ttype", - "Test Type" - ) - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "tsttype-all"}, - options=C.CL_ALL_DISABLED, - inline=True, - switch=False, - input_class_name="border-info bg-info" + dbc.InputGroup( + [ + dbc.InputGroupText( + children=show_tooltip( + self._tooltips, + "help-ttype", + "Test Type" + ) + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "tsttype-all" + }, + options=C.CL_ALL_DISABLED, + inline=True, + class_name="ms-2" + ) + ], + width=2 + ), + dbc.Col( + children=[ + dbc.Checklist( + id={ + "type": "ctrl-cl", + "index": "tsttype" + }, + inline=True + ) + ] ) ], - width=3 - ), - dbc.Col( - children=[ - dbc.Checklist( - id={"type": "ctrl-cl", "index": "tsttype"}, - inline=True, - switch=False, - input_class_name="border-info bg-info" - ) - ] + style={"align-items": "center"}, + size="sm" ) ] ), dbc.Row( class_name="g-0 p-1", children=[ - dbc.Label( - children=show_tooltip( - self._tooltips, - "help-normalize", - "Normalize" - ) - ), - dbc.Col( - children=[ - dbc.Checklist( - id="normalize", - options=[ - { - "value": "normalize", - "label": ( - "Normalize results to CPU " - "frequency 2GHz" - ) - } - ], - value=[], - inline=True, - switch=False, - input_class_name="border-info bg-info" + dbc.InputGroup( + [ + dbc.InputGroupText( + children=show_tooltip( + self._tooltips, + "help-normalize", + "Normalization" + ) + ), + dbc.Col( + children=[ + dbc.Checklist( + id="normalize", + options=[{ + "value": "normalize", + "label": ( + "Normalize to CPU frequency " + "2GHz" + ) + }], + value=[], + inline=True, + class_name="ms-2" + ) + ] ) - ] + ], + style={"align-items": "center"}, + size="sm" ) ] ), @@ -609,7 +610,7 @@ class Layout: class_name="overflow-auto p-0", id="lg-selected", children=[], - style={"max-height": "14em"}, + style={"max-height": "20em"}, flush=True ) ] @@ -638,16 +639,73 @@ class Layout: ] ) ] + ), + dbc.Stack( + id="row-btns-add-tm", + class_name="g-0 p-1", + style=C.STYLE_DISABLED, + gap=2, + children=[ + dbc.Button( + "Add Telemetry Panel", + id={"type": "telemetry-btn", "index": "open"}, + color="info" + ), + dbc.Button("Show URL", id="plot-btn-url", color="info"), + dbc.Modal( + [ + dbc.ModalHeader(dbc.ModalTitle("URL")), + dbc.ModalBody(id="mod-url") + ], + id="plot-mod-url", + size="xl", + is_open=False, + scrollable=True + ) + ] ) ] - def _get_plotting_area( + def _add_plotting_col(self) -> dbc.Col: + """Add column with plots. It is placed on the right side. + + :returns: Column with plots. + :rtype: dbc.Col + """ + return dbc.Col( + id="col-plotting-area", + children=[ + dbc.Spinner( + dbc.Row( + id="plotting-area-trending", + class_name="g-0 p-0", + children=C.PLACEHOLDER + ), + delay_show=C.SPINNER_DELAY + ), + dbc.Row( + id="plotting-area-telemetry", + class_name="g-0 p-0", + children=C.PLACEHOLDER + ) + ], + width=9, + style=C.STYLE_DISABLED, + ) + + def _plotting_area_trending( self, tests: list, - normalize: bool, - url: str - ) -> list: + normalize: bool + ) -> dbc.Col: """Generate the plotting area with all its content. + + :param tests: A list of tests to be displayed in the trending graphs. + :param normalize: If True, the data in graphs is normalized. + :type tests: list + :type normalize: bool + :returns: A collumn with trending graphs (tput and latency) in tabs. + :rtype: dbc.Col """ if not tests: return C.PLACEHOLDER @@ -682,96 +740,275 @@ class Layout: trending = [ dbc.Row( - children=dbc.Tabs( + dbc.Tabs( children=tab_items, id="tabs", active_tab="tab-tput", - ) + ), + class_name="g-0 p-0" ), dbc.Row( - [ - dbc.Col([html.Div( - [ + html.Div( + [ + dbc.Button( + "Download Data", + id="plot-btn-download", + class_name="me-1", + color="info", + style={"padding": "0rem 1rem"} + ), + dcc.Download(id="download-trending-data") + ], + className="d-grid gap-0 d-md-flex justify-content-md-end" + ), + class_name="g-0 p-0" + ) + ] + + return dbc.Col( + children=[ + dbc.Accordion( + dbc.AccordionItem(trending, title="Trending"), + class_name="g-0 p-1", + start_collapsed=False, + always_open=True, + active_item=["item-0", ] + ), + dbc.Modal( + [ + dbc.ModalHeader( + dbc.ModalTitle("Select a Metric"), + close_button=False + ), + dbc.Spinner( + dbc.ModalBody(self._get_telemetry_step_1()), + delay_show=2 * C.SPINNER_DELAY + ), + dbc.ModalFooter([ dbc.Button( - id="plot-btn-url", - children="URL", - class_name="me-1", + "Select", + id={"type": "telemetry-btn", "index": "select"}, + color="success", + disabled=True + ), + dbc.Button( + "Cancel", + id={"type": "telemetry-btn", "index": "cancel"}, color="info", - style={ - "text-transform": "none", - "padding": "0rem 1rem" - } + disabled=False ), - dbc.Modal( - [ - dbc.ModalHeader(dbc.ModalTitle("URL")), - dbc.ModalBody(url) - ], - id="plot-mod-url", - size="xl", - is_open=False, - scrollable=True + dbc.Button( + "Remove All", + id={"type": "telemetry-btn", "index": "rm-all"}, + color="danger", + disabled=False + ) + ]) + ], + id={"type": "plot-mod-telemetry", "index": 0}, + size="lg", + is_open=False, + scrollable=False, + backdrop="static", + keyboard=False + ), + dbc.Modal( + [ + dbc.ModalHeader( + dbc.ModalTitle("Select Labels"), + close_button=False + ), + dbc.Spinner( + dbc.ModalBody(self._get_telemetry_step_2()), + delay_show=2 * C.SPINNER_DELAY + ), + dbc.ModalFooter([ + dbc.Button( + "Back", + id={"type": "telemetry-btn", "index": "back"}, + color="info", + disabled=False + ), + dbc.Button( + "Add Telemetry Panel", + id={"type": "telemetry-btn", "index": "add"}, + color="success", + disabled=True ), dbc.Button( - id="plot-btn-download", - children="Download Data", - class_name="me-1", + "Cancel", + id={"type": "telemetry-btn", "index": "cancel"}, color="info", - style={ - "text-transform": "none", - "padding": "0rem 1rem" - } + disabled=False + ) + ]) + ], + id={"type": "plot-mod-telemetry", "index": 1}, + size="xl", + is_open=False, + scrollable=False, + backdrop="static", + keyboard=False + ) + ] + ) + + @staticmethod + def _plotting_area_telemetry(graphs: list) -> dbc.Col: + """Generate the plotting area with telemetry. + """ + if not graphs: + return C.PLACEHOLDER + + def _plural(iterative): + return "s" if len(iterative) > 1 else str() + + panels = list() + for idx, graph_set in enumerate(graphs): + acc_items = list() + for graph in graph_set[0]: + graph_name = ", ".join(graph[1]) + acc_items.append( + dbc.AccordionItem( + dcc.Graph( + id={"type": "graph-telemetry", "index": graph_name}, + figure=graph[0] + ), + title=(f"Test{_plural(graph[1])}: {graph_name}"), + class_name="g-0 p-0" + ) + ) + panels.append( + dbc.AccordionItem( + [ + dbc.Row( + dbc.Accordion( + children=acc_items, + class_name="g-0 p-0", + always_open=True, + flush=True, + active_item=\ + [f"item-{i}" for i in range(len(acc_items))] ), - dcc.Download(id="download-trending-data") - ], - className=\ - "d-grid gap-0 d-md-flex justify-content-md-end" - )]) - ], - class_name="g-0 p-0" + class_name="g-0 p-0" + ), + dbc.Row( + html.Div( + [ + dbc.Button( + "Remove", + id={ + "type": "tm-btn-remove", + "index": idx + }, + class_name="me-1", + color="danger", + style={"padding": "0rem 1rem"} + ), + dbc.Button( + "Download Data", + id={ + "type": "tm-btn-download", + "index": idx + }, + class_name="me-1", + color="info", + style={"padding": "0rem 1rem"} + ) + ], + className=\ + "d-grid gap-0 d-md-flex justify-content-md-end" + ), + class_name="g-0 p-0" + ) + ], + class_name="g-0 p-0", + title=( + f"Metric{_plural(graph_set[1])}: ", + ", ".join(graph_set[1]) + ) + ) ) - ] - acc_items = [ - dbc.AccordionItem( - title="Trending", - children=trending + return dbc.Col( + dbc.Accordion( + panels, + class_name="g-0 p-1", + always_open=True, + active_item=[f"item-{i}" for i in range(len(panels))] + ) + ) + + @staticmethod + def _get_telemetry_step_1() -> list: + """Return the content of the modal window used in the step 1 of metrics + selection. + + :returns: A list of dbc rows with 'input' and 'search output'. + :rtype: list + """ + return [ + dbc.Row( + class_name="g-0 p-1", + children=[ + dbc.Input( + id={"type": "telemetry-search-in", "index": 0}, + placeholder="Start typing a metric name...", + type="text" + ) + ] + ), + dbc.Row( + class_name="g-0 p-1", + children=[ + dbc.ListGroup( + class_name="overflow-auto p-0", + id={"type": "telemetry-search-out", "index": 0}, + children=[], + style={"max-height": "14em"}, + flush=True + ) + ] ) ] - return dbc.Col( - children=[ - dbc.Row( - dbc.Accordion( - children=acc_items, - class_name="g-0 p-1", - start_collapsed=False, - always_open=True, - active_item=[f"item-{i}" for i in range(len(acc_items))] + @staticmethod + def _get_telemetry_step_2() -> list: + """Return the content of the modal window used in the step 2 of metrics + selection. + + :returns: A list of dbc rows with 'container with dynamic dropdowns' and + 'search output'. + :rtype: list + """ + return [ + dbc.Row( + id={"type": "tm-container", "index": 0}, + class_name="g-0 p-1", + children=["Add content here."] + ), + dbc.Row( + class_name="g-0 p-2", + children=[ + dbc.Checkbox( + id={"type": "cb-all-in-one", "index": 0}, + label="All Metrics in one Graph" ), - class_name="g-0 p-0", - ), - # dbc.Row( - # dbc.Col([html.Div( - # [ - # dbc.Button( - # id="btn-add-telemetry", - # children="Add Panel with Telemetry", - # class_name="me-1", - # color="info", - # style={ - # "text-transform": "none", - # "padding": "0rem 1rem" - # } - # ) - # ], - # className=\ - # "d-grid gap-0 d-md-flex justify-content-md-end" - # )]), - # class_name="g-0 p-0" - # ) - ] - ) + ] + ), + dbc.Row( + class_name="g-0 p-1", + children=[ + dbc.Textarea( + id={"type": "tm-list-metrics", "index": 0}, + rows=20, + size="sm", + wrap="off", + readonly=True + ) + ] + ) + ] def callbacks(self, app): """Callbacks for the whole application. @@ -779,68 +1016,109 @@ class Layout: :param app: The application. :type app: Flask """ - + @app.callback( - [ - Output("store-control-panel", "data"), - Output("store-selected-tests", "data"), - Output("plotting-area", "children"), - Output("row-card-sel-tests", "style"), - Output("row-btns-sel-tests", "style"), - Output("lg-selected", "children"), - - Output({"type": "ctrl-dd", "index": "dut"}, "value"), - Output({"type": "ctrl-dd", "index": "phy"}, "options"), - Output({"type": "ctrl-dd", "index": "phy"}, "disabled"), - Output({"type": "ctrl-dd", "index": "phy"}, "value"), - Output({"type": "ctrl-dd", "index": "area"}, "options"), - Output({"type": "ctrl-dd", "index": "area"}, "disabled"), - Output({"type": "ctrl-dd", "index": "area"}, "value"), - Output({"type": "ctrl-dd", "index": "test"}, "options"), - Output({"type": "ctrl-dd", "index": "test"}, "disabled"), - Output({"type": "ctrl-dd", "index": "test"}, "value"), - Output({"type": "ctrl-cl", "index": "core"}, "options"), - Output({"type": "ctrl-cl", "index": "core"}, "value"), - Output({"type": "ctrl-cl", "index": "core-all"}, "value"), - Output({"type": "ctrl-cl", "index": "core-all"}, "options"), - Output({"type": "ctrl-cl", "index": "frmsize"}, "options"), - Output({"type": "ctrl-cl", "index": "frmsize"}, "value"), - Output({"type": "ctrl-cl", "index": "frmsize-all"}, "value"), - Output({"type": "ctrl-cl", "index": "frmsize-all"}, "options"), - Output({"type": "ctrl-cl", "index": "tsttype"}, "options"), - Output({"type": "ctrl-cl", "index": "tsttype"}, "value"), - Output({"type": "ctrl-cl", "index": "tsttype-all"}, "value"), - Output({"type": "ctrl-cl", "index": "tsttype-all"}, "options"), - Output({"type": "ctrl-btn", "index": "add-test"}, "disabled"), - Output("normalize", "value") - ], - [ - State("store-control-panel", "data"), - State("store-selected-tests", "data"), - State({"type": "sel-cl", "index": ALL}, "value") - ], - [ - Input("url", "href"), - Input("normalize", "value"), + Output("store", "data"), + Output("plotting-area-trending", "children"), + Output("plotting-area-telemetry", "children"), + Output("col-plotting-area", "style"), + Output("row-card-sel-tests", "style"), + Output("row-btns-sel-tests", "style"), + Output("row-btns-add-tm", "style"), + Output("lg-selected", "children"), + Output({"type": "telemetry-search-out", "index": ALL}, "children"), + Output({"type": "plot-mod-telemetry", "index": ALL}, "is_open"), + Output({"type": "telemetry-btn", "index": ALL}, "disabled"), + Output({"type": "tm-container", "index": ALL}, "children"), + Output({"type": "tm-list-metrics", "index": ALL}, "value"), + Output({"type": "ctrl-dd", "index": "dut"}, "value"), + Output({"type": "ctrl-dd", "index": "phy"}, "options"), + Output({"type": "ctrl-dd", "index": "phy"}, "disabled"), + Output({"type": "ctrl-dd", "index": "phy"}, "value"), + Output({"type": "ctrl-dd", "index": "area"}, "options"), + Output({"type": "ctrl-dd", "index": "area"}, "disabled"), + Output({"type": "ctrl-dd", "index": "area"}, "value"), + Output({"type": "ctrl-dd", "index": "test"}, "options"), + Output({"type": "ctrl-dd", "index": "test"}, "disabled"), + Output({"type": "ctrl-dd", "index": "test"}, "value"), + Output({"type": "ctrl-cl", "index": "core"}, "options"), + Output({"type": "ctrl-cl", "index": "core"}, "value"), + Output({"type": "ctrl-cl", "index": "core-all"}, "value"), + Output({"type": "ctrl-cl", "index": "core-all"}, "options"), + Output({"type": "ctrl-cl", "index": "frmsize"}, "options"), + Output({"type": "ctrl-cl", "index": "frmsize"}, "value"), + Output({"type": "ctrl-cl", "index": "frmsize-all"}, "value"), + Output({"type": "ctrl-cl", "index": "frmsize-all"}, "options"), + Output({"type": "ctrl-cl", "index": "tsttype"}, "options"), + Output({"type": "ctrl-cl", "index": "tsttype"}, "value"), + Output({"type": "ctrl-cl", "index": "tsttype-all"}, "value"), + Output({"type": "ctrl-cl", "index": "tsttype-all"}, "options"), + Output({"type": "ctrl-btn", "index": "add-test"}, "disabled"), + Output("normalize", "value"), + + State("store", "data"), + State({"type": "sel-cl", "index": ALL}, "value"), + State({"type": "cb-all-in-one", "index": ALL}, "value"), + State({"type": "telemetry-search-out", "index": ALL}, "children"), + State({"type": "plot-mod-telemetry", "index": ALL}, "is_open"), + State({"type": "telemetry-btn", "index": ALL}, "disabled"), + State({"type": "tm-container", "index": ALL}, "children"), + State({"type": "tm-list-metrics", "index": ALL}, "value"), + + Input("url", "href"), + Input({"type": "tele-cl", "index": ALL}, "value"), + Input({"type": "tm-dd", "index": ALL}, "value"), + + Input("normalize", "value"), + Input({"type": "telemetry-search-in", "index": ALL}, "value"), + Input({"type": "telemetry-btn", "index": ALL}, "n_clicks"), + Input({"type": "tm-btn-remove", "index": ALL}, "n_clicks"), + Input({"type": "ctrl-dd", "index": ALL}, "value"), + Input({"type": "ctrl-cl", "index": ALL}, "value"), + Input({"type": "ctrl-btn", "index": ALL}, "n_clicks"), - Input({"type": "ctrl-dd", "index": ALL}, "value"), - Input({"type": "ctrl-cl", "index": ALL}, "value"), - Input({"type": "ctrl-btn", "index": ALL}, "n_clicks") - ] + prevent_initial_call=True ) def _update_application( - control_panel: dict, - store_sel: list, + store: dict, lst_sel: list, + all_in_one: list, + search_out: list, + is_open: list, + tm_btns_disabled: list, + tm_dd: list, + list_metrics: list, href: str, - normalize: list, + cl_metrics: list, + tm_dd_in: list, *_ ) -> tuple: """Update the application when the event is detected. """ - ctrl_panel = ControlPanel(CP_PARAMS, control_panel) - on_draw = False + if store is None: + store = { + "control-panel": dict(), + "selected-tests": list(), + "telemetry-data": dict(), + "selected-metrics": dict(), + "telemetry-panels": list(), + "telemetry-all-in-one": list(), + "url": str() + } + + ctrl_panel = ControlPanel( + CP_PARAMS, + store.get("control-panel", dict()) + ) + store_sel = store["selected-tests"] + tm_data = store["telemetry-data"] + tm_user = store["selected-metrics"] + tm_panels = store["telemetry-panels"] + tm_all_in_one = store["telemetry-all-in-one"] + + plotting_area_telemetry = no_update + on_draw = [False, False] # 0 --> trending, 1 --> telemetry # Parse the url: parsed_url = url_decode(href) @@ -849,18 +1127,39 @@ class Layout: else: url_params = None - plotting_area = no_update - row_card_sel_tests = no_update - row_btns_sel_tests = no_update - lg_selected = no_update + if tm_user is None: + # Telemetry user data + # The data provided by user or result of user action + tm_user = { + # List of unique metrics: + "unique_metrics": list(), + # List of metrics selected by user: + "selected_metrics": list(), + # Labels from metrics selected by user (key: label name, + # value: list of all possible values): + "unique_labels": dict(), + # Labels selected by the user (subset of 'unique_labels'): + "selected_labels": dict(), + # All unique metrics with labels (output from the step 1) + # converted from pandas dataframe to dictionary. + "unique_metrics_with_labels": dict(), + # Metrics with labels selected by the user using dropdowns. + "selected_metrics_with_labels": dict() + } + tm = TelemetryData(store_sel) if store_sel else TelemetryData() trigger = Trigger(callback_context.triggered) - if trigger.type == "url" and url_params: + telemetry = None try: store_sel = literal_eval(url_params["store_sel"][0]) normalize = literal_eval(url_params["norm"][0]) - except (KeyError, IndexError): + telemetry = literal_eval(url_params["telemetry"][0]) + tm_all_in_one = literal_eval(url_params["all-in-one"][0]) + if not isinstance(telemetry, list): + telemetry = [telemetry, ] + tm_all_in_one = [tm_all_in_one, ] + except (KeyError, IndexError, AttributeError, ValueError): pass if store_sel: last_test = store_sel[-1] @@ -902,10 +1201,17 @@ class Layout: "cl-normalize-val": normalize, "btn-add-dis": False }) - on_draw = True + on_draw[0] = True + if telemetry: + tm = TelemetryData(store_sel) + tm.from_dataframe(self._data) + tm_data = tm.to_json() + tm.from_json(tm_data) + tm_panels = telemetry + on_draw[1] = True elif trigger.type == "normalize": - ctrl_panel.set({"cl-normalize-val": normalize}) - on_draw = True + ctrl_panel.set({"cl-normalize-val": trigger.value}) + on_draw[0] = True elif trigger.type == "ctrl-dd": if trigger.idx == "dut": try: @@ -1049,7 +1355,7 @@ class Layout: else: ctrl_panel.set({"btn-add-dis": True}) elif trigger.type == "ctrl-btn": - on_draw = True + on_draw[0] = True if trigger.idx == "add-test": dut = ctrl_panel.get("dd-dut-val") phy = ctrl_panel.get("dd-phy-val") @@ -1094,48 +1400,306 @@ class Layout: store_sel = new_store_sel elif trigger.idx == "rm-test-all": store_sel = list() - - if on_draw: + elif trigger.type == "telemetry-btn": + if trigger.idx in ("open", "back"): + tm.from_dataframe(self._data) + tm_data = tm.to_json() + tm_user["unique_metrics"] = tm.unique_metrics + tm_user["selected_metrics"] = list() + tm_user["unique_labels"] = dict() + tm_user["selected_labels"] = dict() + search_out = ( + get_list_group_items(tm_user["unique_metrics"], + "tele-cl", False), + ) + is_open = (True, False) + tm_btns_disabled[1], tm_btns_disabled[5] = True, True + elif trigger.idx == "select": + tm.from_json(tm_data) + if any(cl_metrics): + if not tm_user["selected_metrics"]: + tm_user["selected_metrics"] = \ + tm_user["unique_metrics"] + metrics = [a for a, b in \ + zip(tm_user["selected_metrics"], cl_metrics) if b] + tm_user["selected_metrics"] = metrics + tm_user["unique_labels"] = \ + tm.get_selected_labels(metrics) + tm_user["unique_metrics_with_labels"] = \ + tm.unique_metrics_with_labels + list_metrics[0] = tm.str_metrics + tm_dd[0] = _get_dd_container(tm_user["unique_labels"]) + if list_metrics[0]: + tm_btns_disabled[1] = True + tm_btns_disabled[4] = False + is_open = (False, True) + else: + tm_user = None + is_open = (False, False) + elif trigger.idx == "add": + tm.from_json(tm_data) + tm_panels.append(tm_user["selected_metrics_with_labels"]) + tm_all_in_one.append(all_in_one) + is_open = (False, False) + tm_btns_disabled[1], tm_btns_disabled[5] = True, True + on_draw = [True, True] + elif trigger.idx == "cancel": + is_open = (False, False) + tm_btns_disabled[1], tm_btns_disabled[5] = True, True + elif trigger.idx == "rm-all": + tm_panels = list() + tm_all_in_one = list() + tm_user = None + is_open = (False, False) + tm_btns_disabled[1], tm_btns_disabled[5] = True, True + plotting_area_telemetry = C.PLACEHOLDER + elif trigger.type == "telemetry-search-in": + tm.from_metrics(tm_user["unique_metrics"]) + tm_user["selected_metrics"] = \ + tm.search_unique_metrics(trigger.value) + search_out = (get_list_group_items( + tm_user["selected_metrics"], + type="tele-cl", + colorize=False + ), ) + is_open = (True, False) + elif trigger.type == "tele-cl": + if any(cl_metrics): + tm_btns_disabled[1] = False + else: + tm_btns_disabled[1] = True + is_open = (True, False) + elif trigger.type == "tm-dd": + tm.from_metrics_with_labels( + tm_user["unique_metrics_with_labels"] + ) + selected = dict() + previous_itm = None + for itm in tm_dd_in: + if itm is None: + show_new = True + elif isinstance(itm, str): + show_new = False + selected[itm] = list() + elif isinstance(itm, list): + if previous_itm is not None: + selected[previous_itm] = itm + show_new = True + previous_itm = itm + tm_dd[0] = _get_dd_container( + tm_user["unique_labels"], + selected, + show_new + ) + sel_metrics = tm.filter_selected_metrics_by_labels(selected) + tm_user["selected_metrics_with_labels"] = sel_metrics.to_dict() + if not sel_metrics.empty: + list_metrics[0] = tm.metrics_to_str(sel_metrics) + tm_btns_disabled[5] = False + else: + list_metrics[0] = str() + elif trigger.type == "tm-btn-remove": + del tm_panels[trigger.idx] + del tm_all_in_one[trigger.idx] + tm.from_json(tm_data) + on_draw = [True, True] + + new_url_params = { + "store_sel": store_sel, + "norm": ctrl_panel.get("cl-normalize-val") + } + if tm_panels: + new_url_params["telemetry"] = tm_panels + new_url_params["all-in-one"] = tm_all_in_one + + if on_draw[0]: # Trending if store_sel: - lg_selected = get_list_group_items(store_sel) - plotting_area = self._get_plotting_area( + lg_selected = get_list_group_items(store_sel, "sel-cl") + plotting_area_trending = self._plotting_area_trending( store_sel, - bool(normalize), - gen_new_url( - parsed_url, - {"store_sel": store_sel, "norm": normalize} - ) + bool(ctrl_panel.get("cl-normalize-val")) ) + if on_draw[1]: # Telemetry + tm_graphs = list() + for panel, aio in zip(tm_panels, tm_all_in_one): + tm_graphs.append(graph_tm_trending( + tm.select_tm_trending_data(panel), + self._graph_layout, + bool(aio[0]) + )) + plotting_area_telemetry = \ + Layout._plotting_area_telemetry(tm_graphs) + col_plotting_area = C.STYLE_ENABLED row_card_sel_tests = C.STYLE_ENABLED row_btns_sel_tests = C.STYLE_ENABLED + row_btns_add_tm = C.STYLE_ENABLED else: - plotting_area = C.PLACEHOLDER + plotting_area_trending = no_update + plotting_area_telemetry = C.PLACEHOLDER + col_plotting_area = C.STYLE_DISABLED row_card_sel_tests = C.STYLE_DISABLED row_btns_sel_tests = C.STYLE_DISABLED + row_btns_add_tm = C.STYLE_DISABLED + lg_selected = no_update store_sel = list() - + tm_panels = list() + tm_all_in_one = list() + tm_user = None + else: + plotting_area_trending = no_update + col_plotting_area = no_update + row_card_sel_tests = no_update + row_btns_sel_tests = no_update + row_btns_add_tm = no_update + lg_selected = no_update + + store["url"] = gen_new_url(parsed_url, new_url_params) + store["control-panel"] = ctrl_panel.panel + store["selected-tests"] = store_sel + store["telemetry-data"] = tm_data + store["selected-metrics"] = tm_user + store["telemetry-panels"] = tm_panels + store["telemetry-all-in-one"] = tm_all_in_one ret_val = [ - ctrl_panel.panel, - store_sel, - plotting_area, + store, + plotting_area_trending, + plotting_area_telemetry, + col_plotting_area, row_card_sel_tests, row_btns_sel_tests, - lg_selected + row_btns_add_tm, + lg_selected, + search_out, + is_open, + tm_btns_disabled, + tm_dd, + list_metrics ] ret_val.extend(ctrl_panel.values) return ret_val @app.callback( Output("plot-mod-url", "is_open"), - [Input("plot-btn-url", "n_clicks")], - [State("plot-mod-url", "is_open")], + Output("mod-url", "children"), + State("store", "data"), + State("plot-mod-url", "is_open"), + Input("plot-btn-url", "n_clicks") ) - def toggle_plot_mod_url(n, is_open): + def toggle_plot_mod_url(store, is_open, n_clicks): """Toggle the modal window with url. """ - if n: - return not is_open - return is_open + if not store: + raise PreventUpdate + + if n_clicks: + return not is_open, store.get("url", str()) + return is_open, store["url"] + + def _get_dd_container( + all_labels: dict, + selected_labels: dict=dict(), + show_new=True + ) -> list: + """Generate a container with dropdown selection boxes depenting on + the input data. + + :param all_labels: A dictionary with unique labels and their + possible values. + :param selected_labels: A dictionalry with user selected lables and + their values. + :param show_new: If True, a dropdown selection box to add a new + label is displayed. + :type all_labels: dict + :type selected_labels: dict + :type show_new: bool + :returns: A list of dbc rows with dropdown selection boxes. + :rtype: list + """ + + def _row( + id: str, + lopts: list=list(), + lval: str=str(), + vopts: list=list(), + vvals: list=list() + ) -> dbc.Row: + """Generates a dbc row with dropdown boxes. + + :param id: A string added to the dropdown ID. + :param lopts: A list of options for 'label' dropdown. + :param lval: Value of 'label' dropdown. + :param vopts: A list of options for 'value' dropdown. + :param vvals: A list of values for 'value' dropdown. + :type id: str + :type lopts: list + :type lval: str + :type vopts: list + :type vvals: list + :returns: dbc row with dropdown boxes. + :rtype: dbc.Row + """ + children = list() + if lopts: + children.append( + dbc.Col( + width=6, + children=[ + dcc.Dropdown( + id={ + "type": "tm-dd", + "index": f"label-{id}" + }, + placeholder="Select a label...", + optionHeight=20, + multi=False, + options=lopts, + value=lval if lval else None + ) + ] + ) + ) + if vopts: + children.append( + dbc.Col( + width=6, + children=[ + dcc.Dropdown( + id={ + "type": "tm-dd", + "index": f"value-{id}" + }, + placeholder="Select a value...", + optionHeight=20, + multi=True, + options=vopts, + value=vvals if vvals else None + ) + ] + ) + ) + + return dbc.Row(class_name="g-0 p-1", children=children) + + container = list() + + # Display rows with items in 'selected_labels'; label on the left, + # values on the right: + keys_left = list(all_labels.keys()) + for idx, label in enumerate(selected_labels.keys()): + container.append(_row( + id=idx, + lopts=deepcopy(keys_left), + lval=label, + vopts=all_labels[label], + vvals=selected_labels[label] + )) + keys_left.remove(label) + + # Display row with dd with labels on the left, right side is empty: + if show_new and keys_left: + container.append(_row(id="new", lopts=keys_left)) + + return container @app.callback( Output("metadata-tput-lat", "children"), @@ -1219,11 +1783,12 @@ class Layout: @app.callback( Output("download-trending-data", "data"), - State("store-selected-tests", "data"), + State("store", "data"), Input("plot-btn-download", "n_clicks"), + Input({"type": "tm-btn-download", "index": ALL}, "n_clicks"), prevent_initial_call=True ) - def _download_trending_data(store_sel, _): + def _download_data(store: list, *_) -> dict: """Download the data :param store_sel: List of tests selected by user stored in the @@ -1234,14 +1799,34 @@ class Layout: :rtype: dict """ - if not store_sel: + if not store: raise PreventUpdate - + if not store["selected-tests"]: + raise PreventUpdate + df = pd.DataFrame() - for itm in store_sel: - sel_data = select_trending_data(self._data, itm) - if sel_data is None: - continue - df = pd.concat([df, sel_data], ignore_index=True) + + trigger = Trigger(callback_context.triggered) + if not trigger.value: + raise PreventUpdate + + if trigger.type == "plot-btn-download": + data = list() + for itm in store["selected-tests"]: + sel_data = select_trending_data(self._data, itm) + if sel_data is None: + continue + data.append(sel_data) + df = pd.concat(data, ignore_index=True, copy=False) + file_name = C.TREND_DOWNLOAD_FILE_NAME + elif trigger.type == "tm-btn-download": + tm = TelemetryData(store["selected-tests"]) + tm.from_json(store["telemetry-data"]) + df = tm.select_tm_trending_data( + store["telemetry-panels"][trigger.idx] + ) + file_name = C.TELEMETRY_DOWNLOAD_FILE_NAME + else: + raise PreventUpdate - return dcc.send_data_frame(df.to_csv, C.TREND_DOWNLOAD_FILE_NAME) + return dcc.send_data_frame(df.to_csv, file_name)