fix(cdash): Revert to data driven app
[csit.git] / csit.infra.dash / app / cdash / report / 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 re
18 import plotly.graph_objects as go
19 import pandas as pd
20
21 from copy import deepcopy
22
23 from ..utils.constants import Constants as C
24 from ..utils.utils import get_color
25
26
27 def select_iterative_data(data: pd.DataFrame, itm:dict) -> pd.DataFrame:
28     """Select the data for graphs and tables from the provided data frame.
29
30     :param data: Data frame with data for graphs and tables.
31     :param itm: Item (in this case job name) which data will be selected from
32         the input data frame.
33     :type data: pandas.DataFrame
34     :type itm: str
35     :returns: A data frame with selected data.
36     :rtype: pandas.DataFrame
37     """
38
39     phy = itm["phy"].split("-")
40     if len(phy) == 4:
41         topo, arch, nic, drv = phy
42         if drv == "dpdk":
43             drv = ""
44         else:
45             drv += "-"
46             drv = drv.replace("_", "-")
47     else:
48         return None
49
50     core = str() if itm["dut"] == "trex" else f"{itm['core']}"
51     ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
52     dut_v100 = "none" if itm["dut"] == "trex" else itm["dut"]
53     dut_v101 = itm["dut"]
54
55     df = data.loc[(
56         (data["release"] == itm["rls"]) &
57         (
58             (
59                 (data["version"] == "1.0.0") &
60                 (data["dut_type"].str.lower() == dut_v100)
61             ) |
62             (
63                 (data["version"] == "1.0.1") &
64                 (data["dut_type"].str.lower() == dut_v101)
65             )
66         ) &
67         (data["test_type"] == ttype) &
68         (data["passed"] == True)
69     )]
70     regex_test = \
71         f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$"
72     df = df[
73         (df.job.str.endswith(f"{topo}-{arch}")) &
74         (df.dut_version.str.contains(itm["dutver"].replace(".r", "-r").\
75             replace("rls", "release"))) &
76         (df.test_id.str.contains(regex_test, regex=True))
77     ]
78
79     return df
80
81
82 def graph_iterative(data: pd.DataFrame, sel:dict, layout: dict,
83         normalize: bool) -> tuple:
84     """Generate the statistical box graph with iterative data (MRR, NDR and PDR,
85     for PDR also Latencies).
86
87     :param data: Data frame with iterative data.
88     :param sel: Selected tests.
89     :param layout: Layout of plot.ly graph.
90     :param normalize: If True, the data is normalized to CPU frquency
91         Constants.NORM_FREQUENCY.
92     :param data: pandas.DataFrame
93     :param sel: dict
94     :param layout: dict
95     :param normalize: bool
96     :returns: Tuple of graphs - throughput and latency.
97     :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
98     """
99
100     fig_tput = None
101     fig_lat = None
102
103     tput_traces = list()
104     y_tput_max = 0
105     lat_traces = list()
106     y_lat_max = 0
107     x_lat = list()
108     show_latency = False
109     show_tput = False
110     for idx, itm in enumerate(sel):
111         itm_data = select_iterative_data(data, itm)
112         if itm_data.empty:
113             continue
114         phy = itm["phy"].split("-")
115         topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
116         norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
117             if normalize else 1.0
118         if itm["testtype"] == "mrr":
119             y_data_raw = itm_data[C.VALUE_ITER[itm["testtype"]]].to_list()[0]
120             y_data = [(y * norm_factor) for y in y_data_raw]
121             if len(y_data) > 0:
122                 y_tput_max = \
123                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
124         else:
125             y_data_raw = itm_data[C.VALUE_ITER[itm["testtype"]]].to_list()
126             y_data = [(y * norm_factor) for y in y_data_raw]
127             if y_data:
128                 y_tput_max = \
129                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
130         nr_of_samples = len(y_data)
131         tput_kwargs = dict(
132             y=y_data,
133             name=(
134                 f"{idx + 1}. "
135                 f"({nr_of_samples:02d} "
136                 f"run{'s' if nr_of_samples > 1 else ''}) "
137                 f"{itm['id']}"
138             ),
139             hoverinfo=u"y+name",
140             boxpoints="all",
141             jitter=0.3,
142             marker=dict(color=get_color(idx))
143         )
144         tput_traces.append(go.Box(**tput_kwargs))
145         show_tput = True
146
147         if itm["testtype"] == "pdr":
148             y_lat_row = itm_data[C.VALUE_ITER["pdr-lat"]].to_list()
149             y_lat = [(y / norm_factor) for y in y_lat_row]
150             if y_lat:
151                 y_lat_max = max(y_lat) if max(y_lat) > y_lat_max else y_lat_max
152             nr_of_samples = len(y_lat)
153             lat_kwargs = dict(
154                 y=y_lat,
155                 name=(
156                     f"{idx + 1}. "
157                     f"({nr_of_samples:02d} "
158                     f"run{u's' if nr_of_samples > 1 else u''}) "
159                     f"{itm['id']}"
160                 ),
161                 hoverinfo="all",
162                 boxpoints="all",
163                 jitter=0.3,
164                 marker=dict(color=get_color(idx))
165             )
166             x_lat.append(idx + 1)
167             lat_traces.append(go.Box(**lat_kwargs))
168             show_latency = True
169         else:
170             lat_traces.append(go.Box())
171
172     if show_tput:
173         pl_tput = deepcopy(layout["plot-throughput"])
174         pl_tput["xaxis"]["tickvals"] = [i for i in range(len(sel))]
175         pl_tput["xaxis"]["ticktext"] = [str(i + 1) for i in range(len(sel))]
176         if y_tput_max:
177             pl_tput["yaxis"]["range"] = [0, (int(y_tput_max / 1e6) + 1) * 1e6]
178         fig_tput = go.Figure(data=tput_traces, layout=pl_tput)
179
180     if show_latency:
181         pl_lat = deepcopy(layout["plot-latency"])
182         pl_lat["xaxis"]["tickvals"] = [i for i in range(len(x_lat))]
183         pl_lat["xaxis"]["ticktext"] = x_lat
184         if y_lat_max:
185             pl_lat["yaxis"]["range"] = [0, (int(y_lat_max / 10) + 1) * 10]
186         fig_lat = go.Figure(data=lat_traces, layout=pl_lat)
187
188     return fig_tput, fig_lat