-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdashboard.py
455 lines (382 loc) · 14.3 KB
/
dashboard.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Third-party libraries
import math
from typing import List
from flasgger import swag_from
from flask import request
from flask_restx import Resource
from api.models import (
EventsModel,
SiteModel,
ExceedanceModel,
)
from api.utils.data_formatters import (
filter_non_private_sites,
filter_non_private_devices,
)
# Middlewares
from api.utils.http import AirQoRequests
from api.utils.pollutants import (
generate_pie_chart_data,
d3_generate_pie_chart_data,
PM_COLOR_CATEGORY,
set_pm25_category_background,
)
from api.utils.request_validators import validate_request_json
from main import rest_api_v2
import logging
logger = logging.getLogger(__name__)
@rest_api_v2.route("/dashboard/chart/data")
class ChartDataResource(Resource):
@swag_from("/api/docs/dashboard/customised_chart_post.yml")
@validate_request_json(
"sites|required:list",
"startDate|required:datetime",
"endDate|required:datetime",
"frequency|required:str",
"pollutant|required:str",
"chartType|required:str",
)
def post(self):
tenant = request.args.get("tenant", "airqo")
json_data = request.get_json()
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get(
"sites", []
)
start_date = json_data["startDate"]
end_date = json_data["endDate"]
frequency = json_data["frequency"]
pollutant = json_data["pollutant"]
chart_type = json_data["chartType"]
colors = ["#7F7F7F", "#E377C2", "#17BECF", "#BCBD22", "#3f51b5"]
events_model = EventsModel(tenant)
data = events_model.get_chart_events(
sites, start_date, end_date, pollutant, frequency
)
chart_datasets = []
chart_labels = []
for record in data:
site = record.get("site", {})
site_name = f"{site.get('name') or site.get('description') or site.get('generated_name')}"
dataset = {}
sorted_values = sorted(
record.get("values", []), key=lambda item: item.get("time")
)
if chart_type.lower() == "pie":
category_count = generate_pie_chart_data(
records=sorted_values, key="value", pollutant=pollutant
)
try:
labels, values = zip(*category_count.items())
except ValueError:
values = []
labels = []
background_colors = [
PM_COLOR_CATEGORY.get(label, "#808080") for label in labels
]
color = background_colors
dataset.update(
{
"data": values,
"label": f"{site_name} {pollutant}",
"backgroundColor": color,
"fill": False,
}
)
else:
try:
values, labels = zip(
*[
(data.get("value"), data.get("time"))
for data in sorted_values
]
)
except ValueError:
values = []
labels = []
color = colors.pop()
dataset.update(
{
"data": values,
"label": f"{site_name} {pollutant}",
"borderColor": color,
"backgroundColor": color,
"fill": False,
}
)
if not chart_labels:
chart_labels = labels
chart_datasets.append(dataset)
return (
AirQoRequests.create_response(
"successfully retrieved chart data",
data={"labels": chart_labels, "datasets": chart_datasets},
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/chart/d3/data")
class D3ChartDataResource(Resource):
@swag_from("/api/docs/dashboard/d3_chart_data_post.yml")
@validate_request_json(
"sites|required:list",
"startDate|required:datetime",
"endDate|required:datetime",
"frequency|required:str",
"pollutant|required:str",
"chartType|required:str",
)
def _get_validated_filter(self, json_data):
"""
Validates that exactly one of 'airqlouds', 'sites', or 'devices' is provided in the request,
and applies filtering if necessary.
Args:
json_data (dict): JSON payload from the request.
Returns:
tuple: The name of the filter ("sites", "devices", or "airqlouds") and its validated value if valid.
Raises:
ValueError: If more than one or none of the filters are provided.
"""
error_message: str = ""
validated_data: List[str] = None
# TODO Lias with device registry to cleanup this makeshift implementation
devices = ["devices", "device_ids", "device_names"]
sites = ["sites", "site_names", "site_ids"]
valid_filters = [
"sites",
"site_names",
"site_ids",
"devices",
"device_ids",
"airqlouds",
"device_names",
]
provided_filters = [key for key in valid_filters if json_data.get(key)]
if len(provided_filters) != 1:
raise ValueError(
"Specify exactly one of 'airqlouds', 'sites', 'device_names', or 'devices' in the request body."
)
filter_type = provided_filters[0]
filter_value = json_data.get(filter_type)
if filter_type in sites:
validated_value = filter_non_private_sites(filter_type, filter_value)
elif filter_type in devices:
validated_value = filter_non_private_devices(filter_type, filter_value)
else:
return filter_type, filter_value, None
if validated_value and validated_value.get("status") == "success":
# TODO This should be cleaned up.
validated_data = validated_value.get("data", {}).get(
"sites" if filter_type in sites else "devices", []
)
else:
error_message = validated_value.get("message", "Validation failed")
return filter_type, validated_data, error_message
def post(self):
json_data = request.get_json()
try:
filter_type, filter_value, error_message = self._get_validated_filter(
json_data
)
if error_message:
return error_message, AirQoRequests.Status.HTTP_400_BAD_REQUEST
except Exception as e:
logger.exception(f"An error has occured; {e}")
start_date = json_data["startDate"]
end_date = json_data["endDate"]
frequency = json_data["frequency"]
pollutant = json_data["pollutant"]
chart_type = json_data["chartType"]
events_model = EventsModel("airqo")
data = events_model.get_d3_chart_events_v2(
filter_value, start_date, end_date, pollutant, frequency
)
if chart_type.lower() == "pie":
data = d3_generate_pie_chart_data(data, pollutant)
return (
AirQoRequests.create_response(
"successfully retrieved d3 chart data", data=data
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/sites")
class MonitoringSiteResource(Resource):
@swag_from("/api/docs/dashboard/monitoring_site_get.yml")
def get(self):
tenant = request.args.get("tenant", "airqo")
ms_model = SiteModel(tenant)
sites = ms_model.get_all_sites()
return (
AirQoRequests.create_response(
"monitoring site data successfully fetched", data=sites
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/historical/daily-averages")
class DailyAveragesResource(Resource):
@swag_from("/api/docs/dashboard/device_daily_measurements_get.yml")
@validate_request_json(
"pollutant|required:str",
"startDate|required:datetime",
"endDate|required:datetime",
"sites|optional:list",
)
def post(self):
tenant = request.args.get("tenant", "airqo")
json_data = request.get_json()
pollutant = json_data["pollutant"]
start_date = json_data["startDate"]
end_date = json_data["endDate"]
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get(
"sites", []
)
events_model = EventsModel(tenant)
site_model = SiteModel(tenant)
sites = site_model.get_sites(sites)
data = events_model.get_averages_by_pollutant_from_bigquery(
start_date, end_date, pollutant
)
values = []
labels = []
background_colors = []
for v in data:
value = v.get("value", None)
site_id = v.get("site_id", None)
if not site_id or not value or math.isnan(value):
continue
site = list(filter(lambda s: s.get("site_id") == site_id, sites))
if not site:
continue
site = site[0]
values.append(value)
labels.append(
site.get("name")
or site.get("description")
or site.get("generated_name")
)
background_colors.append(set_pm25_category_background(value))
return (
AirQoRequests.create_response(
"daily averages successfully fetched",
data={
"average_values": values,
"labels": labels,
"background_colors": background_colors,
},
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/historical/daily-averages-devices")
class DailyAveragesResource2(Resource):
@swag_from("/api/docs/dashboard/device_daily_measurements_get.yml")
@validate_request_json(
"pollutant|required:str",
"startDate|required:datetime",
"endDate|required:datetime",
"devices|optional:list",
)
def post(self):
tenant = request.args.get("tenant", "airqo")
json_data = request.get_json()
pollutant = json_data["pollutant"]
start_date = json_data["startDate"]
end_date = json_data["endDate"]
devices = filter_non_private_devices(devices=json_data.get("devices", {})).get(
"devices", []
)
events_model = EventsModel(tenant)
data = events_model.get_device_averages_from_bigquery(
start_date, end_date, pollutant, devices=devices
)
values = []
labels = []
background_colors = []
for v in data:
value = v.get("value", None)
device_id = v.get("device_id", None)
if not device_id or not value or math.isnan(value):
continue
values.append(value)
labels.append(device_id)
background_colors.append(set_pm25_category_background(value))
return (
AirQoRequests.create_response(
"daily averages successfully fetched",
data={
"average_values": values,
"labels": labels,
"background_colors": background_colors,
},
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/exceedances")
class ExceedancesResource(Resource):
@swag_from("/api/docs/dashboard/exceedances_post.yml")
@validate_request_json(
"pollutant|required:str",
"standard|required:str",
"startDate|required:datetime",
"endDate|required:datetime",
"sites|optional:list",
)
def post(self):
tenant = request.args.get("tenant", "airqo")
json_data = request.get_json()
pollutant = json_data["pollutant"]
standard = json_data["standard"]
start_date = json_data["startDate"]
end_date = json_data["endDate"]
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get(
"sites", []
)
exc_model = ExceedanceModel(tenant)
data = exc_model.get_exceedances(
start_date, end_date, pollutant, standard, sites=sites
)
return (
AirQoRequests.create_response(
"exceedance data successfully fetched",
data=data,
),
AirQoRequests.Status.HTTP_200_OK,
)
@rest_api_v2.route("/dashboard/exceedances-devices")
class ExceedancesResource2(Resource):
@swag_from("/api/docs/dashboard/exceedances_post.yml")
@validate_request_json(
"pollutant|required:str",
"standard|required:str",
"startDate|required:datetime",
"endDate|required:datetime",
"devices|optional:list",
)
def post(self):
tenant = request.args.get("tenant", "airqo")
json_data = request.get_json()
pollutant = json_data["pollutant"]
standard = json_data["standard"]
start_date = json_data["startDate"]
end_date = json_data["endDate"]
devices = filter_non_private_devices(devices=json_data.get("devices", {})).get(
"devices", []
)
events_model = EventsModel(tenant)
data = events_model.get_device_readings_from_bigquery(
start_date, end_date, pollutant, devices=devices
)
exc_model = ExceedanceModel(tenant)
standards_mapping = exc_model.standards_mapping
device_exceedances = exc_model.count_standard_categories(
data, standards_mapping, standard, pollutant
)
return AirQoRequests.create_response(
message="exceedance data successfully fetched",
data=[
{
"device_id": device_id,
"total": sum(exceedances.values()),
"exceedances": exceedances,
}
for device_id, exceedances in device_exceedances.items()
],
success=AirQoRequests.Status.HTTP_200_OK,
)