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