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


import pandas as pd

from ..utils.constants import Constants as C


def select_search_data(data: pd.DataFrame, selection: list) -> pd.DataFrame:
    """Return the searched data based on the user's "selection".

    :param data: Input data to be searched through.
    :param selection: User selection.
    :type data: pandas.DataFrame
    :type selection: list[dict]
    :returns: A dataframe with selected tests.
    :trype: pandas.DataFrame
    """

    sel_data = data[selection["datatype"]]

    if selection["datatype"] == "trending":
        df = pd.DataFrame(sel_data.loc[
            sel_data["dut_type"] == selection["dut"]
        ])
    else:
        df = pd.DataFrame(sel_data.loc[(
            (sel_data["dut_type"] == selection["dut"]) &
            (sel_data["release"] == selection["release"])
        )])
    try:
        df = df[
            df.full_id.str.contains(
                selection["regexp"].replace(" ", ".*"),
                regex=True
            )
        ]
    except Exception:
        return pd.DataFrame()

    return df


def search_table(data: pd.DataFrame, selection: list) -> pd.DataFrame:
    """Generate a table listing tests based on user's selection.

    :param data: Input data (all tests).
    :param selection: User selection.
    :type data: pandas.DataFrame
    :type selection: list[dict]
    :returns: A dataframe with selected tests/
    :rtype: pandas.DataFrame
    """

    sel = select_search_data(data, selection)
    if sel.empty:
        return pd.DataFrame()

    l_tb, l_nic, l_drv, l_test, = list(), list(), list(), list()
    if selection["datatype"] == "trending":
        cols = ["job", "test_id"]
    else:
        l_dutver = list()
        cols = ["job", "test_id", "dut_version"]
    for _, row in sel[cols].drop_duplicates().iterrows():
        l_id = row["test_id"].split(".")
        suite = l_id[-2].replace("2n1l-", "").replace("1n1l-", "").\
            replace("2n-", "")
        l_tb.append("-".join(row["job"].split("-")[-2:]))
        l_nic.append(suite.split("-")[0])
        if selection["datatype"] != "trending":
            l_dutver.append(row["dut_version"])
        for driver in C.DRIVERS:
            if driver in suite:
                l_drv.append(driver)
                break
        else:
            l_drv.append("dpdk")
        l_test.append(l_id[-1])

    if selection["datatype"] == "trending":
        selected = pd.DataFrame.from_dict({
            "Test Bed": l_tb,
            "NIC": l_nic,
            "Driver": l_drv,
            "Test": l_test
        })

        selected.sort_values(
            by=["Test Bed", "NIC", "Driver", "Test"],
            ascending=True,
            inplace=True
        )
    else:
        selected = pd.DataFrame.from_dict({
            "DUT Version": l_dutver,
            "Test Bed": l_tb,
            "NIC": l_nic,
            "Driver": l_drv,
            "Test": l_test
        })

        selected.sort_values(
            by=["DUT Version", "Test Bed", "NIC", "Driver", "Test"],
            ascending=True,
            inplace=True
        )

    return selected