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