aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/dash/app/pal/trending/layout.py
blob: 69ce0c416dae4175a1b39c1819d43fb9909cce31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright (c) 2022 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:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Plotly Dash HTML layout override.
"""

import logging

from dash import dcc
from dash import html
from dash import Input, Output, callback
from dash.exceptions import PreventUpdate
from yaml import load, FullLoader, YAMLError


class Layout:
    """
    """

    def __init__(self, app, html_layout_file, spec_file):
        """
        """

        # Inputs
        self._app = app
        self._html_layout_file = html_layout_file
        self._spec_file = spec_file

        # Read from files:
        self._html_layout = ""
        self._spec_test = None

        try:
            with open(self._html_layout_file, "r") as layout_file:
                self._html_layout = layout_file.read()
        except IOError as err:
            logging.error(f"Not possible to open the file {layout_file}\n{err}")

        try:
            with open(self._spec_file, "r") as file_read:
                self._spec_test = load(file_read, Loader=FullLoader)
        except IOError as err:
            logging.error(f"Not possible to open the file {spec_file}\n{err}")
        except YAMLError as err:
            logging.error(
                f"An error occurred while parsing the specification file "
                f"{spec_file}\n"
                f"{err}"
            )

        # Callbacks:
        if self._app is not None and hasattr(self, 'callbacks'):
            self.callbacks(self._app)

        # User choice (one test):
        self._test_selection = {
            "phy": "",
            "area": "",
            "test": "",
            "core": "",
            "frame-size": "",
            "test-type": ""
        }

    @property
    def html_layout(self):
        return self._html_layout

    @property
    def spec_test(self):
        return self._spec_test

    def _reset_test_selection(self):
        self._test_selection = {
            "phy": "",
            "area": "",
            "test": "",
            "core": "",
            "frame-size": "",
            "test-type": ""
        }

    def add_content(self):
        """
        """
        if self._html_layout and self._spec_test:
            return html.Div(
                id="div-main",
                children=[
                    self._add_ctrl_div(),
                    self._add_plotting_div()
                ]
            )
        else:
            return html.Div(
            id="div-main-error",
            children="An Error Occured."
        )

    def _add_ctrl_div(self):
        """Add div with controls. It is placed on the left side.
        """
        return html.Div(
            id="div-controls",
            children=[
                html.Div(
                    id="div-controls-tabs",
                    children=[
                        self._add_ctrl_select(),
                        self._add_ctrl_shown()
                    ]
                )
            ],
            style={
                "display": "inline-block",
                "width": "18%",
                "padding": "5px"
            }
        )

    def _add_plotting_div(self):
        """Add div with plots and tables. It is placed on the right side.
        """
        return html.Div(
            id="div-plotting-area",
            children=[
                # Only a visible note.
                # TODO: Add content.
                html.H3(
                    "Graphs and Tables",
                    style={
                        "vertical-align": "middle",
                        "text-align": "center"
                    }
                )
            ],
            style={
                "vertical-align": "middle",
                "display": "inline-block",
                "width": "80%",
                "padding": "5px"
            }
        )

    def _add_ctrl_shown(self):
        """
        """
        return html.Div(
            id="div-ctrl-shown",
            children="List of selected tests"
        )

    def _add_ctrl_select(self):
        """
        """
        return html.Div(
            id="div-ctrl-select",
            children=[
                html.Br(),
                html.Div(
                    children="Physical Test Bed Topology, NIC and Driver"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-phy",
                    placeholder="Select a Physical Test Bed Topology...",
                    multi=False,
                    clearable=False,
                    options=[
                        {"label": k, "value": k} for k in self._spec_test.keys()
                    ],
                ),
                html.Br(),
                html.Div(
                    children="Area"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-area",
                    placeholder="Select an Area...",
                    disabled=True,
                    multi=False,
                    clearable=False,
                ),
                html.Br(),
                html.Div(
                    children="Test"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-test",
                    placeholder="Select a Test...",
                    disabled=True,
                    multi=False,
                    clearable=False,
                ),

                # Change to radio buttons:
                html.Br(),
                html.Div(
                    children="Number of Cores"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-core",
                    placeholder="Select a Number of Cores...",
                    disabled=True,
                    multi=False,
                    clearable=False,
                ),
                html.Br(),
                html.Div(
                    children="Frame Size"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-framesize",
                    placeholder="Select a Frame Size...",
                    disabled=True,
                    multi=False,
                    clearable=False,
                ),
                html.Br(),
                html.Div(
                    children="Test Type"
                ),
                dcc.Dropdown(
                    id="dd-ctrl-testtype",
                    placeholder="Select a Test Type...",
                    disabled=True,
                    multi=False,
                    clearable=False,
                ),
                            html.Br(),
                html.Button(
                    id="btn-ctrl-add",
                    children="Add",
                    disabled=True
                ),
                html.Br(),
                html.Div(
                    id="div-ctrl-info"
                )
            ]
        )

    def callbacks(self, app):

        @app.callback(
            Output("dd-ctrl-area", "options"),
            Output("dd-ctrl-area", "disabled"),
            Input("dd-ctrl-phy", "value"),
        )
        def _update_dd_area(phy):
            """
            """

            if phy is None:
                raise PreventUpdate

            try:
                options = [
                    {"label": self._spec_test[phy][v]["label"], "value": v}
                        for v in [v for v in self._spec_test[phy].keys()]
                ]
            except KeyError:
                options = list()

            return options, False

        @app.callback(
            Output("dd-ctrl-test", "options"),
            Output("dd-ctrl-test", "disabled"),
            Input("dd-ctrl-phy", "value"),
            Input("dd-ctrl-area", "value"),
        )
        def _update_dd_test(phy, area):
            """
            """

            if not all((phy, area, )):
                raise PreventUpdate

            try:
                options = [
                    {"label": v, "value": v}
                        for v in self._spec_test[phy][area]["test"]
                ]
            except KeyError:
                options = list()

            return options, False

        @app.callback(
            Output("btn-ctrl-add", "disabled"),
            Input("dd-ctrl-phy", "value"),
            Input("dd-ctrl-area", "value"),
            Input("dd-ctrl-test", "value"),
        )
        def _update_btn_add(phy, area, test):
            """
            """

            if all((phy, area, test, )):
                self._test_selection["phy"] = phy
                self._test_selection["area"] = area
                self._test_selection["test"] = test
                return False
            else:
                return True

        @app.callback(
            Output("div-ctrl-info", "children"),
            Output("dd-ctrl-phy", "value"),
            Output("dd-ctrl-area", "value"),
            Output("dd-ctrl-test", "value"),
            Output("btn-ctrl-add", "n_clicks"),
            Input("btn-ctrl-add", "n_clicks")
        )
        def _print_user_selection(n_clicks):
            """
            """

            logging.info(f"\n\n{n_clicks}\n\n")

            if not n_clicks:
                raise PreventUpdate

            selected = (
                f"{self._test_selection['phy']} # "
                f"{self._test_selection['area']} # "
                f"{self._test_selection['test']} # "
                f"{n_clicks}\n"
            )

            self._reset_test_selection()

            return (
                selected,
                None,
                None,
                None,
                0,
            )