fix(dash): improve date formatting in hover
[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         d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
49         hover_itm = (
50             f"date: {row['start_time'].strftime('%Y-%m-%d %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"{d_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=data["start_time"],
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=data["start_time"],
91                 y=data["passed"],
92                 name=u"Passed",
93                 hovertext=hover,
94                 hoverinfo=u"text"
95             ),
96             go.Bar(
97                 x=data["start_time"],
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