1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Functions used by Dash applications.
18 import plotly.graph_objects as go
19 import dash_bootstrap_components as dbc
26 from datetime import datetime
28 from ..utils.constants import Constants as C
29 from ..utils.url_processing import url_encode
32 def get_color(idx: int) -> str:
33 """Returns a color from the list defined in Constants.PLOT_COLORS defined by
36 :param idx: Index of the color.
38 :returns: Color defined by hex code.
41 return C.PLOT_COLORS[idx % len(C.PLOT_COLORS)]
44 def show_tooltip(tooltips:dict, id: str, title: str,
45 clipboard_id: str=None) -> list:
46 """Generate list of elements to display a text (e.g. a title) with a
47 tooltip and optionaly with Copy&Paste icon and the clipboard
48 functionality enabled.
50 :param tooltips: Dictionary with tooltips.
51 :param id: Tooltip ID.
52 :param title: A text for which the tooltip will be displayed.
53 :param clipboard_id: If defined, a Copy&Paste icon is displayed and the
54 clipboard functionality is enabled.
58 :type clipboard_id: str
59 :returns: List of elements to display a text with a tooltip and
60 optionaly with Copy&Paste icon.
65 dcc.Clipboard(target_id=clipboard_id, title="Copy URL") \
66 if clipboard_id else str(),
74 class_name="border ms-1",
77 children=tooltips.get(id, str()),
84 def label(key: str) -> str:
85 """Returns a label for input elements (dropdowns, ...).
87 If the label is not defined, the function returns the provided key.
89 :param key: The key to the label defined in Constants.LABELS.
94 return C.LABELS.get(key, key)
97 def sync_checklists(options: list, sel: list, all: list, id: str) -> tuple:
98 """Synchronize a checklist with defined "options" with its "All" checklist.
100 :param options: List of options for the cheklist.
101 :param sel: List of selected options.
102 :param all: List of selected option from "All" checklist.
103 :param id: ID of a checklist to be used for synchronization.
104 :returns: Tuple of lists with otions for both checklists.
105 :rtype: tuple of lists
107 opts = {v["value"] for v in options}
109 sel = list(opts) if all else list()
111 all = ["all", ] if set(sel) == opts else list()
115 def list_tests(selection: dict) -> list:
116 """Transform list of tests to a list of dictionaries usable by checkboxes.
118 :param selection: List of tests to be displayed in "Selected tests" window.
119 :type selection: list
120 :returns: List of dictionaries with "label", "value" pairs for a checkbox.
124 return [{"label": v["id"], "value": v["id"]} for v in selection]
129 def get_date(s_date: str) -> datetime:
130 """Transform string reprezentation of date to datetime.datetime data type.
132 :param s_date: String reprezentation of date.
134 :returns: Date as datetime.datetime.
135 :rtype: datetime.datetime
137 return datetime(int(s_date[0:4]), int(s_date[5:7]), int(s_date[8:10]))
140 def gen_new_url(url_components: dict, params: dict) -> str:
141 """Generate a new URL with encoded parameters.
143 :param url_components: Dictionary with URL elements. It should contain
144 "scheme", "netloc" and "path".
145 :param url_components: URL parameters to be encoded to the URL.
146 :type parsed_url: dict
148 :returns Encoded URL with parameters.
155 "scheme": url_components.get("scheme", ""),
156 "netloc": url_components.get("netloc", ""),
157 "path": url_components.get("path", ""),
165 def get_duts(df: pd.DataFrame) -> list:
166 """Get the list of DUTs from the pre-processed information about jobs.
168 :param df: DataFrame with information about jobs.
169 :type df: pandas.DataFrame
170 :returns: Alphabeticaly sorted list of DUTs.
173 return sorted(list(df["dut"].unique()))
176 def get_ttypes(df: pd.DataFrame, dut: str) -> list:
177 """Get the list of test types from the pre-processed information about
180 :param df: DataFrame with information about jobs.
181 :param dut: The DUT for which the list of test types will be populated.
182 :type df: pandas.DataFrame
184 :returns: Alphabeticaly sorted list of test types.
187 return sorted(list(df.loc[(df["dut"] == dut)]["ttype"].unique()))
190 def get_cadences(df: pd.DataFrame, dut: str, ttype: str) -> list:
191 """Get the list of cadences from the pre-processed information about
194 :param df: DataFrame with information about jobs.
195 :param dut: The DUT for which the list of cadences will be populated.
196 :param ttype: The test type for which the list of cadences will be
198 :type df: pandas.DataFrame
201 :returns: Alphabeticaly sorted list of cadences.
204 return sorted(list(df.loc[(
206 (df["ttype"] == ttype)
207 )]["cadence"].unique()))
210 def get_test_beds(df: pd.DataFrame, dut: str, ttype: str, cadence: str) -> list:
211 """Get the list of test beds from the pre-processed information about
214 :param df: DataFrame with information about jobs.
215 :param dut: The DUT for which the list of test beds will be populated.
216 :param ttype: The test type for which the list of test beds will be
218 :param cadence: The cadence for which the list of test beds will be
220 :type df: pandas.DataFrame
224 :returns: Alphabeticaly sorted list of test beds.
227 return sorted(list(df.loc[(
229 (df["ttype"] == ttype) &
230 (df["cadence"] == cadence)
231 )]["tbed"].unique()))
234 def get_job(df: pd.DataFrame, dut, ttype, cadence, testbed):
235 """Get the name of a job defined by dut, ttype, cadence, test bed.
236 Input information comes from the control panel.
238 :param df: DataFrame with information about jobs.
239 :param dut: The DUT for which the job name will be created.
240 :param ttype: The test type for which the job name will be created.
241 :param cadence: The cadence for which the job name will be created.
242 :param testbed: The test bed for which the job name will be created.
243 :type df: pandas.DataFrame
253 (df["ttype"] == ttype) &
254 (df["cadence"] == cadence) &
255 (df["tbed"] == testbed)
259 def generate_options(opts: list, sort: bool=True) -> list:
260 """Return list of options for radio items in control panel. The items in
261 the list are dictionaries with keys "label" and "value".
263 :params opts: List of options (str) to be used for the generated list.
265 :returns: List of options (dict).
270 return [{"label": i, "value": i} for i in opts]
273 def set_job_params(df: pd.DataFrame, job: str) -> dict:
274 """Create a dictionary with all options and values for (and from) the
277 :param df: DataFrame with information about jobs.
278 :params job: The name of job for and from which the dictionary will be
280 :type df: pandas.DataFrame
282 :returns: Dictionary with all options and values for (and from) the
287 l_job = job.split("-")
293 "tbed": "-".join(l_job[-2:]),
294 "duts": generate_options(get_duts(df)),
295 "ttypes": generate_options(get_ttypes(df, l_job[1])),
296 "cadences": generate_options(get_cadences(df, l_job[1], l_job[3])),
297 "tbeds": generate_options(
298 get_test_beds(df, l_job[1], l_job[3], l_job[4]))
302 def get_list_group_items(
306 add_index: bool=False
308 """Generate list of ListGroupItems with checkboxes with selected items.
310 :param items: List of items to be displayed in the ListGroup.
311 :param type: The type part of an element ID.
312 :param colorize: If True, the color of labels is set, otherwise the default
314 :param add_index: Add index to the list items.
318 :type add_index: bool
319 :returns: List of ListGroupItems with checkboxes with selected items.
324 for i, l in enumerate(items):
325 idx = f"{i + 1}. " if add_index else str()
326 label = f"{idx}{l['id']}" if isinstance(l, dict) else f"{idx}{l}"
331 id={"type": type, "index": i},
334 label_class_name="m-0 p-0",
336 "font-size": ".875em",
337 "color": get_color(i) if colorize else "#55595c"
349 def relative_change_stdev(mean1, mean2, std1, std2):
350 """Compute relative standard deviation of change of two values.
352 The "1" values are the base for comparison.
353 Results are returned as percentage (and percentual points for stdev).
354 Linearized theory is used, so results are wrong for relatively large stdev.
356 :param mean1: Mean of the first number.
357 :param mean2: Mean of the second number.
358 :param std1: Standard deviation estimate of the first number.
359 :param std2: Standard deviation estimate of the second number.
364 :returns: Relative change and its stdev.
367 mean1, mean2 = float(mean1), float(mean2)
368 quotient = mean2 / mean1
370 second = std2 / mean2
371 std = quotient * sqrt(first * first + second * second)
372 return (quotient - 1) * 100, std * 100
375 def get_hdrh_latencies(row: pd.Series, name: str) -> dict:
376 """Get the HDRH latencies from the test data.
378 :param row: A row fron the data frame with test data.
379 :param name: The test name to be displayed as the graph title.
380 :type row: pandas.Series
382 :returns: Dictionary with HDRH latencies.
386 latencies = {"name": name}
387 for key in C.LAT_HDRH:
389 latencies[key] = row[key]
396 def graph_hdrh_latency(data: dict, layout: dict) -> go.Figure:
397 """Generate HDR Latency histogram graphs.
399 :param data: HDRH data.
400 :param layout: Layout of plot.ly graph.
403 :returns: HDR latency Histogram.
404 :rtype: plotly.graph_objects.Figure
410 for idx, (lat_name, lat_hdrh) in enumerate(data.items()):
412 decoded = hdrh.histogram.HdrHistogram.decode(lat_hdrh)
413 except (hdrh.codec.HdrLengthException, TypeError):
420 for item in decoded.get_recorded_iterator():
421 # The real value is "percentile".
422 # For 100%, we cut that down to "x_perc" to avoid
424 percentile = item.percentile_level_iterated_to
425 x_perc = min(percentile, C.PERCENTILE_MAX)
426 xaxis.append(previous_x)
427 yaxis.append(item.value_iterated_to)
429 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
430 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
431 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
432 f"Latency: {item.value_iterated_to}uSec"
434 next_x = 100.0 / (100.0 - x_perc)
436 yaxis.append(item.value_iterated_to)
438 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
439 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
440 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
441 f"Latency: {item.value_iterated_to}uSec"
444 prev_perc = percentile
450 name=C.GRAPH_LAT_HDRH_DESC[lat_name],
452 legendgroup=C.GRAPH_LAT_HDRH_DESC[lat_name],
453 showlegend=bool(idx % 2),
455 color=get_color(int(idx/2)),
457 width=1 if idx % 2 else 2
465 fig.add_traces(traces)
466 layout_hdrh = layout.get("plot-hdrh-latency", None)
468 fig.update_layout(layout_hdrh)