1 # Copyright (c) 2024 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 """Implementation of graphs for trending data.
18 import plotly.graph_objects as go
23 from ..utils.constants import Constants as C
24 from ..utils.utils import get_color, get_hdrh_latencies
25 from ..utils.anomalies import classify_anomalies
28 def select_trending_data(data: pd.DataFrame, itm: dict) -> pd.DataFrame:
29 """Select the data for graphs from the provided data frame.
31 :param data: Data frame with data for graphs.
32 :param itm: Item (in this case job name) which data will be selected from
34 :type data: pandas.DataFrame
36 :returns: A data frame with selected data.
37 :rtype: pandas.DataFrame
40 phy = itm["phy"].split("-")
42 topo, arch, nic, drv = phy
47 drv = drv.replace("_", "-")
51 if itm["testtype"] in ("ndr", "pdr"):
53 elif itm["testtype"] == "mrr":
55 elif itm["testtype"] == "soak":
57 elif itm["area"] == "hoststack":
58 test_type = "hoststack"
60 (data["test_type"] == test_type) &
61 (data["passed"] == True)
63 df = df[df.job.str.endswith(f"{topo}-{arch}")]
64 core = str() if itm["dut"] == "trex" else f"{itm['core']}"
65 ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
66 df = df[df.test_id.str.contains(
67 f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$",
69 )].sort_values(by="start_time", ignore_index=True)
78 normalize: bool=False,
81 """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
82 (result_latency_forward_pdr_50_avg).
84 :param data: Data frame with test results.
85 :param sel: Selected tests.
86 :param layout: Layout of plot.ly graph.
87 :param normalize: If True, the data is normalized to CPU frquency
88 Constants.NORM_FREQUENCY.
89 :param trials: If True, MRR trials are displayed in the trending graph.
90 :type data: pandas.DataFrame
95 :returns: Trending graph(s)
96 :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
103 def _generate_trending_traces(
110 """Generate the trending traces for the trending graph.
112 :param ttype: Test type (MRR, NDR, PDR).
113 :param name: The test name to be displayed as the graph title.
114 :param df: Data frame with test data.
115 :param color: The color of the trace (samples and trend line).
116 :param nf: The factor used for normalization of the results to
117 CPU frequency set to Constants.NORM_FREQUENCY.
120 :type df: pandas.DataFrame
123 :returns: Traces (samples, trending line, anomalies)
127 df = df.dropna(subset=[C.VALUE[ttype], ])
129 return list(), list()
133 customdata_samples = list()
134 name_lst = name.split("-")
135 for _, row in df.iterrows():
136 h_tput, h_band, h_lat = str(), str(), str()
137 if ttype in ("mrr", "mrr-bandwidth"):
139 f"tput avg [{row['result_receive_rate_rate_unit']}]: "
140 f"{row['result_receive_rate_rate_avg'] * nf:,.0f}<br>"
141 f"tput stdev [{row['result_receive_rate_rate_unit']}]: "
142 f"{row['result_receive_rate_rate_stdev'] * nf:,.0f}<br>"
144 if pd.notna(row["result_receive_rate_bandwidth_avg"]):
147 f"[{row['result_receive_rate_bandwidth_unit']}]: "
148 f"{row['result_receive_rate_bandwidth_avg'] * nf:,.0f}"
151 f"[{row['result_receive_rate_bandwidth_unit']}]: "
152 f"{row['result_receive_rate_bandwidth_stdev']* nf:,.0f}"
155 elif ttype in ("ndr", "ndr-bandwidth"):
157 f"tput [{row['result_ndr_lower_rate_unit']}]: "
158 f"{row['result_ndr_lower_rate_value'] * nf:,.0f}<br>"
160 if pd.notna(row["result_ndr_lower_bandwidth_value"]):
162 f"bandwidth [{row['result_ndr_lower_bandwidth_unit']}]:"
163 f" {row['result_ndr_lower_bandwidth_value'] * nf:,.0f}"
166 elif ttype in ("pdr", "pdr-bandwidth", "latency"):
168 f"tput [{row['result_pdr_lower_rate_unit']}]: "
169 f"{row['result_pdr_lower_rate_value'] * nf:,.0f}<br>"
171 if pd.notna(row["result_pdr_lower_bandwidth_value"]):
173 f"bandwidth [{row['result_pdr_lower_bandwidth_unit']}]:"
174 f" {row['result_pdr_lower_bandwidth_value'] * nf:,.0f}"
177 if pd.notna(row["result_latency_forward_pdr_50_avg"]):
180 f"[{row['result_latency_forward_pdr_50_unit']}]: "
181 f"{row['result_latency_forward_pdr_50_avg'] / nf:,.0f}"
184 elif ttype in ("hoststack-cps", "hoststack-rps",
185 "hoststack-cps-bandwidth",
186 "hoststack-rps-bandwidth", "hoststack-latency"):
188 f"tput [{row['result_rate_unit']}]: "
189 f"{row['result_rate_value'] * nf:,.0f}<br>"
192 f"bandwidth [{row['result_bandwidth_unit']}]: "
193 f"{row['result_bandwidth_value'] * nf:,.0f}<br>"
196 f"latency [{row['result_latency_unit']}]: "
197 f"{row['result_latency_value'] / nf:,.0f}<br>"
199 elif ttype in ("hoststack-bps", ):
201 f"bandwidth [{row['result_bandwidth_unit']}]: "
202 f"{row['result_bandwidth_value'] * nf:,.0f}<br>"
204 elif ttype in ("soak", "soak-bandwidth"):
206 f"tput [{row['result_critical_rate_lower_rate_unit']}]: "
207 f"{row['result_critical_rate_lower_rate_value'] * nf:,.0f}"
210 if pd.notna(row["result_critical_rate_lower_bandwidth_value"]):
211 bv = row['result_critical_rate_lower_bandwidth_value']
214 f"[{row['result_critical_rate_lower_bandwidth_unit']}]:"
219 hosts = f"<br>hosts: {', '.join(row['hosts'])}"
220 except (KeyError, TypeError):
223 f"dut: {name_lst[0]}<br>"
224 f"infra: {'-'.join(name_lst[1:5])}<br>"
225 f"test: {'-'.join(name_lst[5:])}<br>"
226 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
227 f"{h_tput}{h_band}{h_lat}"
228 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
229 f"csit-ref: {row['job']}/{row['build']}"
232 hover.append(hover_itm)
233 if ttype == "latency":
234 customdata_samples.append(get_hdrh_latencies(row, name))
235 customdata.append({"name": name})
237 customdata_samples.append(
238 {"name": name, "show_telemetry": True}
240 customdata.append({"name": name})
242 x_axis = df["start_time"].tolist()
243 if "latency" in ttype:
244 y_data = [(v / nf) for v in df[C.VALUE[ttype]].tolist()]
246 y_data = [(v * nf) for v in df[C.VALUE[ttype]].tolist()]
247 units = df[C.UNIT[ttype]].unique().tolist()
250 anomalies, trend_avg, trend_stdev = classify_anomalies(
251 {k: v for k, v in zip(x_axis, y_data)}
253 except ValueError as err:
255 return list(), list()
258 for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
260 hosts = f"<br>hosts: {', '.join(row['hosts'])}"
261 except (KeyError, TypeError):
264 f"dut: {name_lst[0]}<br>"
265 f"infra: {'-'.join(name_lst[1:5])}<br>"
266 f"test: {'-'.join(name_lst[5:])}<br>"
267 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
268 f"trend [{row[C.UNIT[ttype]]}]: {avg:,.0f}<br>"
269 f"stdev [{row[C.UNIT[ttype]]}]: {stdev:,.0f}<br>"
270 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
271 f"csit-ref: {row['job']}/{row['build']}"
274 if ttype == "latency":
275 hover_itm = hover_itm.replace("[pps]", "[us]")
276 hover_trend.append(hover_itm)
279 go.Scatter( # Samples
293 customdata=customdata_samples
295 go.Scatter( # Trend line
309 customdata=customdata
316 anomaly_color = list()
318 for idx, anomaly in enumerate(anomalies):
319 if anomaly in ("regression", "progression"):
320 anomaly_x.append(x_axis[idx])
321 anomaly_y.append(trend_avg[idx])
322 anomaly_color.append(C.ANOMALY_COLOR[anomaly])
324 f"dut: {name_lst[0]}<br>"
325 f"infra: {'-'.join(name_lst[1:5])}<br>"
326 f"test: {'-'.join(name_lst[5:])}<br>"
327 f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}<br>"
328 f"trend [pps]: {trend_avg[idx]:,.0f}<br>"
329 f"classification: {anomaly}"
331 if ttype == "latency":
332 hover_itm = hover_itm.replace("[pps]", "[us]")
333 hover.append(hover_itm)
334 anomaly_color.extend([0.0, 0.5, 1.0])
345 customdata=customdata,
348 "symbol": "circle-open",
349 "color": anomaly_color,
350 "colorscale": C.COLORSCALE_LAT \
351 if ttype == "latency" else C.COLORSCALE_TPUT,
359 "title": "Circles Marking Data Classification",
360 "titleside": "right",
362 "tickvals": [0.167, 0.500, 0.833],
363 "ticktext": C.TICK_TEXT_LAT \
364 if ttype == "latency" else C.TICK_TEXT_TPUT,
377 def _add_mrr_trials_traces(
384 """Add the traces with mrr trials.
386 :param ttype: Test type (mrr, mrr-bandwidth).
387 :param name: The test name to be displayed in hover.
388 :param df: Data frame with test data.
389 :param color: The color of the trace.
390 :param nf: The factor used for normalization of the results to
391 CPU frequency set to Constants.NORM_FREQUENCY.
394 :type df: pandas.DataFrame
397 :returns: list of Traces
401 x_axis = df["start_time"].tolist()
402 y_data = df[C.VALUE[ttype].replace("avg", "values")].tolist()
404 for idx_trial in range(10):
406 for idx_run in range(len(x_axis)):
408 y_axis.append(y_data[idx_run][idx_trial] * nf)
411 traces.append(go.Scatter(
431 for idx, itm in enumerate(sel):
432 df = select_trending_data(data, itm)
433 if df is None or df.empty:
437 phy = itm["phy"].split("-")
438 topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
439 norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY.get(topo_arch, 1.0)) \
440 if topo_arch else 1.0
444 if itm["area"] == "hoststack":
445 ttype = f"hoststack-{itm['testtype']}"
447 ttype = itm["testtype"]
449 traces, units = _generate_trending_traces(
458 fig_tput = go.Figure()
459 if trials and "mrr" in ttype:
460 traces.extend(_add_mrr_trials_traces(
467 fig_tput.add_traces(traces)
469 if ttype in C.TESTS_WITH_BANDWIDTH:
470 traces, _ = _generate_trending_traces(
471 f"{ttype}-bandwidth",
479 fig_band = go.Figure()
480 if trials and "mrr" in ttype:
481 traces.extend(_add_mrr_trials_traces(
482 f"{ttype}-bandwidth",
488 fig_band.add_traces(traces)
490 if ttype in C.TESTS_WITH_LATENCY:
491 traces, _ = _generate_trending_traces(
492 "latency" if ttype == "pdr" else "hoststack-latency",
500 fig_lat = go.Figure()
501 fig_lat.add_traces(traces)
503 y_units.update(units)
506 fig_layout = layout.get("plot-trending-tput", dict())
507 fig_layout["yaxis"]["title"] = \
508 f"Throughput [{'|'.join(sorted(y_units))}]"
509 fig_tput.update_layout(fig_layout)
511 fig_band.update_layout(layout.get("plot-trending-bandwidth", dict()))
513 fig_lat.update_layout(layout.get("plot-trending-lat", dict()))
515 return fig_tput, fig_band, fig_lat
518 def graph_tm_trending(
521 all_in_one: bool=False
523 """Generates one trending graph per test, each graph includes all selected
526 :param data: Data frame with telemetry data.
527 :param layout: Layout of plot.ly graph.
528 :param all_in_one: If True, all telemetry traces are placed in one graph,
529 otherwise they are split to separate graphs grouped by test ID.
530 :type data: pandas.DataFrame
532 :type all_in_one: bool
533 :returns: List of generated graphs together with test names.
534 list(tuple(plotly.graph_objects.Figure(), str()), tuple(...), ...)
541 def _generate_traces(
547 """Generates a trending graph for given test with all metrics.
549 :param data: Data frame with telemetry data for the given test.
550 :param test: The name of the test.
551 :param all_in_one: If True, all telemetry traces are placed in one
552 graph, otherwise they are split to separate graphs grouped by
554 :param color_index: The index of the test used if all_in_one is True.
555 :type data: pandas.DataFrame
557 :type all_in_one: bool
558 :type color_index: int
559 :returns: List of traces.
563 metrics = data.tm_metric.unique().tolist()
564 for idx, metric in enumerate(metrics):
565 if "-pdr" in test and "='pdr'" not in metric:
567 if "-ndr" in test and "='ndr'" not in metric:
570 df = data.loc[(data["tm_metric"] == metric)]
571 x_axis = df["start_time"].tolist()
572 y_data = [float(itm) for itm in df["tm_value"].tolist()]
574 for i, (_, row) in enumerate(df.iterrows()):
575 if row["test_type"] == "mrr":
577 f"mrr avg [{row[C.UNIT['mrr']]}]: "
578 f"{row[C.VALUE['mrr']]:,.0f}<br>"
579 f"mrr stdev [{row[C.UNIT['mrr']]}]: "
580 f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
582 elif row["test_type"] == "ndrpdr":
585 f"pdr [{row[C.UNIT['pdr']]}]: "
586 f"{row[C.VALUE['pdr']]:,.0f}<br>"
590 f"ndr [{row[C.UNIT['ndr']]}]: "
591 f"{row[C.VALUE['ndr']]:,.0f}<br>"
599 f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
600 f"value: {y_data[i]:,.2f}<br>"
602 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
603 f"csit-ref: {row['job']}/{row['build']}<br>"
606 anomalies, trend_avg, trend_stdev = classify_anomalies(
607 {k: v for k, v in zip(x_axis, y_data)}
610 for avg, stdev, (_, row) in \
611 zip(trend_avg, trend_stdev, df.iterrows()):
614 f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
615 f"trend: {avg:,.2f}<br>"
616 f"stdev: {stdev:,.2f}<br>"
617 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
618 f"csit-ref: {row['job']}/{row['build']}"
623 color = get_color(color_index * len(metrics) + idx)
624 metric_name = f"{test}<br>{metric}"
626 color = get_color(idx)
630 go.Scatter( # Samples
641 hoverinfo="text+name",
643 legendgroup=metric_name
648 go.Scatter( # Trend line
659 hoverinfo="text+name",
661 legendgroup=metric_name
667 anomaly_color = list()
669 for idx, anomaly in enumerate(anomalies):
670 if anomaly in ("regression", "progression"):
671 anomaly_x.append(x_axis[idx])
672 anomaly_y.append(trend_avg[idx])
673 anomaly_color.append(C.ANOMALY_COLOR[anomaly])
675 f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}"
676 f"<br>trend: {trend_avg[idx]:,.2f}"
677 f"<br>classification: {anomaly}"
679 hover.append(hover_itm)
680 anomaly_color.extend([0.0, 0.5, 1.0])
687 hoverinfo="text+name",
689 legendgroup=metric_name,
693 "symbol": "circle-open",
694 "color": anomaly_color,
695 "colorscale": C.COLORSCALE_TPUT,
703 "title": "Circles Marking Data Classification",
704 "titleside": "right",
706 "tickvals": [0.167, 0.500, 0.833],
707 "ticktext": C.TICK_TEXT_TPUT,
717 unique_metrics = set()
719 unique_metrics.add(itm.split("{", 1)[0])
720 return traces, unique_metrics
722 tm_trending_graphs = list()
723 graph_layout = layout.get("plot-trending-telemetry", dict())
730 for idx, test in enumerate(data.test_name.unique()):
731 df = data.loc[(data["test_name"] == test)]
732 traces, metrics = _generate_traces(df, test, all_in_one, idx)
734 all_metrics.update(metrics)
736 all_traces.extend(traces)
737 all_tests.append(test)
740 graph.add_traces(traces)
741 graph.update_layout(graph_layout)
742 tm_trending_graphs.append((graph, [test, ], ))
746 graph.add_traces(all_traces)
747 graph.update_layout(graph_layout)
748 tm_trending_graphs.append((graph, all_tests, ))
750 return tm_trending_graphs, list(all_metrics)