forked from openshift/osd-alert-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
121 lines (103 loc) · 3.41 KB
/
wsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Main WSGI application module
"""
import re
from datetime import date, datetime, timedelta, timezone
import dash_bootstrap_components as dbc
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from webui import StandardDataTable, WebUISession
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
application = app.server
def get_navbar(since, until, max_date):
"""
Returns navbar page component
:param since: datetime.datetime for start of query time window
:param until: datetime.datetime for end of query time window
:max_date: maximum datetime.date allowed to be picked by date picker
"""
return dbc.NavbarSimple(
[
dbc.InputGroup(
[
# dbc.InputGroupText("")
dcc.DatePickerRange(
id="date-picker",
start_date=since.date(),
end_date=until.date(),
max_date_allowed=max_date,
updatemode="bothdates",
className="dash-bootstrap",
)
]
)
],
brand="OSD Alert Analysis",
brand_href="#",
)
def question_answer_generator(question_instances):
"""
Generates a question header and an answer table for a list of Question instances
"""
for question in question_instances:
answer = question.get_answer()
yield dbc.Row(dbc.Col(html.Br()))
yield dbc.Row(dbc.Col(html.H2(question.description)))
yield dbc.Row(
dbc.Col(
html.Div(
children=[StandardDataTable(answer.columns, answer.data)],
className="dbc-row-selectable",
)
)
)
app.layout = html.Div(
children=[
dcc.Location(id="url", refresh=False),
html.Div(id="page-content-div"),
]
)
@app.callback(Output("page-content-div", "children"), [Input("url", "pathname")])
def display_page(pathname):
"""
Generates page content on URL change (i.e., page load)
"""
max_date = datetime.now(timezone.utc) - timedelta(days=1)
try:
clean_dates_list = re.sub(r"[^\d/-]+", "", pathname.strip("/")).rsplit("/", 2)
since = datetime.fromisoformat(clean_dates_list[0])
until = datetime.fromisoformat(clean_dates_list[1])
except (ValueError, IndexError):
since = max_date - timedelta(days=30)
until = max_date
session = WebUISession(since, until)
return html.Div(
[
get_navbar(since, until, max_date),
dbc.Container(
list(question_answer_generator(session.question_instances)),
fluid=False,
),
]
)
@app.callback(
Output("url", "pathname"),
Input("date-picker", "start_date"),
Input("date-picker", "end_date"),
)
def update_date_range(start_date, end_date):
"""
Query date range picker input handler
"""
path_string = "/"
try:
path_string += date.fromisoformat(start_date).isoformat()
path_string += "/" + date.fromisoformat(end_date).isoformat()
except ValueError:
# pylint: disable=raise-missing-from
raise PreventUpdate
else:
return path_string
if __name__ == "__main__":
app.run_server(debug=False, dev_tools_hot_reload=False)