d9f49407d94256c6a1302334280eb951ba5cf9e6
[csit.git] / resources / tools / dash / app / pal / stats / graphs.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 """
15 """
16
17 import plotly.graph_objects as go
18 import pandas as pd
19
20 from datetime import datetime, timedelta
21
22 def select_data(data: pd.DataFrame, itm:str, start: datetime,
23         end: datetime) -> pd.DataFrame:
24     """
25     """
26
27     df = data.loc[
28         (data["job"] == itm) &
29         (data["start_time"] >= start) & (data["start_time"] <= end)
30     ].sort_values(by="start_time", ignore_index=True)
31     df = df.dropna(subset=["duration", ])
32
33     return df
34
35
36 def graph_statistics(df: pd.DataFrame, job:str, layout: dict,
37         start: datetime=datetime.utcnow()-timedelta(days=180),
38         end: datetime=datetime.utcnow()) -> tuple:
39     """
40     """
41
42     data = select_data(df, job, start, end)
43     if data.empty:
44         return None, None
45
46     hover = list()
47     for _, row in data.iterrows():
48         hover_itm = (
49             f"date: {row['start_time'].strftime('%d-%m-%Y %H:%M:%S')}<br>"
50             f"duration: "
51             f"{(int(row['duration']) // 3600):02d}:"
52             f"{((int(row['duration']) % 3600) // 60):02d}<br>"
53             f"passed: {row['passed']}<br>"
54             f"failed: {row['failed']}<br>"
55             f"{row['dut_type']}-ref: {row['dut_version']}<br>"
56             f"csit-ref: {row['job']}/{row['build']}<br>"
57             f"hosts: {', '.join(row['hosts'])}"
58         )
59         hover.append(hover_itm)
60
61     # Job durations:
62     fig_duration = go.Figure(
63         data=go.Scatter(
64             x=data["start_time"],
65             y=data["duration"],
66             name=u"Duration",
67             text=hover,
68             hoverinfo=u"text"
69         )
70     )
71
72     tickvals = [0, ]
73     step = max(data["duration"]) / 5
74     for i in range(5):
75         tickvals.append(int(step * (i + 1)))
76     layout_duration = layout.get("plot-stats-duration", dict())
77     if layout_duration:
78         layout_duration["yaxis"]["tickvals"] = tickvals
79         layout_duration["yaxis"]["ticktext"] = [
80             f"{(val // 3600):02d}:{((val % 3600) // 60):02d}" \
81                 for val in tickvals
82         ]
83         fig_duration.update_layout(layout_duration)
84
85     # Passed / failed:
86     fig_passed = go.Figure(
87         data=[
88             go.Bar(
89                 x=data["start_time"],
90                 y=data["passed"],
91                 name=u"Passed",
92                 hovertext=hover,
93                 hoverinfo=u"text"
94             ),
95             go.Bar(
96                 x=data["start_time"],
97                 y=data["failed"],
98                 name=u"Failed",
99                 hovertext=hover,
100                 hoverinfo=u"text"
101             )
102         ]
103     )
104     layout_pf = layout.get("plot-stats-passed", dict())
105     if layout_pf:
106         fig_passed.update_layout(layout_pf)
107
108     return fig_passed, fig_duration