UTI: code clean-up
[csit.git] / resources / tools / dash / app / pal / news / tables.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 """The tables with news.
15 """
16
17 import pandas as pd
18 import dash_bootstrap_components as dbc
19
20 from ..utils.constants import Constants as C
21
22
23 def table_news(data: pd.DataFrame, job: str) -> list:
24     """Generates the tables with news:
25     1. Falied tests from the last run
26     2. Regressions and progressions calculated from the last C.NEWS_TIME_PERIOD
27        days.
28
29     :param data: Trending data with calculated annomalies to be displayed in the
30         tables.
31     :param job: The job name.
32     :type data: pandas.DataFrame
33     :type job: str
34     """
35
36     job_data = data.loc[(data["job"] == job)]
37     failed = job_data["failed"].to_list()[0]
38     regressions = {"Test Name": list(), "Last Regression": list()}
39     for itm in job_data["regressions"].to_list()[0]:
40         regressions["Test Name"].append(itm[0])
41         regressions["Last Regression"].append(itm[1].strftime('%Y-%m-%d %H:%M'))
42     progressions = {"Test Name": list(), "Last Progression": list()}
43     for itm in job_data["progressions"].to_list()[0]:
44         progressions["Test Name"].append(itm[0])
45         progressions["Last Progression"].append(
46             itm[1].strftime('%Y-%m-%d %H:%M'))
47
48     return [
49         dbc.Table.from_dataframe(pd.DataFrame.from_dict({
50             "Job": job_data["job"],
51             "Last Build": job_data["build"],
52             "Date": job_data["start"],
53             "DUT": job_data["dut_type"],
54             "DUT Version": job_data["dut_version"],
55             "Hosts": ", ".join(job_data["hosts"].to_list()[0])
56         }), bordered=True, striped=True, hover=True, size="sm", color="light"),
57         dbc.Table.from_dataframe(pd.DataFrame.from_dict({
58             (
59                 f"Last Failed Tests on "
60                 f"{job_data['start'].values[0]} ({len(failed)})"
61             ): failed
62         }), bordered=True, striped=True, hover=True, size="sm", color="light"),
63         dbc.Label(
64             class_name="p-0",
65             size="lg",
66             children=(
67                 f"Regressions during the last {C.NEWS_TIME_PERIOD} days "
68                 f"({len(regressions['Test Name'])})"
69             )
70         ),
71         dbc.Table.from_dataframe(
72             pd.DataFrame.from_dict(regressions),
73             bordered=True, striped=True, hover=True, size="sm", color="light"),
74         dbc.Label(
75             class_name="p-0",
76             size="lg",
77             children=(
78                 f"Progressions during the last {C.NEWS_TIME_PERIOD} days "
79                 f"({len(progressions['Test Name'])})"
80             )
81         ),
82         dbc.Table.from_dataframe(
83             pd.DataFrame.from_dict(progressions),
84             bordered=True, striped=True, hover=True, size="sm", color="light")
85     ]