1
|
|
|
import falcon |
2
|
|
|
import simplejson as json |
3
|
|
|
import mysql.connector |
4
|
|
|
import config |
5
|
|
|
from datetime import datetime, timedelta, timezone |
6
|
|
|
from core import utilities |
7
|
|
|
from decimal import Decimal |
8
|
|
|
import excelexporters.equipmentstatistics |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Reporting: |
12
|
|
|
@staticmethod |
13
|
|
|
def __init__(): |
14
|
|
|
pass |
15
|
|
|
|
16
|
|
|
@staticmethod |
17
|
|
|
def on_options(req, resp): |
18
|
|
|
resp.status = falcon.HTTP_200 |
19
|
|
|
|
20
|
|
|
#################################################################################################################### |
21
|
|
|
# PROCEDURES |
22
|
|
|
# Step 1: valid parameters |
23
|
|
|
# Step 2: query the equipment |
24
|
|
|
# Step 3: query energy categories |
25
|
|
|
# Step 4: query associated points |
26
|
|
|
# Step 5: query base period energy input |
27
|
|
|
# Step 6: query reporting period energy input |
28
|
|
|
# Step 7: query tariff data |
29
|
|
|
# Step 8: query associated points data |
30
|
|
|
# Step 9: construct the report |
31
|
|
|
#################################################################################################################### |
32
|
|
|
@staticmethod |
33
|
|
|
def on_get(req, resp): |
34
|
|
|
print(req.params) |
35
|
|
|
equipment_id = req.params.get('equipmentid') |
36
|
|
|
period_type = req.params.get('periodtype') |
37
|
|
|
base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
38
|
|
|
base_end_datetime_local = req.params.get('baseperiodenddatetime') |
39
|
|
|
reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
40
|
|
|
reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
41
|
|
|
|
42
|
|
|
################################################################################################################ |
43
|
|
|
# Step 1: valid parameters |
44
|
|
|
################################################################################################################ |
45
|
|
|
if equipment_id is None: |
46
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
47
|
|
|
else: |
48
|
|
|
equipment_id = str.strip(equipment_id) |
49
|
|
|
if not equipment_id.isdigit() or int(equipment_id) <= 0: |
50
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
51
|
|
|
|
52
|
|
|
if period_type is None: |
53
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
54
|
|
|
else: |
55
|
|
|
period_type = str.strip(period_type) |
56
|
|
|
if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
57
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
58
|
|
|
|
59
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
60
|
|
|
if config.utc_offset[0] == '-': |
61
|
|
|
timezone_offset = -timezone_offset |
62
|
|
|
|
63
|
|
|
base_start_datetime_utc = None |
64
|
|
|
if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
65
|
|
|
base_start_datetime_local = str.strip(base_start_datetime_local) |
66
|
|
|
try: |
67
|
|
|
base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
68
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
69
|
|
|
timedelta(minutes=timezone_offset) |
70
|
|
|
except ValueError: |
71
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
72
|
|
|
description="API.INVALID_BASE_PERIOD_START_DATETIME") |
73
|
|
|
|
74
|
|
|
base_end_datetime_utc = None |
75
|
|
|
if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
76
|
|
|
base_end_datetime_local = str.strip(base_end_datetime_local) |
77
|
|
|
try: |
78
|
|
|
base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
79
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
80
|
|
|
timedelta(minutes=timezone_offset) |
81
|
|
|
except ValueError: |
82
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
83
|
|
|
description="API.INVALID_BASE_PERIOD_END_DATETIME") |
84
|
|
|
|
85
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
86
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
87
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
88
|
|
|
description='API.INVALID_BASE_PERIOD_END_DATETIME') |
89
|
|
|
|
90
|
|
|
if reporting_start_datetime_local is None: |
91
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
92
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
93
|
|
|
else: |
94
|
|
|
reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
95
|
|
|
try: |
96
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
97
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
98
|
|
|
timedelta(minutes=timezone_offset) |
99
|
|
|
except ValueError: |
100
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
101
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
102
|
|
|
|
103
|
|
|
if reporting_end_datetime_local is None: |
104
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
105
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
106
|
|
|
else: |
107
|
|
|
reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
108
|
|
|
try: |
109
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
110
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
111
|
|
|
timedelta(minutes=timezone_offset) |
112
|
|
|
except ValueError: |
113
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
114
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
115
|
|
|
|
116
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
117
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
118
|
|
|
description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
119
|
|
|
|
120
|
|
|
################################################################################################################ |
121
|
|
|
# Step 2: query the equipment |
122
|
|
|
################################################################################################################ |
123
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
124
|
|
|
cursor_system = cnx_system.cursor() |
125
|
|
|
|
126
|
|
|
cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
127
|
|
|
cursor_energy = cnx_energy.cursor() |
128
|
|
|
|
129
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
130
|
|
|
cursor_historical = cnx_historical.cursor() |
131
|
|
|
|
132
|
|
|
cursor_system.execute(" SELECT id, name, cost_center_id " |
133
|
|
|
" FROM tbl_equipments " |
134
|
|
|
" WHERE id = %s ", (equipment_id,)) |
135
|
|
|
row_equipment = cursor_system.fetchone() |
136
|
|
|
if row_equipment is None: |
137
|
|
|
if cursor_system: |
138
|
|
|
cursor_system.close() |
139
|
|
|
if cnx_system: |
140
|
|
|
cnx_system.disconnect() |
141
|
|
|
|
142
|
|
|
if cursor_energy: |
143
|
|
|
cursor_energy.close() |
144
|
|
|
if cnx_energy: |
145
|
|
|
cnx_energy.disconnect() |
146
|
|
|
|
147
|
|
|
if cnx_historical: |
148
|
|
|
cnx_historical.close() |
149
|
|
|
if cursor_historical: |
150
|
|
|
cursor_historical.disconnect() |
151
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.EQUIPMENT_NOT_FOUND') |
152
|
|
|
|
153
|
|
|
equipment = dict() |
154
|
|
|
equipment['id'] = row_equipment[0] |
155
|
|
|
equipment['name'] = row_equipment[1] |
156
|
|
|
equipment['cost_center_id'] = row_equipment[2] |
157
|
|
|
|
158
|
|
|
################################################################################################################ |
159
|
|
|
# Step 3: query energy categories |
160
|
|
|
################################################################################################################ |
161
|
|
|
energy_category_set = set() |
162
|
|
|
# query energy categories in base period |
163
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
164
|
|
|
" FROM tbl_equipment_input_category_hourly " |
165
|
|
|
" WHERE equipment_id = %s " |
166
|
|
|
" AND start_datetime_utc >= %s " |
167
|
|
|
" AND start_datetime_utc < %s ", |
168
|
|
|
(equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
169
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
170
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
171
|
|
|
for row_energy_category in rows_energy_categories: |
172
|
|
|
energy_category_set.add(row_energy_category[0]) |
173
|
|
|
|
174
|
|
|
# query energy categories in reporting period |
175
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
176
|
|
|
" FROM tbl_equipment_input_category_hourly " |
177
|
|
|
" WHERE equipment_id = %s " |
178
|
|
|
" AND start_datetime_utc >= %s " |
179
|
|
|
" AND start_datetime_utc < %s ", |
180
|
|
|
(equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
181
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
182
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
183
|
|
|
for row_energy_category in rows_energy_categories: |
184
|
|
|
energy_category_set.add(row_energy_category[0]) |
185
|
|
|
|
186
|
|
|
# query all energy categories in base period and reporting period |
187
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
188
|
|
|
" FROM tbl_energy_categories " |
189
|
|
|
" ORDER BY id ", ) |
190
|
|
|
rows_energy_categories = cursor_system.fetchall() |
191
|
|
|
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
192
|
|
|
if cursor_system: |
193
|
|
|
cursor_system.close() |
194
|
|
|
if cnx_system: |
195
|
|
|
cnx_system.disconnect() |
196
|
|
|
|
197
|
|
|
if cursor_energy: |
198
|
|
|
cursor_energy.close() |
199
|
|
|
if cnx_energy: |
200
|
|
|
cnx_energy.disconnect() |
201
|
|
|
|
202
|
|
|
if cnx_historical: |
203
|
|
|
cnx_historical.close() |
204
|
|
|
if cursor_historical: |
205
|
|
|
cursor_historical.disconnect() |
206
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, |
207
|
|
|
title='API.NOT_FOUND', |
208
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
209
|
|
|
energy_category_dict = dict() |
210
|
|
|
for row_energy_category in rows_energy_categories: |
211
|
|
|
if row_energy_category[0] in energy_category_set: |
212
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
213
|
|
|
"unit_of_measure": row_energy_category[2], |
214
|
|
|
"kgce": row_energy_category[3], |
215
|
|
|
"kgco2e": row_energy_category[4]} |
216
|
|
|
|
217
|
|
|
################################################################################################################ |
218
|
|
|
# Step 4: query associated points |
219
|
|
|
################################################################################################################ |
220
|
|
|
point_list = list() |
221
|
|
|
cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
222
|
|
|
" FROM tbl_equipments e, tbl_equipments_parameters ep, tbl_points p " |
223
|
|
|
" WHERE e.id = %s AND e.id = ep.equipment_id AND ep.parameter_type = 'point' " |
224
|
|
|
" AND ep.point_id = p.id " |
225
|
|
|
" ORDER BY p.id ", (equipment['id'],)) |
226
|
|
|
rows_points = cursor_system.fetchall() |
227
|
|
|
if rows_points is not None and len(rows_points) > 0: |
228
|
|
|
for row in rows_points: |
229
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
230
|
|
|
|
231
|
|
|
################################################################################################################ |
232
|
|
|
# Step 5: query base period energy input |
233
|
|
|
################################################################################################################ |
234
|
|
|
base = dict() |
235
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
236
|
|
|
for energy_category_id in energy_category_set: |
237
|
|
|
base[energy_category_id] = dict() |
238
|
|
|
base[energy_category_id]['timestamps'] = list() |
239
|
|
|
base[energy_category_id]['values'] = list() |
240
|
|
|
base[energy_category_id]['subtotal'] = Decimal(0.0) |
241
|
|
|
base[energy_category_id]['mean'] = None |
242
|
|
|
base[energy_category_id]['median'] = None |
243
|
|
|
base[energy_category_id]['minimum'] = None |
244
|
|
|
base[energy_category_id]['maximum'] = None |
245
|
|
|
base[energy_category_id]['stdev'] = None |
246
|
|
|
base[energy_category_id]['variance'] = None |
247
|
|
|
|
248
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
249
|
|
|
" FROM tbl_equipment_input_category_hourly " |
250
|
|
|
" WHERE equipment_id = %s " |
251
|
|
|
" AND energy_category_id = %s " |
252
|
|
|
" AND start_datetime_utc >= %s " |
253
|
|
|
" AND start_datetime_utc < %s " |
254
|
|
|
" ORDER BY start_datetime_utc ", |
255
|
|
|
(equipment['id'], |
256
|
|
|
energy_category_id, |
257
|
|
|
base_start_datetime_utc, |
258
|
|
|
base_end_datetime_utc)) |
259
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
260
|
|
|
|
261
|
|
|
rows_equipment_periodically, \ |
262
|
|
|
base[energy_category_id]['mean'], \ |
263
|
|
|
base[energy_category_id]['median'], \ |
264
|
|
|
base[energy_category_id]['minimum'], \ |
265
|
|
|
base[energy_category_id]['maximum'], \ |
266
|
|
|
base[energy_category_id]['stdev'], \ |
267
|
|
|
base[energy_category_id]['variance'] = \ |
268
|
|
|
utilities.statistics_hourly_data_by_period(rows_equipment_hourly, |
269
|
|
|
base_start_datetime_utc, |
270
|
|
|
base_end_datetime_utc, |
271
|
|
|
period_type) |
272
|
|
|
|
273
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
274
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
275
|
|
|
timedelta(minutes=timezone_offset) |
276
|
|
|
if period_type == 'hourly': |
277
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
278
|
|
|
elif period_type == 'daily': |
279
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
280
|
|
|
elif period_type == 'monthly': |
281
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
282
|
|
|
elif period_type == 'yearly': |
283
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
284
|
|
|
|
285
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
286
|
|
|
else row_equipment_periodically[1] |
287
|
|
|
base[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
288
|
|
|
base[energy_category_id]['values'].append(actual_value) |
289
|
|
|
base[energy_category_id]['subtotal'] += actual_value |
290
|
|
|
|
291
|
|
|
################################################################################################################ |
292
|
|
|
# Step 6: query reporting period energy input |
293
|
|
|
################################################################################################################ |
294
|
|
|
reporting = dict() |
295
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
296
|
|
|
for energy_category_id in energy_category_set: |
297
|
|
|
reporting[energy_category_id] = dict() |
298
|
|
|
reporting[energy_category_id]['timestamps'] = list() |
299
|
|
|
reporting[energy_category_id]['values'] = list() |
300
|
|
|
reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
301
|
|
|
reporting[energy_category_id]['mean'] = None |
302
|
|
|
reporting[energy_category_id]['median'] = None |
303
|
|
|
reporting[energy_category_id]['minimum'] = None |
304
|
|
|
reporting[energy_category_id]['maximum'] = None |
305
|
|
|
reporting[energy_category_id]['stdev'] = None |
306
|
|
|
reporting[energy_category_id]['variance'] = None |
307
|
|
|
|
308
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
309
|
|
|
" FROM tbl_equipment_input_category_hourly " |
310
|
|
|
" WHERE equipment_id = %s " |
311
|
|
|
" AND energy_category_id = %s " |
312
|
|
|
" AND start_datetime_utc >= %s " |
313
|
|
|
" AND start_datetime_utc < %s " |
314
|
|
|
" ORDER BY start_datetime_utc ", |
315
|
|
|
(equipment['id'], |
316
|
|
|
energy_category_id, |
317
|
|
|
reporting_start_datetime_utc, |
318
|
|
|
reporting_end_datetime_utc)) |
319
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
320
|
|
|
|
321
|
|
|
rows_equipment_periodically, \ |
322
|
|
|
reporting[energy_category_id]['mean'], \ |
323
|
|
|
reporting[energy_category_id]['median'], \ |
324
|
|
|
reporting[energy_category_id]['minimum'], \ |
325
|
|
|
reporting[energy_category_id]['maximum'], \ |
326
|
|
|
reporting[energy_category_id]['stdev'], \ |
327
|
|
|
reporting[energy_category_id]['variance'] = \ |
328
|
|
|
utilities.statistics_hourly_data_by_period(rows_equipment_hourly, |
329
|
|
|
reporting_start_datetime_utc, |
330
|
|
|
reporting_end_datetime_utc, |
331
|
|
|
period_type) |
332
|
|
|
|
333
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
334
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
335
|
|
|
timedelta(minutes=timezone_offset) |
336
|
|
|
if period_type == 'hourly': |
337
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
338
|
|
|
elif period_type == 'daily': |
339
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
340
|
|
|
elif period_type == 'monthly': |
341
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
342
|
|
|
elif period_type == 'yearly': |
343
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
344
|
|
|
|
345
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
346
|
|
|
else row_equipment_periodically[1] |
347
|
|
|
reporting[energy_category_id]['timestamps'].append(current_datetime) |
348
|
|
|
reporting[energy_category_id]['values'].append(actual_value) |
349
|
|
|
reporting[energy_category_id]['subtotal'] += actual_value |
350
|
|
|
|
351
|
|
|
################################################################################################################ |
352
|
|
|
# Step 7: query tariff data |
353
|
|
|
################################################################################################################ |
354
|
|
|
parameters_data = dict() |
355
|
|
|
parameters_data['names'] = list() |
356
|
|
|
parameters_data['timestamps'] = list() |
357
|
|
|
parameters_data['values'] = list() |
358
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
359
|
|
|
for energy_category_id in energy_category_set: |
360
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(equipment['cost_center_id'], |
361
|
|
|
energy_category_id, |
362
|
|
|
reporting_start_datetime_utc, |
363
|
|
|
reporting_end_datetime_utc) |
364
|
|
|
tariff_timestamp_list = list() |
365
|
|
|
tariff_value_list = list() |
366
|
|
|
for k, v in energy_category_tariff_dict.items(): |
367
|
|
|
# convert k from utc to local |
368
|
|
|
k = k + timedelta(minutes=timezone_offset) |
369
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
370
|
|
|
tariff_value_list.append(v) |
371
|
|
|
|
372
|
|
|
parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
373
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
374
|
|
|
parameters_data['values'].append(tariff_value_list) |
375
|
|
|
|
376
|
|
|
################################################################################################################ |
377
|
|
|
# Step 8: query associated points data |
378
|
|
|
################################################################################################################ |
379
|
|
|
for point in point_list: |
380
|
|
|
point_values = [] |
381
|
|
|
point_timestamps = [] |
382
|
|
|
if point['object_type'] == 'ANALOG_VALUE': |
383
|
|
|
query = (" SELECT utc_date_time, actual_value " |
384
|
|
|
" FROM tbl_analog_value " |
385
|
|
|
" WHERE point_id = %s " |
386
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
387
|
|
|
" ORDER BY utc_date_time ") |
388
|
|
|
cursor_historical.execute(query, (point['id'], |
389
|
|
|
reporting_start_datetime_utc, |
390
|
|
|
reporting_end_datetime_utc)) |
391
|
|
|
rows = cursor_historical.fetchall() |
392
|
|
|
|
393
|
|
|
if rows is not None and len(rows) > 0: |
394
|
|
|
for row in rows: |
395
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
396
|
|
|
timedelta(minutes=timezone_offset) |
397
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
398
|
|
|
point_timestamps.append(current_datetime) |
399
|
|
|
point_values.append(row[1]) |
400
|
|
|
|
401
|
|
|
elif point['object_type'] == 'ENERGY_VALUE': |
402
|
|
|
query = (" SELECT utc_date_time, actual_value " |
403
|
|
|
" FROM tbl_energy_value " |
404
|
|
|
" WHERE point_id = %s " |
405
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
406
|
|
|
" ORDER BY utc_date_time ") |
407
|
|
|
cursor_historical.execute(query, (point['id'], |
408
|
|
|
reporting_start_datetime_utc, |
409
|
|
|
reporting_end_datetime_utc)) |
410
|
|
|
rows = cursor_historical.fetchall() |
411
|
|
|
|
412
|
|
|
if rows is not None and len(rows) > 0: |
413
|
|
|
for row in rows: |
414
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
415
|
|
|
timedelta(minutes=timezone_offset) |
416
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
417
|
|
|
point_timestamps.append(current_datetime) |
418
|
|
|
point_values.append(row[1]) |
419
|
|
|
elif point['object_type'] == 'DIGITAL_VALUE': |
420
|
|
|
query = (" SELECT utc_date_time, actual_value " |
421
|
|
|
" FROM tbl_digital_value " |
422
|
|
|
" WHERE point_id = %s " |
423
|
|
|
" AND utc_date_time BETWEEN %s AND %s ") |
424
|
|
|
cursor_historical.execute(query, (point['id'], |
425
|
|
|
reporting_start_datetime_utc, |
426
|
|
|
reporting_end_datetime_utc)) |
427
|
|
|
rows = cursor_historical.fetchall() |
428
|
|
|
|
429
|
|
|
if rows is not None and len(rows) > 0: |
430
|
|
|
for row in rows: |
431
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
432
|
|
|
timedelta(minutes=timezone_offset) |
433
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
434
|
|
|
point_timestamps.append(current_datetime) |
435
|
|
|
point_values.append(row[1]) |
436
|
|
|
|
437
|
|
|
parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
438
|
|
|
parameters_data['timestamps'].append(point_timestamps) |
439
|
|
|
parameters_data['values'].append(point_values) |
440
|
|
|
|
441
|
|
|
################################################################################################################ |
442
|
|
|
# Step 9: construct the report |
443
|
|
|
################################################################################################################ |
444
|
|
|
if cursor_system: |
445
|
|
|
cursor_system.close() |
446
|
|
|
if cnx_system: |
447
|
|
|
cnx_system.disconnect() |
448
|
|
|
|
449
|
|
|
if cursor_energy: |
450
|
|
|
cursor_energy.close() |
451
|
|
|
if cnx_energy: |
452
|
|
|
cnx_energy.disconnect() |
453
|
|
|
|
454
|
|
|
result = dict() |
455
|
|
|
|
456
|
|
|
result['equipment'] = dict() |
457
|
|
|
result['equipment']['name'] = equipment['name'] |
458
|
|
|
|
459
|
|
|
result['base_period'] = dict() |
460
|
|
|
result['base_period']['names'] = list() |
461
|
|
|
result['base_period']['units'] = list() |
462
|
|
|
result['base_period']['timestamps'] = list() |
463
|
|
|
result['base_period']['values'] = list() |
464
|
|
|
result['base_period']['subtotals'] = list() |
465
|
|
|
result['base_period']['means'] = list() |
466
|
|
|
result['base_period']['medians'] = list() |
467
|
|
|
result['base_period']['minimums'] = list() |
468
|
|
|
result['base_period']['maximums'] = list() |
469
|
|
|
result['base_period']['stdevs'] = list() |
470
|
|
|
result['base_period']['variances'] = list() |
471
|
|
|
|
472
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
473
|
|
|
for energy_category_id in energy_category_set: |
474
|
|
|
result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
475
|
|
|
result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
476
|
|
|
result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
477
|
|
|
result['base_period']['values'].append(base[energy_category_id]['values']) |
478
|
|
|
result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
479
|
|
|
result['base_period']['means'].append(base[energy_category_id]['mean']) |
480
|
|
|
result['base_period']['medians'].append(base[energy_category_id]['median']) |
481
|
|
|
result['base_period']['minimums'].append(base[energy_category_id]['minimum']) |
482
|
|
|
result['base_period']['maximums'].append(base[energy_category_id]['maximum']) |
483
|
|
|
result['base_period']['stdevs'].append(base[energy_category_id]['stdev']) |
484
|
|
|
result['base_period']['variances'].append(base[energy_category_id]['variance']) |
485
|
|
|
|
486
|
|
|
result['reporting_period'] = dict() |
487
|
|
|
result['reporting_period']['names'] = list() |
488
|
|
|
result['reporting_period']['energy_category_ids'] = list() |
489
|
|
|
result['reporting_period']['units'] = list() |
490
|
|
|
result['reporting_period']['timestamps'] = list() |
491
|
|
|
result['reporting_period']['values'] = list() |
492
|
|
|
result['reporting_period']['subtotals'] = list() |
493
|
|
|
result['reporting_period']['means'] = list() |
494
|
|
|
result['reporting_period']['means_increment_rate'] = list() |
495
|
|
|
result['reporting_period']['medians'] = list() |
496
|
|
|
result['reporting_period']['medians_increment_rate'] = list() |
497
|
|
|
result['reporting_period']['minimums'] = list() |
498
|
|
|
result['reporting_period']['minimums_increment_rate'] = list() |
499
|
|
|
result['reporting_period']['maximums'] = list() |
500
|
|
|
result['reporting_period']['maximums_increment_rate'] = list() |
501
|
|
|
result['reporting_period']['stdevs'] = list() |
502
|
|
|
result['reporting_period']['stdevs_increment_rate'] = list() |
503
|
|
|
result['reporting_period']['variances'] = list() |
504
|
|
|
result['reporting_period']['variances_increment_rate'] = list() |
505
|
|
|
|
506
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
507
|
|
|
for energy_category_id in energy_category_set: |
508
|
|
|
result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
509
|
|
|
result['reporting_period']['energy_category_ids'].append(energy_category_id) |
510
|
|
|
result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
511
|
|
|
result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
512
|
|
|
result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
513
|
|
|
result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
514
|
|
|
result['reporting_period']['means'].append(reporting[energy_category_id]['mean']) |
515
|
|
|
result['reporting_period']['means_increment_rate'].append( |
516
|
|
|
(reporting[energy_category_id]['mean'] - base[energy_category_id]['mean']) / |
517
|
|
|
base[energy_category_id]['mean'] if (base[energy_category_id]['mean'] is not None and |
518
|
|
|
base[energy_category_id]['mean'] > Decimal(0.0)) |
519
|
|
|
else None) |
520
|
|
|
result['reporting_period']['medians'].append(reporting[energy_category_id]['median']) |
521
|
|
|
result['reporting_period']['medians_increment_rate'].append( |
522
|
|
|
(reporting[energy_category_id]['median'] - base[energy_category_id]['median']) / |
523
|
|
|
base[energy_category_id]['median'] if (base[energy_category_id]['median'] is not None and |
524
|
|
|
base[energy_category_id]['median'] > Decimal(0.0)) |
525
|
|
|
else None) |
526
|
|
|
result['reporting_period']['minimums'].append(reporting[energy_category_id]['minimum']) |
527
|
|
|
result['reporting_period']['minimums_increment_rate'].append( |
528
|
|
|
(reporting[energy_category_id]['minimum'] - base[energy_category_id]['minimum']) / |
529
|
|
|
base[energy_category_id]['minimum'] if (base[energy_category_id]['minimum'] is not None and |
530
|
|
|
base[energy_category_id]['minimum'] > Decimal(0.0)) |
531
|
|
|
else None) |
532
|
|
|
result['reporting_period']['maximums'].append(reporting[energy_category_id]['maximum']) |
533
|
|
|
result['reporting_period']['maximums_increment_rate'].append( |
534
|
|
|
(reporting[energy_category_id]['maximum'] - base[energy_category_id]['maximum']) / |
535
|
|
|
base[energy_category_id]['maximum'] if (base[energy_category_id]['maximum'] is not None and |
536
|
|
|
base[energy_category_id]['maximum'] > Decimal(0.0)) |
537
|
|
|
else None) |
538
|
|
|
result['reporting_period']['stdevs'].append(reporting[energy_category_id]['stdev']) |
539
|
|
|
result['reporting_period']['stdevs_increment_rate'].append( |
540
|
|
|
(reporting[energy_category_id]['stdev'] - base[energy_category_id]['stdev']) / |
541
|
|
|
base[energy_category_id]['stdev'] if (base[energy_category_id]['stdev'] is not None and |
542
|
|
|
base[energy_category_id]['stdev'] > Decimal(0.0)) |
543
|
|
|
else None) |
544
|
|
|
result['reporting_period']['variances'].append(reporting[energy_category_id]['variance']) |
545
|
|
|
result['reporting_period']['variances_increment_rate'].append( |
546
|
|
|
(reporting[energy_category_id]['variance'] - base[energy_category_id]['variance']) / |
547
|
|
|
base[energy_category_id]['variance'] if (base[energy_category_id]['variance'] is not None and |
548
|
|
|
base[energy_category_id]['variance'] > Decimal(0.0)) |
549
|
|
|
else None) |
550
|
|
|
|
551
|
|
|
result['parameters'] = { |
552
|
|
|
"names": parameters_data['names'], |
553
|
|
|
"timestamps": parameters_data['timestamps'], |
554
|
|
|
"values": parameters_data['values'] |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
# export result to Excel file and then encode the file to base64 string |
558
|
|
|
result['excel_bytes_base64'] = excelexporters.equipmentstatistics.export(result, |
559
|
|
|
equipment['name'], |
560
|
|
|
reporting_start_datetime_local, |
561
|
|
|
reporting_end_datetime_local, |
562
|
|
|
period_type) |
563
|
|
|
resp.body = json.dumps(result) |
564
|
|
|
|