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
|
|
|
|
8
|
|
|
import config |
9
|
|
|
import excelexporters.tenantsaving |
10
|
|
|
from core import utilities |
11
|
|
|
from core.useractivity import access_control, api_key_control |
12
|
|
|
|
13
|
|
|
|
14
|
|
View Code Duplication |
class Reporting: |
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def __init__(): |
17
|
|
|
""""Initializes Reporting""" |
18
|
|
|
pass |
19
|
|
|
|
20
|
|
|
@staticmethod |
21
|
|
|
def on_options(req, resp): |
22
|
|
|
resp.status = falcon.HTTP_200 |
23
|
|
|
|
24
|
|
|
#################################################################################################################### |
25
|
|
|
# PROCEDURES |
26
|
|
|
# Step 1: valid parameters |
27
|
|
|
# Step 2: query the tenant |
28
|
|
|
# Step 3: query energy categories |
29
|
|
|
# Step 4: query associated sensors |
30
|
|
|
# Step 5: query associated points |
31
|
|
|
# Step 6: query base period energy saving |
32
|
|
|
# Step 7: query reporting period energy saving |
33
|
|
|
# Step 8: query tariff data |
34
|
|
|
# Step 9: query associated sensors and points data |
35
|
|
|
# Step 10: construct the report |
36
|
|
|
#################################################################################################################### |
37
|
|
|
@staticmethod |
38
|
|
|
def on_get(req, resp): |
39
|
|
|
if 'API-KEY' not in req.headers or \ |
40
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
41
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
42
|
|
|
access_control(req) |
43
|
|
|
else: |
44
|
|
|
api_key_control(req) |
45
|
|
|
print(req.params) |
46
|
|
|
tenant_id = req.params.get('tenantid') |
47
|
|
|
tenant_uuid = req.params.get('tenantuuid') |
48
|
|
|
period_type = req.params.get('periodtype') |
49
|
|
|
base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
50
|
|
|
base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
51
|
|
|
reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
52
|
|
|
reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
53
|
|
|
language = req.params.get('language') |
54
|
|
|
quick_mode = req.params.get('quickmode') |
55
|
|
|
|
56
|
|
|
################################################################################################################ |
57
|
|
|
# Step 1: valid parameters |
58
|
|
|
################################################################################################################ |
59
|
|
|
if tenant_id is None and tenant_uuid is None: |
60
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
61
|
|
|
title='API.BAD_REQUEST', |
62
|
|
|
description='API.INVALID_TENANT_ID') |
63
|
|
|
|
64
|
|
|
if tenant_id is not None: |
65
|
|
|
tenant_id = str.strip(tenant_id) |
66
|
|
|
if not tenant_id.isdigit() or int(tenant_id) <= 0: |
67
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
68
|
|
|
title='API.BAD_REQUEST', |
69
|
|
|
description='API.INVALID_TENANT_ID') |
70
|
|
|
|
71
|
|
|
if tenant_uuid is not None: |
72
|
|
|
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) |
73
|
|
|
match = regex.match(str.strip(tenant_uuid)) |
74
|
|
|
if not bool(match): |
75
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
76
|
|
|
title='API.BAD_REQUEST', |
77
|
|
|
description='API.INVALID_TENANT_UUID') |
78
|
|
|
|
79
|
|
|
if period_type is None: |
80
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
81
|
|
|
description='API.INVALID_PERIOD_TYPE') |
82
|
|
|
else: |
83
|
|
|
period_type = str.strip(period_type) |
84
|
|
|
if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
85
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
86
|
|
|
description='API.INVALID_PERIOD_TYPE') |
87
|
|
|
|
88
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
89
|
|
|
if config.utc_offset[0] == '-': |
90
|
|
|
timezone_offset = -timezone_offset |
91
|
|
|
|
92
|
|
|
base_start_datetime_utc = None |
93
|
|
|
if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
94
|
|
|
base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
95
|
|
|
try: |
96
|
|
|
base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
97
|
|
|
except ValueError: |
98
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
99
|
|
|
description="API.INVALID_BASE_PERIOD_START_DATETIME") |
100
|
|
|
base_start_datetime_utc = \ |
101
|
|
|
base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
102
|
|
|
# nomalize the start datetime |
103
|
|
|
if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
104
|
|
|
base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
105
|
|
|
else: |
106
|
|
|
base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
107
|
|
|
|
108
|
|
|
base_end_datetime_utc = None |
109
|
|
|
if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
110
|
|
|
base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
111
|
|
|
try: |
112
|
|
|
base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
113
|
|
|
except ValueError: |
114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
115
|
|
|
description="API.INVALID_BASE_PERIOD_END_DATETIME") |
116
|
|
|
base_end_datetime_utc = \ |
117
|
|
|
base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
118
|
|
|
|
119
|
|
|
if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
120
|
|
|
base_start_datetime_utc >= base_end_datetime_utc: |
121
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
122
|
|
|
description='API.INVALID_BASE_PERIOD_END_DATETIME') |
123
|
|
|
|
124
|
|
|
if reporting_period_start_datetime_local is None: |
125
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
126
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
127
|
|
|
else: |
128
|
|
|
reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
129
|
|
|
try: |
130
|
|
|
reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
131
|
|
|
'%Y-%m-%dT%H:%M:%S') |
132
|
|
|
except ValueError: |
133
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
134
|
|
|
description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
135
|
|
|
reporting_start_datetime_utc = \ |
136
|
|
|
reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
137
|
|
|
# nomalize the start datetime |
138
|
|
|
if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
139
|
|
|
reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
140
|
|
|
else: |
141
|
|
|
reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
142
|
|
|
|
143
|
|
|
if reporting_period_end_datetime_local is None: |
144
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
145
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
146
|
|
|
else: |
147
|
|
|
reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
148
|
|
|
try: |
149
|
|
|
reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
150
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
151
|
|
|
timedelta(minutes=timezone_offset) |
152
|
|
|
except ValueError: |
153
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
154
|
|
|
description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
155
|
|
|
|
156
|
|
|
if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
157
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
158
|
|
|
description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
159
|
|
|
|
160
|
|
|
# if turn quick mode on, do not return parameters data and excel file |
161
|
|
|
is_quick_mode = False |
162
|
|
|
if quick_mode is not None and \ |
163
|
|
|
len(str.strip(quick_mode)) > 0 and \ |
164
|
|
|
str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
165
|
|
|
is_quick_mode = True |
166
|
|
|
|
167
|
|
|
trans = utilities.get_translation(language) |
168
|
|
|
trans.install() |
169
|
|
|
_ = trans.gettext |
170
|
|
|
|
171
|
|
|
################################################################################################################ |
172
|
|
|
# Step 2: query the tenant |
173
|
|
|
################################################################################################################ |
174
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
175
|
|
|
cursor_system = cnx_system.cursor() |
176
|
|
|
|
177
|
|
|
cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
178
|
|
|
cursor_energy = cnx_energy.cursor() |
179
|
|
|
|
180
|
|
|
cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db) |
181
|
|
|
cursor_energy_plan = cnx_energy_plan.cursor() |
182
|
|
|
|
183
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
184
|
|
|
cursor_historical = cnx_historical.cursor() |
185
|
|
|
|
186
|
|
|
if tenant_id is not None: |
187
|
|
|
cursor_system.execute(" SELECT id, name, area, cost_center_id " |
188
|
|
|
" FROM tbl_tenants " |
189
|
|
|
" WHERE id = %s ", (tenant_id,)) |
190
|
|
|
row_tenant = cursor_system.fetchone() |
191
|
|
|
elif tenant_uuid is not None: |
192
|
|
|
cursor_system.execute(" SELECT id, name, area, cost_center_id " |
193
|
|
|
" FROM tbl_tenants " |
194
|
|
|
" WHERE uuid = %s ", (tenant_uuid,)) |
195
|
|
|
row_tenant = cursor_system.fetchone() |
196
|
|
|
|
197
|
|
|
if row_tenant is None: |
|
|
|
|
198
|
|
|
if cursor_system: |
199
|
|
|
cursor_system.close() |
200
|
|
|
if cnx_system: |
201
|
|
|
cnx_system.close() |
202
|
|
|
|
203
|
|
|
if cursor_energy: |
204
|
|
|
cursor_energy.close() |
205
|
|
|
if cnx_energy: |
206
|
|
|
cnx_energy.close() |
207
|
|
|
|
208
|
|
|
if cursor_energy_plan: |
209
|
|
|
cursor_energy_plan.close() |
210
|
|
|
if cnx_energy_plan: |
211
|
|
|
cnx_energy_plan.close() |
212
|
|
|
|
213
|
|
|
if cursor_historical: |
214
|
|
|
cursor_historical.close() |
215
|
|
|
if cnx_historical: |
216
|
|
|
cnx_historical.close() |
217
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') |
218
|
|
|
|
219
|
|
|
tenant = dict() |
220
|
|
|
tenant['id'] = row_tenant[0] |
221
|
|
|
tenant['name'] = row_tenant[1] |
222
|
|
|
tenant['area'] = row_tenant[2] |
223
|
|
|
tenant['cost_center_id'] = row_tenant[3] |
224
|
|
|
|
225
|
|
|
################################################################################################################ |
226
|
|
|
# Step 3: query energy categories |
227
|
|
|
################################################################################################################ |
228
|
|
|
energy_category_set = set() |
229
|
|
|
# query energy categories in base period |
230
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
231
|
|
|
" FROM tbl_tenant_input_category_hourly " |
232
|
|
|
" WHERE tenant_id = %s " |
233
|
|
|
" AND start_datetime_utc >= %s " |
234
|
|
|
" AND start_datetime_utc < %s ", |
235
|
|
|
(tenant['id'], base_start_datetime_utc, base_end_datetime_utc)) |
236
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
237
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
238
|
|
|
for row_energy_category in rows_energy_categories: |
239
|
|
|
energy_category_set.add(row_energy_category[0]) |
240
|
|
|
|
241
|
|
|
# query energy categories in reporting period |
242
|
|
|
cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
243
|
|
|
" FROM tbl_tenant_input_category_hourly " |
244
|
|
|
" WHERE tenant_id = %s " |
245
|
|
|
" AND start_datetime_utc >= %s " |
246
|
|
|
" AND start_datetime_utc < %s ", |
247
|
|
|
(tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
248
|
|
|
rows_energy_categories = cursor_energy.fetchall() |
249
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
250
|
|
|
for row_energy_category in rows_energy_categories: |
251
|
|
|
energy_category_set.add(row_energy_category[0]) |
252
|
|
|
|
253
|
|
|
# query all energy categories in base period and reporting period |
254
|
|
|
cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
255
|
|
|
" FROM tbl_energy_categories " |
256
|
|
|
" ORDER BY id ", ) |
257
|
|
|
rows_energy_categories = cursor_system.fetchall() |
258
|
|
|
if rows_energy_categories is None or len(rows_energy_categories) == 0: |
259
|
|
|
if cursor_system: |
260
|
|
|
cursor_system.close() |
261
|
|
|
if cnx_system: |
262
|
|
|
cnx_system.close() |
263
|
|
|
|
264
|
|
|
if cursor_energy: |
265
|
|
|
cursor_energy.close() |
266
|
|
|
if cnx_energy: |
267
|
|
|
cnx_energy.close() |
268
|
|
|
|
269
|
|
|
if cursor_energy_plan: |
270
|
|
|
cursor_energy_plan.close() |
271
|
|
|
if cnx_energy_plan: |
272
|
|
|
cnx_energy_plan.close() |
273
|
|
|
|
274
|
|
|
if cursor_historical: |
275
|
|
|
cursor_historical.close() |
276
|
|
|
if cnx_historical: |
277
|
|
|
cnx_historical.close() |
278
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, |
279
|
|
|
title='API.NOT_FOUND', |
280
|
|
|
description='API.ENERGY_CATEGORY_NOT_FOUND') |
281
|
|
|
energy_category_dict = dict() |
282
|
|
|
for row_energy_category in rows_energy_categories: |
283
|
|
|
if row_energy_category[0] in energy_category_set: |
284
|
|
|
energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
285
|
|
|
"unit_of_measure": row_energy_category[2], |
286
|
|
|
"kgce": row_energy_category[3], |
287
|
|
|
"kgco2e": row_energy_category[4]} |
288
|
|
|
|
289
|
|
|
################################################################################################################ |
290
|
|
|
# Step 4: query associated sensors |
291
|
|
|
################################################################################################################ |
292
|
|
|
point_list = list() |
293
|
|
|
cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
294
|
|
|
" FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, " |
295
|
|
|
" tbl_points p, tbl_sensors_points sp " |
296
|
|
|
" WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id " |
297
|
|
|
" AND s.id = sp.sensor_id AND sp.point_id = p.id " |
298
|
|
|
" ORDER BY p.id ", (tenant['id'],)) |
299
|
|
|
rows_points = cursor_system.fetchall() |
300
|
|
|
if rows_points is not None and len(rows_points) > 0: |
301
|
|
|
for row in rows_points: |
302
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
303
|
|
|
|
304
|
|
|
################################################################################################################ |
305
|
|
|
# Step 5: query associated points |
306
|
|
|
################################################################################################################ |
307
|
|
|
cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
308
|
|
|
" FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
309
|
|
|
" WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id " |
310
|
|
|
" ORDER BY p.id ", (tenant['id'],)) |
311
|
|
|
rows_points = cursor_system.fetchall() |
312
|
|
|
if rows_points is not None and len(rows_points) > 0: |
313
|
|
|
for row in rows_points: |
314
|
|
|
point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
315
|
|
|
|
316
|
|
|
################################################################################################################ |
317
|
|
|
# Step 6: query base period energy saving |
318
|
|
|
################################################################################################################ |
319
|
|
|
base = dict() |
320
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
321
|
|
|
for energy_category_id in energy_category_set: |
322
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
323
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
324
|
|
|
|
325
|
|
|
base[energy_category_id] = dict() |
326
|
|
|
base[energy_category_id]['timestamps'] = list() |
327
|
|
|
base[energy_category_id]['values_plan'] = list() |
328
|
|
|
base[energy_category_id]['values_actual'] = list() |
329
|
|
|
base[energy_category_id]['values_saving'] = list() |
330
|
|
|
base[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
331
|
|
|
base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
332
|
|
|
base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
333
|
|
|
base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
334
|
|
|
base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
335
|
|
|
base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
336
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
337
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
338
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
339
|
|
|
# query base period's energy plan |
340
|
|
|
cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
341
|
|
|
" FROM tbl_tenant_input_category_hourly " |
342
|
|
|
" WHERE tenant_id = %s " |
343
|
|
|
" AND energy_category_id = %s " |
344
|
|
|
" AND start_datetime_utc >= %s " |
345
|
|
|
" AND start_datetime_utc < %s " |
346
|
|
|
" ORDER BY start_datetime_utc ", |
347
|
|
|
(tenant['id'], |
348
|
|
|
energy_category_id, |
349
|
|
|
base_start_datetime_utc, |
350
|
|
|
base_end_datetime_utc)) |
351
|
|
|
rows_tenant_hourly = cursor_energy_plan.fetchall() |
352
|
|
|
|
353
|
|
|
rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
354
|
|
|
base_start_datetime_utc, |
355
|
|
|
base_end_datetime_utc, |
356
|
|
|
period_type) |
357
|
|
|
for row_tenant_periodically in rows_tenant_periodically: |
358
|
|
|
current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
359
|
|
|
timedelta(minutes=timezone_offset) |
360
|
|
|
if period_type == 'hourly': |
361
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
362
|
|
|
elif period_type == 'daily': |
363
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
364
|
|
|
elif period_type == 'weekly': |
365
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
366
|
|
|
elif period_type == 'monthly': |
367
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
368
|
|
|
elif period_type == 'yearly': |
369
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
370
|
|
|
|
371
|
|
|
plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
372
|
|
|
base[energy_category_id]['timestamps'].append(current_datetime) |
|
|
|
|
373
|
|
|
base[energy_category_id]['values_plan'].append(plan_value) |
374
|
|
|
base[energy_category_id]['subtotal_plan'] += plan_value |
375
|
|
|
base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
376
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
377
|
|
|
|
378
|
|
|
# query base period's energy actual |
379
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
380
|
|
|
" FROM tbl_tenant_input_category_hourly " |
381
|
|
|
" WHERE tenant_id = %s " |
382
|
|
|
" AND energy_category_id = %s " |
383
|
|
|
" AND start_datetime_utc >= %s " |
384
|
|
|
" AND start_datetime_utc < %s " |
385
|
|
|
" ORDER BY start_datetime_utc ", |
386
|
|
|
(tenant['id'], |
387
|
|
|
energy_category_id, |
388
|
|
|
base_start_datetime_utc, |
389
|
|
|
base_end_datetime_utc)) |
390
|
|
|
rows_tenant_hourly = cursor_energy.fetchall() |
391
|
|
|
|
392
|
|
|
rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
393
|
|
|
base_start_datetime_utc, |
394
|
|
|
base_end_datetime_utc, |
395
|
|
|
period_type) |
396
|
|
|
for row_tenant_periodically in rows_tenant_periodically: |
397
|
|
|
current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
398
|
|
|
timedelta(minutes=timezone_offset) |
399
|
|
|
if period_type == 'hourly': |
400
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
401
|
|
|
elif period_type == 'daily': |
402
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
403
|
|
|
elif period_type == 'weekly': |
404
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
405
|
|
|
elif period_type == 'monthly': |
406
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
407
|
|
|
elif period_type == 'yearly': |
408
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
409
|
|
|
|
410
|
|
|
actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
411
|
|
|
base[energy_category_id]['values_actual'].append(actual_value) |
412
|
|
|
base[energy_category_id]['subtotal_actual'] += actual_value |
413
|
|
|
base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
414
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
415
|
|
|
|
416
|
|
|
# calculate base period's energy saving |
417
|
|
|
for i in range(len(base[energy_category_id]['values_plan'])): |
418
|
|
|
base[energy_category_id]['values_saving'].append( |
419
|
|
|
base[energy_category_id]['values_plan'][i] - |
420
|
|
|
base[energy_category_id]['values_actual'][i]) |
421
|
|
|
|
422
|
|
|
base[energy_category_id]['subtotal_saving'] = \ |
423
|
|
|
base[energy_category_id]['subtotal_plan'] - \ |
424
|
|
|
base[energy_category_id]['subtotal_actual'] |
425
|
|
|
base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
426
|
|
|
base[energy_category_id]['subtotal_in_kgce_plan'] - \ |
427
|
|
|
base[energy_category_id]['subtotal_in_kgce_actual'] |
428
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
429
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
430
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_actual'] |
431
|
|
|
################################################################################################################ |
432
|
|
|
# Step 7: query reporting period energy saving |
433
|
|
|
################################################################################################################ |
434
|
|
|
reporting = dict() |
435
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
436
|
|
|
for energy_category_id in energy_category_set: |
437
|
|
|
kgce = energy_category_dict[energy_category_id]['kgce'] |
438
|
|
|
kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
439
|
|
|
|
440
|
|
|
reporting[energy_category_id] = dict() |
441
|
|
|
reporting[energy_category_id]['timestamps'] = list() |
442
|
|
|
reporting[energy_category_id]['values_plan'] = list() |
443
|
|
|
reporting[energy_category_id]['values_actual'] = list() |
444
|
|
|
reporting[energy_category_id]['values_saving'] = list() |
445
|
|
|
reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
446
|
|
|
reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
447
|
|
|
reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
448
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
449
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
450
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
451
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
452
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
453
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
454
|
|
|
# query reporting period's energy plan |
455
|
|
|
cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
456
|
|
|
" FROM tbl_tenant_input_category_hourly " |
457
|
|
|
" WHERE tenant_id = %s " |
458
|
|
|
" AND energy_category_id = %s " |
459
|
|
|
" AND start_datetime_utc >= %s " |
460
|
|
|
" AND start_datetime_utc < %s " |
461
|
|
|
" ORDER BY start_datetime_utc ", |
462
|
|
|
(tenant['id'], |
463
|
|
|
energy_category_id, |
464
|
|
|
reporting_start_datetime_utc, |
465
|
|
|
reporting_end_datetime_utc)) |
466
|
|
|
rows_tenant_hourly = cursor_energy_plan.fetchall() |
467
|
|
|
|
468
|
|
|
rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
469
|
|
|
reporting_start_datetime_utc, |
470
|
|
|
reporting_end_datetime_utc, |
471
|
|
|
period_type) |
472
|
|
|
for row_tenant_periodically in rows_tenant_periodically: |
473
|
|
|
current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
474
|
|
|
timedelta(minutes=timezone_offset) |
475
|
|
|
if period_type == 'hourly': |
476
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
477
|
|
|
elif period_type == 'daily': |
478
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
479
|
|
|
elif period_type == 'weekly': |
480
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
481
|
|
|
elif period_type == 'monthly': |
482
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
483
|
|
|
elif period_type == 'yearly': |
484
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
485
|
|
|
|
486
|
|
|
plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
487
|
|
|
reporting[energy_category_id]['timestamps'].append(current_datetime) |
488
|
|
|
reporting[energy_category_id]['values_plan'].append(plan_value) |
489
|
|
|
reporting[energy_category_id]['subtotal_plan'] += plan_value |
490
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
491
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
492
|
|
|
|
493
|
|
|
# query reporting period's energy actual |
494
|
|
|
cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
495
|
|
|
" FROM tbl_tenant_input_category_hourly " |
496
|
|
|
" WHERE tenant_id = %s " |
497
|
|
|
" AND energy_category_id = %s " |
498
|
|
|
" AND start_datetime_utc >= %s " |
499
|
|
|
" AND start_datetime_utc < %s " |
500
|
|
|
" ORDER BY start_datetime_utc ", |
501
|
|
|
(tenant['id'], |
502
|
|
|
energy_category_id, |
503
|
|
|
reporting_start_datetime_utc, |
504
|
|
|
reporting_end_datetime_utc)) |
505
|
|
|
rows_tenant_hourly = cursor_energy.fetchall() |
506
|
|
|
|
507
|
|
|
rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
508
|
|
|
reporting_start_datetime_utc, |
509
|
|
|
reporting_end_datetime_utc, |
510
|
|
|
period_type) |
511
|
|
|
for row_tenant_periodically in rows_tenant_periodically: |
512
|
|
|
current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
513
|
|
|
timedelta(minutes=timezone_offset) |
514
|
|
|
if period_type == 'hourly': |
515
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
516
|
|
|
elif period_type == 'daily': |
517
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
518
|
|
|
elif period_type == 'weekly': |
519
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
520
|
|
|
elif period_type == 'monthly': |
521
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m') |
522
|
|
|
elif period_type == 'yearly': |
523
|
|
|
current_datetime = current_datetime_local.strftime('%Y') |
524
|
|
|
|
525
|
|
|
actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
526
|
|
|
reporting[energy_category_id]['values_actual'].append(actual_value) |
527
|
|
|
reporting[energy_category_id]['subtotal_actual'] += actual_value |
528
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
529
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
530
|
|
|
|
531
|
|
|
# calculate reporting period's energy savings |
532
|
|
|
for i in range(len(reporting[energy_category_id]['values_plan'])): |
533
|
|
|
reporting[energy_category_id]['values_saving'].append( |
534
|
|
|
reporting[energy_category_id]['values_plan'][i] - |
535
|
|
|
reporting[energy_category_id]['values_actual'][i]) |
536
|
|
|
|
537
|
|
|
reporting[energy_category_id]['subtotal_saving'] = \ |
538
|
|
|
reporting[energy_category_id]['subtotal_plan'] - \ |
539
|
|
|
reporting[energy_category_id]['subtotal_actual'] |
540
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
541
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_plan'] - \ |
542
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_actual'] |
543
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
544
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
545
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
546
|
|
|
################################################################################################################ |
547
|
|
|
# Step 8: query tariff data |
548
|
|
|
################################################################################################################ |
549
|
|
|
parameters_data = dict() |
550
|
|
|
parameters_data['names'] = list() |
551
|
|
|
parameters_data['timestamps'] = list() |
552
|
|
|
parameters_data['values'] = list() |
553
|
|
|
if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
554
|
|
|
and not is_quick_mode: |
555
|
|
|
for energy_category_id in energy_category_set: |
556
|
|
|
energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'], |
557
|
|
|
energy_category_id, |
558
|
|
|
reporting_start_datetime_utc, |
559
|
|
|
reporting_end_datetime_utc) |
560
|
|
|
tariff_timestamp_list = list() |
561
|
|
|
tariff_value_list = list() |
562
|
|
|
for k, v in energy_category_tariff_dict.items(): |
563
|
|
|
# convert k from utc to local |
564
|
|
|
k = k + timedelta(minutes=timezone_offset) |
565
|
|
|
tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
566
|
|
|
tariff_value_list.append(v) |
567
|
|
|
|
568
|
|
|
parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
569
|
|
|
parameters_data['timestamps'].append(tariff_timestamp_list) |
570
|
|
|
parameters_data['values'].append(tariff_value_list) |
571
|
|
|
|
572
|
|
|
################################################################################################################ |
573
|
|
|
# Step 9: query associated sensors and points data |
574
|
|
|
################################################################################################################ |
575
|
|
|
if not is_quick_mode: |
576
|
|
|
for point in point_list: |
577
|
|
|
point_values = [] |
578
|
|
|
point_timestamps = [] |
579
|
|
|
if point['object_type'] == 'ENERGY_VALUE': |
580
|
|
|
query = (" SELECT utc_date_time, actual_value " |
581
|
|
|
" FROM tbl_energy_value " |
582
|
|
|
" WHERE point_id = %s " |
583
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
584
|
|
|
" ORDER BY utc_date_time ") |
585
|
|
|
cursor_historical.execute(query, (point['id'], |
586
|
|
|
reporting_start_datetime_utc, |
587
|
|
|
reporting_end_datetime_utc)) |
588
|
|
|
rows = cursor_historical.fetchall() |
589
|
|
|
|
590
|
|
|
if rows is not None and len(rows) > 0: |
591
|
|
|
for row in rows: |
592
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
593
|
|
|
timedelta(minutes=timezone_offset) |
594
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
595
|
|
|
point_timestamps.append(current_datetime) |
596
|
|
|
point_values.append(row[1]) |
597
|
|
|
elif point['object_type'] == 'ANALOG_VALUE': |
598
|
|
|
query = (" SELECT utc_date_time, actual_value " |
599
|
|
|
" FROM tbl_analog_value " |
600
|
|
|
" WHERE point_id = %s " |
601
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
602
|
|
|
" ORDER BY utc_date_time ") |
603
|
|
|
cursor_historical.execute(query, (point['id'], |
604
|
|
|
reporting_start_datetime_utc, |
605
|
|
|
reporting_end_datetime_utc)) |
606
|
|
|
rows = cursor_historical.fetchall() |
607
|
|
|
|
608
|
|
|
if rows is not None and len(rows) > 0: |
609
|
|
|
for row in rows: |
610
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
611
|
|
|
timedelta(minutes=timezone_offset) |
612
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
613
|
|
|
point_timestamps.append(current_datetime) |
614
|
|
|
point_values.append(row[1]) |
615
|
|
|
elif point['object_type'] == 'DIGITAL_VALUE': |
616
|
|
|
query = (" SELECT utc_date_time, actual_value " |
617
|
|
|
" FROM tbl_digital_value " |
618
|
|
|
" WHERE point_id = %s " |
619
|
|
|
" AND utc_date_time BETWEEN %s AND %s " |
620
|
|
|
" ORDER BY utc_date_time ") |
621
|
|
|
cursor_historical.execute(query, (point['id'], |
622
|
|
|
reporting_start_datetime_utc, |
623
|
|
|
reporting_end_datetime_utc)) |
624
|
|
|
rows = cursor_historical.fetchall() |
625
|
|
|
|
626
|
|
|
if rows is not None and len(rows) > 0: |
627
|
|
|
for row in rows: |
628
|
|
|
current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
629
|
|
|
timedelta(minutes=timezone_offset) |
630
|
|
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
631
|
|
|
point_timestamps.append(current_datetime) |
632
|
|
|
point_values.append(row[1]) |
633
|
|
|
|
634
|
|
|
parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
635
|
|
|
parameters_data['timestamps'].append(point_timestamps) |
636
|
|
|
parameters_data['values'].append(point_values) |
637
|
|
|
|
638
|
|
|
################################################################################################################ |
639
|
|
|
# Step 10: construct the report |
640
|
|
|
################################################################################################################ |
641
|
|
|
if cursor_system: |
642
|
|
|
cursor_system.close() |
643
|
|
|
if cnx_system: |
644
|
|
|
cnx_system.close() |
645
|
|
|
|
646
|
|
|
if cursor_energy: |
647
|
|
|
cursor_energy.close() |
648
|
|
|
if cnx_energy: |
649
|
|
|
cnx_energy.close() |
650
|
|
|
|
651
|
|
|
if cursor_energy_plan: |
652
|
|
|
cursor_energy_plan.close() |
653
|
|
|
if cnx_energy_plan: |
654
|
|
|
cnx_energy_plan.close() |
655
|
|
|
|
656
|
|
|
if cursor_historical: |
657
|
|
|
cursor_historical.close() |
658
|
|
|
if cnx_historical: |
659
|
|
|
cnx_historical.close() |
660
|
|
|
|
661
|
|
|
result = dict() |
662
|
|
|
|
663
|
|
|
result['tenant'] = dict() |
664
|
|
|
result['tenant']['name'] = tenant['name'] |
665
|
|
|
result['tenant']['area'] = tenant['area'] |
666
|
|
|
|
667
|
|
|
result['base_period'] = dict() |
668
|
|
|
result['base_period']['names'] = list() |
669
|
|
|
result['base_period']['units'] = list() |
670
|
|
|
result['base_period']['timestamps'] = list() |
671
|
|
|
result['base_period']['values_saving'] = list() |
672
|
|
|
result['base_period']['subtotals_saving'] = list() |
673
|
|
|
result['base_period']['subtotals_in_kgce_saving'] = list() |
674
|
|
|
result['base_period']['subtotals_in_kgco2e_saving'] = list() |
675
|
|
|
result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
676
|
|
|
result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
677
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
678
|
|
|
for energy_category_id in energy_category_set: |
679
|
|
|
result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
680
|
|
|
result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
681
|
|
|
result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
682
|
|
|
result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
683
|
|
|
result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
684
|
|
|
result['base_period']['subtotals_in_kgce_saving'].append( |
685
|
|
|
base[energy_category_id]['subtotal_in_kgce_saving']) |
686
|
|
|
result['base_period']['subtotals_in_kgco2e_saving'].append( |
687
|
|
|
base[energy_category_id]['subtotal_in_kgco2e_saving']) |
688
|
|
|
result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
689
|
|
|
result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
690
|
|
|
|
691
|
|
|
result['reporting_period'] = dict() |
692
|
|
|
result['reporting_period']['names'] = list() |
693
|
|
|
result['reporting_period']['energy_category_ids'] = list() |
694
|
|
|
result['reporting_period']['units'] = list() |
695
|
|
|
result['reporting_period']['timestamps'] = list() |
696
|
|
|
result['reporting_period']['values_saving'] = list() |
697
|
|
|
result['reporting_period']['rates_saving'] = list() |
698
|
|
|
result['reporting_period']['subtotals_saving'] = list() |
699
|
|
|
result['reporting_period']['subtotals_in_kgce_saving'] = list() |
700
|
|
|
result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
701
|
|
|
result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
702
|
|
|
result['reporting_period']['increment_rates_saving'] = list() |
703
|
|
|
result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
704
|
|
|
result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
705
|
|
|
result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
706
|
|
|
result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
707
|
|
|
|
708
|
|
|
if energy_category_set is not None and len(energy_category_set) > 0: |
709
|
|
|
for energy_category_id in energy_category_set: |
710
|
|
|
result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
711
|
|
|
result['reporting_period']['energy_category_ids'].append(energy_category_id) |
712
|
|
|
result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
713
|
|
|
result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
714
|
|
|
result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
715
|
|
|
result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
716
|
|
|
result['reporting_period']['subtotals_in_kgce_saving'].append( |
717
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_saving']) |
718
|
|
|
result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
719
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
720
|
|
|
result['reporting_period']['subtotals_per_unit_area_saving'].append( |
721
|
|
|
reporting[energy_category_id]['subtotal_saving'] / tenant['area'] |
722
|
|
|
if tenant['area'] != Decimal(0.0) else None) |
723
|
|
|
result['reporting_period']['increment_rates_saving'].append( |
724
|
|
|
(reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
725
|
|
|
base[energy_category_id]['subtotal_saving'] |
726
|
|
|
if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
727
|
|
|
result['reporting_period']['total_in_kgce_saving'] += \ |
728
|
|
|
reporting[energy_category_id]['subtotal_in_kgce_saving'] |
729
|
|
|
result['reporting_period']['total_in_kgco2e_saving'] += \ |
730
|
|
|
reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
731
|
|
|
|
732
|
|
|
rate = list() |
733
|
|
|
for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
734
|
|
|
if index < len(base[energy_category_id]['values_saving']) \ |
735
|
|
|
and base[energy_category_id]['values_saving'][index] != Decimal(0.0) \ |
736
|
|
|
and value != Decimal(0.0): |
737
|
|
|
rate.append((value - base[energy_category_id]['values_saving'][index]) |
738
|
|
|
/ base[energy_category_id]['values_saving'][index]) |
739
|
|
|
else: |
740
|
|
|
rate.append(None) |
741
|
|
|
result['reporting_period']['rates_saving'].append(rate) |
742
|
|
|
|
743
|
|
|
result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
744
|
|
|
result['reporting_period']['total_in_kgce_saving'] / tenant['area'] \ |
745
|
|
|
if tenant['area'] != Decimal(0.0) else None |
746
|
|
|
|
747
|
|
|
result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
748
|
|
|
(result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
749
|
|
|
result['base_period']['total_in_kgce_saving'] \ |
750
|
|
|
if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
751
|
|
|
|
752
|
|
|
result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
753
|
|
|
result['reporting_period']['total_in_kgco2e_saving'] / tenant['area'] if tenant['area'] > 0.0 else None |
754
|
|
|
|
755
|
|
|
result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
756
|
|
|
(result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
757
|
|
|
result['base_period']['total_in_kgco2e_saving'] \ |
758
|
|
|
if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
759
|
|
|
|
760
|
|
|
result['parameters'] = { |
761
|
|
|
"names": parameters_data['names'], |
762
|
|
|
"timestamps": parameters_data['timestamps'], |
763
|
|
|
"values": parameters_data['values'] |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
# export result to Excel file and then encode the file to base64 string |
767
|
|
|
if not is_quick_mode: |
768
|
|
|
result['excel_bytes_base64'] = excelexporters.tenantsaving.export(result, |
769
|
|
|
tenant['name'], |
770
|
|
|
base_period_start_datetime_local, |
771
|
|
|
base_period_end_datetime_local, |
772
|
|
|
reporting_period_start_datetime_local, |
773
|
|
|
reporting_period_end_datetime_local, |
774
|
|
|
period_type, |
775
|
|
|
language) |
776
|
|
|
|
777
|
|
|
resp.text = json.dumps(result) |
778
|
|
|
|