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
|
|
|
|
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 space |
23
|
|
|
# Step 3: query energy categories |
24
|
|
|
# Step 4: query associated sensors |
25
|
|
|
# Step 5: query associated points |
26
|
|
|
# Step 6: query child spaces |
27
|
|
|
# Step 7: query base period energy input |
28
|
|
|
# Step 8: query base period energy cost |
29
|
|
|
# Step 9: query reporting period energy input |
30
|
|
|
# Step 10: query reporting period energy cost |
31
|
|
|
# Step 11: query tariff data |
32
|
|
|
# Step 12: query associated sensors and points data |
33
|
|
|
# Step 13: query child spaces energy input |
34
|
|
|
# Step 14: query child spaces energy cost |
35
|
|
|
# Step 15: construct the report |
36
|
|
|
#################################################################################################################### |
37
|
|
|
@staticmethod |
38
|
|
|
def on_get(req, resp): |
39
|
|
|
print(req.params) |
40
|
|
|
user_uuid = req.params.get('useruuid') |
41
|
|
|
period_type = req.params.get('periodtype') |
42
|
|
|
base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
43
|
|
|
base_end_datetime_local = req.params.get('baseperiodenddatetime') |
44
|
|
|
reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
45
|
|
|
reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
46
|
|
|
|
47
|
|
|
################################################################################################################ |
48
|
|
|
# Step 1: valid parameters |
49
|
|
|
################################################################################################################ |
50
|
|
|
if user_uuid is None: |
51
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_USER_UUID') |
52
|
|
|
else: |
53
|
|
|
user_uuid = str.strip(user_uuid) |
54
|
|
|
if len(user_uuid) != 36: |
55
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_USER_UUID') |
56
|
|
|
|
57
|
|
|
if period_type is None: |
58
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
59
|
|
|
else: |
60
|
|
|
period_type = str.strip(period_type) |
61
|
|
|
if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
62
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
63
|
|
|
|
64
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
65
|
|
|
if config.utc_offset[0] == '-': |
66
|
|
|
timezone_offset = -timezone_offset |
67
|
|
|
|
68
|
|
|
base_start_datetime_utc = None |
69
|
|
|
if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
70
|
|
|
base_start_datetime_local = str.strip(base_start_datetime_local) |
71
|
|
|
try: |
72
|
|
|
base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
73
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
74
|
|
|
timedelta(minutes=timezone_offset) |
75
|
|
|
except ValueError: |
76
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
77
|
|
|
description="API.INVALID_BASE_PERIOD_START_DATETIME") |
78
|
|
|
|
79
|
|
|
base_end_datetime_utc = None |
80
|
|
|
if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
81
|
|
|
base_end_datetime_local = str.strip(base_end_datetime_local) |
82
|
|
|
try: |
83
|
|
|
base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
84
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
85
|
|
|
timedelta(minutes=timezone_offset) |
86
|
|
|
except ValueError: |
87
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
88
|
|
|
description="API.INVALID_BASE_PERIOD_END_DATETIME") |
89
|
|
|
|
90
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
91
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
92
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
93
|
|
|
description='API.INVALID_BASE_PERIOD_END_DATETIME') |
94
|
|
|
|
95
|
|
|
if reporting_start_datetime_local is None: |
96
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
97
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
98
|
|
|
else: |
99
|
|
|
reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
100
|
|
|
try: |
101
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
102
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
103
|
|
|
timedelta(minutes=timezone_offset) |
104
|
|
|
except ValueError: |
105
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
106
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
107
|
|
|
|
108
|
|
|
if reporting_end_datetime_local is None: |
109
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
110
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
111
|
|
|
else: |
112
|
|
|
reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
113
|
|
|
try: |
114
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
115
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
116
|
|
|
timedelta(minutes=timezone_offset) |
117
|
|
|
except ValueError: |
118
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
119
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
120
|
|
|
|
121
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
122
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
123
|
|
|
description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
124
|
|
|
|
125
|
|
|
################################################################################################################ |
126
|
|
|
# Step 2: query the space |
127
|
|
|
################################################################################################################ |
128
|
|
|
|
129
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
130
|
|
|
cursor_user = cnx_user.cursor() |
131
|
|
|
|
132
|
|
|
cursor_user.execute(" SELECT id, is_admin, privilege_id " |
133
|
|
|
" FROM tbl_users " |
134
|
|
|
" WHERE uuid = %s ", (user_uuid,)) |
135
|
|
|
row_user = cursor_user.fetchone() |
136
|
|
|
if row_user is None: |
137
|
|
|
if cursor_user: |
138
|
|
|
cursor_user.close() |
139
|
|
|
if cnx_user: |
140
|
|
|
cnx_user.disconnect() |
141
|
|
|
|
142
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, 'API.NOT_FOUND', 'API.USER_NOT_FOUND') |
143
|
|
|
|
144
|
|
|
user = {'id': row_user[0], 'is_admin': row_user[1], 'privilege_id': row_user[2]} |
145
|
|
|
if user['is_admin']: |
146
|
|
|
# todo: make sure the space id is always 1 for admin |
147
|
|
|
space_id = 1 |
148
|
|
|
else: |
149
|
|
|
cursor_user.execute(" SELECT data " |
150
|
|
|
" FROM tbl_privileges " |
151
|
|
|
" WHERE id = %s ", (user['privilege_id'],)) |
152
|
|
|
row_privilege = cursor_user.fetchone() |
153
|
|
|
if row_privilege is None: |
154
|
|
|
if cursor_user: |
155
|
|
|
cursor_user.close() |
156
|
|
|
if cnx_user: |
157
|
|
|
cnx_user.disconnect() |
158
|
|
|
|
159
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, 'API.NOT_FOUND', 'API.USER_PRIVILEGE_NOT_FOUND') |
160
|
|
|
|
161
|
|
|
privilege_data = json.loads(row_privilege[0]) |
162
|
|
|
if 'spaces' not in privilege_data.keys() \ |
163
|
|
|
or privilege_data['spaces'] is None \ |
164
|
|
|
or len(privilege_data['spaces']) == 0: |
165
|
|
|
if cursor_user: |
166
|
|
|
cursor_user.close() |
167
|
|
|
if cnx_user: |
168
|
|
|
cnx_user.disconnect() |
169
|
|
|
|
170
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, 'API.NOT_FOUND', 'API.USER_PRIVILEGE_NOT_FOUND') |
171
|
|
|
# todo: how to deal with multiple spaces in privilege data |
172
|
|
|
space_id = privilege_data['spaces'][0] |
173
|
|
|
|
174
|
|
|
if cursor_user: |
175
|
|
|
cursor_user.close() |
176
|
|
|
if cnx_user: |
177
|
|
|
cnx_user.disconnect() |
178
|
|
|
|
179
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
180
|
|
|
cursor_system = cnx_system.cursor() |
181
|
|
|
|
182
|
|
|
cursor_system.execute(" SELECT id, name, area, cost_center_id " |
183
|
|
|
" FROM tbl_spaces " |
184
|
|
|
" WHERE id = %s ", (space_id,)) |
185
|
|
|
row_space = cursor_system.fetchone() |
186
|
|
|
if row_space is None: |
187
|
|
|
if cursor_system: |
188
|
|
|
cursor_system.close() |
189
|
|
|
if cnx_system: |
190
|
|
|
cnx_system.disconnect() |
191
|
|
|
|
192
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SPACE_NOT_FOUND') |
193
|
|
|
|
194
|
|
|
space = dict() |
195
|
|
|
space['id'] = row_space[0] |
196
|
|
|
space['name'] = row_space[1] |
197
|
|
|
space['area'] = row_space[2] |
198
|
|
|
space['cost_center_id'] = row_space[3] |
199
|
|
|
|
200
|
|
|
################################################################################################################ |
201
|
|
|
# Step 3: query energy categories |
202
|
|
|
################################################################################################################ |
203
|
|
|
cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
204
|
|
|
cursor_energy = cnx_energy.cursor() |
205
|
|
|
|
206
|
|
|
cnx_billing = mysql.connector.connect(**config.myems_billing_db) |
207
|
|
|
cursor_billing = cnx_billing.cursor() |
208
|
|
|
|
209
|
|
|
energy_category_set = set() |
210
|
|
|
# query energy categories in base period |
211
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
212
|
|
|
" FROM tbl_space_input_category_hourly " |
213
|
|
|
" WHERE space_id = %s " |
214
|
|
|
" AND start_datetime_utc >= %s " |
215
|
|
|
" AND start_datetime_utc < %s ", |
216
|
|
|
(space['id'], base_start_datetime_utc, base_end_datetime_utc)) |
217
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
218
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
219
|
|
|
for row_energy_category in rows_energy_categories: |
220
|
|
|
energy_category_set.add(row_energy_category[0]) |
221
|
|
|
|
222
|
|
|
# query energy categories in reporting period |
223
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
224
|
|
|
" FROM tbl_space_input_category_hourly " |
225
|
|
|
" WHERE space_id = %s " |
226
|
|
|
" AND start_datetime_utc >= %s " |
227
|
|
|
" AND start_datetime_utc < %s ", |
228
|
|
|
(space['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
229
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
230
|
|
|
if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
231
|
|
|
for row_energy_category in rows_energy_categories: |
232
|
|
|
energy_category_set.add(row_energy_category[0]) |
233
|
|
|
|
234
|
|
|
# query all energy categories in base period and reporting period |
235
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
236
|
|
|
" FROM tbl_energy_categories " |
237
|
|
|
" ORDER BY id ", ) |
238
|
|
|
rows_energy_categories = cursor_system.fetchall() |
239
|
|
View Code Duplication |
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
|
|
|
240
|
|
|
if cursor_system: |
241
|
|
|
cursor_system.close() |
242
|
|
|
if cnx_system: |
243
|
|
|
cnx_system.disconnect() |
244
|
|
|
|
245
|
|
|
if cursor_energy: |
246
|
|
|
cursor_energy.close() |
247
|
|
|
if cnx_energy: |
248
|
|
|
cnx_energy.disconnect() |
249
|
|
|
|
250
|
|
|
if cursor_billing: |
251
|
|
|
cursor_billing.close() |
252
|
|
|
if cnx_billing: |
253
|
|
|
cnx_billing.disconnect() |
254
|
|
|
|
255
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, |
256
|
|
|
title='API.NOT_FOUND', |
257
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
258
|
|
|
energy_category_dict = dict() |
259
|
|
|
for row_energy_category in rows_energy_categories: |
260
|
|
|
if row_energy_category[0] in energy_category_set: |
261
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
262
|
|
|
"unit_of_measure": row_energy_category[2], |
263
|
|
|
"kgce": row_energy_category[3], |
264
|
|
|
"kgco2e": row_energy_category[4]} |
265
|
|
|
|
266
|
|
|
################################################################################################################ |
267
|
|
|
# Step 4: query associated sensors |
268
|
|
|
################################################################################################################ |
269
|
|
|
point_list = list() |
270
|
|
|
cursor_system.execute(" SELECT po.id, po.name, po.units, po.object_type " |
271
|
|
|
" FROM tbl_spaces sp, tbl_sensors se, tbl_spaces_sensors spse, " |
272
|
|
|
" tbl_points po, tbl_sensors_points sepo " |
273
|
|
|
" WHERE sp.id = %s AND sp.id = spse.space_id AND spse.sensor_id = se.id " |
274
|
|
|
" AND se.id = sepo.sensor_id AND sepo.point_id = po.id " |
275
|
|
|
" ORDER BY po.id ", (space['id'], )) |
276
|
|
|
rows_points = cursor_system.fetchall() |
277
|
|
|
if rows_points is not None and len(rows_points) > 0: |
278
|
|
|
for row in rows_points: |
279
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
280
|
|
|
|
281
|
|
|
################################################################################################################ |
282
|
|
|
# Step 5: query associated points |
283
|
|
|
################################################################################################################ |
284
|
|
|
cursor_system.execute(" SELECT po.id, po.name, po.units, po.object_type " |
285
|
|
|
" FROM tbl_spaces sp, tbl_spaces_points sppo, tbl_points po " |
286
|
|
|
" WHERE sp.id = %s AND sp.id = sppo.space_id AND sppo.point_id = po.id " |
287
|
|
|
" ORDER BY po.id ", (space['id'], )) |
288
|
|
|
rows_points = cursor_system.fetchall() |
289
|
|
|
if rows_points is not None and len(rows_points) > 0: |
290
|
|
|
for row in rows_points: |
291
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
292
|
|
|
|
293
|
|
|
################################################################################################################ |
294
|
|
|
# Step 6: query child spaces |
295
|
|
|
################################################################################################################ |
296
|
|
|
child_space_list = list() |
297
|
|
|
cursor_system.execute(" SELECT id, name " |
298
|
|
|
" FROM tbl_spaces " |
299
|
|
|
" WHERE parent_space_id = %s " |
300
|
|
|
" ORDER BY id ", (space['id'], )) |
301
|
|
|
rows_child_spaces = cursor_system.fetchall() |
302
|
|
|
if rows_child_spaces is not None and len(rows_child_spaces) > 0: |
303
|
|
|
for row in rows_child_spaces: |
304
|
|
|
child_space_list.append({"id": row[0], "name": row[1]}) |
305
|
|
|
|
306
|
|
|
################################################################################################################ |
307
|
|
|
# Step 7: query base period energy input |
308
|
|
|
################################################################################################################ |
309
|
|
|
base_input = dict() |
310
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
311
|
|
|
for energy_category_id in energy_category_set: |
312
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
313
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
314
|
|
|
|
315
|
|
|
base_input[energy_category_id] = dict() |
316
|
|
|
base_input[energy_category_id]['timestamps'] = list() |
317
|
|
|
base_input[energy_category_id]['values'] = list() |
318
|
|
|
base_input[energy_category_id]['subtotal'] = Decimal(0.0) |
319
|
|
|
base_input[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
320
|
|
|
base_input[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
321
|
|
|
|
322
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
323
|
|
|
" FROM tbl_space_input_category_hourly " |
324
|
|
|
" WHERE space_id = %s " |
325
|
|
|
" AND energy_category_id = %s " |
326
|
|
|
" AND start_datetime_utc >= %s " |
327
|
|
|
" AND start_datetime_utc < %s " |
328
|
|
|
" ORDER BY start_datetime_utc ", |
329
|
|
|
(space['id'], |
330
|
|
|
energy_category_id, |
331
|
|
|
base_start_datetime_utc, |
332
|
|
|
base_end_datetime_utc)) |
333
|
|
|
rows_space_hourly = cursor_energy.fetchall() |
334
|
|
|
|
335
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
336
|
|
|
base_start_datetime_utc, |
337
|
|
|
base_end_datetime_utc, |
338
|
|
|
period_type) |
339
|
|
|
for row_space_periodically in rows_space_periodically: |
340
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
341
|
|
|
timedelta(minutes=timezone_offset) |
342
|
|
|
if period_type == 'hourly': |
343
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
344
|
|
|
elif period_type == 'daily': |
345
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
346
|
|
|
elif period_type == 'monthly': |
347
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
348
|
|
|
elif period_type == 'yearly': |
349
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
350
|
|
|
|
351
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
352
|
|
|
base_input[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
353
|
|
|
base_input[energy_category_id]['values'].append(actual_value) |
354
|
|
|
base_input[energy_category_id]['subtotal'] += actual_value |
355
|
|
|
base_input[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
356
|
|
|
base_input[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
357
|
|
|
|
358
|
|
|
################################################################################################################ |
359
|
|
|
# Step 8: query base period energy cost |
360
|
|
|
################################################################################################################ |
361
|
|
|
base_cost = dict() |
362
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
363
|
|
|
for energy_category_id in energy_category_set: |
364
|
|
|
base_cost[energy_category_id] = dict() |
365
|
|
|
base_cost[energy_category_id]['timestamps'] = list() |
366
|
|
|
base_cost[energy_category_id]['values'] = list() |
367
|
|
|
base_cost[energy_category_id]['subtotal'] = Decimal(0.0) |
368
|
|
|
|
369
|
|
|
cursor_billing.execute(" SELECT start_datetime_utc, actual_value " |
370
|
|
|
" FROM tbl_space_input_category_hourly " |
371
|
|
|
" WHERE space_id = %s " |
372
|
|
|
" AND energy_category_id = %s " |
373
|
|
|
" AND start_datetime_utc >= %s " |
374
|
|
|
" AND start_datetime_utc < %s " |
375
|
|
|
" ORDER BY start_datetime_utc ", |
376
|
|
|
(space['id'], |
377
|
|
|
energy_category_id, |
378
|
|
|
base_start_datetime_utc, |
379
|
|
|
base_end_datetime_utc)) |
380
|
|
|
rows_space_hourly = cursor_billing.fetchall() |
381
|
|
|
|
382
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
383
|
|
|
base_start_datetime_utc, |
384
|
|
|
base_end_datetime_utc, |
385
|
|
|
period_type) |
386
|
|
|
for row_space_periodically in rows_space_periodically: |
387
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
388
|
|
|
timedelta(minutes=timezone_offset) |
389
|
|
|
if period_type == 'hourly': |
390
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
391
|
|
|
elif period_type == 'daily': |
392
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
393
|
|
|
elif period_type == 'monthly': |
394
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
395
|
|
|
elif period_type == 'yearly': |
396
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
397
|
|
|
|
398
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
399
|
|
|
base_cost[energy_category_id]['timestamps'].append(current_datetime) |
400
|
|
|
base_cost[energy_category_id]['values'].append(actual_value) |
401
|
|
|
base_cost[energy_category_id]['subtotal'] += actual_value |
402
|
|
|
|
403
|
|
|
################################################################################################################ |
404
|
|
|
# Step 9: query reporting period energy input |
405
|
|
|
################################################################################################################ |
406
|
|
|
reporting_input = dict() |
407
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
408
|
|
|
for energy_category_id in energy_category_set: |
409
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
410
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
411
|
|
|
|
412
|
|
|
reporting_input[energy_category_id] = dict() |
413
|
|
|
reporting_input[energy_category_id]['timestamps'] = list() |
414
|
|
|
reporting_input[energy_category_id]['values'] = list() |
415
|
|
|
reporting_input[energy_category_id]['subtotal'] = Decimal(0.0) |
416
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
417
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
418
|
|
|
reporting_input[energy_category_id]['toppeak'] = Decimal(0.0) |
419
|
|
|
reporting_input[energy_category_id]['onpeak'] = Decimal(0.0) |
420
|
|
|
reporting_input[energy_category_id]['midpeak'] = Decimal(0.0) |
421
|
|
|
reporting_input[energy_category_id]['offpeak'] = Decimal(0.0) |
422
|
|
|
|
423
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
424
|
|
|
" FROM tbl_space_input_category_hourly " |
425
|
|
|
" WHERE space_id = %s " |
426
|
|
|
" AND energy_category_id = %s " |
427
|
|
|
" AND start_datetime_utc >= %s " |
428
|
|
|
" AND start_datetime_utc < %s " |
429
|
|
|
" ORDER BY start_datetime_utc ", |
430
|
|
|
(space['id'], |
431
|
|
|
energy_category_id, |
432
|
|
|
reporting_start_datetime_utc, |
433
|
|
|
reporting_end_datetime_utc)) |
434
|
|
|
rows_space_hourly = cursor_energy.fetchall() |
435
|
|
|
|
436
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
437
|
|
|
reporting_start_datetime_utc, |
438
|
|
|
reporting_end_datetime_utc, |
439
|
|
|
period_type) |
440
|
|
|
for row_space_periodically in rows_space_periodically: |
441
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
442
|
|
|
timedelta(minutes=timezone_offset) |
443
|
|
|
if period_type == 'hourly': |
444
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
445
|
|
|
elif period_type == 'daily': |
446
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
447
|
|
|
elif period_type == 'monthly': |
448
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
449
|
|
|
elif period_type == 'yearly': |
450
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
451
|
|
|
|
452
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
453
|
|
|
reporting_input[energy_category_id]['timestamps'].append(current_datetime) |
454
|
|
|
reporting_input[energy_category_id]['values'].append(actual_value) |
455
|
|
|
reporting_input[energy_category_id]['subtotal'] += actual_value |
456
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
457
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
458
|
|
|
|
459
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_peak_types(space['cost_center_id'], |
460
|
|
|
energy_category_id, |
461
|
|
|
reporting_start_datetime_utc, |
462
|
|
|
reporting_end_datetime_utc) |
463
|
|
|
for row in rows_space_hourly: |
464
|
|
|
peak_type = energy_category_tariff_dict.get(row[0], None) |
465
|
|
|
if peak_type == 'toppeak': |
466
|
|
|
reporting_input[energy_category_id]['toppeak'] += row[1] |
467
|
|
|
elif peak_type == 'onpeak': |
468
|
|
|
reporting_input[energy_category_id]['onpeak'] += row[1] |
469
|
|
|
elif peak_type == 'midpeak': |
470
|
|
|
reporting_input[energy_category_id]['midpeak'] += row[1] |
471
|
|
|
elif peak_type == 'offpeak': |
472
|
|
|
reporting_input[energy_category_id]['offpeak'] += row[1] |
473
|
|
|
|
474
|
|
|
################################################################################################################ |
475
|
|
|
# Step 10: query reporting period energy cost |
476
|
|
|
################################################################################################################ |
477
|
|
|
reporting_cost = dict() |
478
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
479
|
|
|
for energy_category_id in energy_category_set: |
480
|
|
|
|
481
|
|
|
reporting_cost[energy_category_id] = dict() |
482
|
|
|
reporting_cost[energy_category_id]['timestamps'] = list() |
483
|
|
|
reporting_cost[energy_category_id]['values'] = list() |
484
|
|
|
reporting_cost[energy_category_id]['subtotal'] = Decimal(0.0) |
485
|
|
|
reporting_cost[energy_category_id]['toppeak'] = Decimal(0.0) |
486
|
|
|
reporting_cost[energy_category_id]['onpeak'] = Decimal(0.0) |
487
|
|
|
reporting_cost[energy_category_id]['midpeak'] = Decimal(0.0) |
488
|
|
|
reporting_cost[energy_category_id]['offpeak'] = Decimal(0.0) |
489
|
|
|
|
490
|
|
|
cursor_billing.execute(" SELECT start_datetime_utc, actual_value " |
491
|
|
|
" FROM tbl_space_input_category_hourly " |
492
|
|
|
" WHERE space_id = %s " |
493
|
|
|
" AND energy_category_id = %s " |
494
|
|
|
" AND start_datetime_utc >= %s " |
495
|
|
|
" AND start_datetime_utc < %s " |
496
|
|
|
" ORDER BY start_datetime_utc ", |
497
|
|
|
(space['id'], |
498
|
|
|
energy_category_id, |
499
|
|
|
reporting_start_datetime_utc, |
500
|
|
|
reporting_end_datetime_utc)) |
501
|
|
|
rows_space_hourly = cursor_billing.fetchall() |
502
|
|
|
|
503
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
504
|
|
|
reporting_start_datetime_utc, |
505
|
|
|
reporting_end_datetime_utc, |
506
|
|
|
period_type) |
507
|
|
|
for row_space_periodically in rows_space_periodically: |
508
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
509
|
|
|
timedelta(minutes=timezone_offset) |
510
|
|
|
if period_type == 'hourly': |
511
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
512
|
|
|
elif period_type == 'daily': |
513
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
514
|
|
|
elif period_type == 'monthly': |
515
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
516
|
|
|
elif period_type == 'yearly': |
517
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
518
|
|
|
|
519
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
520
|
|
|
reporting_cost[energy_category_id]['timestamps'].append(current_datetime) |
521
|
|
|
reporting_cost[energy_category_id]['values'].append(actual_value) |
522
|
|
|
reporting_cost[energy_category_id]['subtotal'] += actual_value |
523
|
|
|
|
524
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_peak_types(space['cost_center_id'], |
525
|
|
|
energy_category_id, |
526
|
|
|
reporting_start_datetime_utc, |
527
|
|
|
reporting_end_datetime_utc) |
528
|
|
|
for row in rows_space_hourly: |
529
|
|
|
peak_type = energy_category_tariff_dict.get(row[0], None) |
530
|
|
|
if peak_type == 'toppeak': |
531
|
|
|
reporting_cost[energy_category_id]['toppeak'] += row[1] |
532
|
|
|
elif peak_type == 'onpeak': |
533
|
|
|
reporting_cost[energy_category_id]['onpeak'] += row[1] |
534
|
|
|
elif peak_type == 'midpeak': |
535
|
|
|
reporting_cost[energy_category_id]['midpeak'] += row[1] |
536
|
|
|
elif peak_type == 'offpeak': |
537
|
|
|
reporting_cost[energy_category_id]['offpeak'] += row[1] |
538
|
|
|
################################################################################################################ |
539
|
|
|
# Step 11: query tariff data |
540
|
|
|
################################################################################################################ |
541
|
|
|
parameters_data = dict() |
542
|
|
|
parameters_data['names'] = list() |
543
|
|
|
parameters_data['timestamps'] = list() |
544
|
|
|
parameters_data['values'] = list() |
545
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
546
|
|
|
for energy_category_id in energy_category_set: |
547
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(space['cost_center_id'], |
548
|
|
|
energy_category_id, |
549
|
|
|
reporting_start_datetime_utc, |
550
|
|
|
reporting_end_datetime_utc) |
551
|
|
|
tariff_timestamp_list = list() |
552
|
|
|
tariff_value_list = list() |
553
|
|
|
for k, v in energy_category_tariff_dict.items(): |
554
|
|
|
# convert k from utc to local |
555
|
|
|
k = k + timedelta(minutes=timezone_offset) |
556
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
557
|
|
|
tariff_value_list.append(v) |
558
|
|
|
|
559
|
|
|
parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
560
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
561
|
|
|
parameters_data['values'].append(tariff_value_list) |
562
|
|
|
|
563
|
|
|
################################################################################################################ |
564
|
|
|
# Step 12: query associated sensors and points data |
565
|
|
|
################################################################################################################ |
566
|
|
|
|
567
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
568
|
|
|
cursor_historical = cnx_historical.cursor() |
569
|
|
|
|
570
|
|
|
for point in point_list: |
571
|
|
|
point_values = [] |
572
|
|
|
point_timestamps = [] |
573
|
|
|
if point['object_type'] == 'ANALOG_VALUE': |
574
|
|
|
query = (" SELECT utc_date_time, actual_value " |
575
|
|
|
" FROM tbl_analog_value " |
576
|
|
|
" WHERE point_id = %s " |
577
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
578
|
|
|
" ORDER BY utc_date_time ") |
579
|
|
|
cursor_historical.execute(query, (point['id'], |
580
|
|
|
reporting_start_datetime_utc, |
581
|
|
|
reporting_end_datetime_utc)) |
582
|
|
|
rows = cursor_historical.fetchall() |
583
|
|
|
|
584
|
|
|
if rows is not None and len(rows) > 0: |
585
|
|
|
for row in rows: |
586
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
587
|
|
|
timedelta(minutes=timezone_offset) |
588
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
589
|
|
|
point_timestamps.append(current_datetime) |
590
|
|
|
point_values.append(row[1]) |
591
|
|
|
|
592
|
|
|
elif point['object_type'] == 'ENERGY_VALUE': |
593
|
|
|
query = (" SELECT utc_date_time, actual_value " |
594
|
|
|
" FROM tbl_energy_value " |
595
|
|
|
" WHERE point_id = %s " |
596
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
597
|
|
|
" ORDER BY utc_date_time ") |
598
|
|
|
cursor_historical.execute(query, (point['id'], |
599
|
|
|
reporting_start_datetime_utc, |
600
|
|
|
reporting_end_datetime_utc)) |
601
|
|
|
rows = cursor_historical.fetchall() |
602
|
|
|
|
603
|
|
|
if rows is not None and len(rows) > 0: |
604
|
|
|
for row in rows: |
605
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
606
|
|
|
timedelta(minutes=timezone_offset) |
607
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
608
|
|
|
point_timestamps.append(current_datetime) |
609
|
|
|
point_values.append(row[1]) |
610
|
|
|
elif point['object_type'] == 'DIGITAL_VALUE': |
611
|
|
|
query = (" SELECT utc_date_time, actual_value " |
612
|
|
|
" FROM tbl_digital_value " |
613
|
|
|
" WHERE point_id = %s " |
614
|
|
|
" AND utc_date_time BETWEEN %s AND %s ") |
615
|
|
|
cursor_historical.execute(query, (point['id'], |
616
|
|
|
reporting_start_datetime_utc, |
617
|
|
|
reporting_end_datetime_utc)) |
618
|
|
|
rows = cursor_historical.fetchall() |
619
|
|
|
|
620
|
|
|
if rows is not None and len(rows) > 0: |
621
|
|
|
for row in rows: |
622
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
623
|
|
|
timedelta(minutes=timezone_offset) |
624
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
625
|
|
|
point_timestamps.append(current_datetime) |
626
|
|
|
point_values.append(row[1]) |
627
|
|
|
|
628
|
|
|
parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
629
|
|
|
parameters_data['timestamps'].append(point_timestamps) |
630
|
|
|
parameters_data['values'].append(point_values) |
631
|
|
|
|
632
|
|
|
################################################################################################################ |
633
|
|
|
# Step 13: query child spaces energy input |
634
|
|
|
################################################################################################################ |
635
|
|
|
child_space_input = dict() |
636
|
|
|
|
637
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
638
|
|
|
for energy_category_id in energy_category_set: |
639
|
|
|
child_space_input[energy_category_id] = dict() |
640
|
|
|
child_space_input[energy_category_id]['child_space_names'] = list() |
641
|
|
|
child_space_input[energy_category_id]['subtotals'] = list() |
642
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgce'] = list() |
643
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgco2e'] = list() |
644
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
645
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
646
|
|
|
for child_space in child_space_list: |
647
|
|
|
child_space_input[energy_category_id]['child_space_names'].append(child_space['name']) |
648
|
|
|
|
649
|
|
|
cursor_energy.execute(" SELECT SUM(actual_value) " |
650
|
|
|
" FROM tbl_space_input_category_hourly " |
651
|
|
|
" WHERE space_id = %s " |
652
|
|
|
" AND energy_category_id = %s " |
653
|
|
|
" AND start_datetime_utc >= %s " |
654
|
|
|
" AND start_datetime_utc < %s " |
655
|
|
|
" ORDER BY start_datetime_utc ", |
656
|
|
|
(child_space['id'], |
657
|
|
|
energy_category_id, |
658
|
|
|
reporting_start_datetime_utc, |
659
|
|
|
reporting_end_datetime_utc)) |
660
|
|
|
row_subtotal = cursor_energy.fetchone() |
661
|
|
|
|
662
|
|
|
subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0] |
663
|
|
|
child_space_input[energy_category_id]['subtotals'].append(subtotal) |
664
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgce'].append(subtotal * kgce) |
665
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgco2e'].append(subtotal * kgco2e) |
666
|
|
|
|
667
|
|
|
################################################################################################################ |
668
|
|
|
# Step 14: query child spaces energy cost |
669
|
|
|
################################################################################################################ |
670
|
|
|
child_space_cost = dict() |
671
|
|
|
|
672
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
673
|
|
|
for energy_category_id in energy_category_set: |
674
|
|
|
child_space_cost[energy_category_id] = dict() |
675
|
|
|
child_space_cost[energy_category_id]['child_space_names'] = list() |
676
|
|
|
child_space_cost[energy_category_id]['subtotals'] = list() |
677
|
|
|
for child_space in child_space_list: |
678
|
|
|
child_space_cost[energy_category_id]['child_space_names'].append(child_space['name']) |
679
|
|
|
|
680
|
|
|
cursor_billing.execute(" SELECT SUM(actual_value) " |
681
|
|
|
" FROM tbl_space_input_category_hourly " |
682
|
|
|
" WHERE space_id = %s " |
683
|
|
|
" AND energy_category_id = %s " |
684
|
|
|
" AND start_datetime_utc >= %s " |
685
|
|
|
" AND start_datetime_utc < %s " |
686
|
|
|
" ORDER BY start_datetime_utc ", |
687
|
|
|
(child_space['id'], |
688
|
|
|
energy_category_id, |
689
|
|
|
reporting_start_datetime_utc, |
690
|
|
|
reporting_end_datetime_utc)) |
691
|
|
|
row_subtotal = cursor_billing.fetchone() |
692
|
|
|
|
693
|
|
|
subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0] |
694
|
|
|
child_space_cost[energy_category_id]['subtotals'].append(subtotal) |
695
|
|
|
|
696
|
|
|
################################################################################################################ |
697
|
|
|
# Step 15: construct the report |
698
|
|
|
################################################################################################################ |
699
|
|
|
if cursor_system: |
700
|
|
|
cursor_system.close() |
701
|
|
|
if cnx_system: |
702
|
|
|
cnx_system.disconnect() |
703
|
|
|
|
704
|
|
|
if cursor_energy: |
705
|
|
|
cursor_energy.close() |
706
|
|
|
if cnx_energy: |
707
|
|
|
cnx_energy.disconnect() |
708
|
|
|
|
709
|
|
|
if cursor_billing: |
710
|
|
|
cursor_billing.close() |
711
|
|
|
if cnx_billing: |
712
|
|
|
cnx_billing.disconnect() |
713
|
|
|
|
714
|
|
|
if cursor_historical: |
715
|
|
|
cursor_historical.close() |
716
|
|
|
if cnx_historical: |
717
|
|
|
cnx_historical.disconnect() |
718
|
|
|
|
719
|
|
|
result = dict() |
720
|
|
|
|
721
|
|
|
result['space'] = dict() |
722
|
|
|
result['space']['name'] = space['name'] |
723
|
|
|
result['space']['area'] = space['area'] |
724
|
|
|
|
725
|
|
|
result['base_period_input'] = dict() |
726
|
|
|
result['base_period_input']['names'] = list() |
727
|
|
|
result['base_period_input']['units'] = list() |
728
|
|
|
result['base_period_input']['timestamps'] = list() |
729
|
|
|
result['base_period_input']['values'] = list() |
730
|
|
|
result['base_period_input']['subtotals'] = list() |
731
|
|
|
result['base_period_input']['subtotals_in_kgce'] = list() |
732
|
|
|
result['base_period_input']['subtotals_in_kgco2e'] = list() |
733
|
|
|
result['base_period_input']['total_in_kgce'] = Decimal(0.0) |
734
|
|
|
result['base_period_input']['total_in_kgco2e'] = Decimal(0.0) |
735
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
736
|
|
|
for energy_category_id in energy_category_set: |
737
|
|
|
result['base_period_input']['names'].append( |
738
|
|
|
energy_category_dict[energy_category_id]['name']) |
739
|
|
|
result['base_period_input']['units'].append( |
740
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
741
|
|
|
result['base_period_input']['timestamps'].append( |
742
|
|
|
base_input[energy_category_id]['timestamps']) |
743
|
|
|
result['base_period_input']['values'].append( |
744
|
|
|
base_input[energy_category_id]['values']) |
745
|
|
|
result['base_period_input']['subtotals'].append( |
746
|
|
|
base_input[energy_category_id]['subtotal']) |
747
|
|
|
result['base_period_input']['subtotals_in_kgce'].append( |
748
|
|
|
base_input[energy_category_id]['subtotal_in_kgce']) |
749
|
|
|
result['base_period_input']['subtotals_in_kgco2e'].append( |
750
|
|
|
base_input[energy_category_id]['subtotal_in_kgco2e']) |
751
|
|
|
result['base_period_input']['total_in_kgce'] += \ |
752
|
|
|
base_input[energy_category_id]['subtotal_in_kgce'] |
753
|
|
|
result['base_period_input']['total_in_kgco2e'] += \ |
754
|
|
|
base_input[energy_category_id]['subtotal_in_kgco2e'] |
755
|
|
|
|
756
|
|
|
result['base_period_cost'] = dict() |
757
|
|
|
result['base_period_cost']['names'] = list() |
758
|
|
|
result['base_period_cost']['units'] = list() |
759
|
|
|
result['base_period_cost']['timestamps'] = list() |
760
|
|
|
result['base_period_cost']['values'] = list() |
761
|
|
|
result['base_period_cost']['subtotals'] = list() |
762
|
|
|
result['base_period_cost']['total'] = Decimal(0.0) |
763
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
764
|
|
|
for energy_category_id in energy_category_set: |
765
|
|
|
result['base_period_cost']['names'].append( |
766
|
|
|
energy_category_dict[energy_category_id]['name']) |
767
|
|
|
result['base_period_cost']['units'].append( |
768
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
769
|
|
|
result['base_period_cost']['timestamps'].append( |
770
|
|
|
base_cost[energy_category_id]['timestamps']) |
771
|
|
|
result['base_period_cost']['values'].append( |
772
|
|
|
base_cost[energy_category_id]['values']) |
773
|
|
|
result['base_period_cost']['subtotals'].append( |
774
|
|
|
base_cost[energy_category_id]['subtotal']) |
775
|
|
|
result['base_period_cost']['total'] += base_cost[energy_category_id]['subtotal'] |
776
|
|
|
|
777
|
|
|
result['reporting_period_input'] = dict() |
778
|
|
|
result['reporting_period_input']['names'] = list() |
779
|
|
|
result['reporting_period_input']['energy_category_ids'] = list() |
780
|
|
|
result['reporting_period_input']['units'] = list() |
781
|
|
|
result['reporting_period_input']['timestamps'] = list() |
782
|
|
|
result['reporting_period_input']['values'] = list() |
783
|
|
|
result['reporting_period_input']['subtotals'] = list() |
784
|
|
|
result['reporting_period_input']['subtotals_in_kgce'] = list() |
785
|
|
|
result['reporting_period_input']['subtotals_in_kgco2e'] = list() |
786
|
|
|
result['reporting_period_input']['subtotals_per_unit_area'] = list() |
787
|
|
|
result['reporting_period_input']['toppeaks'] = list() |
788
|
|
|
result['reporting_period_input']['onpeaks'] = list() |
789
|
|
|
result['reporting_period_input']['midpeaks'] = list() |
790
|
|
|
result['reporting_period_input']['offpeaks'] = list() |
791
|
|
|
result['reporting_period_input']['increment_rates'] = list() |
792
|
|
|
result['reporting_period_input']['total_in_kgce'] = Decimal(0.0) |
793
|
|
|
result['reporting_period_input']['total_in_kgco2e'] = Decimal(0.0) |
794
|
|
|
result['reporting_period_input']['increment_rate_in_kgce'] = Decimal(0.0) |
795
|
|
|
result['reporting_period_input']['increment_rate_in_kgco2e'] = Decimal(0.0) |
796
|
|
|
|
797
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
798
|
|
|
for energy_category_id in energy_category_set: |
799
|
|
|
result['reporting_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
800
|
|
|
result['reporting_period_input']['energy_category_ids'].append(energy_category_id) |
801
|
|
|
result['reporting_period_input']['units'].append( |
802
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
803
|
|
|
result['reporting_period_input']['timestamps'].append( |
804
|
|
|
reporting_input[energy_category_id]['timestamps']) |
805
|
|
|
result['reporting_period_input']['values'].append( |
806
|
|
|
reporting_input[energy_category_id]['values']) |
807
|
|
|
result['reporting_period_input']['subtotals'].append( |
808
|
|
|
reporting_input[energy_category_id]['subtotal']) |
809
|
|
|
result['reporting_period_input']['subtotals_in_kgce'].append( |
810
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgce']) |
811
|
|
|
result['reporting_period_input']['subtotals_in_kgco2e'].append( |
812
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgco2e']) |
813
|
|
|
result['reporting_period_input']['subtotals_per_unit_area'].append( |
814
|
|
|
reporting_input[energy_category_id]['subtotal'] / space['area'] |
815
|
|
|
if space['area'] > 0.0 else None) |
816
|
|
|
result['reporting_period_input']['toppeaks'].append( |
817
|
|
|
reporting_input[energy_category_id]['toppeak']) |
818
|
|
|
result['reporting_period_input']['onpeaks'].append( |
819
|
|
|
reporting_input[energy_category_id]['onpeak']) |
820
|
|
|
result['reporting_period_input']['midpeaks'].append( |
821
|
|
|
reporting_input[energy_category_id]['midpeak']) |
822
|
|
|
result['reporting_period_input']['offpeaks'].append( |
823
|
|
|
reporting_input[energy_category_id]['offpeak']) |
824
|
|
|
result['reporting_period_input']['increment_rates'].append( |
825
|
|
|
(reporting_input[energy_category_id]['subtotal'] - |
826
|
|
|
base_input[energy_category_id]['subtotal']) / |
827
|
|
|
base_input[energy_category_id]['subtotal'] |
828
|
|
|
if base_input[energy_category_id]['subtotal'] > 0.0 else None) |
829
|
|
|
result['reporting_period_input']['total_in_kgce'] += \ |
830
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgce'] |
831
|
|
|
result['reporting_period_input']['total_in_kgco2e'] += \ |
832
|
|
|
reporting_input[energy_category_id]['subtotal_in_kgco2e'] |
833
|
|
|
|
834
|
|
|
result['reporting_period_input']['total_in_kgco2e_per_unit_area'] = \ |
835
|
|
|
result['reporting_period_input']['total_in_kgce'] / space['area'] if space['area'] > 0.0 else None |
836
|
|
|
|
837
|
|
|
result['reporting_period_input']['increment_rate_in_kgce'] = \ |
838
|
|
|
(result['reporting_period_input']['total_in_kgce'] - result['base_period_input']['total_in_kgce']) / \ |
839
|
|
|
result['base_period_input']['total_in_kgce'] \ |
840
|
|
|
if result['base_period_input']['total_in_kgce'] > Decimal(0.0) else None |
841
|
|
|
|
842
|
|
|
result['reporting_period_input']['total_in_kgce_per_unit_area'] = \ |
843
|
|
|
result['reporting_period_input']['total_in_kgco2e'] / space['area'] if space['area'] > 0.0 else None |
844
|
|
|
|
845
|
|
|
result['reporting_period_input']['increment_rate_in_kgco2e'] = \ |
846
|
|
|
(result['reporting_period_input']['total_in_kgco2e'] - result['base_period_input']['total_in_kgco2e']) / \ |
847
|
|
|
result['base_period_input']['total_in_kgco2e'] \ |
848
|
|
|
if result['base_period_input']['total_in_kgco2e'] > Decimal(0.0) else None |
849
|
|
|
|
850
|
|
|
result['reporting_period_cost'] = dict() |
851
|
|
|
result['reporting_period_cost']['names'] = list() |
852
|
|
|
result['reporting_period_cost']['energy_category_ids'] = list() |
853
|
|
|
result['reporting_period_cost']['units'] = list() |
854
|
|
|
result['reporting_period_cost']['timestamps'] = list() |
855
|
|
|
result['reporting_period_cost']['values'] = list() |
856
|
|
|
result['reporting_period_cost']['subtotals'] = list() |
857
|
|
|
result['reporting_period_cost']['subtotals_per_unit_area'] = list() |
858
|
|
|
result['reporting_period_cost']['toppeaks'] = list() |
859
|
|
|
result['reporting_period_cost']['onpeaks'] = list() |
860
|
|
|
result['reporting_period_cost']['midpeaks'] = list() |
861
|
|
|
result['reporting_period_cost']['offpeaks'] = list() |
862
|
|
|
result['reporting_period_cost']['increment_rates'] = list() |
863
|
|
|
result['reporting_period_cost']['total'] = Decimal(0.0) |
864
|
|
|
result['reporting_period_cost']['total_per_unit_area'] = Decimal(0.0) |
865
|
|
|
result['reporting_period_cost']['total_increment_rate'] = Decimal(0.0) |
866
|
|
|
result['reporting_period_cost']['total_unit'] = config.currency_unit |
867
|
|
|
|
868
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
869
|
|
|
for energy_category_id in energy_category_set: |
870
|
|
|
result['reporting_period_cost']['names'].append(energy_category_dict[energy_category_id]['name']) |
871
|
|
|
result['reporting_period_cost']['energy_category_ids'].append(energy_category_id) |
872
|
|
|
result['reporting_period_cost']['units'].append(config.currency_unit) |
873
|
|
|
result['reporting_period_cost']['timestamps'].append( |
874
|
|
|
reporting_cost[energy_category_id]['timestamps']) |
875
|
|
|
result['reporting_period_cost']['values'].append( |
876
|
|
|
reporting_cost[energy_category_id]['values']) |
877
|
|
|
result['reporting_period_cost']['subtotals'].append( |
878
|
|
|
reporting_cost[energy_category_id]['subtotal']) |
879
|
|
|
result['reporting_period_cost']['subtotals_per_unit_area'].append( |
880
|
|
|
reporting_cost[energy_category_id]['subtotal'] / space['area'] |
881
|
|
|
if space['area'] > 0.0 else None) |
882
|
|
|
result['reporting_period_cost']['toppeaks'].append( |
883
|
|
|
reporting_cost[energy_category_id]['toppeak']) |
884
|
|
|
result['reporting_period_cost']['onpeaks'].append( |
885
|
|
|
reporting_cost[energy_category_id]['onpeak']) |
886
|
|
|
result['reporting_period_cost']['midpeaks'].append( |
887
|
|
|
reporting_cost[energy_category_id]['midpeak']) |
888
|
|
|
result['reporting_period_cost']['offpeaks'].append( |
889
|
|
|
reporting_cost[energy_category_id]['offpeak']) |
890
|
|
|
result['reporting_period_cost']['increment_rates'].append( |
891
|
|
|
(reporting_cost[energy_category_id]['subtotal'] - |
892
|
|
|
base_cost[energy_category_id]['subtotal']) / |
893
|
|
|
base_cost[energy_category_id]['subtotal'] |
894
|
|
|
if base_cost[energy_category_id]['subtotal'] > 0.0 else None) |
895
|
|
|
result['reporting_period_cost']['total'] += reporting_cost[energy_category_id]['subtotal'] |
896
|
|
|
result['reporting_period_cost']['total_per_unit_area'] = \ |
897
|
|
|
result['reporting_period_cost']['total'] / space['area'] if space['area'] > 0.0 else None |
898
|
|
|
|
899
|
|
|
result['reporting_period_cost']['total_increment_rate'] = \ |
900
|
|
|
(result['reporting_period_cost']['total'] - result['base_period_cost']['total']) / \ |
901
|
|
|
result['reporting_period_cost']['total'] \ |
902
|
|
|
if result['reporting_period_cost']['total'] > Decimal(0.0) else None |
903
|
|
|
|
904
|
|
|
result['parameters'] = { |
905
|
|
|
"names": parameters_data['names'], |
906
|
|
|
"timestamps": parameters_data['timestamps'], |
907
|
|
|
"values": parameters_data['values'] |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
result['child_space_input'] = dict() |
911
|
|
|
result['child_space_input']['energy_category_names'] = list() # 1D array [energy category] |
912
|
|
|
result['child_space_input']['units'] = list() # 1D array [energy category] |
913
|
|
|
result['child_space_input']['child_space_names_array'] = list() # 2D array [energy category][child space] |
914
|
|
|
result['child_space_input']['subtotals_array'] = list() # 2D array [energy category][child space] |
915
|
|
|
result['child_space_input']['subtotals_in_kgce_array'] = list() # 2D array [energy category][child space] |
916
|
|
|
result['child_space_input']['subtotals_in_kgco2e_array'] = list() # 2D array [energy category][child space] |
917
|
|
View Code Duplication |
if energy_category_set is not None and len(energy_category_set) > 0: |
|
|
|
|
918
|
|
|
for energy_category_id in energy_category_set: |
919
|
|
|
result['child_space_input']['energy_category_names'].append( |
920
|
|
|
energy_category_dict[energy_category_id]['name']) |
921
|
|
|
result['child_space_input']['units'].append( |
922
|
|
|
energy_category_dict[energy_category_id]['unit_of_measure']) |
923
|
|
|
result['child_space_input']['child_space_names_array'].append( |
924
|
|
|
child_space_input[energy_category_id]['child_space_names']) |
925
|
|
|
result['child_space_input']['subtotals_array'].append( |
926
|
|
|
child_space_input[energy_category_id]['subtotals']) |
927
|
|
|
result['child_space_input']['subtotals_in_kgce_array'].append( |
928
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgce']) |
929
|
|
|
result['child_space_input']['subtotals_in_kgco2e_array'].append( |
930
|
|
|
child_space_input[energy_category_id]['subtotals_in_kgco2e']) |
931
|
|
|
|
932
|
|
|
result['child_space_cost'] = dict() |
933
|
|
|
result['child_space_cost']['energy_category_names'] = list() # 1D array [energy category] |
934
|
|
|
result['child_space_cost']['units'] = list() # 1D array [energy category] |
935
|
|
|
result['child_space_cost']['child_space_names_array'] = list() # 2D array [energy category][child space] |
936
|
|
|
result['child_space_cost']['subtotals_array'] = list() # 2D array [energy category][child space] |
937
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
938
|
|
|
for energy_category_id in energy_category_set: |
939
|
|
|
result['child_space_cost']['energy_category_names'].append( |
940
|
|
|
energy_category_dict[energy_category_id]['name']) |
941
|
|
|
result['child_space_cost']['units'].append(config.currency_unit) |
942
|
|
|
result['child_space_cost']['child_space_names_array'].append( |
943
|
|
|
child_space_cost[energy_category_id]['child_space_names']) |
944
|
|
|
result['child_space_cost']['subtotals_array'].append( |
945
|
|
|
child_space_cost[energy_category_id]['subtotals']) |
946
|
|
|
|
947
|
|
|
resp.body = json.dumps(result) |
948
|
|
|
|