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.spaceenergycategory |
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 space |
24
|
|
|
# Step 3: query energy categories |
25
|
|
|
# Step 4: query associated sensors |
26
|
|
|
# Step 5: query associated points |
27
|
|
|
# Step 6: query child spaces |
28
|
|
|
# Step 7: query base period energy input |
29
|
|
|
# Step 8: query reporting period energy input |
30
|
|
|
# Step 9: query tariff data |
31
|
|
|
# Step 10: query associated sensors and points data |
32
|
|
|
# Step 11: query child spaces energy input |
33
|
|
|
# Step 12: construct the report |
34
|
|
|
#################################################################################################################### |
35
|
|
|
@staticmethod |
36
|
|
|
def on_get(req, resp): |
37
|
|
|
print(req.params) |
38
|
|
|
space_id = req.params.get('spaceid') |
39
|
|
|
period_type = req.params.get('periodtype') |
40
|
|
|
base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
41
|
|
|
base_end_datetime_local = req.params.get('baseperiodenddatetime') |
42
|
|
|
reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
43
|
|
|
reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
44
|
|
|
|
45
|
|
|
################################################################################################################ |
46
|
|
|
# Step 1: valid parameters |
47
|
|
|
################################################################################################################ |
48
|
|
|
if space_id is None: |
49
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
50
|
|
|
else: |
51
|
|
|
space_id = str.strip(space_id) |
52
|
|
|
if not space_id.isdigit() or int(space_id) <= 0: |
53
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
54
|
|
|
|
55
|
|
|
if period_type is None: |
56
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
57
|
|
|
else: |
58
|
|
|
period_type = str.strip(period_type) |
59
|
|
|
if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
60
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
61
|
|
|
|
62
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
63
|
|
|
if config.utc_offset[0] == '-': |
64
|
|
|
timezone_offset = -timezone_offset |
65
|
|
|
|
66
|
|
|
base_start_datetime_utc = None |
67
|
|
|
if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
68
|
|
|
base_start_datetime_local = str.strip(base_start_datetime_local) |
69
|
|
|
try: |
70
|
|
|
base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
71
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
72
|
|
|
timedelta(minutes=timezone_offset) |
73
|
|
|
except ValueError: |
74
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
75
|
|
|
description="API.INVALID_BASE_PERIOD_START_DATETIME") |
76
|
|
|
|
77
|
|
|
base_end_datetime_utc = None |
78
|
|
|
if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
79
|
|
|
base_end_datetime_local = str.strip(base_end_datetime_local) |
80
|
|
|
try: |
81
|
|
|
base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
82
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
83
|
|
|
timedelta(minutes=timezone_offset) |
84
|
|
|
except ValueError: |
85
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
86
|
|
|
description="API.INVALID_BASE_PERIOD_END_DATETIME") |
87
|
|
|
|
88
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
89
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
90
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
91
|
|
|
description='API.INVALID_BASE_PERIOD_END_DATETIME') |
92
|
|
|
|
93
|
|
|
if reporting_start_datetime_local is None: |
94
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
95
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
96
|
|
|
else: |
97
|
|
|
reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
98
|
|
|
try: |
99
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
100
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
101
|
|
|
timedelta(minutes=timezone_offset) |
102
|
|
|
except ValueError: |
103
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
104
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
105
|
|
|
|
106
|
|
|
if reporting_end_datetime_local is None: |
107
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
108
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
109
|
|
|
else: |
110
|
|
|
reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
111
|
|
|
try: |
112
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
113
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
114
|
|
|
timedelta(minutes=timezone_offset) |
115
|
|
|
except ValueError: |
116
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
117
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
118
|
|
|
|
119
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
120
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
121
|
|
|
description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
122
|
|
|
|
123
|
|
|
################################################################################################################ |
124
|
|
|
# Step 2: query the space |
125
|
|
|
################################################################################################################ |
126
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
127
|
|
|
cursor_system = cnx_system.cursor() |
128
|
|
|
|
129
|
|
|
cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
130
|
|
|
cursor_energy = cnx_energy.cursor() |
131
|
|
|
|
132
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
133
|
|
|
cursor_historical = cnx_historical.cursor() |
134
|
|
|
|
135
|
|
|
cursor_system.execute(" SELECT id, name, area, cost_center_id " |
136
|
|
|
" FROM tbl_spaces " |
137
|
|
|
" WHERE id = %s ", (space_id,)) |
138
|
|
|
row_space = cursor_system.fetchone() |
139
|
|
|
if row_space is None: |
140
|
|
|
if cursor_system: |
141
|
|
|
cursor_system.close() |
142
|
|
|
if cnx_system: |
143
|
|
|
cnx_system.disconnect() |
144
|
|
|
|
145
|
|
|
if cursor_energy: |
146
|
|
|
cursor_energy.close() |
147
|
|
|
if cnx_energy: |
148
|
|
|
cnx_energy.disconnect() |
149
|
|
|
|
150
|
|
|
if cnx_historical: |
151
|
|
|
cnx_historical.close() |
152
|
|
|
if cursor_historical: |
153
|
|
|
cursor_historical.disconnect() |
154
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SPACE_NOT_FOUND') |
155
|
|
|
|
156
|
|
|
space = dict() |
157
|
|
|
space['id'] = row_space[0] |
158
|
|
|
space['name'] = row_space[1] |
159
|
|
|
space['area'] = row_space[2] |
160
|
|
|
space['cost_center_id'] = row_space[3] |
161
|
|
|
|
162
|
|
|
################################################################################################################ |
163
|
|
|
# Step 3: query energy categories |
164
|
|
|
################################################################################################################ |
165
|
|
|
energy_category_set = set() |
166
|
|
|
# query energy categories in base period |
167
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
168
|
|
|
" FROM tbl_space_input_category_hourly " |
169
|
|
|
" WHERE space_id = %s " |
170
|
|
|
" AND start_datetime_utc >= %s " |
171
|
|
|
" AND start_datetime_utc < %s ", |
172
|
|
|
(space['id'], base_start_datetime_utc, base_end_datetime_utc)) |
173
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
174
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
175
|
|
|
for row_energy_category in rows_energy_categories: |
176
|
|
|
energy_category_set.add(row_energy_category[0]) |
177
|
|
|
|
178
|
|
|
# query energy categories in reporting period |
179
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
180
|
|
|
" FROM tbl_space_input_category_hourly " |
181
|
|
|
" WHERE space_id = %s " |
182
|
|
|
" AND start_datetime_utc >= %s " |
183
|
|
|
" AND start_datetime_utc < %s ", |
184
|
|
|
(space['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
185
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
186
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
187
|
|
|
for row_energy_category in rows_energy_categories: |
188
|
|
|
energy_category_set.add(row_energy_category[0]) |
189
|
|
|
|
190
|
|
|
# query all energy categories in base period and reporting period |
191
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
192
|
|
|
" FROM tbl_energy_categories " |
193
|
|
|
" ORDER BY id ", ) |
194
|
|
|
rows_energy_categories = cursor_system.fetchall() |
195
|
|
|
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
196
|
|
|
if cursor_system: |
197
|
|
|
cursor_system.close() |
198
|
|
|
if cnx_system: |
199
|
|
|
cnx_system.disconnect() |
200
|
|
|
|
201
|
|
|
if cursor_energy: |
202
|
|
|
cursor_energy.close() |
203
|
|
|
if cnx_energy: |
204
|
|
|
cnx_energy.disconnect() |
205
|
|
|
|
206
|
|
|
if cnx_historical: |
207
|
|
|
cnx_historical.close() |
208
|
|
|
if cursor_historical: |
209
|
|
|
cursor_historical.disconnect() |
210
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, |
211
|
|
|
title='API.NOT_FOUND', |
212
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
213
|
|
|
energy_category_dict = dict() |
214
|
|
|
for row_energy_category in rows_energy_categories: |
215
|
|
|
if row_energy_category[0] in energy_category_set: |
216
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
217
|
|
|
"unit_of_measure": row_energy_category[2], |
218
|
|
|
"kgce": row_energy_category[3], |
219
|
|
|
"kgco2e": row_energy_category[4]} |
220
|
|
|
|
221
|
|
|
################################################################################################################ |
222
|
|
|
# Step 4: query associated sensors |
223
|
|
|
################################################################################################################ |
224
|
|
|
point_list = list() |
225
|
|
|
cursor_system.execute(" SELECT po.id, po.name, po.units, po.object_type " |
226
|
|
|
" FROM tbl_spaces sp, tbl_sensors se, tbl_spaces_sensors spse, " |
227
|
|
|
" tbl_points po, tbl_sensors_points sepo " |
228
|
|
|
" WHERE sp.id = %s AND sp.id = spse.space_id AND spse.sensor_id = se.id " |
229
|
|
|
" AND se.id = sepo.sensor_id AND sepo.point_id = po.id " |
230
|
|
|
" ORDER BY po.id ", (space['id'], )) |
231
|
|
|
rows_points = cursor_system.fetchall() |
232
|
|
|
if rows_points is not None and len(rows_points) > 0: |
233
|
|
|
for row in rows_points: |
234
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
235
|
|
|
|
236
|
|
|
################################################################################################################ |
237
|
|
|
# Step 5: query associated points |
238
|
|
|
################################################################################################################ |
239
|
|
|
cursor_system.execute(" SELECT po.id, po.name, po.units, po.object_type " |
240
|
|
|
" FROM tbl_spaces sp, tbl_spaces_points sppo, tbl_points po " |
241
|
|
|
" WHERE sp.id = %s AND sp.id = sppo.space_id AND sppo.point_id = po.id " |
242
|
|
|
" ORDER BY po.id ", (space['id'], )) |
243
|
|
|
rows_points = cursor_system.fetchall() |
244
|
|
|
if rows_points is not None and len(rows_points) > 0: |
245
|
|
|
for row in rows_points: |
246
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
247
|
|
|
|
248
|
|
|
################################################################################################################ |
249
|
|
|
# Step 6: query child spaces |
250
|
|
|
################################################################################################################ |
251
|
|
|
child_space_list = list() |
252
|
|
|
cursor_system.execute(" SELECT id, name " |
253
|
|
|
" FROM tbl_spaces " |
254
|
|
|
" WHERE parent_space_id = %s " |
255
|
|
|
" ORDER BY id ", (space['id'], )) |
256
|
|
|
rows_child_spaces = cursor_system.fetchall() |
257
|
|
|
if rows_child_spaces is not None and len(rows_child_spaces) > 0: |
258
|
|
|
for row in rows_child_spaces: |
259
|
|
|
child_space_list.append({"id": row[0], "name": row[1]}) |
260
|
|
|
|
261
|
|
|
################################################################################################################ |
262
|
|
|
# Step 7: query base period energy input |
263
|
|
|
################################################################################################################ |
264
|
|
|
base = dict() |
265
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
266
|
|
|
for energy_category_id in energy_category_set: |
267
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
268
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
269
|
|
|
|
270
|
|
|
base[energy_category_id] = dict() |
271
|
|
|
base[energy_category_id]['timestamps'] = list() |
272
|
|
|
base[energy_category_id]['values'] = list() |
273
|
|
|
base[energy_category_id]['subtotal'] = Decimal(0.0) |
274
|
|
|
base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
275
|
|
|
base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
276
|
|
|
|
277
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
278
|
|
|
" FROM tbl_space_input_category_hourly " |
279
|
|
|
" WHERE space_id = %s " |
280
|
|
|
" AND energy_category_id = %s " |
281
|
|
|
" AND start_datetime_utc >= %s " |
282
|
|
|
" AND start_datetime_utc < %s " |
283
|
|
|
" ORDER BY start_datetime_utc ", |
284
|
|
|
(space['id'], |
285
|
|
|
energy_category_id, |
286
|
|
|
base_start_datetime_utc, |
287
|
|
|
base_end_datetime_utc)) |
288
|
|
|
rows_space_hourly = cursor_energy.fetchall() |
289
|
|
|
|
290
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
291
|
|
|
base_start_datetime_utc, |
292
|
|
|
base_end_datetime_utc, |
293
|
|
|
period_type) |
294
|
|
|
for row_space_periodically in rows_space_periodically: |
295
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
296
|
|
|
timedelta(minutes=timezone_offset) |
297
|
|
|
if period_type == 'hourly': |
298
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
299
|
|
|
elif period_type == 'daily': |
300
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
301
|
|
|
elif period_type == 'monthly': |
302
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
303
|
|
|
elif period_type == 'yearly': |
304
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
305
|
|
|
|
306
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
307
|
|
|
base[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
308
|
|
|
base[energy_category_id]['values'].append(actual_value) |
309
|
|
|
base[energy_category_id]['subtotal'] += actual_value |
310
|
|
|
base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
311
|
|
|
base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
312
|
|
|
|
313
|
|
|
################################################################################################################ |
314
|
|
|
# Step 8: query reporting period energy input |
315
|
|
|
################################################################################################################ |
316
|
|
|
reporting = dict() |
317
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
318
|
|
|
for energy_category_id in energy_category_set: |
319
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
320
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
321
|
|
|
|
322
|
|
|
reporting[energy_category_id] = dict() |
323
|
|
|
reporting[energy_category_id]['timestamps'] = list() |
324
|
|
|
reporting[energy_category_id]['values'] = list() |
325
|
|
|
reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
326
|
|
|
reporting[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
327
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
328
|
|
|
reporting[energy_category_id]['toppeak'] = Decimal(0.0) |
329
|
|
|
reporting[energy_category_id]['onpeak'] = Decimal(0.0) |
330
|
|
|
reporting[energy_category_id]['midpeak'] = Decimal(0.0) |
331
|
|
|
reporting[energy_category_id]['offpeak'] = Decimal(0.0) |
332
|
|
|
|
333
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
334
|
|
|
" FROM tbl_space_input_category_hourly " |
335
|
|
|
" WHERE space_id = %s " |
336
|
|
|
" AND energy_category_id = %s " |
337
|
|
|
" AND start_datetime_utc >= %s " |
338
|
|
|
" AND start_datetime_utc < %s " |
339
|
|
|
" ORDER BY start_datetime_utc ", |
340
|
|
|
(space['id'], |
341
|
|
|
energy_category_id, |
342
|
|
|
reporting_start_datetime_utc, |
343
|
|
|
reporting_end_datetime_utc)) |
344
|
|
|
rows_space_hourly = cursor_energy.fetchall() |
345
|
|
|
|
346
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
347
|
|
|
reporting_start_datetime_utc, |
348
|
|
|
reporting_end_datetime_utc, |
349
|
|
|
period_type) |
350
|
|
|
for row_space_periodically in rows_space_periodically: |
351
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
352
|
|
|
timedelta(minutes=timezone_offset) |
353
|
|
|
if period_type == 'hourly': |
354
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
355
|
|
|
elif period_type == 'daily': |
356
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
357
|
|
|
elif period_type == 'monthly': |
358
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
359
|
|
|
elif period_type == 'yearly': |
360
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
361
|
|
|
|
362
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
363
|
|
|
reporting[energy_category_id]['timestamps'].append(current_datetime) |
364
|
|
|
reporting[energy_category_id]['values'].append(actual_value) |
365
|
|
|
reporting[energy_category_id]['subtotal'] += actual_value |
366
|
|
|
reporting[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
367
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
368
|
|
|
|
369
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_peak_types(space['cost_center_id'], |
370
|
|
|
energy_category_id, |
371
|
|
|
reporting_start_datetime_utc, |
372
|
|
|
reporting_end_datetime_utc) |
373
|
|
|
for row in rows_space_hourly: |
374
|
|
|
peak_type = energy_category_tariff_dict.get(row[0], None) |
375
|
|
|
if peak_type == 'toppeak': |
376
|
|
|
reporting[energy_category_id]['toppeak'] += row[1] |
377
|
|
|
elif peak_type == 'onpeak': |
378
|
|
|
reporting[energy_category_id]['onpeak'] += row[1] |
379
|
|
|
elif peak_type == 'midpeak': |
380
|
|
|
reporting[energy_category_id]['midpeak'] += row[1] |
381
|
|
|
elif peak_type == 'offpeak': |
382
|
|
|
reporting[energy_category_id]['offpeak'] += row[1] |
383
|
|
|
|
384
|
|
|
################################################################################################################ |
385
|
|
|
# Step 9: query tariff data |
386
|
|
|
################################################################################################################ |
387
|
|
|
parameters_data = dict() |
388
|
|
|
parameters_data['names'] = list() |
389
|
|
|
parameters_data['timestamps'] = list() |
390
|
|
|
parameters_data['values'] = list() |
391
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
392
|
|
|
for energy_category_id in energy_category_set: |
393
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(space['cost_center_id'], |
394
|
|
|
energy_category_id, |
395
|
|
|
reporting_start_datetime_utc, |
396
|
|
|
reporting_end_datetime_utc) |
397
|
|
|
tariff_timestamp_list = list() |
398
|
|
|
tariff_value_list = list() |
399
|
|
|
for k, v in energy_category_tariff_dict.items(): |
400
|
|
|
# convert k from utc to local |
401
|
|
|
k = k + timedelta(minutes=timezone_offset) |
402
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
403
|
|
|
tariff_value_list.append(v) |
404
|
|
|
|
405
|
|
|
parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
406
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
407
|
|
|
parameters_data['values'].append(tariff_value_list) |
408
|
|
|
|
409
|
|
|
################################################################################################################ |
410
|
|
|
# Step 10: query associated sensors and points data |
411
|
|
|
################################################################################################################ |
412
|
|
|
for point in point_list: |
413
|
|
|
point_values = [] |
414
|
|
|
point_timestamps = [] |
415
|
|
|
if point['object_type'] == 'ANALOG_VALUE': |
416
|
|
|
query = (" SELECT utc_date_time, actual_value " |
417
|
|
|
" FROM tbl_analog_value " |
418
|
|
|
" WHERE point_id = %s " |
419
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
420
|
|
|
" ORDER BY utc_date_time ") |
421
|
|
|
cursor_historical.execute(query, (point['id'], |
422
|
|
|
reporting_start_datetime_utc, |
423
|
|
|
reporting_end_datetime_utc)) |
424
|
|
|
rows = cursor_historical.fetchall() |
425
|
|
|
|
426
|
|
|
if rows is not None and len(rows) > 0: |
427
|
|
|
for row in rows: |
428
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
429
|
|
|
timedelta(minutes=timezone_offset) |
430
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
431
|
|
|
point_timestamps.append(current_datetime) |
432
|
|
|
point_values.append(row[1]) |
433
|
|
|
|
434
|
|
|
elif point['object_type'] == 'ENERGY_VALUE': |
435
|
|
|
query = (" SELECT utc_date_time, actual_value " |
436
|
|
|
" FROM tbl_energy_value " |
437
|
|
|
" WHERE point_id = %s " |
438
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
439
|
|
|
" ORDER BY utc_date_time ") |
440
|
|
|
cursor_historical.execute(query, (point['id'], |
441
|
|
|
reporting_start_datetime_utc, |
442
|
|
|
reporting_end_datetime_utc)) |
443
|
|
|
rows = cursor_historical.fetchall() |
444
|
|
|
|
445
|
|
|
if rows is not None and len(rows) > 0: |
446
|
|
|
for row in rows: |
447
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
448
|
|
|
timedelta(minutes=timezone_offset) |
449
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
450
|
|
|
point_timestamps.append(current_datetime) |
451
|
|
|
point_values.append(row[1]) |
452
|
|
|
elif point['object_type'] == 'DIGITAL_VALUE': |
453
|
|
|
query = (" SELECT utc_date_time, actual_value " |
454
|
|
|
" FROM tbl_digital_value " |
455
|
|
|
" WHERE point_id = %s " |
456
|
|
|
" AND utc_date_time BETWEEN %s AND %s ") |
457
|
|
|
cursor_historical.execute(query, (point['id'], |
458
|
|
|
reporting_start_datetime_utc, |
459
|
|
|
reporting_end_datetime_utc)) |
460
|
|
|
rows = cursor_historical.fetchall() |
461
|
|
|
|
462
|
|
|
if rows is not None and len(rows) > 0: |
463
|
|
|
for row in rows: |
464
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
465
|
|
|
timedelta(minutes=timezone_offset) |
466
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
467
|
|
|
point_timestamps.append(current_datetime) |
468
|
|
|
point_values.append(row[1]) |
469
|
|
|
|
470
|
|
|
parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
471
|
|
|
parameters_data['timestamps'].append(point_timestamps) |
472
|
|
|
parameters_data['values'].append(point_values) |
473
|
|
|
|
474
|
|
|
################################################################################################################ |
475
|
|
|
# Step 11: query child spaces energy input |
476
|
|
|
################################################################################################################ |
477
|
|
|
child_space_data = dict() |
478
|
|
|
|
479
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
480
|
|
|
for energy_category_id in energy_category_set: |
481
|
|
|
child_space_data[energy_category_id] = dict() |
482
|
|
|
child_space_data[energy_category_id]['child_space_names'] = list() |
483
|
|
|
child_space_data[energy_category_id]['subtotals'] = list() |
484
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgce'] = list() |
485
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgco2e'] = list() |
486
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
487
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
488
|
|
|
for child_space in child_space_list: |
489
|
|
|
child_space_data[energy_category_id]['child_space_names'].append(child_space['name']) |
490
|
|
|
|
491
|
|
|
cursor_energy.execute(" SELECT SUM(actual_value) " |
492
|
|
|
" FROM tbl_space_input_category_hourly " |
493
|
|
|
" WHERE space_id = %s " |
494
|
|
|
" AND energy_category_id = %s " |
495
|
|
|
" AND start_datetime_utc >= %s " |
496
|
|
|
" AND start_datetime_utc < %s " |
497
|
|
|
" ORDER BY start_datetime_utc ", |
498
|
|
|
(child_space['id'], |
499
|
|
|
energy_category_id, |
500
|
|
|
reporting_start_datetime_utc, |
501
|
|
|
reporting_end_datetime_utc)) |
502
|
|
|
row_subtotal = cursor_energy.fetchone() |
503
|
|
|
|
504
|
|
|
subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0] |
505
|
|
|
child_space_data[energy_category_id]['subtotals'].append(subtotal) |
506
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgce'].append(subtotal * kgce) |
507
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgco2e'].append(subtotal * kgco2e) |
508
|
|
|
|
509
|
|
|
################################################################################################################ |
510
|
|
|
# Step 12: construct the report |
511
|
|
|
################################################################################################################ |
512
|
|
|
if cursor_system: |
513
|
|
|
cursor_system.close() |
514
|
|
|
if cnx_system: |
515
|
|
|
cnx_system.disconnect() |
516
|
|
|
|
517
|
|
|
if cursor_energy: |
518
|
|
|
cursor_energy.close() |
519
|
|
|
if cnx_energy: |
520
|
|
|
cnx_energy.disconnect() |
521
|
|
|
|
522
|
|
|
result = dict() |
523
|
|
|
|
524
|
|
|
result['space'] = dict() |
525
|
|
|
result['space']['name'] = space['name'] |
526
|
|
|
result['space']['area'] = space['area'] |
527
|
|
|
|
528
|
|
|
result['base_period'] = dict() |
529
|
|
|
result['base_period']['names'] = list() |
530
|
|
|
result['base_period']['units'] = list() |
531
|
|
|
result['base_period']['timestamps'] = list() |
532
|
|
|
result['base_period']['values'] = list() |
533
|
|
|
result['base_period']['subtotals'] = list() |
534
|
|
|
result['base_period']['subtotals_in_kgce'] = list() |
535
|
|
|
result['base_period']['subtotals_in_kgco2e'] = list() |
536
|
|
|
result['base_period']['total_in_kgce'] = Decimal(0.0) |
537
|
|
|
result['base_period']['total_in_kgco2e'] = Decimal(0.0) |
538
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
539
|
|
|
for energy_category_id in energy_category_set: |
540
|
|
|
result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
541
|
|
|
result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
542
|
|
|
result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
543
|
|
|
result['base_period']['values'].append(base[energy_category_id]['values']) |
544
|
|
|
result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
545
|
|
|
result['base_period']['subtotals_in_kgce'].append(base[energy_category_id]['subtotal_in_kgce']) |
546
|
|
|
result['base_period']['subtotals_in_kgco2e'].append(base[energy_category_id]['subtotal_in_kgco2e']) |
547
|
|
|
result['base_period']['total_in_kgce'] += base[energy_category_id]['subtotal_in_kgce'] |
548
|
|
|
result['base_period']['total_in_kgco2e'] += base[energy_category_id]['subtotal_in_kgco2e'] |
549
|
|
|
|
550
|
|
|
result['reporting_period'] = dict() |
551
|
|
|
result['reporting_period']['names'] = list() |
552
|
|
|
result['reporting_period']['energy_category_ids'] = list() |
553
|
|
|
result['reporting_period']['units'] = list() |
554
|
|
|
result['reporting_period']['timestamps'] = list() |
555
|
|
|
result['reporting_period']['values'] = list() |
556
|
|
|
result['reporting_period']['subtotals'] = list() |
557
|
|
|
result['reporting_period']['subtotals_in_kgce'] = list() |
558
|
|
|
result['reporting_period']['subtotals_in_kgco2e'] = list() |
559
|
|
|
result['reporting_period']['subtotals_per_unit_area'] = list() |
560
|
|
|
result['reporting_period']['toppeaks'] = list() |
561
|
|
|
result['reporting_period']['onpeaks'] = list() |
562
|
|
|
result['reporting_period']['midpeaks'] = list() |
563
|
|
|
result['reporting_period']['offpeaks'] = list() |
564
|
|
|
result['reporting_period']['increment_rates'] = list() |
565
|
|
|
result['reporting_period']['total_in_kgce'] = Decimal(0.0) |
566
|
|
|
result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) |
567
|
|
|
result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) |
568
|
|
|
result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) |
569
|
|
|
|
570
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
571
|
|
|
for energy_category_id in energy_category_set: |
572
|
|
|
result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
573
|
|
|
result['reporting_period']['energy_category_ids'].append(energy_category_id) |
574
|
|
|
result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
575
|
|
|
result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
576
|
|
|
result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
577
|
|
|
result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
578
|
|
|
result['reporting_period']['subtotals_in_kgce'].append( |
579
|
|
|
reporting[energy_category_id]['subtotal_in_kgce']) |
580
|
|
|
result['reporting_period']['subtotals_in_kgco2e'].append( |
581
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e']) |
582
|
|
|
result['reporting_period']['subtotals_per_unit_area'].append( |
583
|
|
|
reporting[energy_category_id]['subtotal'] / space['area'] if space['area'] > 0.0 else None) |
584
|
|
|
result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak']) |
585
|
|
|
result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak']) |
586
|
|
|
result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak']) |
587
|
|
|
result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak']) |
588
|
|
|
result['reporting_period']['increment_rates'].append( |
589
|
|
|
(reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
590
|
|
|
base[energy_category_id]['subtotal'] |
591
|
|
|
if base[energy_category_id]['subtotal'] > 0.0 else None) |
592
|
|
|
result['reporting_period']['total_in_kgce'] += reporting[energy_category_id]['subtotal_in_kgce'] |
593
|
|
|
result['reporting_period']['total_in_kgco2e'] += reporting[energy_category_id]['subtotal_in_kgco2e'] |
594
|
|
|
|
595
|
|
|
result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ |
596
|
|
|
result['reporting_period']['total_in_kgce'] / space['area'] if space['area'] > 0.0 else None |
597
|
|
|
|
598
|
|
|
result['reporting_period']['increment_rate_in_kgce'] = \ |
599
|
|
|
(result['reporting_period']['total_in_kgce'] - result['base_period']['total_in_kgce']) / \ |
600
|
|
|
result['base_period']['total_in_kgce'] \ |
601
|
|
|
if result['base_period']['total_in_kgce'] > Decimal(0.0) else None |
602
|
|
|
|
603
|
|
|
result['reporting_period']['total_in_kgce_per_unit_area'] = \ |
604
|
|
|
result['reporting_period']['total_in_kgco2e'] / space['area'] if space['area'] > 0.0 else None |
605
|
|
|
|
606
|
|
|
result['reporting_period']['increment_rate_in_kgco2e'] = \ |
607
|
|
|
(result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ |
608
|
|
|
result['base_period']['total_in_kgco2e'] \ |
609
|
|
|
if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None |
610
|
|
|
|
611
|
|
|
result['parameters'] = { |
612
|
|
|
"names": parameters_data['names'], |
613
|
|
|
"timestamps": parameters_data['timestamps'], |
614
|
|
|
"values": parameters_data['values'] |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
result['child_space'] = dict() |
618
|
|
|
result['child_space']['energy_category_names'] = list() # 1D array [energy category] |
619
|
|
|
result['child_space']['units'] = list() # 1D array [energy category] |
620
|
|
|
result['child_space']['child_space_names_array'] = list() # 2D array [energy category][child space] |
621
|
|
|
result['child_space']['subtotals_array'] = list() # 2D array [energy category][child space] |
622
|
|
|
result['child_space']['subtotals_in_kgce_array'] = list() # 2D array [energy category][child space] |
623
|
|
|
result['child_space']['subtotals_in_kgco2e_array'] = list() # 2D array [energy category][child space] |
624
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
625
|
|
|
for energy_category_id in energy_category_set: |
626
|
|
|
result['child_space']['energy_category_names'].append(energy_category_dict[energy_category_id]['name']) |
627
|
|
|
result['child_space']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
628
|
|
|
result['child_space']['child_space_names_array'].append( |
629
|
|
|
child_space_data[energy_category_id]['child_space_names']) |
630
|
|
|
result['child_space']['subtotals_array'].append( |
631
|
|
|
child_space_data[energy_category_id]['subtotals']) |
632
|
|
|
result['child_space']['subtotals_in_kgce_array'].append( |
633
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgce']) |
634
|
|
|
result['child_space']['subtotals_in_kgco2e_array'].append( |
635
|
|
|
child_space_data[energy_category_id]['subtotals_in_kgco2e']) |
636
|
|
|
# export result to Excel file and then encode the file to base64 string |
637
|
|
|
result['excel_bytes_base64'] = excelexporters.spaceenergycategory.export(result, |
638
|
|
|
space['name'], |
639
|
|
|
reporting_start_datetime_local, |
640
|
|
|
reporting_end_datetime_local, |
641
|
|
|
period_type) |
642
|
|
|
|
643
|
|
|
resp.body = json.dumps(result) |
644
|
|
|
|