-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.py
206 lines (183 loc) · 7.51 KB
/
view.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from __future__ import annotations
__author__ = "Loh Zhi Shen"
__version__ = "1.0.0"
__last_updated__ = "25 August 2022"
import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.graph_objects as go
import datetime
from controller import Controller
class View:
"""View in the MVC Design Pattern.
This class controls the running of the dashboard.
Args:
START (datetime): The time which the server should start.
Controller (Controller): The controller class in the MVC
model which the view will call upon to pull an update.
minutes (int): The between data collection in minutes.
"""
def __init__(self: View, START: datetime.datetime,
controller: Controller, minutes: int = 5) -> None:
# create the dashboard
self.app = dash.Dash(__name__)
# customize the layout
self.app.layout = html.Div(
children = [
dcc.Interval(
id = 'timer',
interval = 1000 * minutes, # should be 15 minutes
n_intervals = 0
),
dcc.Store(
id = 'session',
storage_type = 'session',
data = START
),
html.Div(
className = 'header',
children = [
html.H1(
className = 'header-icon',
children = '🌤️'
),
html.H1(
className = 'header-title',
children = 'SOLAR POWER GENERATION'
),
html.H1(
className = 'header-subtitle',
children = 'Equipment Performance'
)
]
),
html.Div(
className = 'main',
children = [
html.Div(
className = 'left-body',
children = [
dcc.Graph(
id = 'data-visualization',
className = 'graph',
figure = go.Figure()
)
]
),
html.Div(
className = 'right-body',
children = [
html.P(
className = 'alert-title',
children = 'Alerts'
),
html.P(
id = 'null-alert',
className = 'alert-null',
children = 'There are no alerts.'
),
html.P(
id = 'alerts',
className = 'alert-body',
children = []
)
]
)
]
)
]
)
@self.app.callback([
Output('data-visualization', 'figure'),
Output('alerts', 'children'),
Output('null-alert', 'children'),
Output('session', 'data'),
Output('timer', 'n_intervals')
], [
Input('timer', 'n_intervals'),
Input('session', 'data'),
Input('alerts', 'children')
]
)
def pull_update(n_intervals, server_time, old):
"""This callback updates the dashboard with the most
update to date information.
"""
# convert sever_time from str to datetime
time_details = {
'year': int(server_time[0:4]),
'month': int(server_time[5:7]),
'day': int(server_time[8:10]),
'hour': int(server_time[11:13]),
'minute': int(server_time[14:16])
}
server_time = datetime.datetime(**time_details)
# get the last time which data was recoreded
if time_details['minute'] >= 45:
time_details['minute'] = 45
elif time_details['minute'] >= 30:
time_details['minute'] = 30
elif time_details['minute'] >= 15:
time_details['minute'] = 15
else:
time_details['minute'] = 0
time = datetime.datetime(**time_details)
# alert controller to update event
df = controller.identify_outliers(time)
# order for colors
order = df['RESULT'].unique()
if order[0] == 0:
color = ['rgb(27, 158, 119)', 'rgb(217, 95, 2)']
else:
color = ['rgb(217, 95, 2)', 'rgb(27, 158, 119)']
# creating the figure
figure = px.bar(
df, x = 'SOURCE_KEY', y = 'POWER', color = 'RESULT',
title = 'Live Power Generation',
labels = {
'RESULT': 'Outlier',
"SOURCE_KEY": "Inverter ID",
'POWER': 'Power'
},
color_discrete_sequence = color)
figure.update_layout(margin_autoexpand = True)
# creating and logging new alerts
alerts = []
underperforming = df.loc[df['RESULT'], 'SOURCE_KEY'].to_numpy()
with open(
f'Log/{time.year}-{time.month}-{time.day} Log file.txt', 'a+'
) as file:
for key in underperforming:
alerts.append(
html.P(
className = 'alert-new',
children = f"[{time}] {key} triggered performance alert!",
style = {'font-weight': 'bold'}
)
)
file.write(f"[{time}] {key} triggered performance alert!" + '\n')
# unbold past alerts
if time.day == (time - datetime.timedelta(minutes = 15)).day:
for text in old:
alerts.append(
html.P(
className = 'alert-old',
children = text['props']['children']
)
)
# default text if no alerts
if alerts == []:
null_alert = 'There are no alerts.'
else:
null_alert = ''
# update the server_time
next_time = server_time + datetime.timedelta(minutes = 15)
return (figure, alerts, null_alert, next_time, n_intervals + 1)
def run(self, debug = False):
"""Runs the app.
Args:
debug (bool): A bool to indicate whether to run the app in
debug mode.
"""
self.app.run(debug = debug)