feat(core): Adjust T-Rex for external topologies
[csit.git] / csit.infra.dash / app / cdash / trending / graphs.py
index 57fc165..10eb392 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2023 Cisco and/or its affiliates.
+# Copyright (c) 2024 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:
@@ -18,6 +18,8 @@ import logging
 import plotly.graph_objects as go
 import pandas as pd
 
+from numpy import nan
+
 from ..utils.constants import Constants as C
 from ..utils.utils import get_color, get_hdrh_latencies
 from ..utils.anomalies import classify_anomalies
@@ -50,6 +52,8 @@ def select_trending_data(data: pd.DataFrame, itm: dict) -> pd.DataFrame:
         test_type = "ndrpdr"
     elif itm["testtype"] == "mrr":
         test_type = "mrr"
+    elif itm["testtype"] == "soak":
+        test_type = "soak"
     elif itm["area"] == "hoststack":
         test_type = "hoststack"
     df = data.loc[(
@@ -71,7 +75,8 @@ def graph_trending(
         data: pd.DataFrame,
         sel: dict,
         layout: dict,
-        normalize: bool
+        normalize: bool=False,
+        trials: bool=False
     ) -> tuple:
     """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
     (result_latency_forward_pdr_50_avg).
@@ -81,10 +86,12 @@ def graph_trending(
     :param layout: Layout of plot.ly graph.
     :param normalize: If True, the data is normalized to CPU frquency
         Constants.NORM_FREQUENCY.
+    :param trials: If True, MRR trials are displayed in the trending graph.
     :type data: pandas.DataFrame
     :type sel: dict
     :type layout: dict
     :type normalize: bool
+    :type: trials: bool
     :returns: Trending graph(s)
     :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
     """
@@ -126,7 +133,8 @@ def graph_trending(
         customdata_samples = list()
         name_lst = name.split("-")
         for _, row in df.iterrows():
-            h_tput, h_band, h_lat = str(), str(), str()
+            h_tput, h_band, h_lat, h_tput_trials, h_band_trials = \
+                str(), str(), str(), str(), str()
             if ttype in ("mrr", "mrr-bandwidth"):
                 h_tput = (
                     f"tput avg [{row['result_receive_rate_rate_unit']}]: "
@@ -145,6 +153,23 @@ def graph_trending(
                         f"{row['result_receive_rate_bandwidth_stdev']* nf:,.0f}"
                         "<br>"
                     )
+                if trials:
+                    h_tput_trials = (
+                        f"tput trials "
+                        f"[{row['result_receive_rate_rate_unit']}]: "
+                    )
+                    for itm in row["result_receive_rate_rate_values"]:
+                        h_tput_trials += f"{itm * nf:,.0f}; "
+                    h_tput_trials = h_tput_trials[:-2] + "<br>"
+                    if pd.notna(row["result_receive_rate_bandwidth_avg"]):
+                        h_band_trials = (
+                            f"bandwidth trials "
+                            f"[{row['result_receive_rate_bandwidth_unit']}]: "
+                        )
+                        for itm in row["result_receive_rate_bandwidth_values"]:
+                            h_band_trials += f"{itm * nf:,.0f}; "
+                        h_band_trials = h_band_trials[:-2] + "<br>"
+
             elif ttype in ("ndr", "ndr-bandwidth"):
                 h_tput = (
                     f"tput [{row['result_ndr_lower_rate_unit']}]: "
@@ -194,15 +219,33 @@ def graph_trending(
                     f"bandwidth [{row['result_bandwidth_unit']}]: "
                     f"{row['result_bandwidth_value'] * nf:,.0f}<br>"
                 )
+            elif ttype in ("soak", "soak-bandwidth"):
+                h_tput = (
+                    f"tput [{row['result_critical_rate_lower_rate_unit']}]: "
+                    f"{row['result_critical_rate_lower_rate_value'] * nf:,.0f}"
+                    "<br>"
+                )
+                if pd.notna(row["result_critical_rate_lower_bandwidth_value"]):
+                    bv = row['result_critical_rate_lower_bandwidth_value']
+                    h_band = (
+                        "bandwidth "
+                        f"[{row['result_critical_rate_lower_bandwidth_unit']}]:"
+                        f" {bv * nf:,.0f}"
+                        "<br>"
+                    )
+            try:
+                hosts = f"<br>hosts: {', '.join(row['hosts'])}"
+            except (KeyError, TypeError):
+                hosts = str()
             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"{h_tput}{h_band}{h_lat}"
+                f"{h_tput}{h_tput_trials}{h_band}{h_band_trials}{h_lat}"
                 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
-                f"csit-ref: {row['job']}/{row['build']}<br>"
-                f"hosts: {', '.join(row['hosts'])}"
+                f"csit-ref: {row['job']}/{row['build']}"
+                f"{hosts}"
             )
             hover.append(hover_itm)
             if ttype == "latency":
@@ -231,6 +274,10 @@ def graph_trending(
 
         hover_trend = list()
         for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
+            try:
+                hosts = f"<br>hosts: {', '.join(row['hosts'])}"
+            except (KeyError, TypeError):
+                hosts = str()
             hover_itm = (
                 f"dut: {name_lst[0]}<br>"
                 f"infra: {'-'.join(name_lst[1:5])}<br>"
@@ -239,8 +286,8 @@ def graph_trending(
                 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'])}"
+                f"csit-ref: {row['job']}/{row['build']}"
+                f"{hosts}"
             )
             if ttype == "latency":
                 hover_itm = hover_itm.replace("[pps]", "[us]")
@@ -255,7 +302,7 @@ def graph_trending(
                 marker={
                     "size": 5,
                     "color": color,
-                    "symbol": "circle",
+                    "symbol": "circle"
                 },
                 text=hover,
                 hoverinfo="text",
@@ -345,6 +392,57 @@ def graph_trending(
         return traces, units
 
 
+    def _add_mrr_trials_traces(
+            ttype: str,
+            name: str,
+            df: pd.DataFrame,
+            color: str,
+            nf: float
+        ) -> list:
+        """Add the traces with mrr trials.
+
+        :param ttype: Test type (mrr, mrr-bandwidth).
+        :param name: The test name to be displayed in hover.
+        :param df: Data frame with test data.
+        :param color: The color of the trace.
+        :param nf: 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 nf: float
+        :returns: list of Traces
+        :rtype: list
+        """
+        traces = list()
+        x_axis = df["start_time"].tolist()
+        y_data = df[C.VALUE[ttype].replace("avg", "values")].tolist()
+
+        for idx_trial in range(10):
+            y_axis = list()
+            for idx_run in range(len(x_axis)):
+                try:
+                    y_axis.append(y_data[idx_run][idx_trial] * nf)
+                except (IndexError, TypeError, ValueError):
+                    y_axis.append(nan)
+            traces.append(go.Scatter(
+                x=x_axis,
+                y=y_axis,
+                name=name,
+                mode="markers",
+                marker={
+                    "size": 2,
+                    "color": color,
+                    "symbol": "circle"
+                },
+                showlegend=True,
+                legendgroup=name,
+                hoverinfo="skip"
+            ))
+        return traces
+
+
     fig_tput = None
     fig_lat = None
     fig_band = None
@@ -377,9 +475,17 @@ def graph_trending(
         if traces:
             if not fig_tput:
                 fig_tput = go.Figure()
+            if trials and "mrr" in ttype:
+                traces.extend(_add_mrr_trials_traces(
+                    ttype,
+                    itm["id"],
+                    df,
+                    get_color(idx),
+                    norm_factor
+                ))
             fig_tput.add_traces(traces)
 
-        if ttype in ("ndr", "pdr", "mrr", "hoststack-cps", "hoststack-rps"):
+        if ttype in C.TESTS_WITH_BANDWIDTH:
             traces, _ = _generate_trending_traces(
                 f"{ttype}-bandwidth",
                 itm["id"],
@@ -390,9 +496,17 @@ def graph_trending(
             if traces:
                 if not fig_band:
                     fig_band = go.Figure()
+                if trials and "mrr" in ttype:
+                    traces.extend(_add_mrr_trials_traces(
+                        f"{ttype}-bandwidth",
+                        itm["id"],
+                        df,
+                        get_color(idx),
+                        norm_factor
+                    ))
                 fig_band.add_traces(traces)
 
-        if ttype in ("pdr", "hoststack-cps", "hoststack-rps"):
+        if ttype in C.TESTS_WITH_LATENCY:
             traces, _ = _generate_trending_traces(
                 "latency" if ttype == "pdr" else "hoststack-latency",
                 itm["id"],