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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """The tables with news.
18 import dash_bootstrap_components as dbc
20 from datetime import datetime, timedelta
23 def _table_info(job_data: pd.DataFrame) -> dbc.Table:
24 """Generates table with info about the job.
26 :param job_data: Dataframe with information about the job.
27 :type job_data: pandas.DataFrame
28 :returns: Table with job info.
31 return dbc.Table.from_dataframe(
32 pd.DataFrame.from_dict(
34 "Job": job_data["job"],
35 "Last Build": job_data["build"],
36 "Date": job_data["start"],
37 "DUT": job_data["dut_type"],
38 "DUT Version": job_data["dut_version"],
39 "Hosts": ", ".join(job_data["hosts"].to_list()[0])
50 def _table_failed(job_data: pd.DataFrame, failed: list) -> dbc.Table:
51 """Generates table with failed tests from the last run of the job.
53 :param job_data: Dataframe with information about the job.
54 :param failed: List of failed tests.
55 :type job_data: pandas.DataFrame
57 :returns: Table with fialed tests.
60 return dbc.Table.from_dataframe(
61 pd.DataFrame.from_dict(
64 f"Last Failed Tests on "
65 f"{job_data['start'].values[0]} ({len(failed)})"
77 def _table_gressions(itms: dict, color: str) -> dbc.Table:
78 """Generates table with regressions.
80 :param itms: Dictionary with items (regressions or progressions) and their
82 :param color: Color of the table.
83 :type regressions: dict
85 :returns: The table with regressions.
88 return dbc.Table.from_dataframe(
89 pd.DataFrame.from_dict(itms),
98 def table_news(data: pd.DataFrame, job: str, period: int) -> list:
99 """Generates the tables with news:
100 1. Falied tests from the last run
101 2. Regressions and progressions calculated from the last C.NEWS_TIME_PERIOD
104 :param data: Trending data with calculated annomalies to be displayed in the
106 :param job: The job name.
107 :param period: The time period (nr of days from now) taken into account.
108 :type data: pandas.DataFrame
111 :returns: List of tables.
115 last_day = datetime.utcnow() - timedelta(days=period)
117 job_data = data.loc[(data["job"] == job)]
118 r_list.append(_table_info(job_data))
120 failed = job_data["failed"].to_list()[0]
122 r_list.append(_table_failed(job_data, failed))
124 title = f"Regressions in the last {period} days"
125 regressions = {title: list(), "Last Regression": list()}
126 for itm in job_data["regressions"].to_list()[0]:
127 if itm[1] < last_day:
129 regressions[title].append(itm[0])
130 regressions["Last Regression"].append(
131 itm[1].strftime('%Y-%m-%d %H:%M'))
132 if regressions["Last Regression"]:
133 r_list.append(_table_gressions(regressions, "warning"))
135 title = f"Progressions in the last {period} days"
136 progressions = {title: list(), "Last Progression": list()}
137 for itm in job_data["progressions"].to_list()[0]:
138 if itm[1] < last_day:
140 progressions[title].append(itm[0])
141 progressions["Last Progression"].append(
142 itm[1].strftime('%Y-%m-%d %H:%M'))
143 if progressions["Last Progression"]:
144 r_list.append(_table_gressions(progressions, "success"))
149 def table_summary(data: pd.DataFrame, jobs: list, period: int) -> list:
150 """Generates summary (failed tests, regressions and progressions) from the
153 :param data: Trending data with calculated annomalies to be displayed in the
155 :param jobs: List of jobs.
156 :params period: The time period for the summary table.
157 :type data: pandas.DataFrame
160 :returns: List of tables.
169 children=table_news(data, job, period)
172 class_name="gy-2 p-0",
173 start_collapsed=True,