C-Dash: Add GSO to report
[csit.git] / csit.infra.dash / app / cdash / coverage / tables.py
1 # Copyright (c) 2023 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 """The coverage data tables.
15 """
16
17
18 import hdrh.histogram
19 import hdrh.codec
20 import pandas as pd
21 import dash_bootstrap_components as dbc
22
23 from dash import dash_table
24 from dash.dash_table.Format import Format, Scheme
25
26 from ..utils.constants import Constants as C
27
28
29 def select_coverage_data(
30         data: pd.DataFrame,
31         selected: dict,
32         csv: bool=False,
33         show_latency: bool=True
34     ) -> list:
35     """Select coverage data for the tables and generate tables as pandas data
36     frames.
37
38     :param data: Coverage data.
39     :param selected: Dictionary with user selection.
40     :param csv: If True, pandas data frame with selected coverage data is
41         returned for "Download Data" feature.
42     :param show_latency: If True, latency is displayed in the tables.
43     :type data: pandas.DataFrame
44     :type selected: dict
45     :type csv: bool
46     :type show_latency: bool
47     :returns: List of tuples with suite name (str) and data (pandas dataframe)
48         or pandas dataframe if csv is True.
49     :rtype: list[tuple[str, pandas.DataFrame], ] or pandas.DataFrame
50     """
51
52     l_data = list()
53
54     # Filter data selected by the user.
55     phy = selected["phy"].split("-")
56     if len(phy) == 4:
57         topo, arch, nic, drv = phy
58         drv_str = "" if drv == "dpdk" else drv.replace("_", "-")
59     else:
60         return l_data
61
62     df = pd.DataFrame(data.loc[(
63         (data["passed"] == True) &
64         (data["dut_type"] == selected["dut"]) &
65         (data["dut_version"] == selected["dutver"]) &
66         (data["release"] == selected["rls"])
67     )])
68     df = df[
69         (df.job.str.endswith(f"{topo}-{arch}")) &
70         (df.test_id.str.contains(
71             f"^.*\.{selected['area']}\..*{nic}.*{drv_str}.*$",
72             regex=True
73         ))
74     ]
75     if drv == "dpdk":
76         for driver in C.DRIVERS:
77             df.drop(
78                 df[df.test_id.str.contains(f"-{driver}-")].index,
79                 inplace=True
80             )
81
82     ttype = df["test_type"].to_list()[0]
83
84     # Prepare the coverage data
85     def _latency(hdrh_string: str, percentile: float) -> int:
86         """Get latency from HDRH string for given percentile.
87
88         :param hdrh_string: Encoded HDRH string.
89         :param percentile: Given percentile.
90         :type hdrh_string: str
91         :type percentile: float
92         :returns: The latency value for the given percentile from the encoded
93             HDRH string.
94         :rtype: int
95         """
96         try:
97             hdr_lat = hdrh.histogram.HdrHistogram.decode(hdrh_string)
98             return hdr_lat.get_value_at_percentile(percentile)
99         except (hdrh.codec.HdrLengthException, TypeError):
100             return None
101
102     def _get_suite(test_id: str) -> str:
103         """Get the suite name from the test ID.
104         """
105         return test_id.split(".")[-2].replace("2n1l-", "").\
106             replace("1n1l-", "").replace("2n-", "").replace("-ndrpdr", "")
107
108     def _get_test(test_id: str) -> str:
109         """Get the test name from the test ID.
110         """
111         return test_id.split(".")[-1].replace("-ndrpdr", "")
112
113     cov = pd.DataFrame()
114     cov["Suite"] = df.apply(lambda row: _get_suite(row["test_id"]), axis=1)
115     cov["Test Name"] = df.apply(lambda row: _get_test(row["test_id"]), axis=1)
116
117     if ttype == "device":
118         cov = cov.assign(Result="PASS")
119     elif ttype == "mrr":
120         cov["Throughput_Unit"] = df["result_receive_rate_rate_unit"]
121         cov["Throughput_AVG"] = df.apply(
122             lambda row: row["result_receive_rate_rate_avg"] / 1e9, axis=1
123         )
124         cov["Throughput_STDEV"] = df.apply(
125             lambda row: row["result_receive_rate_rate_stdev"] / 1e9, axis=1
126         )
127     else:  # NDRPDR
128         cov["Throughput_Unit"] = df["result_pdr_lower_rate_unit"]
129         cov["Throughput_NDR"] = df.apply(
130             lambda row: row["result_ndr_lower_rate_value"] / 1e6, axis=1
131         )
132         cov["Throughput_NDR_Gbps"] = df.apply(
133             lambda row: row["result_ndr_lower_bandwidth_value"] / 1e9, axis=1
134         )
135         cov["Throughput_PDR"] = df.apply(
136             lambda row: row["result_pdr_lower_rate_value"] / 1e6, axis=1
137         )
138         cov["Throughput_PDR_Gbps"] = df.apply(
139             lambda row: row["result_pdr_lower_bandwidth_value"] / 1e9, axis=1
140         )
141         if show_latency:
142             for way in ("Forward", "Reverse"):
143                 for pdr in (10, 50, 90):
144                     for perc in (50, 90, 99):
145                         latency = f"result_latency_{way.lower()}_pdr_{pdr}_hdrh"
146                         cov[f"Latency {way} [us]_{pdr}% PDR_P{perc}"] = \
147                             df.apply(
148                                 lambda row: _latency(row[latency], perc),
149                                 axis=1
150                             )
151
152     if csv:
153         return cov
154
155     # Split data into tables depending on the test suite.
156     for suite in cov["Suite"].unique().tolist():
157         df_suite = pd.DataFrame(cov.loc[(cov["Suite"] == suite)])
158
159         if ttype !="device":
160             unit = df_suite["Throughput_Unit"].tolist()[0]
161             df_suite.rename(
162                 columns={
163                     "Throughput_NDR": f"Throughput_NDR_M{unit}",
164                     "Throughput_PDR": f"Throughput_PDR_M{unit}",
165                     "Throughput_AVG": f"Throughput_G{unit}_AVG",
166                     "Throughput_STDEV": f"Throughput_G{unit}_STDEV"
167                 },
168                 inplace=True
169             )
170             df_suite.drop(["Suite", "Throughput_Unit"], axis=1, inplace=True)
171
172         l_data.append((suite, df_suite, ))
173
174     return l_data, ttype
175
176
177 def coverage_tables(
178         data: pd.DataFrame,
179         selected: dict,
180         show_latency: bool=True
181     ) -> list:
182     """Generate an accordion with coverage tables.
183
184     :param data: Coverage data.
185     :param selected: Dictionary with user selection.
186     :param show_latency: If True, latency is displayed in the tables.
187     :type data: pandas.DataFrame
188     :type selected: dict
189     :type show_latency: bool
190     :returns: Accordion with suite names (titles) and tables.
191     :rtype: dash_bootstrap_components.Accordion
192     """
193
194     accordion_items = list()
195     sel_data, ttype = \
196         select_coverage_data(data, selected, show_latency=show_latency)
197     for suite, cov_data in sel_data:
198         if ttype == "device":  # VPP Device
199             cols = [
200                 {
201                     "name": col,
202                     "id": col,
203                     "deletable": False,
204                     "selectable": False,
205                     "type": "text"
206                 } for col in cov_data.columns
207             ]
208             style_cell={"textAlign": "left"}
209             style_cell_conditional=[
210                 {
211                     "if": {"column_id": "Result"},
212                     "textAlign": "right"
213                 }
214             ]
215         elif ttype == "mrr":  # MRR
216             cols = list()
217             for idx, col in enumerate(cov_data.columns):
218                 if idx == 0:
219                     cols.append({
220                         "name": ["", "", col],
221                         "id": col,
222                         "deletable": False,
223                         "selectable": False,
224                         "type": "text"
225                     })
226                 else:
227                     cols.append({
228                         "name": col.split("_"),
229                         "id": col,
230                         "deletable": False,
231                         "selectable": False,
232                         "type": "numeric",
233                         "format": Format(precision=2, scheme=Scheme.fixed)
234                     })
235             style_cell={"textAlign": "right"}
236             style_cell_conditional=[
237                 {
238                     "if": {"column_id": "Test Name"},
239                     "textAlign": "left"
240                 }
241             ]
242         else:  # Performance NDRPDR
243             cols = list()
244             for idx, col in enumerate(cov_data.columns):
245                 if idx == 0:
246                     cols.append({
247                         "name": ["", "", col],
248                         "id": col,
249                         "deletable": False,
250                         "selectable": False,
251                         "type": "text"
252                     })
253                 elif idx < 5:
254                     cols.append({
255                         "name": col.split("_"),
256                         "id": col,
257                         "deletable": False,
258                         "selectable": False,
259                         "type": "numeric",
260                         "format": Format(precision=2, scheme=Scheme.fixed)
261                     })
262                 else:
263                     cols.append({
264                         "name": col.split("_"),
265                         "id": col,
266                         "deletable": False,
267                         "selectable": False,
268                         "type": "numeric",
269                         "format": Format(precision=0, scheme=Scheme.fixed)
270                     })
271             style_cell={"textAlign": "right"}
272             style_cell_conditional=[
273                 {
274                     "if": {"column_id": "Test Name"},
275                     "textAlign": "left"
276                 }
277             ]
278
279         accordion_items.append(
280             dbc.AccordionItem(
281                 title=suite,
282                 children=dash_table.DataTable(
283                     columns=cols,
284                     data=cov_data.to_dict("records"),
285                     merge_duplicate_headers=True,
286                     editable=False,
287                     filter_action="none",
288                     sort_action="native",
289                     sort_mode="multi",
290                     selected_columns=[],
291                     selected_rows=[],
292                     page_action="none",
293                     style_cell=style_cell,
294                     style_cell_conditional=style_cell_conditional
295                 )
296             )
297         )
298     return dbc.Accordion(
299         children=accordion_items,
300         class_name="gy-1 p-0",
301         start_collapsed=True,
302         always_open=True
303     )