aboutsummaryrefslogtreecommitdiffstats
path: root/csit.infra.dash/app/cdash/trending/graphs.py
blob: fcfee26e3fc5bb7c657fc1e897182bd2ff4c0231 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# 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:
#
#     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.

"""Implementation of graphs for trending data.
"""

import plotly.graph_objects as go
import pandas as pd

from ..utils.constants import Constants as C
from ..utils.utils import get_color, get_hdrh_latencies
from ..utils.anomalies import classify_anomalies


def select_trending_data(data: pd.DataFrame, itm: dict) -> pd.DataFrame:
    """Select the data for graphs from the provided data frame.

    :param data: Data frame with data for graphs.
    :param itm: Item (in this case job name) which data will be selected from
        the input data frame.
    :type data: pandas.DataFrame
    :type itm: dict
    :returns: A data frame with selected data.
    :rtype: pandas.DataFrame
    """

    phy = itm["phy"].split("-")
    if len(phy) == 4:
        topo, arch, nic, drv = phy
        if drv == "dpdk":
            drv = ""
        else:
            drv += "-"
            drv = drv.replace("_", "-")
    else:
        return None

    if itm["testtype"] in ("ndr", "pdr"):
        test_type = "ndrpdr"
    elif itm["testtype"] == "mrr":
        test_type = "mrr"
    elif itm["area"] == "hoststack":
        test_type = "hoststack"
    df = data.loc[(
        (data["test_type"] == test_type) &
        (data["passed"] == True)
    )]
    df = df[df.job.str.endswith(f"{topo}-{arch}")]
    core = str() if itm["dut"] == "trex" else f"{itm['core']}"
    ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
    df = df[df.test_id.str.contains(
        f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$",
        regex=True
    )].sort_values(by="start_time", ignore_index=True)

    return df


def graph_trending(
        data: pd.DataFrame,
        sel: dict,
        layout: dict,
        normalize: bool
    ) -> tuple:
    """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
    (result_latency_forward_pdr_50_avg).

    :param data: Data frame with test results.
    :param sel: Selected tests.
    :param layout: Layout of plot.ly graph.
    :param normalize: If True, the data is normalized to CPU frquency
        Constants.NORM_FREQUENCY.
    :type data: pandas.DataFrame
    :type sel: dict
    :type layout: dict
    :type normalize: bool
    :returns: Trending graph(s)
    :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
    """

    if not sel:
        return None, None


    def _generate_trending_traces(
            ttype: str,
            name: str,
            df: pd.DataFrame,
            color: str,
            norm_factor: float
        ) -> list:
        """Generate the trending traces for the trending graph.

        :param ttype: Test type (MRR, NDR, PDR).
        :param name: The test name to be displayed as the graph title.
        :param df: Data frame with test data.
        :param color: The color of the trace (samples and trend line).
        :param norm_factor: The factor used for normalization of the results to
            CPU frequency set to Constants.NORM_FREQUENCY.
        :type ttype: str
        :type name: str
        :type df: pandas.DataFrame
        :type color: str
        :type norm_factor: float
        :returns: Traces (samples, trending line, anomalies)
        :rtype: list
        """

        df = df.dropna(subset=[C.VALUE[ttype], ])
        if df.empty:
            return list(), list()

        x_axis = df["start_time"].tolist()
        if ttype == "latency":
            y_data = [(v / norm_factor) for v in df[C.VALUE[ttype]].tolist()]
        else:
            y_data = [(v * norm_factor) for v in df[C.VALUE[ttype]].tolist()]
        units = df[C.UNIT[ttype]].unique().tolist()

        anomalies, trend_avg, trend_stdev = classify_anomalies(
            {k: v for k, v in zip(x_axis, y_data)}
        )

        hover = list()
        customdata = list()
        customdata_samples = list()
        name_lst = name.split("-")
        for idx, (_, row) in enumerate(df.iterrows()):
            hover_itm = (
                f"dut: {name_lst[0]}<br>"
                f"infra: {'-'.join(name_lst[1:5])}<br>"
                f"test: {'-'.join(name_lst[5:])}<br>"
                f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
                f"<prop> [{row[C.UNIT[ttype]]}]: {y_data[idx]:,.0f}<br>"
                f"<stdev>"
                f"<additional-info>"
                f"{row['dut_type']}-ref: {row['dut_version']}<br>"
                f"csit-ref: {row['job']}/{row['build']}<br>"
                f"hosts: {', '.join(row['hosts'])}"
            )
            if ttype == "mrr":
                stdev = (
                    f"stdev [{row['result_receive_rate_rate_unit']}]: "
                    f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
                )
            else:
                stdev = str()
            if ttype in ("hoststack-cps", "hoststack-rps"):
                add_info = (
                    f"bandwidth [{row[C.UNIT['hoststack-bps']]}]: "
                    f"{row[C.VALUE['hoststack-bps']]:,.0f}<br>"
                    f"latency [{row[C.UNIT['hoststack-latency']]}]: "
                    f"{row[C.VALUE['hoststack-latency']]:,.0f}<br>"
                )
            elif ttype in ("ndr", "pdr"):  # Add mrr
                test_type = f"{ttype}-bandwidth"
                add_info = (
                    f"bandwidth [{row[C.UNIT[test_type]]}]: "
                    f"{row[C.VALUE[test_type]]:,.0f}<br>"
                )
            else:
                add_info = str()
            hover_itm = hover_itm.replace(
                "<prop>", "latency" if ttype == "latency" else "average"
            ).replace("<stdev>", stdev).replace("<additional-info>", add_info)
            hover.append(hover_itm)
            if ttype == "latency":
                customdata_samples.append(get_hdrh_latencies(row, name))
                customdata.append({"name": name})
            else:
                customdata_samples.append(
                    {"name": name, "show_telemetry": True}
                )
                customdata.append({"name": name})

        hover_trend = list()
        for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
            hover_itm = (
                f"dut: {name_lst[0]}<br>"
                f"infra: {'-'.join(name_lst[1:5])}<br>"
                f"test: {'-'.join(name_lst[5:])}<br>"
                f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
                f"trend [{row[C.UNIT[ttype]]}]: {avg:,.0f}<br>"
                f"stdev [{row[C.UNIT[ttype]]}]: {stdev:,.0f}<br>"
                f"{row['dut_type']}-ref: {row['dut_version']}<br>"
                f"csit-ref: {row['job']}/{row['build']}<br>"
                f"hosts: {', '.join(row['hosts'])}"
            )
            if ttype == "latency":
                hover_itm = hover_itm.replace("[pps]", "[us]")
            hover_trend.append(hover_itm)

        traces = [
            go.Scatter(  # Samples
                x=x_axis,
                y=y_data,
                name=name,
                mode="markers",
                marker={
                    "size": 5,
                    "color": color,
                    "symbol": "circle",
                },
                text=hover,
                hoverinfo="text",
                showlegend=True,
                legendgroup=name,
                customdata=customdata_samples
            ),
            go.Scatter(  # Trend line
                x=x_axis,
                y=trend_avg,
                name=name,
                mode="lines",
                line={
                    "shape": "linear",
                    "width": 1,
                    "color": color,
                },
                text=hover_trend,
                hoverinfo="text",
                showlegend=False,
                legendgroup=name,
                customdata=customdata
            )
        ]

        if anomalies:
            anomaly_x = list()
            anomaly_y = list()
            anomaly_color = list()
            hover = list()
            for idx, anomaly in enumerate(anomalies):
                if anomaly in ("regression", "progression"):
                    anomaly_x.append(x_axis[idx])
                    anomaly_y.append(trend_avg[idx])
                    anomaly_color.append(C.ANOMALY_COLOR[anomaly])
                    hover_itm = (
                        f"dut: {name_lst[0]}<br>"
                        f"infra: {'-'.join(name_lst[1:5])}<br>"
                        f"test: {'-'.join(name_lst[5:])}<br>"
                        f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}<br>"
                        f"trend [pps]: {trend_avg[idx]:,.0f}<br>"
                        f"classification: {anomaly}"
                    )
                    if ttype == "latency":
                        hover_itm = hover_itm.replace("[pps]", "[us]")
                    hover.append(hover_itm)
            anomaly_color.extend([0.0, 0.5, 1.0])
            traces.append(
                go.Scatter(
                    x=anomaly_x,
                    y=anomaly_y,
                    mode="markers",
                    text=hover,
                    hoverinfo="text",
                    showlegend=False,
                    legendgroup=name,
                    name=name,
                    customdata=customdata,
                    marker={
                        "size": 15,
                        "symbol": "circle-open",
                        "color": anomaly_color,
                        "colorscale": C.COLORSCALE_LAT \
                            if ttype == "latency" else C.COLORSCALE_TPUT,
                        "showscale": True,
                        "line": {
                            "width": 2
                        },
                        "colorbar": {
                            "y": 0.5,
                            "len": 0.8,
                            "title": "Circles Marking Data Classification",
                            "titleside": "right",
                            "tickmode": "array",
                            "tickvals": [0.167, 0.500, 0.833],
                            "ticktext": C.TICK_TEXT_LAT \
                                if ttype == "latency" else C.TICK_TEXT_TPUT,
                            "ticks": "",
                            "ticklen": 0,
                            "tickangle": -90,
                            "thickness": 10
                        }
                    }
                )
            )

        return traces, units


    fig_tput = None
    fig_lat = None
    fig_band = None
    y_units = set()
    for idx, itm in enumerate(sel):
        df = select_trending_data(data, itm)
        if df is None or df.empty:
            continue

        if normalize:
            phy = itm["phy"].split("-")
            topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
            norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
                if topo_arch else 1.0
        else:
            norm_factor = 1.0

        if itm["area"] == "hoststack":
            ttype = f"hoststack-{itm['testtype']}"
        else:
            ttype = itm["testtype"]

        traces, units = _generate_trending_traces(
            ttype,
            itm["id"],
            df,
            get_color(idx),
            norm_factor
        )
        if traces:
            if not fig_tput:
                fig_tput = go.Figure()
            fig_tput.add_traces(traces)

        if ttype in ("ndr", "pdr"):  # Add mrr
            traces, _ = _generate_trending_traces(
                f"{ttype}-bandwidth",
                itm["id"],
                df,
                get_color(idx),
                norm_factor
            )
            if traces:
                if not fig_band:
                    fig_band = go.Figure()
                fig_band.add_traces(traces)

        if itm["testtype"] == "pdr":
            traces, _ = _generate_trending_traces(
                "latency",
                itm["id"],
                df,
                get_color(idx),
                norm_factor
            )
            if traces:
                if not fig_lat:
                    fig_lat = go.Figure()
                fig_lat.add_traces(traces)

        y_units.update(units)

    if fig_tput:
        fig_layout = layout.get("plot-trending-tput", dict())
        fig_layout["yaxis"]["title"] = \
            f"Throughput [{'|'.join(sorted(y_units))}]"
        fig_tput.update_layout(fig_layout)
    if fig_band:
        fig_band.update_layout(layout.get("plot-trending-bandwidth", dict()))
    if fig_lat:
        fig_lat.update_layout(layout.get("plot-trending-lat", dict()))

    return fig_tput, fig_band, fig_lat


def graph_tm_trending(
        data: pd.DataFrame,
        layout: dict,
        all_in_one: bool=False
    ) -> list:
    """Generates one trending graph per test, each graph includes all selected
    metrics.

    :param data: Data frame with telemetry data.
    :param layout: Layout of plot.ly graph.
    :param all_in_one: If True, all telemetry traces are placed in one graph,
        otherwise they are split to separate graphs grouped by test ID.
    :type data: pandas.DataFrame
    :type layout: dict
    :type all_in_one: bool
    :returns: List of generated graphs together with test names.
        list(tuple(plotly.graph_objects.Figure(), str()), tuple(...), ...)
    :rtype: list
    """

    if data.empty:
        return list()

    def _generate_traces(
            data: pd.DataFrame,
            test: str,
            all_in_one: bool,
            color_index: int
        ) -> list:
        """Generates a trending graph for given test with all metrics.

        :param data: Data frame with telemetry data for the given test.
        :param test: The name of the test.
        :param all_in_one: If True, all telemetry traces are placed in one
            graph, otherwise they are split to separate graphs grouped by
            test ID.
        :param color_index: The index of the test used if all_in_one is True.
        :type data: pandas.DataFrame
        :type test: str
        :type all_in_one: bool
        :type color_index: int
        :returns: List of traces.
        :rtype: list
        """
        traces = list()
        metrics = data.tm_metric.unique().tolist()
        for idx, metric in enumerate(metrics):
            if "-pdr" in test and "='pdr'" not in metric:
                continue
            if "-ndr" in test and "='ndr'" not in metric:
                continue

            df = data.loc[(data["tm_metric"] == metric)]
            x_axis = df["start_time"].tolist()
            y_data = [float(itm) for itm in df["tm_value"].tolist()]
            hover = list()
            for i, (_, row) in enumerate(df.iterrows()):
                if row["test_type"] == "mrr":
                    rate = (
                        f"mrr avg [{row[C.UNIT['mrr']]}]: "
                        f"{row[C.VALUE['mrr']]:,.0f}<br>"
                        f"mrr stdev [{row[C.UNIT['mrr']]}]: "
                        f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
                    )
                elif row["test_type"] == "ndrpdr":
                    if "-pdr" in test:
                        rate = (
                            f"pdr [{row[C.UNIT['pdr']]}]: "
                            f"{row[C.VALUE['pdr']]:,.0f}<br>"
                        )
                    elif "-ndr" in test:
                        rate = (
                            f"ndr [{row[C.UNIT['ndr']]}]: "
                            f"{row[C.VALUE['ndr']]:,.0f}<br>"
                        )
                    else:
                        rate = str()
                else:
                    rate = str()
                hover.append(
                    f"date: "
                    f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
                    f"value: {y_data[i]:,.2f}<br>"
                    f"{rate}"
                    f"{row['dut_type']}-ref: {row['dut_version']}<br>"
                    f"csit-ref: {row['job']}/{row['build']}<br>"
                )
            if any(y_data):
                anomalies, trend_avg, trend_stdev = classify_anomalies(
                    {k: v for k, v in zip(x_axis, y_data)}
                )
                hover_trend = list()
                for avg, stdev, (_, row) in \
                        zip(trend_avg, trend_stdev, df.iterrows()):
                    hover_trend.append(
                        f"date: "
                        f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
                        f"trend: {avg:,.2f}<br>"
                        f"stdev: {stdev:,.2f}<br>"
                        f"{row['dut_type']}-ref: {row['dut_version']}<br>"
                        f"csit-ref: {row['job']}/{row['build']}"
                    )
            else:
                anomalies = None
            if all_in_one:
                color = get_color(color_index * len(metrics) + idx)
                metric_name = f"{test}<br>{metric}"
            else:
                color = get_color(idx)
                metric_name = metric

            traces.append(
                go.Scatter(  # Samples
                    x=x_axis,
                    y=y_data,
                    name=metric_name,
                    mode="markers",
                    marker={
                        "size": 5,
                        "color": color,
                        "symbol": "circle",
                    },
                    text=hover,
                    hoverinfo="text+name",
                    showlegend=True,
                    legendgroup=metric_name
                )
            )
            if anomalies:
                traces.append(
                    go.Scatter(  # Trend line
                        x=x_axis,
                        y=trend_avg,
                        name=metric_name,
                        mode="lines",
                        line={
                            "shape": "linear",
                            "width": 1,
                            "color": color,
                        },
                        text=hover_trend,
                        hoverinfo="text+name",
                        showlegend=False,
                        legendgroup=metric_name
                    )
                )

                anomaly_x = list()
                anomaly_y = list()
                anomaly_color = list()
                hover = list()
                for idx, anomaly in enumerate(anomalies):
                    if anomaly in ("regression", "progression"):
                        anomaly_x.append(x_axis[idx])
                        anomaly_y.append(trend_avg[idx])
                        anomaly_color.append(C.ANOMALY_COLOR[anomaly])
                        hover_itm = (
                            f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}"
                            f"<br>trend: {trend_avg[idx]:,.2f}"
                            f"<br>classification: {anomaly}"
                        )
                        hover.append(hover_itm)
                anomaly_color.extend([0.0, 0.5, 1.0])
                traces.append(
                    go.Scatter(
                        x=anomaly_x,
                        y=anomaly_y,
                        mode="markers",
                        text=hover,
                        hoverinfo="text+name",
                        showlegend=False,
                        legendgroup=metric_name,
                        name=metric_name,
                        marker={
                            "size": 15,
                            "symbol": "circle-open",
                            "color": anomaly_color,
                            "colorscale": C.COLORSCALE_TPUT,
                            "showscale": True,
                            "line": {
                                "width": 2
                            },
                            "colorbar": {
                                "y": 0.5,
                                "len": 0.8,
                                "title": "Circles Marking Data Classification",
                                "titleside": "right",
                                "tickmode": "array",
                                "tickvals": [0.167, 0.500, 0.833],
                                "ticktext": C.TICK_TEXT_TPUT,
                                "ticks": "",
                                "ticklen": 0,
                                "tickangle": -90,
                                "thickness": 10
                            }
                        }
                    )
                )

        unique_metrics = set()
        for itm in metrics:
            unique_metrics.add(itm.split("{", 1)[0])
        return traces, unique_metrics

    tm_trending_graphs = list()
    graph_layout = layout.get("plot-trending-telemetry", dict())

    if all_in_one:
        all_traces = list()

    all_metrics = set()
    all_tests = list()
    for idx, test in enumerate(data.test_name.unique()):
        df = data.loc[(data["test_name"] == test)]
        traces, metrics = _generate_traces(df, test, all_in_one, idx)
        if traces:
            all_metrics.update(metrics)
            if all_in_one:
                all_traces.extend(traces)
                all_tests.append(test)
            else:
                graph = go.Figure()
                graph.add_traces(traces)
                graph.update_layout(graph_layout)
                tm_trending_graphs.append((graph, [test, ], ))

    if all_in_one:
        graph = go.Figure()
        graph.add_traces(all_traces)
        graph.update_layout(graph_layout)
        tm_trending_graphs.append((graph, all_tests, ))

    return tm_trending_graphs, list(all_metrics)