aboutsummaryrefslogtreecommitdiffstats
path: root/csit.infra.dash/app/cdash/coverage/tables.py
blob: d38e51e243d9f2e6410e2c75b0400e89526da7b0 (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
# 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:
#
#     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.

"""The coverage data tables.
"""


import hdrh.histogram
import hdrh.codec
import pandas as pd
import dash_bootstrap_components as dbc

from dash import dash_table
from dash.dash_table.Format import Format, Scheme

from ..utils.constants import Constants as C


def select_coverage_data(
        data: pd.DataFrame,
        selected: dict,
        csv: bool=False,
        show_latency: bool=True
    ) -> list:
    """Select coverage data for the tables and generate tables as pandas data
    frames.

    :param data: Coverage data.
    :param selected: Dictionary with user selection.
    :param csv: If True, pandas data frame with selected coverage data is
        returned for "Download Data" feature.
    :param show_latency: If True, latency is displayed in the tables.
    :type data: pandas.DataFrame
    :type selected: dict
    :type csv: bool
    :type show_latency: bool
    :returns: List of tuples with suite name (str) and data (pandas dataframe)
        or pandas dataframe if csv is True.
    :rtype: list[tuple[str, pandas.DataFrame], ] or pandas.DataFrame
    """

    l_data = list()

    # Filter data selected by the user.
    phy = selected["phy"].rsplit("-", maxsplit=2)
    if len(phy) == 3:
        topo_arch, nic, drv = phy
        drv_str = "" if drv == "dpdk" else drv.replace("_", "-")
    else:
        return l_data, None

    df = pd.DataFrame(data.loc[(
        (data["passed"] == True) &
        (data["dut_type"] == selected["dut"]) &
        (data["dut_version"] == selected["dutver"]) &
        (data["release"] == selected["rls"])
    )])
    df = df[
        (df.job.str.endswith(topo_arch)) &
        (df.test_id.str.contains(
            f"^.*\.{selected['area']}\..*{nic}.*{drv_str}.*$",
            regex=True
        ))
    ]
    if drv == "dpdk":
        for driver in C.DRIVERS:
            df.drop(
                df[df.test_id.str.contains(f"-{driver}-")].index,
                inplace=True
            )
    try:
        ttype = df["test_type"].to_list()[0]
    except IndexError:
        return l_data, None

    # Prepare the coverage data
    def _latency(hdrh_string: str, percentile: float) -> int:
        """Get latency from HDRH string for given percentile.

        :param hdrh_string: Encoded HDRH string.
        :param percentile: Given percentile.
        :type hdrh_string: str
        :type percentile: float
        :returns: The latency value for the given percentile from the encoded
            HDRH string.
        :rtype: int
        """
        try:
            hdr_lat = hdrh.histogram.HdrHistogram.decode(hdrh_string)
            return hdr_lat.get_value_at_percentile(percentile)
        except (hdrh.codec.HdrLengthException, TypeError):
            return None

    def _get_suite(test_id: str) -> str:
        """Get the suite name from the test ID.
        """
        return test_id.split(".")[-2].replace("2n1l-", "").\
            replace("1n1l-", "").replace("2n-", "").replace("-ndrpdr", "")

    def _get_test(test_id: str) -> str:
        """Get the test name from the test ID.
        """
        return test_id.split(".")[-1].replace("-ndrpdr", "")

    cov = pd.DataFrame()
    cov["Suite"] = df.apply(lambda row: _get_suite(row["test_id"]), axis=1)
    cov["Test Name"] = df.apply(lambda row: _get_test(row["test_id"]), axis=1)

    if ttype == "device":
        cov = cov.assign(Result="PASS")
    elif ttype == "mrr":
        cov["Throughput_Unit"] = df["result_receive_rate_rate_unit"]
        cov["Throughput_AVG"] = df.apply(
            lambda row: row["result_receive_rate_rate_avg"] / 1e9, axis=1
        )
        cov["Throughput_STDEV"] = df.apply(
            lambda row: row["result_receive_rate_rate_stdev"] / 1e9, axis=1
        )
    else:  # NDRPDR
        cov["Throughput_Unit"] = df["result_pdr_lower_rate_unit"]
        cov["Throughput_NDR"] = df.apply(
            lambda row: row["result_ndr_lower_rate_value"] / 1e6, axis=1
        )
        cov["Throughput_NDR_Gbps"] = df.apply(
            lambda row: row["result_ndr_lower_bandwidth_value"] / 1e9, axis=1
        )
        cov["Throughput_PDR"] = df.apply(
            lambda row: row["result_pdr_lower_rate_value"] / 1e6, axis=1
        )
        cov["Throughput_PDR_Gbps"] = df.apply(
            lambda row: row["result_pdr_lower_bandwidth_value"] / 1e9, axis=1
        )
        if show_latency:
            for way in ("Forward", "Reverse"):
                for pdr in (10, 50, 90):
                    for perc in (50, 90, 99):
                        latency = f"result_latency_{way.lower()}_pdr_{pdr}_hdrh"
                        cov[f"Latency {way} [us]_{pdr}% PDR_P{perc}"] = \
                            df.apply(
                                lambda row: _latency(row[latency], perc),
                                axis=1
                            )

    if csv:
        return cov

    # Split data into tables depending on the test suite.
    for suite in cov["Suite"].unique().tolist():
        df_suite = pd.DataFrame(cov.loc[(cov["Suite"] == suite)])

        if ttype !="device":
            unit = df_suite["Throughput_Unit"].tolist()[0]
            df_suite.rename(
                columns={
                    "Throughput_NDR": f"Throughput_NDR_M{unit}",
                    "Throughput_PDR": f"Throughput_PDR_M{unit}",
                    "Throughput_AVG": f"Throughput_G{unit}_AVG",
                    "Throughput_STDEV": f"Throughput_G{unit}_STDEV"
                },
                inplace=True
            )
            df_suite.drop(["Suite", "Throughput_Unit"], axis=1, inplace=True)

        l_data.append((suite, df_suite, ))

    return l_data, ttype


def coverage_tables(
        data: pd.DataFrame,
        selected: dict,
        show_latency: bool=True,
        start_collapsed: bool=True
    ) -> dbc.Accordion:
    """Generate an accordion with coverage tables.

    :param data: Coverage data.
    :param selected: Dictionary with user selection.
    :param show_latency: If True, latency is displayed in the tables.
    :param start_collapsed: If True, the accordion with tables is collapsed when
        displayed.
    :type data: pandas.DataFrame
    :type selected: dict
    :type show_latency: bool
    :type start_collapsed: bool
    :returns: Accordion with suite names (titles) and tables.
    :rtype: dash_bootstrap_components.Accordion
    """

    accordion_items = list()
    sel_data, ttype = \
        select_coverage_data(data, selected, show_latency=show_latency)
    for suite, cov_data in sel_data:
        if ttype == "device":  # VPP Device
            cols = [
                {
                    "name": col,
                    "id": col,
                    "deletable": False,
                    "selectable": False,
                    "type": "text"
                } for col in cov_data.columns
            ]
            style_cell={"textAlign": "left"}
            style_cell_conditional=[
                {
                    "if": {"column_id": "Result"},
                    "textAlign": "right"
                }
            ]
        elif ttype == "mrr":  # MRR
            cols = list()
            for idx, col in enumerate(cov_data.columns):
                if idx == 0:
                    cols.append({
                        "name": ["", "", col],
                        "id": col,
                        "deletable": False,
                        "selectable": False,
                        "type": "text"
                    })
                else:
                    cols.append({
                        "name": col.split("_"),
                        "id": col,
                        "deletable": False,
                        "selectable": False,
                        "type": "numeric",
                        "format": Format(precision=2, scheme=Scheme.fixed)
                    })
            style_cell={"textAlign": "right"}
            style_cell_conditional=[
                {
                    "if": {"column_id": "Test Name"},
                    "textAlign": "left"
                }
            ]
        else:  # Performance NDRPDR
            cols = list()
            for idx, col in enumerate(cov_data.columns):
                if idx == 0:
                    cols.append({
                        "name": ["", "", col],
                        "id": col,
                        "deletable": False,
                        "selectable": False,
                        "type": "text"
                    })
                elif idx < 5:
                    cols.append({
                        "name": col.split("_"),
                        "id": col,
                        "deletable": False,
                        "selectable": False,
                        "type": "numeric",
                        "format": Format(precision=2, scheme=Scheme.fixed)
                    })
                else:
                    cols.append({
                        "name": col.split("_"),
                        "id": col,
                        "deletable": False,
                        "selectable": False,
                        "type": "numeric",
                        "format": Format(precision=0, scheme=Scheme.fixed)
                    })
            style_cell={"textAlign": "right"}
            style_cell_conditional=[
                {
                    "if": {"column_id": "Test Name"},
                    "textAlign": "left"
                }
            ]

        accordion_items.append(
            dbc.AccordionItem(
                title=suite,
                children=dash_table.DataTable(
                    columns=cols,
                    data=cov_data.to_dict("records"),
                    merge_duplicate_headers=True,
                    editable=False,
                    filter_action="none",
                    sort_action="native",
                    sort_mode="multi",
                    selected_columns=[],
                    selected_rows=[],
                    page_action="none",
                    style_cell=style_cell,
                    style_cell_conditional=style_cell_conditional
                )
            )
        )
    if not accordion_items:
        accordion_items.append(dbc.AccordionItem(
            title="No data.",
            children="No data."
        ))
        start_collapsed = True
    return dbc.Accordion(
        children=accordion_items,
        class_name="gy-1 p-0",
        start_collapsed=start_collapsed,
        always_open=True
    )