C-Dash: Add search in tests
[csit.git] / csit.infra.dash / app / cdash / search / tables.py
1 # Copyright (c) 2024 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 search data tables.
15 """
16
17
18 import pandas as pd
19
20 from ..utils.constants import Constants as C
21
22
23 def select_search_data(data: pd.DataFrame, selection: list) -> pd.DataFrame:
24     """Return the searched data based on the user's "selection".
25
26     :param data: Input data to be searched through.
27     :param selection: User selection.
28     :type data: pandas.DataFrame
29     :type selection: list[dict]
30     :returns: A dataframe with selected tests.
31     :trype: pandas.DataFrame
32     """
33
34     sel_data = data[selection["datatype"]]
35
36     if selection["datatype"] == "trending":
37         df = pd.DataFrame(sel_data.loc[
38             sel_data["dut_type"] == selection["dut"]
39         ])
40     else:
41         df = pd.DataFrame(sel_data.loc[(
42             (sel_data["dut_type"] == selection["dut"]) &
43             (sel_data["release"] == selection["release"])
44         )])
45     try:
46         df = df[
47             df.full_id.str.contains(
48                 selection["regexp"].replace(" ", ".*"),
49                 regex=True
50             )
51         ]
52     except Exception:
53         return pd.DataFrame()
54
55     return df
56
57
58 def search_table(data: pd.DataFrame, selection: list) -> pd.DataFrame:
59     """Generate a table listing tests based on user's selection.
60
61     :param data: Input data (all tests).
62     :param selection: User selection.
63     :type data: pandas.DataFrame
64     :type selection: list[dict]
65     :returns: A dataframe with selected tests/
66     :rtype: pandas.DataFrame
67     """
68
69     sel = select_search_data(data, selection)
70     if sel.empty:
71         return pd.DataFrame()
72
73     l_tb, l_nic, l_drv, l_test, = list(), list(), list(), list()
74     if selection["datatype"] == "trending":
75         cols = ["job", "test_id"]
76     else:
77         l_dutver = list()
78         cols = ["job", "test_id", "dut_version"]
79     for _, row in sel[cols].drop_duplicates().iterrows():
80         l_id = row["test_id"].split(".")
81         suite = l_id[-2].replace("2n1l-", "").replace("1n1l-", "").\
82             replace("2n-", "")
83         l_tb.append("-".join(row["job"].split("-")[-2:]))
84         l_nic.append(suite.split("-")[0])
85         if selection["datatype"] != "trending":
86             l_dutver.append(row["dut_version"])
87         for driver in C.DRIVERS:
88             if driver in suite:
89                 l_drv.append(driver)
90                 break
91         else:
92             l_drv.append("dpdk")
93         l_test.append(l_id[-1])
94
95     if selection["datatype"] == "trending":
96         selected = pd.DataFrame.from_dict({
97             "Test Bed": l_tb,
98             "NIC": l_nic,
99             "Driver": l_drv,
100             "Test": l_test
101         })
102
103         selected.sort_values(
104             by=["Test Bed", "NIC", "Driver", "Test"],
105             ascending=True,
106             inplace=True
107         )
108     else:
109         selected = pd.DataFrame.from_dict({
110             "DUT Version": l_dutver,
111             "Test Bed": l_tb,
112             "NIC": l_nic,
113             "Driver": l_drv,
114             "Test": l_test
115         })
116
117         selected.sort_values(
118             by=["DUT Version", "Test Bed", "NIC", "Driver", "Test"],
119             ascending=True,
120             inplace=True
121         )
122
123     return selected