1
|
|
|
import re |
2
|
|
|
from datetime import datetime, timedelta, timezone |
3
|
|
|
from decimal import Decimal |
4
|
|
|
import falcon |
5
|
|
|
import mysql.connector |
6
|
|
|
import simplejson as json |
7
|
|
|
import config |
8
|
|
|
# import excelexporters.spaceenergyprediction |
9
|
|
|
from core import utilities |
10
|
|
|
from core.useractivity import access_control, api_key_control |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Reporting: |
14
|
|
|
def __init__(self): |
15
|
|
|
""""Initializes Reporting""" |
16
|
|
|
pass |
17
|
|
|
|
18
|
|
|
@staticmethod |
19
|
|
|
def on_options(req, resp): |
20
|
|
|
_ = req |
21
|
|
|
resp.status = falcon.HTTP_200 |
22
|
|
|
|
23
|
|
|
#################################################################################################################### |
24
|
|
|
# PROCEDURES |
25
|
|
|
# Step 1: valid parameters |
26
|
|
|
# Step 2: query the space |
27
|
|
|
# Step 3: query energy categories |
28
|
|
|
# Step 4: query base period energy prediction |
29
|
|
|
# Step 5: query reporting period energy prediction |
30
|
|
|
# Step 6: query tariff data |
31
|
|
|
# Step 7: construct the report |
32
|
|
|
#################################################################################################################### |
33
|
|
|
@staticmethod |
34
|
|
|
def on_get(req, resp): |
35
|
|
|
if 'API-KEY' not in req.headers or \ |
36
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
37
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
38
|
|
|
access_control(req) |
39
|
|
|
else: |
40
|
|
|
api_key_control(req) |
41
|
|
|
print(req.params) |
42
|
|
|
space_id = req.params.get('spaceid') |
43
|
|
|
space_uuid = req.params.get('spaceuuid') |
44
|
|
|
period_type = req.params.get('periodtype') |
45
|
|
|
base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
46
|
|
|
base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
47
|
|
|
reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
48
|
|
|
reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
49
|
|
|
language = req.params.get('language') |
50
|
|
|
quick_mode = req.params.get('quickmode') |
51
|
|
|
|
52
|
|
|
################################################################################################################ |
53
|
|
|
# Step 1: valid parameters |
54
|
|
|
################################################################################################################ |
55
|
|
|
if space_id is None and space_uuid is None: |
56
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
57
|
|
|
title='API.BAD_REQUEST', |
58
|
|
|
description='API.INVALID_SPACE_ID') |
59
|
|
|
|
60
|
|
|
if space_id is not None: |
61
|
|
|
space_id = str.strip(space_id) |
62
|
|
|
if not space_id.isdigit() or int(space_id) <= 0: |
63
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
64
|
|
|
title='API.BAD_REQUEST', |
65
|
|
|
description='API.INVALID_SPACE_ID') |
66
|
|
|
|
67
|
|
|
if space_uuid is not None: |
68
|
|
|
regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) |
69
|
|
|
match = regex.match(str.strip(space_uuid)) |
70
|
|
|
if not bool(match): |
71
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
72
|
|
|
title='API.BAD_REQUEST', |
73
|
|
|
description='API.INVALID_SPACE_UUID') |
74
|
|
|
|
75
|
|
|
if period_type is None: |
76
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
77
|
|
|
description='API.INVALID_PERIOD_TYPE') |
78
|
|
|
else: |
79
|
|
|
period_type = str.strip(period_type) |
80
|
|
|
if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
81
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
82
|
|
|
description='API.INVALID_PERIOD_TYPE') |
83
|
|
|
|
84
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
85
|
|
|
if config.utc_offset[0] == '-': |
86
|
|
|
timezone_offset = -timezone_offset |
87
|
|
|
|
88
|
|
|
base_start_datetime_utc = None |
89
|
|
|
if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
90
|
|
|
base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
91
|
|
|
try: |
92
|
|
|
base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
93
|
|
|
|
94
|
|
|
except ValueError: |
95
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
96
|
|
|
description="API.INVALID_BASE_PERIOD_START_DATETIME") |
97
|
|
|
base_start_datetime_utc = \ |
98
|
|
|
base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
99
|
|
|
# nomalize the start datetime |
100
|
|
|
if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
101
|
|
|
base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
102
|
|
|
else: |
103
|
|
|
base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
104
|
|
|
|
105
|
|
|
base_end_datetime_utc = None |
106
|
|
|
if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
107
|
|
|
base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
108
|
|
|
try: |
109
|
|
|
base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
110
|
|
|
|
111
|
|
|
except ValueError: |
112
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
113
|
|
|
description="API.INVALID_BASE_PERIOD_END_DATETIME") |
114
|
|
|
base_end_datetime_utc = \ |
115
|
|
|
base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
116
|
|
|
|
117
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
118
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
119
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
120
|
|
|
description='API.INVALID_BASE_PERIOD_END_DATETIME') |
121
|
|
|
|
122
|
|
|
if reporting_period_start_datetime_local is None: |
123
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
124
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
125
|
|
|
else: |
126
|
|
|
reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
127
|
|
|
try: |
128
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
129
|
|
|
'%Y-%m-%dT%H:%M:%S') |
130
|
|
|
|
131
|
|
|
except ValueError: |
132
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
133
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
134
|
|
|
reporting_start_datetime_utc = \ |
135
|
|
|
reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
136
|
|
|
# nomalize the start datetime |
137
|
|
|
if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
138
|
|
|
reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
139
|
|
|
else: |
140
|
|
|
reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
141
|
|
|
|
142
|
|
|
if reporting_period_end_datetime_local is None: |
143
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
144
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
145
|
|
|
else: |
146
|
|
|
reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
147
|
|
|
try: |
148
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
149
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
150
|
|
|
timedelta(minutes=timezone_offset) |
151
|
|
|
except ValueError: |
152
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
153
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
154
|
|
|
|
155
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
156
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
157
|
|
|
description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
158
|
|
|
|
159
|
|
|
# if turn quick mode on, do not return parameters data and excel file |
160
|
|
|
is_quick_mode = False |
161
|
|
|
if quick_mode is not None and \ |
162
|
|
|
len(str.strip(quick_mode)) > 0 and \ |
163
|
|
|
str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
164
|
|
|
is_quick_mode = True |
165
|
|
|
|
166
|
|
|
trans = utilities.get_translation(language) |
167
|
|
|
trans.install() |
168
|
|
|
_ = trans.gettext |
169
|
|
|
|
170
|
|
|
################################################################################################################ |
171
|
|
|
# Step 2: query the space |
172
|
|
|
################################################################################################################ |
173
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
174
|
|
|
cursor_system = cnx_system.cursor() |
175
|
|
|
|
176
|
|
|
cnx_energy_prediction = mysql.connector.connect(**config.myems_energy_prediction_db) |
177
|
|
|
cursor_energy_prediction = cnx_energy_prediction.cursor() |
178
|
|
|
|
179
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
180
|
|
|
cursor_historical = cnx_historical.cursor() |
181
|
|
|
|
182
|
|
|
if space_id is not None: |
183
|
|
|
cursor_system.execute(" SELECT id, name, area, number_of_occupants, cost_center_id " |
184
|
|
|
" FROM tbl_spaces " |
185
|
|
|
" WHERE id = %s ", (space_id,)) |
186
|
|
|
row_space = cursor_system.fetchone() |
187
|
|
|
elif space_uuid is not None: |
188
|
|
|
cursor_system.execute(" SELECT id, name, area, number_of_occupants, cost_center_id " |
189
|
|
|
" FROM tbl_spaces " |
190
|
|
|
" WHERE uuid = %s ", (space_uuid,)) |
191
|
|
|
row_space = cursor_system.fetchone() |
192
|
|
|
|
193
|
|
|
if row_space is None: |
|
|
|
|
194
|
|
|
if cursor_system: |
195
|
|
|
cursor_system.close() |
196
|
|
|
if cnx_system: |
197
|
|
|
cnx_system.close() |
198
|
|
|
|
199
|
|
|
if cursor_energy_prediction: |
200
|
|
|
cursor_energy_prediction.close() |
201
|
|
|
if cnx_energy_prediction: |
202
|
|
|
cnx_energy_prediction.close() |
203
|
|
|
|
204
|
|
|
if cursor_historical: |
205
|
|
|
cursor_historical.close() |
206
|
|
|
if cnx_historical: |
207
|
|
|
cnx_historical.close() |
208
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.SPACE_NOT_FOUND') |
209
|
|
|
|
210
|
|
|
space = dict() |
211
|
|
|
space['id'] = row_space[0] |
212
|
|
|
space['name'] = row_space[1] |
213
|
|
|
space['area'] = row_space[2] |
214
|
|
|
space['number_of_occupants'] = row_space[3] |
215
|
|
|
space['cost_center_id'] = row_space[4] |
216
|
|
|
|
217
|
|
|
################################################################################################################ |
218
|
|
|
# Step 3: query energy categories |
219
|
|
|
################################################################################################################ |
220
|
|
|
energy_category_set = set() |
221
|
|
|
# query energy categories in base period |
222
|
|
|
cursor_energy_prediction.execute(" SELECT DISTINCT(energy_category_id) " |
223
|
|
|
" FROM tbl_space_input_category_hourly " |
224
|
|
|
" WHERE space_id = %s " |
225
|
|
|
" AND start_datetime_utc >= %s " |
226
|
|
|
" AND start_datetime_utc < %s ", |
227
|
|
|
(space['id'], base_start_datetime_utc, base_end_datetime_utc)) |
228
|
|
|
rows_energy_categories = cursor_energy_prediction.fetchall() |
229
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
230
|
|
|
for row_energy_category in rows_energy_categories: |
231
|
|
|
energy_category_set.add(row_energy_category[0]) |
232
|
|
|
|
233
|
|
|
# query energy categories in reporting period |
234
|
|
|
cursor_energy_prediction.execute(" SELECT DISTINCT(energy_category_id) " |
235
|
|
|
" FROM tbl_space_input_category_hourly " |
236
|
|
|
" WHERE space_id = %s " |
237
|
|
|
" AND start_datetime_utc >= %s " |
238
|
|
|
" AND start_datetime_utc < %s ", |
239
|
|
|
(space['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
240
|
|
|
rows_energy_categories = cursor_energy_prediction.fetchall() |
241
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
242
|
|
|
for row_energy_category in rows_energy_categories: |
243
|
|
|
energy_category_set.add(row_energy_category[0]) |
244
|
|
|
|
245
|
|
|
# query all energy categories in base period and reporting period |
246
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
247
|
|
|
" FROM tbl_energy_categories " |
248
|
|
|
" ORDER BY id ", ) |
249
|
|
|
rows_energy_categories = cursor_system.fetchall() |
250
|
|
|
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
251
|
|
|
if cursor_system: |
252
|
|
|
cursor_system.close() |
253
|
|
|
if cnx_system: |
254
|
|
|
cnx_system.close() |
255
|
|
|
|
256
|
|
|
if cursor_energy_prediction: |
257
|
|
|
cursor_energy_prediction.close() |
258
|
|
|
if cnx_energy_prediction: |
259
|
|
|
cnx_energy_prediction.close() |
260
|
|
|
|
261
|
|
|
if cursor_historical: |
262
|
|
|
cursor_historical.close() |
263
|
|
|
if cnx_historical: |
264
|
|
|
cnx_historical.close() |
265
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, |
266
|
|
|
title='API.NOT_FOUND', |
267
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
268
|
|
|
energy_category_dict = dict() |
269
|
|
|
for row_energy_category in rows_energy_categories: |
270
|
|
|
if row_energy_category[0] in energy_category_set: |
271
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
272
|
|
|
"unit_of_measure": row_energy_category[2], |
273
|
|
|
"kgce": row_energy_category[3], |
274
|
|
|
"kgco2e": row_energy_category[4]} |
275
|
|
|
|
276
|
|
|
################################################################################################################ |
277
|
|
|
# Step 4: query base period energy prediction |
278
|
|
|
################################################################################################################ |
279
|
|
|
base = dict() |
280
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
281
|
|
|
for energy_category_id in energy_category_set: |
282
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
283
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
284
|
|
|
|
285
|
|
|
base[energy_category_id] = dict() |
286
|
|
|
base[energy_category_id]['timestamps'] = list() |
287
|
|
|
base[energy_category_id]['values'] = list() |
288
|
|
|
base[energy_category_id]['subtotal'] = Decimal(0.0) |
289
|
|
|
base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
290
|
|
|
base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
291
|
|
|
|
292
|
|
|
cursor_energy_prediction.execute(" SELECT start_datetime_utc, actual_value " |
293
|
|
|
" FROM tbl_space_input_category_hourly " |
294
|
|
|
" WHERE space_id = %s " |
295
|
|
|
" AND energy_category_id = %s " |
296
|
|
|
" AND start_datetime_utc >= %s " |
297
|
|
|
" AND start_datetime_utc < %s " |
298
|
|
|
" ORDER BY start_datetime_utc ", |
299
|
|
|
(space['id'], |
300
|
|
|
energy_category_id, |
301
|
|
|
base_start_datetime_utc, |
302
|
|
|
base_end_datetime_utc)) |
303
|
|
|
rows_space_hourly = cursor_energy_prediction.fetchall() |
304
|
|
|
|
305
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
306
|
|
|
base_start_datetime_utc, |
307
|
|
|
base_end_datetime_utc, |
308
|
|
|
period_type) |
309
|
|
|
for row_space_periodically in rows_space_periodically: |
310
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
311
|
|
|
timedelta(minutes=timezone_offset) |
312
|
|
|
if period_type == 'hourly': |
313
|
|
|
current_datetime = current_datetime_local.isoformat()[0:19] |
314
|
|
|
elif period_type == 'daily': |
315
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
316
|
|
|
elif period_type == 'weekly': |
317
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
318
|
|
|
elif period_type == 'monthly': |
319
|
|
|
current_datetime = current_datetime_local.isoformat()[0:7] |
320
|
|
|
elif period_type == 'yearly': |
321
|
|
|
current_datetime = current_datetime_local.isoformat()[0:4] |
322
|
|
|
|
323
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
324
|
|
|
base[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
325
|
|
|
base[energy_category_id]['values'].append(actual_value) |
326
|
|
|
base[energy_category_id]['subtotal'] += actual_value |
327
|
|
|
base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
328
|
|
|
base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
329
|
|
|
|
330
|
|
|
################################################################################################################ |
331
|
|
|
# Step 5: query reporting period energy prediction |
332
|
|
|
################################################################################################################ |
333
|
|
|
reporting = dict() |
334
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
335
|
|
|
for energy_category_id in energy_category_set: |
336
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
337
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
338
|
|
|
|
339
|
|
|
reporting[energy_category_id] = dict() |
340
|
|
|
reporting[energy_category_id]['timestamps'] = list() |
341
|
|
|
reporting[energy_category_id]['values'] = list() |
342
|
|
|
reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
343
|
|
|
reporting[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
344
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
345
|
|
|
reporting[energy_category_id]['toppeak'] = Decimal(0.0) |
346
|
|
|
reporting[energy_category_id]['onpeak'] = Decimal(0.0) |
347
|
|
|
reporting[energy_category_id]['midpeak'] = Decimal(0.0) |
348
|
|
|
reporting[energy_category_id]['offpeak'] = Decimal(0.0) |
349
|
|
|
reporting[energy_category_id]['deep'] = Decimal(0.0) |
350
|
|
|
|
351
|
|
|
cursor_energy_prediction.execute(" SELECT start_datetime_utc, actual_value " |
352
|
|
|
" FROM tbl_space_input_category_hourly " |
353
|
|
|
" WHERE space_id = %s " |
354
|
|
|
" AND energy_category_id = %s " |
355
|
|
|
" AND start_datetime_utc >= %s " |
356
|
|
|
" AND start_datetime_utc < %s " |
357
|
|
|
" ORDER BY start_datetime_utc ", |
358
|
|
|
(space['id'], |
359
|
|
|
energy_category_id, |
360
|
|
|
reporting_start_datetime_utc, |
361
|
|
|
reporting_end_datetime_utc)) |
362
|
|
|
rows_space_hourly = cursor_energy_prediction.fetchall() |
363
|
|
|
|
364
|
|
|
rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
365
|
|
|
reporting_start_datetime_utc, |
366
|
|
|
reporting_end_datetime_utc, |
367
|
|
|
period_type) |
368
|
|
|
for row_space_periodically in rows_space_periodically: |
369
|
|
|
current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
370
|
|
|
timedelta(minutes=timezone_offset) |
371
|
|
|
if period_type == 'hourly': |
372
|
|
|
current_datetime = current_datetime_local.isoformat()[0:19] |
373
|
|
|
elif period_type == 'daily': |
374
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
375
|
|
|
elif period_type == 'weekly': |
376
|
|
|
current_datetime = current_datetime_local.isoformat()[0:10] |
377
|
|
|
elif period_type == 'monthly': |
378
|
|
|
current_datetime = current_datetime_local.isoformat()[0:7] |
379
|
|
|
elif period_type == 'yearly': |
380
|
|
|
current_datetime = current_datetime_local.isoformat()[0:4] |
381
|
|
|
|
382
|
|
|
actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
383
|
|
|
reporting[energy_category_id]['timestamps'].append(current_datetime) |
384
|
|
|
reporting[energy_category_id]['values'].append(actual_value) |
385
|
|
|
reporting[energy_category_id]['subtotal'] += actual_value |
386
|
|
|
reporting[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
387
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
388
|
|
|
|
389
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_peak_types(space['cost_center_id'], |
390
|
|
|
energy_category_id, |
391
|
|
|
reporting_start_datetime_utc, |
392
|
|
|
reporting_end_datetime_utc) |
393
|
|
|
for row in rows_space_hourly: |
394
|
|
|
peak_type = energy_category_tariff_dict.get(row[0], None) |
395
|
|
|
if peak_type == 'toppeak': |
396
|
|
|
reporting[energy_category_id]['toppeak'] += row[1] |
397
|
|
|
elif peak_type == 'onpeak': |
398
|
|
|
reporting[energy_category_id]['onpeak'] += row[1] |
399
|
|
|
elif peak_type == 'midpeak': |
400
|
|
|
reporting[energy_category_id]['midpeak'] += row[1] |
401
|
|
|
elif peak_type == 'offpeak': |
402
|
|
|
reporting[energy_category_id]['offpeak'] += row[1] |
403
|
|
|
elif peak_type == 'deep': |
404
|
|
|
reporting[energy_category_id]['deep'] += row[1] |
405
|
|
|
################################################################################################################ |
406
|
|
|
# Step 6: query tariff data |
407
|
|
|
################################################################################################################ |
408
|
|
|
parameters_data = dict() |
409
|
|
|
parameters_data['names'] = list() |
410
|
|
|
parameters_data['timestamps'] = list() |
411
|
|
|
parameters_data['values'] = list() |
412
|
|
|
if config.is_tariff_appended and not is_quick_mode: |
413
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
414
|
|
|
for energy_category_id in energy_category_set: |
415
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(space['cost_center_id'], |
416
|
|
|
energy_category_id, |
417
|
|
|
reporting_start_datetime_utc, |
418
|
|
|
reporting_end_datetime_utc) |
419
|
|
|
tariff_timestamp_list = list() |
420
|
|
|
tariff_value_list = list() |
421
|
|
|
for k, v in energy_category_tariff_dict.items(): |
422
|
|
|
# convert k from utc to local |
423
|
|
|
k = k + timedelta(minutes=timezone_offset) |
424
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19]) |
425
|
|
|
tariff_value_list.append(v) |
426
|
|
|
|
427
|
|
|
parameters_data['names'].append(_('Tariff') + '-' |
428
|
|
|
+ energy_category_dict[energy_category_id]['name']) |
429
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
430
|
|
|
parameters_data['values'].append(tariff_value_list) |
431
|
|
|
|
432
|
|
|
################################################################################################################ |
433
|
|
|
# Step 7: construct the report |
434
|
|
|
################################################################################################################ |
435
|
|
|
if cursor_system: |
436
|
|
|
cursor_system.close() |
437
|
|
|
if cnx_system: |
438
|
|
|
cnx_system.close() |
439
|
|
|
|
440
|
|
|
if cursor_energy_prediction: |
441
|
|
|
cursor_energy_prediction.close() |
442
|
|
|
if cnx_energy_prediction: |
443
|
|
|
cnx_energy_prediction.close() |
444
|
|
|
|
445
|
|
|
if cursor_historical: |
446
|
|
|
cursor_historical.close() |
447
|
|
|
if cnx_historical: |
448
|
|
|
cnx_historical.close() |
449
|
|
|
|
450
|
|
|
result = dict() |
451
|
|
|
|
452
|
|
|
result['space'] = dict() |
453
|
|
|
result['space']['id'] = space['id'] |
454
|
|
|
result['space']['name'] = space['name'] |
455
|
|
|
result['space']['area'] = space['area'] |
456
|
|
|
result['space']['number_of_occupants'] = space['number_of_occupants'] |
457
|
|
|
|
458
|
|
|
result['base_period'] = dict() |
459
|
|
|
result['base_period']['names'] = list() |
460
|
|
|
result['base_period']['units'] = list() |
461
|
|
|
result['base_period']['timestamps'] = list() |
462
|
|
|
result['base_period']['values'] = list() |
463
|
|
|
result['base_period']['subtotals'] = list() |
464
|
|
|
result['base_period']['subtotals_in_kgce'] = list() |
465
|
|
|
result['base_period']['subtotals_in_kgco2e'] = list() |
466
|
|
|
result['base_period']['total_in_kgce'] = Decimal(0.0) |
467
|
|
|
result['base_period']['total_in_kgco2e'] = Decimal(0.0) |
468
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
469
|
|
|
for energy_category_id in energy_category_set: |
470
|
|
|
result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
471
|
|
|
result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
472
|
|
|
result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
473
|
|
|
result['base_period']['values'].append(base[energy_category_id]['values']) |
474
|
|
|
result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
475
|
|
|
result['base_period']['subtotals_in_kgce'].append(base[energy_category_id]['subtotal_in_kgce']) |
476
|
|
|
result['base_period']['subtotals_in_kgco2e'].append(base[energy_category_id]['subtotal_in_kgco2e']) |
477
|
|
|
result['base_period']['total_in_kgce'] += base[energy_category_id]['subtotal_in_kgce'] |
478
|
|
|
result['base_period']['total_in_kgco2e'] += base[energy_category_id]['subtotal_in_kgco2e'] |
479
|
|
|
|
480
|
|
|
result['reporting_period'] = dict() |
481
|
|
|
result['reporting_period']['names'] = list() |
482
|
|
|
result['reporting_period']['energy_category_ids'] = list() |
483
|
|
|
result['reporting_period']['units'] = list() |
484
|
|
|
result['reporting_period']['timestamps'] = list() |
485
|
|
|
result['reporting_period']['values'] = list() |
486
|
|
|
result['reporting_period']['rates'] = list() |
487
|
|
|
result['reporting_period']['subtotals'] = list() |
488
|
|
|
result['reporting_period']['subtotals_in_kgce'] = list() |
489
|
|
|
result['reporting_period']['subtotals_in_kgco2e'] = list() |
490
|
|
|
result['reporting_period']['subtotals_per_unit_area'] = list() |
491
|
|
|
result['reporting_period']['subtotals_per_capita'] = list() |
492
|
|
|
result['reporting_period']['toppeaks'] = list() |
493
|
|
|
result['reporting_period']['onpeaks'] = list() |
494
|
|
|
result['reporting_period']['midpeaks'] = list() |
495
|
|
|
result['reporting_period']['offpeaks'] = list() |
496
|
|
|
result['reporting_period']['deeps'] = list() |
497
|
|
|
result['reporting_period']['increment_rates'] = list() |
498
|
|
|
result['reporting_period']['total_in_kgce'] = Decimal(0.0) |
499
|
|
|
result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) |
500
|
|
|
result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) |
501
|
|
|
result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) |
502
|
|
|
|
503
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
504
|
|
|
for energy_category_id in energy_category_set: |
505
|
|
|
result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
506
|
|
|
result['reporting_period']['energy_category_ids'].append(energy_category_id) |
507
|
|
|
result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
508
|
|
|
result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
509
|
|
|
result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
510
|
|
|
result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
511
|
|
|
result['reporting_period']['subtotals_in_kgce'].append( |
512
|
|
|
reporting[energy_category_id]['subtotal_in_kgce']) |
513
|
|
|
result['reporting_period']['subtotals_in_kgco2e'].append( |
514
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e']) |
515
|
|
|
result['reporting_period']['subtotals_per_unit_area'].append( |
516
|
|
|
reporting[energy_category_id]['subtotal'] / space['area'] if space['area'] > 0.0 else None) |
517
|
|
|
result['reporting_period']['subtotals_per_capita'].append( |
518
|
|
|
reporting[energy_category_id]['subtotal'] / space['number_of_occupants'] |
519
|
|
|
if space['number_of_occupants'] > 0.0 else None) |
520
|
|
|
result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak']) |
521
|
|
|
result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak']) |
522
|
|
|
result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak']) |
523
|
|
|
result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak']) |
524
|
|
|
result['reporting_period']['deeps'].append(reporting[energy_category_id]['deep']) |
525
|
|
|
result['reporting_period']['increment_rates'].append( |
526
|
|
|
(reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
527
|
|
|
base[energy_category_id]['subtotal'] |
528
|
|
|
if base[energy_category_id]['subtotal'] > 0.0 else None) |
529
|
|
|
result['reporting_period']['total_in_kgce'] += reporting[energy_category_id]['subtotal_in_kgce'] |
530
|
|
|
result['reporting_period']['total_in_kgco2e'] += reporting[energy_category_id]['subtotal_in_kgco2e'] |
531
|
|
|
|
532
|
|
|
rate = list() |
533
|
|
|
for index, value in enumerate(reporting[energy_category_id]['values']): |
534
|
|
|
if index < len(base[energy_category_id]['values']) \ |
535
|
|
|
and base[energy_category_id]['values'][index] != 0 and value != 0: |
536
|
|
|
rate.append((value - base[energy_category_id]['values'][index]) |
537
|
|
|
/ base[energy_category_id]['values'][index]) |
538
|
|
|
else: |
539
|
|
|
rate.append(None) |
540
|
|
|
result['reporting_period']['rates'].append(rate) |
541
|
|
|
|
542
|
|
|
result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ |
543
|
|
|
result['reporting_period']['total_in_kgce'] / space['area'] if space['area'] > 0.0 else None |
544
|
|
|
|
545
|
|
|
result['reporting_period']['total_in_kgco2e_per_capita'] = \ |
546
|
|
|
result['reporting_period']['total_in_kgce'] / space['number_of_occupants'] \ |
547
|
|
|
if space['number_of_occupants'] > 0.0 else None |
548
|
|
|
|
549
|
|
|
result['reporting_period']['increment_rate_in_kgce'] = \ |
550
|
|
|
(result['reporting_period']['total_in_kgce'] - result['base_period']['total_in_kgce']) / \ |
551
|
|
|
result['base_period']['total_in_kgce'] \ |
552
|
|
|
if result['base_period']['total_in_kgce'] > Decimal(0.0) else None |
553
|
|
|
|
554
|
|
|
result['reporting_period']['total_in_kgce_per_unit_area'] = \ |
555
|
|
|
result['reporting_period']['total_in_kgco2e'] / space['area'] if space['area'] > 0.0 else None |
556
|
|
|
|
557
|
|
|
result['reporting_period']['total_in_kgce_per_capita'] = \ |
558
|
|
|
result['reporting_period']['total_in_kgco2e'] / space['number_of_occupants'] \ |
559
|
|
|
if space['number_of_occupants'] > 0.0 else None |
560
|
|
|
|
561
|
|
|
result['reporting_period']['increment_rate_in_kgco2e'] = \ |
562
|
|
|
(result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ |
563
|
|
|
result['base_period']['total_in_kgco2e'] \ |
564
|
|
|
if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None |
565
|
|
|
|
566
|
|
|
result['parameters'] = { |
567
|
|
|
"names": parameters_data['names'], |
568
|
|
|
"timestamps": parameters_data['timestamps'], |
569
|
|
|
"values": parameters_data['values'] |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
# export result to Excel file and then encode the file to base64 string |
573
|
|
|
# TODO |
574
|
|
|
# if not is_quick_mode: |
575
|
|
|
# result['excel_bytes_base64'] = \ |
576
|
|
|
# excelexporters.spaceenergyprediction.export(result, |
577
|
|
|
# space['name'], |
578
|
|
|
# base_period_start_datetime_local, |
579
|
|
|
# base_period_end_datetime_local, |
580
|
|
|
# reporting_period_start_datetime_local, |
581
|
|
|
# reporting_period_end_datetime_local, |
582
|
|
|
# period_type, |
583
|
|
|
# language) |
584
|
|
|
|
585
|
|
|
resp.text = json.dumps(result) |
586
|
|
|
|