2fabf8e6aef40722040d1a9fa79b795c68c9962a
[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     x_axis = [d for d in data["start_time"] if d >= start and d <= end]
44     if not x_axis:
45         return None, None
46
47     hover = list()
48     for _, row in data.iterrows():
49         hover_itm = (
50             f"date: {row['start_time'].strftime('%d-%m-%Y %H:%M:%S')}<br>"
51             f"duration: "
52             f"{(int(row['duration']) // 3600):02d}:"
53             f"{((int(row['duration']) % 3600) // 60):02d}<br>"
54             f"passed: {row['passed']}<br>"
55             f"failed: {row['failed']}<br>"
56             f"{row['dut_type']}-ref: {row['dut_version']}<br>"
57             f"csit-ref: {row['job']}/{row['build']}<br>"
58             f"hosts: {', '.join(row['hosts'])}"
59         )
60         hover.append(hover_itm)
61
62     # Job durations:
63     fig_duration = go.Figure(
64         data=go.Scatter(
65             x=x_axis,
66             y=data["duration"],
67             name=u"Duration",
68             text=hover,
69             hoverinfo=u"text"
70         )
71     )
72
73     tickvals = [0, ]
74     step = max(data["duration"]) / 5
75     for i in range(5):
76         tickvals.append(int(step * (i + 1)))
77     layout_duration = layout.get("plot-stats-duration", dict())
78     if layout_duration:
79         layout_duration["yaxis"]["tickvals"] = tickvals
80         layout_duration["yaxis"]["ticktext"] = [
81             f"{(val // 3600):02d}:{((val % 3600) // 60):02d}" \
82                 for val in tickvals
83         ]
84         fig_duration.update_layout(layout_duration)
85
86     # Passed / failed:
87     fig_passed = go.Figure(
88         data=[
89             go.Bar(
90                 x=x_axis,
91                 y=data["passed"],
92                 name=u"Passed",
93                 hovertext=hover,
94                 hoverinfo=u"text"
95             ),
96             go.Bar(
97                 x=x_axis,
98                 y=data["failed"],
99                 name=u"Failed",
100                 hovertext=hover,
101                 hoverinfo=u"text"
102             )
103         ]
104     )
105     layout_pf = layout.get("plot-stats-passed", dict())
106     if layout_pf:
107         fig_passed.update_layout(layout_pf)
108
109     return fig_passed, fig_duration