CDash: Improvements in layout
[csit.git] / csit.infra.dash / app / pal / trending / graphs.py
1 # Copyright (c) 2022 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """
15 """
16
17 import plotly.graph_objects as go
18 import pandas as pd
19
20 import hdrh.histogram
21 import hdrh.codec
22
23 from ..utils.constants import Constants as C
24 from ..utils.utils import classify_anomalies, get_color
25
26
27 def _get_hdrh_latencies(row: pd.Series, name: str) -> dict:
28     """Get the HDRH latencies from the test data.
29
30     :param row: A row fron the data frame with test data.
31     :param name: The test name to be displayed as the graph title.
32     :type row: pandas.Series
33     :type name: str
34     :returns: Dictionary with HDRH latencies.
35     :rtype: dict
36     """
37
38     latencies = {"name": name}
39     for key in C.LAT_HDRH:
40         try:
41             latencies[key] = row[key]
42         except KeyError:
43             return None
44
45     return latencies
46
47
48 def select_trending_data(data: pd.DataFrame, itm:dict) -> pd.DataFrame:
49     """Select the data for graphs from the provided data frame.
50
51     :param data: Data frame with data for graphs.
52     :param itm: Item (in this case job name) which data will be selected from
53         the input data frame.
54     :type data: pandas.DataFrame
55     :type itm: str
56     :returns: A data frame with selected data.
57     :rtype: pandas.DataFrame
58     """
59
60     phy = itm["phy"].split("-")
61     if len(phy) == 4:
62         topo, arch, nic, drv = phy
63         if drv == "dpdk":
64             drv = ""
65         else:
66             drv += "-"
67             drv = drv.replace("_", "-")
68     else:
69         return None
70
71     core = str() if itm["dut"] == "trex" else f"{itm['core']}"
72     ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
73     dut_v100 = "none" if itm["dut"] == "trex" else itm["dut"]
74     dut_v101 = itm["dut"]
75
76     df = data.loc[(
77         (
78             (
79                 (data["version"] == "1.0.0") &
80                 (data["dut_type"].str.lower() == dut_v100)
81             ) |
82             (
83                 (data["version"] == "1.0.1") &
84                 (data["dut_type"].str.lower() == dut_v101)
85             )
86         ) &
87         (data["test_type"] == ttype) &
88         (data["passed"] == True)
89     )]
90     df = df[df.job.str.endswith(f"{topo}-{arch}")]
91     df = df[df.test_id.str.contains(
92         f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$",
93         regex=True
94     )].sort_values(by="start_time", ignore_index=True)
95
96     return df
97
98
99 def _generate_trending_traces(ttype: str, name: str, df: pd.DataFrame,
100     color: str, norm_factor: float) -> list:
101     """Generate the trending traces for the trending graph.
102
103     :param ttype: Test type (MRR, NDR, PDR).
104     :param name: The test name to be displayed as the graph title.
105     :param df: Data frame with test data.
106     :param color: The color of the trace (samples and trend line).
107     :param norm_factor: The factor used for normalization of the results to CPU
108         frequency set to Constants.NORM_FREQUENCY.
109     :type ttype: str
110     :type name: str
111     :type df: pandas.DataFrame
112     :type color: str
113     :type norm_factor: float
114     :returns: Traces (samples, trending line, anomalies)
115     :rtype: list
116     """
117
118     df = df.dropna(subset=[C.VALUE[ttype], ])
119     if df.empty:
120         return list()
121
122     x_axis = df["start_time"].tolist()
123     if ttype == "pdr-lat":
124         y_data = [(itm / norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
125     else:
126         y_data = [(itm * norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
127
128     anomalies, trend_avg, trend_stdev = classify_anomalies(
129         {k: v for k, v in zip(x_axis, y_data)}
130     )
131
132     hover = list()
133     customdata = list()
134     customdata_samples = list()
135     for idx, (_, row) in enumerate(df.iterrows()):
136         d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
137         hover_itm = (
138             f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
139             f"<prop> [{row[C.UNIT[ttype]]}]: {y_data[idx]:,.0f}<br>"
140             f"<stdev>"
141             f"{d_type}-ref: {row['dut_version']}<br>"
142             f"csit-ref: {row['job']}/{row['build']}<br>"
143             f"hosts: {', '.join(row['hosts'])}"
144         )
145         if ttype == "mrr":
146             stdev = (
147                 f"stdev [{row['result_receive_rate_rate_unit']}]: "
148                 f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
149             )
150         else:
151             stdev = ""
152         hover_itm = hover_itm.replace(
153             "<prop>", "latency" if ttype == "pdr-lat" else "average"
154         ).replace("<stdev>", stdev)
155         hover.append(hover_itm)
156         if ttype == "pdr-lat":
157             customdata_samples.append(_get_hdrh_latencies(row, name))
158             customdata.append({"name": name})
159         else:
160             customdata_samples.append({"name": name, "show_telemetry": True})
161             customdata.append({"name": name})
162
163     hover_trend = list()
164     for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
165         d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
166         hover_itm = (
167             f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
168             f"trend [pps]: {avg:,.0f}<br>"
169             f"stdev [pps]: {stdev:,.0f}<br>"
170             f"{d_type}-ref: {row['dut_version']}<br>"
171             f"csit-ref: {row['job']}/{row['build']}<br>"
172             f"hosts: {', '.join(row['hosts'])}"
173         )
174         if ttype == "pdr-lat":
175             hover_itm = hover_itm.replace("[pps]", "[us]")
176         hover_trend.append(hover_itm)
177
178     traces = [
179         go.Scatter(  # Samples
180             x=x_axis,
181             y=y_data,
182             name=name,
183             mode="markers",
184             marker={
185                 "size": 5,
186                 "color": color,
187                 "symbol": "circle",
188             },
189             text=hover,
190             hoverinfo="text+name",
191             showlegend=True,
192             legendgroup=name,
193             customdata=customdata_samples
194         ),
195         go.Scatter(  # Trend line
196             x=x_axis,
197             y=trend_avg,
198             name=name,
199             mode="lines",
200             line={
201                 "shape": "linear",
202                 "width": 1,
203                 "color": color,
204             },
205             text=hover_trend,
206             hoverinfo="text+name",
207             showlegend=False,
208             legendgroup=name,
209             customdata=customdata
210         )
211     ]
212
213     if anomalies:
214         anomaly_x = list()
215         anomaly_y = list()
216         anomaly_color = list()
217         hover = list()
218         for idx, anomaly in enumerate(anomalies):
219             if anomaly in ("regression", "progression"):
220                 anomaly_x.append(x_axis[idx])
221                 anomaly_y.append(trend_avg[idx])
222                 anomaly_color.append(C.ANOMALY_COLOR[anomaly])
223                 hover_itm = (
224                     f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}<br>"
225                     f"trend [pps]: {trend_avg[idx]:,.0f}<br>"
226                     f"classification: {anomaly}"
227                 )
228                 if ttype == "pdr-lat":
229                     hover_itm = hover_itm.replace("[pps]", "[us]")
230                 hover.append(hover_itm)
231         anomaly_color.extend([0.0, 0.5, 1.0])
232         traces.append(
233             go.Scatter(
234                 x=anomaly_x,
235                 y=anomaly_y,
236                 mode="markers",
237                 text=hover,
238                 hoverinfo="text+name",
239                 showlegend=False,
240                 legendgroup=name,
241                 name=name,
242                 customdata=customdata,
243                 marker={
244                     "size": 15,
245                     "symbol": "circle-open",
246                     "color": anomaly_color,
247                     "colorscale": C.COLORSCALE_LAT \
248                         if ttype == "pdr-lat" else C.COLORSCALE_TPUT,
249                     "showscale": True,
250                     "line": {
251                         "width": 2
252                     },
253                     "colorbar": {
254                         "y": 0.5,
255                         "len": 0.8,
256                         "title": "Circles Marking Data Classification",
257                         "titleside": "right",
258                         "tickmode": "array",
259                         "tickvals": [0.167, 0.500, 0.833],
260                         "ticktext": C.TICK_TEXT_LAT \
261                             if ttype == "pdr-lat" else C.TICK_TEXT_TPUT,
262                         "ticks": "",
263                         "ticklen": 0,
264                         "tickangle": -90,
265                         "thickness": 10
266                     }
267                 }
268             )
269         )
270
271     return traces
272
273
274 def graph_trending(data: pd.DataFrame, sel:dict, layout: dict,
275     normalize: bool) -> tuple:
276     """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
277     (result_latency_forward_pdr_50_avg).
278
279     :param data: Data frame with test results.
280     :param sel: Selected tests.
281     :param layout: Layout of plot.ly graph.
282     :param normalize: If True, the data is normalized to CPU frquency
283         Constants.NORM_FREQUENCY.
284     :type data: pandas.DataFrame
285     :type sel: dict
286     :type layout: dict
287     :type normalize: bool
288     :returns: Trending graph(s)
289     :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
290     """
291
292     if not sel:
293         return None, None
294
295     fig_tput = None
296     fig_lat = None
297     for idx, itm in enumerate(sel):
298
299         df = select_trending_data(data, itm)
300         if df is None or df.empty:
301             continue
302
303         if normalize:
304             phy = itm["phy"].split("-")
305             topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
306             norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
307                 if topo_arch else 1.0
308         else:
309             norm_factor = 1.0
310         traces = _generate_trending_traces(itm["testtype"], itm["id"], df,
311             get_color(idx), norm_factor)
312         if traces:
313             if not fig_tput:
314                 fig_tput = go.Figure()
315             fig_tput.add_traces(traces)
316
317         if itm["testtype"] == "pdr":
318             traces = _generate_trending_traces("pdr-lat", itm["id"], df,
319                 get_color(idx), norm_factor)
320             if traces:
321                 if not fig_lat:
322                     fig_lat = go.Figure()
323                 fig_lat.add_traces(traces)
324
325     if fig_tput:
326         fig_tput.update_layout(layout.get("plot-trending-tput", dict()))
327     if fig_lat:
328         fig_lat.update_layout(layout.get("plot-trending-lat", dict()))
329
330     return fig_tput, fig_lat
331
332
333 def graph_hdrh_latency(data: dict, layout: dict) -> go.Figure:
334     """Generate HDR Latency histogram graphs.
335
336     :param data: HDRH data.
337     :param layout: Layout of plot.ly graph.
338     :type data: dict
339     :type layout: dict
340     :returns: HDR latency Histogram.
341     :rtype: plotly.graph_objects.Figure
342     """
343
344     fig = None
345
346     traces = list()
347     for idx, (lat_name, lat_hdrh) in enumerate(data.items()):
348         try:
349             decoded = hdrh.histogram.HdrHistogram.decode(lat_hdrh)
350         except (hdrh.codec.HdrLengthException, TypeError):
351             continue
352         previous_x = 0.0
353         prev_perc = 0.0
354         xaxis = list()
355         yaxis = list()
356         hovertext = list()
357         for item in decoded.get_recorded_iterator():
358             # The real value is "percentile".
359             # For 100%, we cut that down to "x_perc" to avoid
360             # infinity.
361             percentile = item.percentile_level_iterated_to
362             x_perc = min(percentile, C.PERCENTILE_MAX)
363             xaxis.append(previous_x)
364             yaxis.append(item.value_iterated_to)
365             hovertext.append(
366                 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
367                 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
368                 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
369                 f"Latency: {item.value_iterated_to}uSec"
370             )
371             next_x = 100.0 / (100.0 - x_perc)
372             xaxis.append(next_x)
373             yaxis.append(item.value_iterated_to)
374             hovertext.append(
375                 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
376                 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
377                 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
378                 f"Latency: {item.value_iterated_to}uSec"
379             )
380             previous_x = next_x
381             prev_perc = percentile
382
383         traces.append(
384             go.Scatter(
385                 x=xaxis,
386                 y=yaxis,
387                 name=C.GRAPH_LAT_HDRH_DESC[lat_name],
388                 mode="lines",
389                 legendgroup=C.GRAPH_LAT_HDRH_DESC[lat_name],
390                 showlegend=bool(idx % 2),
391                 line=dict(
392                     color=get_color(int(idx/2)),
393                     dash="solid",
394                     width=1 if idx % 2 else 2
395                 ),
396                 hovertext=hovertext,
397                 hoverinfo="text"
398             )
399         )
400     if traces:
401         fig = go.Figure()
402         fig.add_traces(traces)
403         layout_hdrh = layout.get("plot-hdrh-latency", None)
404         if lat_hdrh:
405             fig.update_layout(layout_hdrh)
406
407     return fig