1
|
|
|
import falcon |
2
|
|
|
import simplejson as json |
3
|
|
|
import mysql.connector |
4
|
|
|
import config |
5
|
|
|
from datetime import datetime, timedelta, timezone |
6
|
|
|
import utilities |
7
|
|
|
from decimal import * |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Reporting: |
11
|
|
|
@staticmethod |
12
|
|
|
def __init__(): |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
|
19
|
|
|
#################################################################################################################### |
20
|
|
|
# PROCEDURES |
21
|
|
|
# Step 1: valid parameters |
22
|
|
|
# Step 2: query the equipment |
23
|
|
|
# Step 3: query energy categories |
24
|
|
|
# Step 4: query associated points |
25
|
|
|
# Step 5: query base period energy input |
26
|
|
|
# Step 6: query base period energy output |
27
|
|
|
# Step 7: query reporting period energy input |
28
|
|
|
# Step 8: query reporting period energy output |
29
|
|
|
# Step 9: query tariff data |
30
|
|
|
# Step 10: query associated sensors and points data |
31
|
|
|
# Step 11: construct the report |
32
|
|
|
#################################################################################################################### |
33
|
|
|
@staticmethod |
34
|
|
|
def on_get(req, resp): |
35
|
|
|
print(req.params) |
36
|
|
|
equipment_id = req.params.get('equipmentid') |
37
|
|
|
period_type = req.params.get('periodtype') |
38
|
|
|
base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
39
|
|
|
base_end_datetime_local = req.params.get('baseperiodenddatetime') |
40
|
|
|
reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
41
|
|
|
reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
42
|
|
|
|
43
|
|
|
################################################################################################################ |
44
|
|
|
# Step 1: valid parameters |
45
|
|
|
################################################################################################################ |
46
|
|
|
if equipment_id is None: |
47
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
48
|
|
|
else: |
49
|
|
|
equipment_id = str.strip(equipment_id) |
50
|
|
|
if not equipment_id.isdigit() or int(equipment_id) <= 0: |
51
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
52
|
|
|
|
53
|
|
|
if period_type is None: |
54
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
55
|
|
|
else: |
56
|
|
|
period_type = str.strip(period_type) |
57
|
|
|
if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
58
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
59
|
|
|
|
60
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
61
|
|
|
if config.utc_offset[0] == '-': |
62
|
|
|
timezone_offset = -timezone_offset |
63
|
|
|
|
64
|
|
|
base_start_datetime_utc = None |
65
|
|
|
if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
66
|
|
|
base_start_datetime_local = str.strip(base_start_datetime_local) |
67
|
|
|
try: |
68
|
|
|
base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
69
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
70
|
|
|
timedelta(minutes=timezone_offset) |
71
|
|
|
except ValueError: |
72
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
73
|
|
|
description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") |
74
|
|
|
|
75
|
|
|
base_end_datetime_utc = None |
76
|
|
|
if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
77
|
|
|
base_end_datetime_local = str.strip(base_end_datetime_local) |
78
|
|
|
try: |
79
|
|
|
base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
80
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
81
|
|
|
timedelta(minutes=timezone_offset) |
82
|
|
|
except ValueError: |
83
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
84
|
|
|
description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") |
85
|
|
|
|
86
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
87
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
88
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
89
|
|
|
description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') |
90
|
|
|
|
91
|
|
|
if reporting_start_datetime_local is None: |
92
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
93
|
|
|
description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
94
|
|
|
else: |
95
|
|
|
reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
96
|
|
|
try: |
97
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
98
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
99
|
|
|
timedelta(minutes=timezone_offset) |
100
|
|
|
except ValueError: |
101
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
102
|
|
|
description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
103
|
|
|
|
104
|
|
|
if reporting_end_datetime_local is None: |
105
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
106
|
|
|
description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
107
|
|
|
else: |
108
|
|
|
reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
109
|
|
|
try: |
110
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
111
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
112
|
|
|
timedelta(minutes=timezone_offset) |
113
|
|
|
except ValueError: |
114
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
115
|
|
|
description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
116
|
|
|
|
117
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
118
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
119
|
|
|
description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
120
|
|
|
|
121
|
|
|
################################################################################################################ |
122
|
|
|
# Step 2: query the equipment |
123
|
|
|
################################################################################################################ |
124
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
125
|
|
|
cursor_system = cnx_system.cursor() |
126
|
|
|
|
127
|
|
|
cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
128
|
|
|
cursor_energy = cnx_energy.cursor() |
129
|
|
|
|
130
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
131
|
|
|
cursor_historical = cnx_historical.cursor() |
132
|
|
|
|
133
|
|
|
cursor_system.execute(" SELECT id, name, cost_center_id " |
134
|
|
|
" FROM tbl_equipments " |
135
|
|
|
" WHERE id = %s ", (equipment_id,)) |
136
|
|
|
row_equipment = cursor_system.fetchone() |
137
|
|
View Code Duplication |
if row_equipment is None: |
|
|
|
|
138
|
|
|
if cursor_system: |
139
|
|
|
cursor_system.close() |
140
|
|
|
if cnx_system: |
141
|
|
|
cnx_system.disconnect() |
142
|
|
|
|
143
|
|
|
if cursor_energy: |
144
|
|
|
cursor_energy.close() |
145
|
|
|
if cnx_energy: |
146
|
|
|
cnx_energy.disconnect() |
147
|
|
|
|
148
|
|
|
if cnx_historical: |
149
|
|
|
cnx_historical.close() |
150
|
|
|
if cursor_historical: |
151
|
|
|
cursor_historical.disconnect() |
152
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.EQUIPMENT_NOT_FOUND') |
153
|
|
|
|
154
|
|
|
equipment = dict() |
155
|
|
|
equipment['id'] = row_equipment[0] |
156
|
|
|
equipment['name'] = row_equipment[1] |
157
|
|
|
equipment['cost_center_id'] = row_equipment[2] |
158
|
|
|
|
159
|
|
|
################################################################################################################ |
160
|
|
|
# Step 3: query input energy categories and output energy categories |
161
|
|
|
################################################################################################################ |
162
|
|
|
energy_category_set_input = set() |
163
|
|
|
energy_category_set_output = set() |
164
|
|
|
# query input energy categories in base period |
165
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
166
|
|
|
" FROM tbl_equipment_input_category_hourly " |
167
|
|
|
" WHERE equipment_id = %s " |
168
|
|
|
" AND start_datetime_utc >= %s " |
169
|
|
|
" AND start_datetime_utc < %s ", |
170
|
|
|
(equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
171
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
172
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
173
|
|
|
for row_energy_category in rows_energy_categories: |
174
|
|
|
energy_category_set_input.add(row_energy_category[0]) |
175
|
|
|
|
176
|
|
|
# query input energy categories in reporting period |
177
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
178
|
|
|
" FROM tbl_equipment_input_category_hourly " |
179
|
|
|
" WHERE equipment_id = %s " |
180
|
|
|
" AND start_datetime_utc >= %s " |
181
|
|
|
" AND start_datetime_utc < %s ", |
182
|
|
|
(equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
183
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
184
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
185
|
|
|
for row_energy_category in rows_energy_categories: |
186
|
|
|
energy_category_set_input.add(row_energy_category[0]) |
187
|
|
|
|
188
|
|
|
# query output energy categories in base period |
189
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
190
|
|
|
" FROM tbl_equipment_output_category_hourly " |
191
|
|
|
" WHERE equipment_id = %s " |
192
|
|
|
" AND start_datetime_utc >= %s " |
193
|
|
|
" AND start_datetime_utc < %s ", |
194
|
|
|
(equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
195
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
196
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
197
|
|
|
for row_energy_category in rows_energy_categories: |
198
|
|
|
energy_category_set_output.add(row_energy_category[0]) |
199
|
|
|
|
200
|
|
|
# query output energy categories in reporting period |
201
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
202
|
|
|
" FROM tbl_equipment_output_category_hourly " |
203
|
|
|
" WHERE equipment_id = %s " |
204
|
|
|
" AND start_datetime_utc >= %s " |
205
|
|
|
" AND start_datetime_utc < %s ", |
206
|
|
|
(equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
207
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
208
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
209
|
|
|
for row_energy_category in rows_energy_categories: |
210
|
|
|
energy_category_set_output.add(row_energy_category[0]) |
211
|
|
|
|
212
|
|
|
# query properties of all energy categories above |
213
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
214
|
|
|
" FROM tbl_energy_categories " |
215
|
|
|
" ORDER BY id ", ) |
216
|
|
|
rows_energy_categories = cursor_system.fetchall() |
217
|
|
|
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
218
|
|
|
if cursor_system: |
219
|
|
|
cursor_system.close() |
220
|
|
|
if cnx_system: |
221
|
|
|
cnx_system.disconnect() |
222
|
|
|
|
223
|
|
|
if cursor_energy: |
224
|
|
|
cursor_energy.close() |
225
|
|
|
if cnx_energy: |
226
|
|
|
cnx_energy.disconnect() |
227
|
|
|
|
228
|
|
|
if cnx_historical: |
229
|
|
|
cnx_historical.close() |
230
|
|
|
if cursor_historical: |
231
|
|
|
cursor_historical.disconnect() |
232
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, |
233
|
|
|
title='API.NOT_FOUND', |
234
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
235
|
|
|
energy_category_dict = dict() |
236
|
|
|
for row_energy_category in rows_energy_categories: |
237
|
|
|
if row_energy_category[0] in energy_category_set_input or \ |
238
|
|
|
row_energy_category[0] in energy_category_set_output: |
239
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
240
|
|
|
"unit_of_measure": row_energy_category[2], |
241
|
|
|
"kgce": row_energy_category[3], |
242
|
|
|
"kgco2e": row_energy_category[4]} |
243
|
|
|
|
244
|
|
|
################################################################################################################ |
245
|
|
|
# Step 4: query associated points |
246
|
|
|
################################################################################################################ |
247
|
|
|
point_list = list() |
248
|
|
|
cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
249
|
|
|
" FROM tbl_equipments e, tbl_equipments_parameters ep, tbl_points p " |
250
|
|
|
" WHERE e.id = %s AND e.id = ep.equipment_id AND ep.parameter_type = 'point' " |
251
|
|
|
" AND ep.point_id = p.id " |
252
|
|
|
" ORDER BY p.id ", (equipment['id'],)) |
253
|
|
|
rows_points = cursor_system.fetchall() |
254
|
|
|
if rows_points is not None and len(rows_points) > 0: |
255
|
|
|
for row in rows_points: |
256
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
257
|
|
|
|
258
|
|
|
################################################################################################################ |
259
|
|
|
# Step 5: query base period energy input |
260
|
|
|
################################################################################################################ |
261
|
|
|
base_input = dict() |
262
|
|
|
if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
263
|
|
|
for energy_category_id in energy_category_set_input: |
264
|
|
|
base_input[energy_category_id] = dict() |
265
|
|
|
base_input[energy_category_id]['timestamps'] = list() |
266
|
|
|
base_input[energy_category_id]['values'] = list() |
267
|
|
|
base_input[energy_category_id]['subtotal'] = Decimal(0.0) |
268
|
|
|
|
269
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
270
|
|
|
" FROM tbl_equipment_input_category_hourly " |
271
|
|
|
" WHERE equipment_id = %s " |
272
|
|
|
" AND energy_category_id = %s " |
273
|
|
|
" AND start_datetime_utc >= %s " |
274
|
|
|
" AND start_datetime_utc < %s " |
275
|
|
|
" ORDER BY start_datetime_utc ", |
276
|
|
|
(equipment['id'], |
277
|
|
|
energy_category_id, |
278
|
|
|
base_start_datetime_utc, |
279
|
|
|
base_end_datetime_utc)) |
280
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
281
|
|
|
|
282
|
|
|
rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
283
|
|
|
base_start_datetime_utc, |
284
|
|
|
base_end_datetime_utc, |
285
|
|
|
period_type) |
286
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
287
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
288
|
|
|
timedelta(minutes=timezone_offset) |
289
|
|
|
if period_type == 'hourly': |
290
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
291
|
|
|
elif period_type == 'daily': |
292
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
293
|
|
|
elif period_type == 'monthly': |
294
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
295
|
|
|
elif period_type == 'yearly': |
296
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
297
|
|
|
|
298
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
299
|
|
|
else row_equipment_periodically[1] |
300
|
|
|
base_input[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
301
|
|
|
base_input[energy_category_id]['values'].append(actual_value) |
302
|
|
|
base_input[energy_category_id]['subtotal'] += actual_value |
303
|
|
|
|
304
|
|
|
################################################################################################################ |
305
|
|
|
# Step 6: query base period energy output |
306
|
|
|
################################################################################################################ |
307
|
|
|
base_output = dict() |
308
|
|
|
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
309
|
|
|
for energy_category_id in energy_category_set_output: |
310
|
|
|
base_output[energy_category_id] = dict() |
311
|
|
|
base_output[energy_category_id]['timestamps'] = list() |
312
|
|
|
base_output[energy_category_id]['values'] = list() |
313
|
|
|
base_output[energy_category_id]['subtotal'] = Decimal(0.0) |
314
|
|
|
|
315
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
316
|
|
|
" FROM tbl_equipment_output_category_hourly " |
317
|
|
|
" WHERE equipment_id = %s " |
318
|
|
|
" AND energy_category_id = %s " |
319
|
|
|
" AND start_datetime_utc >= %s " |
320
|
|
|
" AND start_datetime_utc < %s " |
321
|
|
|
" ORDER BY start_datetime_utc ", |
322
|
|
|
(equipment['id'], |
323
|
|
|
energy_category_id, |
324
|
|
|
base_start_datetime_utc, |
325
|
|
|
base_end_datetime_utc)) |
326
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
327
|
|
|
|
328
|
|
|
rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
329
|
|
|
base_start_datetime_utc, |
330
|
|
|
base_end_datetime_utc, |
331
|
|
|
period_type) |
332
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
333
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
334
|
|
|
timedelta(minutes=timezone_offset) |
335
|
|
|
if period_type == 'hourly': |
336
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
337
|
|
|
elif period_type == 'daily': |
338
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
339
|
|
|
elif period_type == 'monthly': |
340
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
341
|
|
|
elif period_type == 'yearly': |
342
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
343
|
|
|
|
344
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
345
|
|
|
else row_equipment_periodically[1] |
346
|
|
|
base_output[energy_category_id]['timestamps'].append(current_datetime) |
347
|
|
|
base_output[energy_category_id]['values'].append(actual_value) |
348
|
|
|
base_output[energy_category_id]['subtotal'] += actual_value |
349
|
|
|
################################################################################################################ |
350
|
|
|
# Step 7: query reporting period energy input |
351
|
|
|
################################################################################################################ |
352
|
|
|
reporting_input = dict() |
353
|
|
|
if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
354
|
|
|
for energy_category_id in energy_category_set_input: |
355
|
|
|
|
356
|
|
|
reporting_input[energy_category_id] = dict() |
357
|
|
|
reporting_input[energy_category_id]['timestamps'] = list() |
358
|
|
|
reporting_input[energy_category_id]['values'] = list() |
359
|
|
|
reporting_input[energy_category_id]['subtotal'] = Decimal(0.0) |
360
|
|
|
|
361
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
362
|
|
|
" FROM tbl_equipment_input_category_hourly " |
363
|
|
|
" WHERE equipment_id = %s " |
364
|
|
|
" AND energy_category_id = %s " |
365
|
|
|
" AND start_datetime_utc >= %s " |
366
|
|
|
" AND start_datetime_utc < %s " |
367
|
|
|
" ORDER BY start_datetime_utc ", |
368
|
|
|
(equipment['id'], |
369
|
|
|
energy_category_id, |
370
|
|
|
reporting_start_datetime_utc, |
371
|
|
|
reporting_end_datetime_utc)) |
372
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
373
|
|
|
|
374
|
|
|
rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
375
|
|
|
reporting_start_datetime_utc, |
376
|
|
|
reporting_end_datetime_utc, |
377
|
|
|
period_type) |
378
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
379
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
380
|
|
|
timedelta(minutes=timezone_offset) |
381
|
|
|
if period_type == 'hourly': |
382
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
383
|
|
|
elif period_type == 'daily': |
384
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
385
|
|
|
elif period_type == 'monthly': |
386
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
387
|
|
|
elif period_type == 'yearly': |
388
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
389
|
|
|
|
390
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
391
|
|
|
else row_equipment_periodically[1] |
392
|
|
|
reporting_input[energy_category_id]['timestamps'].append(current_datetime) |
393
|
|
|
reporting_input[energy_category_id]['values'].append(actual_value) |
394
|
|
|
reporting_input[energy_category_id]['subtotal'] += actual_value |
395
|
|
|
|
396
|
|
|
################################################################################################################ |
397
|
|
|
# Step 8: query reporting period energy output |
398
|
|
|
################################################################################################################ |
399
|
|
|
reporting_output = dict() |
400
|
|
|
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
401
|
|
|
for energy_category_id in energy_category_set_output: |
402
|
|
|
|
403
|
|
|
reporting_output[energy_category_id] = dict() |
404
|
|
|
reporting_output[energy_category_id]['timestamps'] = list() |
405
|
|
|
reporting_output[energy_category_id]['values'] = list() |
406
|
|
|
reporting_output[energy_category_id]['subtotal'] = Decimal(0.0) |
407
|
|
|
|
408
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
409
|
|
|
" FROM tbl_equipment_output_category_hourly " |
410
|
|
|
" WHERE equipment_id = %s " |
411
|
|
|
" AND energy_category_id = %s " |
412
|
|
|
" AND start_datetime_utc >= %s " |
413
|
|
|
" AND start_datetime_utc < %s " |
414
|
|
|
" ORDER BY start_datetime_utc ", |
415
|
|
|
(equipment['id'], |
416
|
|
|
energy_category_id, |
417
|
|
|
reporting_start_datetime_utc, |
418
|
|
|
reporting_end_datetime_utc)) |
419
|
|
|
rows_equipment_hourly = cursor_energy.fetchall() |
420
|
|
|
|
421
|
|
|
rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
422
|
|
|
reporting_start_datetime_utc, |
423
|
|
|
reporting_end_datetime_utc, |
424
|
|
|
period_type) |
425
|
|
|
for row_equipment_periodically in rows_equipment_periodically: |
426
|
|
|
current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
427
|
|
|
timedelta(minutes=timezone_offset) |
428
|
|
|
if period_type == 'hourly': |
429
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
430
|
|
|
elif period_type == 'daily': |
431
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
432
|
|
|
elif period_type == 'monthly': |
433
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
434
|
|
|
elif period_type == 'yearly': |
435
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
436
|
|
|
|
437
|
|
|
actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
438
|
|
|
else row_equipment_periodically[1] |
439
|
|
|
reporting_output[energy_category_id]['timestamps'].append(current_datetime) |
440
|
|
|
reporting_output[energy_category_id]['values'].append(actual_value) |
441
|
|
|
reporting_output[energy_category_id]['subtotal'] += actual_value |
442
|
|
|
|
443
|
|
|
################################################################################################################ |
444
|
|
|
# Step 9: query tariff data |
445
|
|
|
################################################################################################################ |
446
|
|
|
parameters_data = dict() |
447
|
|
|
parameters_data['names'] = list() |
448
|
|
|
parameters_data['timestamps'] = list() |
449
|
|
|
parameters_data['values'] = list() |
450
|
|
|
if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
451
|
|
|
for energy_category_id in energy_category_set_input: |
452
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(equipment['cost_center_id'], |
453
|
|
|
energy_category_id, |
454
|
|
|
reporting_start_datetime_utc, |
455
|
|
|
reporting_end_datetime_utc) |
456
|
|
|
tariff_timestamp_list = list() |
457
|
|
|
tariff_value_list = list() |
458
|
|
|
for k, v in energy_category_tariff_dict.items(): |
459
|
|
|
# convert k from utc to local |
460
|
|
|
k = k + timedelta(minutes=timezone_offset) |
461
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
462
|
|
|
tariff_value_list.append(v) |
463
|
|
|
|
464
|
|
|
parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
465
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
466
|
|
|
parameters_data['values'].append(tariff_value_list) |
467
|
|
|
|
468
|
|
|
################################################################################################################ |
469
|
|
|
# Step 10: query associated sensors and points data |
470
|
|
|
################################################################################################################ |
471
|
|
|
for point in point_list: |
472
|
|
|
point_values = [] |
473
|
|
|
point_timestamps = [] |
474
|
|
|
if point['object_type'] == 'ANALOG_VALUE': |
475
|
|
|
query = (" SELECT utc_date_time, actual_value " |
476
|
|
|
" FROM tbl_analog_value " |
477
|
|
|
" WHERE point_id = %s " |
478
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
479
|
|
|
" ORDER BY utc_date_time ") |
480
|
|
|
cursor_historical.execute(query, (point['id'], |
481
|
|
|
reporting_start_datetime_utc, |
482
|
|
|
reporting_end_datetime_utc)) |
483
|
|
|
rows = cursor_historical.fetchall() |
484
|
|
|
|
485
|
|
|
if rows is not None and len(rows) > 0: |
486
|
|
|
for row in rows: |
487
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
488
|
|
|
timedelta(minutes=timezone_offset) |
489
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
490
|
|
|
point_timestamps.append(current_datetime) |
491
|
|
|
point_values.append(row[1]) |
492
|
|
|
|
493
|
|
|
elif point['object_type'] == 'ENERGY_VALUE': |
494
|
|
|
query = (" SELECT utc_date_time, actual_value " |
495
|
|
|
" FROM tbl_energy_value " |
496
|
|
|
" WHERE point_id = %s " |
497
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
498
|
|
|
" ORDER BY utc_date_time ") |
499
|
|
|
cursor_historical.execute(query, (point['id'], |
500
|
|
|
reporting_start_datetime_utc, |
501
|
|
|
reporting_end_datetime_utc)) |
502
|
|
|
rows = cursor_historical.fetchall() |
503
|
|
|
|
504
|
|
|
if rows is not None and len(rows) > 0: |
505
|
|
|
for row in rows: |
506
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
507
|
|
|
timedelta(minutes=timezone_offset) |
508
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
509
|
|
|
point_timestamps.append(current_datetime) |
510
|
|
|
point_values.append(row[1]) |
511
|
|
|
elif point['object_type'] == 'DIGITAL_VALUE': |
512
|
|
|
query = (" SELECT utc_date_time, actual_value " |
513
|
|
|
" FROM tbl_digital_value " |
514
|
|
|
" WHERE point_id = %s " |
515
|
|
|
" AND utc_date_time BETWEEN %s AND %s ") |
516
|
|
|
cursor_historical.execute(query, (point['id'], |
517
|
|
|
reporting_start_datetime_utc, |
518
|
|
|
reporting_end_datetime_utc)) |
519
|
|
|
rows = cursor_historical.fetchall() |
520
|
|
|
|
521
|
|
|
if rows is not None and len(rows) > 0: |
522
|
|
|
for row in rows: |
523
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
524
|
|
|
timedelta(minutes=timezone_offset) |
525
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
526
|
|
|
point_timestamps.append(current_datetime) |
527
|
|
|
point_values.append(row[1]) |
528
|
|
|
|
529
|
|
|
parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
530
|
|
|
parameters_data['timestamps'].append(point_timestamps) |
531
|
|
|
parameters_data['values'].append(point_values) |
532
|
|
|
|
533
|
|
|
################################################################################################################ |
534
|
|
|
# Step 11: construct the report |
535
|
|
|
################################################################################################################ |
536
|
|
|
if cursor_system: |
537
|
|
|
cursor_system.close() |
538
|
|
|
if cnx_system: |
539
|
|
|
cnx_system.disconnect() |
540
|
|
|
|
541
|
|
|
if cursor_energy: |
542
|
|
|
cursor_energy.close() |
543
|
|
|
if cnx_energy: |
544
|
|
|
cnx_energy.disconnect() |
545
|
|
|
|
546
|
|
|
result = dict() |
547
|
|
|
|
548
|
|
|
result['equipment'] = dict() |
549
|
|
|
result['equipment']['name'] = equipment['name'] |
550
|
|
|
|
551
|
|
|
result['base_period_input'] = dict() |
552
|
|
|
result['base_period_input']['names'] = list() |
553
|
|
|
result['base_period_input']['units'] = list() |
554
|
|
|
result['base_period_input']['timestamps'] = list() |
555
|
|
|
result['base_period_input']['values'] = list() |
556
|
|
|
result['base_period_input']['subtotals'] = list() |
557
|
|
|
if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
558
|
|
|
for energy_category_id in energy_category_set_input: |
559
|
|
|
result['base_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
560
|
|
|
result['base_period_input']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
561
|
|
|
result['base_period_input']['timestamps'].append(base_input[energy_category_id]['timestamps']) |
562
|
|
|
result['base_period_input']['values'].append(base_input[energy_category_id]['values']) |
563
|
|
|
result['base_period_input']['subtotals'].append(base_input[energy_category_id]['subtotal']) |
564
|
|
|
|
565
|
|
|
result['base_period_output'] = dict() |
566
|
|
|
result['base_period_output']['names'] = list() |
567
|
|
|
result['base_period_output']['units'] = list() |
568
|
|
|
result['base_period_output']['timestamps'] = list() |
569
|
|
|
result['base_period_output']['values'] = list() |
570
|
|
|
result['base_period_output']['subtotals'] = list() |
571
|
|
|
|
572
|
|
|
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
573
|
|
|
for energy_category_id in energy_category_set_output: |
574
|
|
|
result['base_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
575
|
|
|
result['base_period_output']['units'].append( |
576
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
577
|
|
|
result['base_period_output']['timestamps'].append(base_output[energy_category_id]['timestamps']) |
578
|
|
|
result['base_period_output']['values'].append(base_output[energy_category_id]['values']) |
579
|
|
|
result['base_period_output']['subtotals'].append(base_output[energy_category_id]['subtotal']) |
580
|
|
|
|
581
|
|
|
result['base_period_efficiency'] = dict() |
582
|
|
|
result['base_period_efficiency']['names'] = list() |
583
|
|
|
result['base_period_efficiency']['units'] = list() |
584
|
|
|
result['base_period_efficiency']['timestamps'] = list() |
585
|
|
|
result['base_period_efficiency']['values'] = list() |
586
|
|
|
result['base_period_efficiency']['cumulations'] = list() |
587
|
|
|
|
588
|
|
View Code Duplication |
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
|
|
|
589
|
|
|
for energy_category_id_output in energy_category_set_output: |
590
|
|
|
for energy_category_id_input in energy_category_set_input: |
591
|
|
|
result['base_period_efficiency']['names'].append( |
592
|
|
|
energy_category_dict[energy_category_id_output]['name'] + '/' + |
593
|
|
|
energy_category_dict[energy_category_id_input]['name']) |
594
|
|
|
result['base_period_efficiency']['units'].append( |
595
|
|
|
energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
596
|
|
|
energy_category_dict[energy_category_id_input]['unit_of_measure']) |
597
|
|
|
result['base_period_efficiency']['timestamps'].append( |
598
|
|
|
base_output[energy_category_id_output]['timestamps']) |
599
|
|
|
efficiency_values = list() |
600
|
|
|
for i in range(len(base_output[energy_category_id_output]['timestamps'])): |
601
|
|
|
efficiency_values.append((base_output[energy_category_id_output]['values'][i] / |
602
|
|
|
base_input[energy_category_id_input]['values'][i]) |
603
|
|
|
if base_input[energy_category_id_input]['values'][i] > Decimal(0.0) |
604
|
|
|
else None) |
605
|
|
|
result['base_period_efficiency']['values'].append(efficiency_values) |
606
|
|
|
|
607
|
|
|
base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
608
|
|
|
base_input[energy_category_id_input]['subtotal']) if \ |
609
|
|
|
base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
610
|
|
|
result['base_period_efficiency']['cumulations'].append(base_cumulation) |
611
|
|
|
|
612
|
|
|
result['reporting_period_input'] = dict() |
613
|
|
|
result['reporting_period_input']['names'] = list() |
614
|
|
|
result['reporting_period_input']['energy_category_ids'] = list() |
615
|
|
|
result['reporting_period_input']['units'] = list() |
616
|
|
|
result['reporting_period_input']['timestamps'] = list() |
617
|
|
|
result['reporting_period_input']['values'] = list() |
618
|
|
|
result['reporting_period_input']['subtotals'] = list() |
619
|
|
|
result['reporting_period_input']['increment_rates'] = list() |
620
|
|
|
|
621
|
|
View Code Duplication |
if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
|
|
|
622
|
|
|
for energy_category_id in energy_category_set_input: |
623
|
|
|
result['reporting_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
624
|
|
|
result['reporting_period_input']['energy_category_ids'].append(energy_category_id) |
625
|
|
|
result['reporting_period_input']['units'].append( |
626
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
627
|
|
|
result['reporting_period_input']['timestamps'].append( |
628
|
|
|
reporting_input[energy_category_id]['timestamps']) |
629
|
|
|
result['reporting_period_input']['values'].append( |
630
|
|
|
reporting_input[energy_category_id]['values']) |
631
|
|
|
result['reporting_period_input']['subtotals'].append( |
632
|
|
|
reporting_input[energy_category_id]['subtotal']) |
633
|
|
|
result['reporting_period_input']['increment_rates'].append( |
634
|
|
|
(reporting_input[energy_category_id]['subtotal'] - |
635
|
|
|
base_input[energy_category_id]['subtotal']) / |
636
|
|
|
base_input[energy_category_id]['subtotal'] |
637
|
|
|
if base_input[energy_category_id]['subtotal'] > 0.0 else None) |
638
|
|
|
|
639
|
|
|
result['reporting_period_output'] = dict() |
640
|
|
|
result['reporting_period_output']['names'] = list() |
641
|
|
|
result['reporting_period_output']['energy_category_ids'] = list() |
642
|
|
|
result['reporting_period_output']['units'] = list() |
643
|
|
|
result['reporting_period_output']['timestamps'] = list() |
644
|
|
|
result['reporting_period_output']['values'] = list() |
645
|
|
|
result['reporting_period_output']['subtotals'] = list() |
646
|
|
|
result['reporting_period_output']['increment_rates'] = list() |
647
|
|
|
|
648
|
|
View Code Duplication |
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
|
|
|
649
|
|
|
for energy_category_id in energy_category_set_output: |
650
|
|
|
result['reporting_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
651
|
|
|
result['reporting_period_output']['energy_category_ids'].append(energy_category_id) |
652
|
|
|
result['reporting_period_output']['units'].append( |
653
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
654
|
|
|
result['reporting_period_output']['timestamps'].append( |
655
|
|
|
reporting_output[energy_category_id]['timestamps']) |
656
|
|
|
result['reporting_period_output']['values'].append(reporting_output[energy_category_id]['values']) |
657
|
|
|
result['reporting_period_output']['subtotals'].append(reporting_output[energy_category_id]['subtotal']) |
658
|
|
|
result['reporting_period_output']['increment_rates'].append( |
659
|
|
|
(reporting_output[energy_category_id]['subtotal'] - |
660
|
|
|
base_output[energy_category_id]['subtotal']) / |
661
|
|
|
base_output[energy_category_id]['subtotal'] |
662
|
|
|
if base_output[energy_category_id]['subtotal'] > 0.0 else None) |
663
|
|
|
|
664
|
|
|
result['reporting_period_efficiency'] = dict() |
665
|
|
|
result['reporting_period_efficiency']['names'] = list() |
666
|
|
|
result['reporting_period_efficiency']['units'] = list() |
667
|
|
|
result['reporting_period_efficiency']['timestamps'] = list() |
668
|
|
|
result['reporting_period_efficiency']['values'] = list() |
669
|
|
|
result['reporting_period_efficiency']['cumulations'] = list() |
670
|
|
|
result['reporting_period_efficiency']['increment_rates'] = list() |
671
|
|
|
|
672
|
|
View Code Duplication |
if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
|
|
|
673
|
|
|
for energy_category_id_output in energy_category_set_output: |
674
|
|
|
for energy_category_id_input in energy_category_set_input: |
675
|
|
|
result['reporting_period_efficiency']['names'].append( |
676
|
|
|
energy_category_dict[energy_category_id_output]['name'] + '/' + |
677
|
|
|
energy_category_dict[energy_category_id_input]['name']) |
678
|
|
|
result['reporting_period_efficiency']['units'].append( |
679
|
|
|
energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
680
|
|
|
energy_category_dict[energy_category_id_input]['unit_of_measure']) |
681
|
|
|
result['reporting_period_efficiency']['timestamps'].append( |
682
|
|
|
reporting_output[energy_category_id_output]['timestamps']) |
683
|
|
|
efficiency_values = list() |
684
|
|
|
for i in range(len(reporting_output[energy_category_id_output]['timestamps'])): |
685
|
|
|
efficiency_values.append((reporting_output[energy_category_id_output]['values'][i] / |
686
|
|
|
reporting_input[energy_category_id_input]['values'][i]) |
687
|
|
|
if reporting_input[energy_category_id_input]['values'][i] > |
688
|
|
|
Decimal(0.0) else None) |
689
|
|
|
result['reporting_period_efficiency']['values'].append(efficiency_values) |
690
|
|
|
|
691
|
|
|
base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
692
|
|
|
base_input[energy_category_id_input]['subtotal']) if \ |
693
|
|
|
base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
694
|
|
|
|
695
|
|
|
reporting_cumulation = (reporting_output[energy_category_id_output]['subtotal'] / |
696
|
|
|
reporting_input[energy_category_id_input]['subtotal']) if \ |
697
|
|
|
reporting_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
698
|
|
|
|
699
|
|
|
result['reporting_period_efficiency']['cumulations'].append(reporting_cumulation) |
700
|
|
|
result['reporting_period_efficiency']['increment_rates'].append( |
701
|
|
|
((reporting_cumulation - base_cumulation) / base_cumulation if (base_cumulation > Decimal(0.0)) |
702
|
|
|
else None) |
703
|
|
|
) |
704
|
|
|
|
705
|
|
|
result['parameters'] = { |
706
|
|
|
"names": parameters_data['names'], |
707
|
|
|
"timestamps": parameters_data['timestamps'], |
708
|
|
|
"values": parameters_data['values'] |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
resp.body = json.dumps(result) |
712
|
|
|
|