@@ 10-568 (lines=559) @@ | ||
7 | from decimal import * |
|
8 | ||
9 | ||
10 | class Reporting: |
|
11 | @staticmethod |
|
12 | def __init__(): |
|
13 | pass |
|
14 | ||
15 | @staticmethod |
|
16 | def on_options(req, resp): |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | ||
19 | #################################################################################################################### |
|
20 | # PROCEDURES |
|
21 | # Step 1: valid parameters |
|
22 | # Step 2: query the 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 input |
|
27 | # Step 7: query reporting period energy input |
|
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_historical = mysql.connector.connect(**config.myems_historical_db) |
|
130 | cursor_historical = cnx_historical.cursor() |
|
131 | ||
132 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
133 | " FROM tbl_shopfloors " |
|
134 | " WHERE id = %s ", (shopfloor_id,)) |
|
135 | row_shopfloor = cursor_system.fetchone() |
|
136 | if row_shopfloor is None: |
|
137 | if cursor_system: |
|
138 | cursor_system.close() |
|
139 | if cnx_system: |
|
140 | cnx_system.disconnect() |
|
141 | ||
142 | if cursor_energy: |
|
143 | cursor_energy.close() |
|
144 | if cnx_energy: |
|
145 | cnx_energy.disconnect() |
|
146 | ||
147 | if cnx_historical: |
|
148 | cnx_historical.close() |
|
149 | if cursor_historical: |
|
150 | cursor_historical.disconnect() |
|
151 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SHOPFLOOR_NOT_FOUND') |
|
152 | ||
153 | shopfloor = dict() |
|
154 | shopfloor['id'] = row_shopfloor[0] |
|
155 | shopfloor['name'] = row_shopfloor[1] |
|
156 | shopfloor['area'] = row_shopfloor[2] |
|
157 | shopfloor['cost_center_id'] = row_shopfloor[3] |
|
158 | ||
159 | ################################################################################################################ |
|
160 | # Step 3: query energy categories |
|
161 | ################################################################################################################ |
|
162 | energy_category_set = set() |
|
163 | # query energy categories in base period |
|
164 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
165 | " FROM tbl_shopfloor_input_category_hourly " |
|
166 | " WHERE shopfloor_id = %s " |
|
167 | " AND start_datetime_utc >= %s " |
|
168 | " AND start_datetime_utc < %s ", |
|
169 | (shopfloor['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
170 | rows_energy_categories = cursor_energy.fetchall() |
|
171 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
172 | for row_energy_category in rows_energy_categories: |
|
173 | energy_category_set.add(row_energy_category[0]) |
|
174 | ||
175 | # query energy categories in reporting period |
|
176 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
177 | " FROM tbl_shopfloor_input_category_hourly " |
|
178 | " WHERE shopfloor_id = %s " |
|
179 | " AND start_datetime_utc >= %s " |
|
180 | " AND start_datetime_utc < %s ", |
|
181 | (shopfloor['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
182 | rows_energy_categories = cursor_energy.fetchall() |
|
183 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
184 | for row_energy_category in rows_energy_categories: |
|
185 | energy_category_set.add(row_energy_category[0]) |
|
186 | ||
187 | # query all energy categories in base period and reporting period |
|
188 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
189 | " FROM tbl_energy_categories " |
|
190 | " ORDER BY id ", ) |
|
191 | rows_energy_categories = cursor_system.fetchall() |
|
192 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
193 | if cursor_system: |
|
194 | cursor_system.close() |
|
195 | if cnx_system: |
|
196 | cnx_system.disconnect() |
|
197 | ||
198 | if cursor_energy: |
|
199 | cursor_energy.close() |
|
200 | if cnx_energy: |
|
201 | cnx_energy.disconnect() |
|
202 | ||
203 | if cnx_historical: |
|
204 | cnx_historical.close() |
|
205 | if cursor_historical: |
|
206 | cursor_historical.disconnect() |
|
207 | raise falcon.HTTPError(falcon.HTTP_404, |
|
208 | title='API.NOT_FOUND', |
|
209 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
210 | energy_category_dict = dict() |
|
211 | for row_energy_category in rows_energy_categories: |
|
212 | if row_energy_category[0] in energy_category_set: |
|
213 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
214 | "unit_of_measure": row_energy_category[2], |
|
215 | "kgce": row_energy_category[3], |
|
216 | "kgco2e": row_energy_category[4]} |
|
217 | ||
218 | ################################################################################################################ |
|
219 | # Step 4: query associated sensors |
|
220 | ################################################################################################################ |
|
221 | point_list = list() |
|
222 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
223 | " FROM tbl_shopfloors st, tbl_sensors se, tbl_shopfloors_sensors ss, " |
|
224 | " tbl_points p, tbl_sensors_points sp " |
|
225 | " WHERE st.id = %s AND st.id = ss.shopfloor_id AND ss.sensor_id = se.id " |
|
226 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
227 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
228 | rows_points = cursor_system.fetchall() |
|
229 | if rows_points is not None and len(rows_points) > 0: |
|
230 | for row in rows_points: |
|
231 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
232 | ||
233 | ################################################################################################################ |
|
234 | # Step 5: query associated points |
|
235 | ################################################################################################################ |
|
236 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
237 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
|
238 | " WHERE s.id = %s AND s.id = sp.shopfloor_id AND sp.point_id = p.id " |
|
239 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
240 | rows_points = cursor_system.fetchall() |
|
241 | if rows_points is not None and len(rows_points) > 0: |
|
242 | for row in rows_points: |
|
243 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
244 | ||
245 | ################################################################################################################ |
|
246 | # Step 6: query base period energy input |
|
247 | ################################################################################################################ |
|
248 | base = dict() |
|
249 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
250 | for energy_category_id in energy_category_set: |
|
251 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
252 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
253 | ||
254 | base[energy_category_id] = dict() |
|
255 | base[energy_category_id]['timestamps'] = list() |
|
256 | base[energy_category_id]['values'] = list() |
|
257 | base[energy_category_id]['subtotal'] = Decimal(0.0) |
|
258 | base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
259 | base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
260 | ||
261 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
262 | " FROM tbl_shopfloor_input_category_hourly " |
|
263 | " WHERE shopfloor_id = %s " |
|
264 | " AND energy_category_id = %s " |
|
265 | " AND start_datetime_utc >= %s " |
|
266 | " AND start_datetime_utc < %s " |
|
267 | " ORDER BY start_datetime_utc ", |
|
268 | (shopfloor['id'], |
|
269 | energy_category_id, |
|
270 | base_start_datetime_utc, |
|
271 | base_end_datetime_utc)) |
|
272 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
273 | ||
274 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
275 | base_start_datetime_utc, |
|
276 | base_end_datetime_utc, |
|
277 | period_type) |
|
278 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
279 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
280 | timedelta(minutes=timezone_offset) |
|
281 | if period_type == 'hourly': |
|
282 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
283 | elif period_type == 'daily': |
|
284 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
285 | elif period_type == 'monthly': |
|
286 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
287 | elif period_type == 'yearly': |
|
288 | current_datetime = current_datetime_local.strftime('%Y') |
|
289 | ||
290 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
291 | else row_shopfloor_periodically[1] |
|
292 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
293 | base[energy_category_id]['values'].append(actual_value) |
|
294 | base[energy_category_id]['subtotal'] += actual_value |
|
295 | base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
296 | base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
297 | ||
298 | ################################################################################################################ |
|
299 | # Step 8: query reporting period energy input |
|
300 | ################################################################################################################ |
|
301 | reporting = dict() |
|
302 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
303 | for energy_category_id in energy_category_set: |
|
304 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
305 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
306 | ||
307 | reporting[energy_category_id] = dict() |
|
308 | reporting[energy_category_id]['timestamps'] = list() |
|
309 | reporting[energy_category_id]['values'] = list() |
|
310 | reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
|
311 | reporting[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
312 | reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
313 | reporting[energy_category_id]['toppeak'] = Decimal(0.0) |
|
314 | reporting[energy_category_id]['onpeak'] = Decimal(0.0) |
|
315 | reporting[energy_category_id]['midpeak'] = Decimal(0.0) |
|
316 | reporting[energy_category_id]['offpeak'] = Decimal(0.0) |
|
317 | ||
318 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
319 | " FROM tbl_shopfloor_input_category_hourly " |
|
320 | " WHERE shopfloor_id = %s " |
|
321 | " AND energy_category_id = %s " |
|
322 | " AND start_datetime_utc >= %s " |
|
323 | " AND start_datetime_utc < %s " |
|
324 | " ORDER BY start_datetime_utc ", |
|
325 | (shopfloor['id'], |
|
326 | energy_category_id, |
|
327 | reporting_start_datetime_utc, |
|
328 | reporting_end_datetime_utc)) |
|
329 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
330 | ||
331 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
332 | reporting_start_datetime_utc, |
|
333 | reporting_end_datetime_utc, |
|
334 | period_type) |
|
335 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
336 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
337 | timedelta(minutes=timezone_offset) |
|
338 | if period_type == 'hourly': |
|
339 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
340 | elif period_type == 'daily': |
|
341 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
342 | elif period_type == 'monthly': |
|
343 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
344 | elif period_type == 'yearly': |
|
345 | current_datetime = current_datetime_local.strftime('%Y') |
|
346 | ||
347 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
348 | else row_shopfloor_periodically[1] |
|
349 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
350 | reporting[energy_category_id]['values'].append(actual_value) |
|
351 | reporting[energy_category_id]['subtotal'] += actual_value |
|
352 | reporting[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
353 | reporting[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
354 | ||
355 | energy_category_tariff_dict = utilities.get_energy_category_peak_types(shopfloor['cost_center_id'], |
|
356 | energy_category_id, |
|
357 | reporting_start_datetime_utc, |
|
358 | reporting_end_datetime_utc) |
|
359 | for row in rows_shopfloor_hourly: |
|
360 | peak_type = energy_category_tariff_dict.get(row[0], None) |
|
361 | if peak_type == 'toppeak': |
|
362 | reporting[energy_category_id]['toppeak'] += row[1] |
|
363 | elif peak_type == 'onpeak': |
|
364 | reporting[energy_category_id]['onpeak'] += row[1] |
|
365 | elif peak_type == 'midpeak': |
|
366 | reporting[energy_category_id]['midpeak'] += row[1] |
|
367 | elif peak_type == 'offpeak': |
|
368 | reporting[energy_category_id]['offpeak'] += row[1] |
|
369 | ||
370 | ################################################################################################################ |
|
371 | # Step 9: query tariff data |
|
372 | ################################################################################################################ |
|
373 | parameters_data = dict() |
|
374 | parameters_data['names'] = list() |
|
375 | parameters_data['timestamps'] = list() |
|
376 | parameters_data['values'] = list() |
|
377 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
378 | for energy_category_id in energy_category_set: |
|
379 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(shopfloor['cost_center_id'], |
|
380 | energy_category_id, |
|
381 | reporting_start_datetime_utc, |
|
382 | reporting_end_datetime_utc) |
|
383 | tariff_timestamp_list = list() |
|
384 | tariff_value_list = list() |
|
385 | for k, v in energy_category_tariff_dict.items(): |
|
386 | # convert k from utc to local |
|
387 | k = k + timedelta(minutes=timezone_offset) |
|
388 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
389 | tariff_value_list.append(v) |
|
390 | ||
391 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
|
392 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
393 | parameters_data['values'].append(tariff_value_list) |
|
394 | ||
395 | ################################################################################################################ |
|
396 | # Step 10: query associated sensors and points data |
|
397 | ################################################################################################################ |
|
398 | for point in point_list: |
|
399 | point_values = [] |
|
400 | point_timestamps = [] |
|
401 | if point['object_type'] == 'ANALOG_VALUE': |
|
402 | query = (" SELECT utc_date_time, actual_value " |
|
403 | " FROM tbl_analog_value " |
|
404 | " WHERE point_id = %s " |
|
405 | " AND utc_date_time BETWEEN %s AND %s " |
|
406 | " ORDER BY utc_date_time ") |
|
407 | cursor_historical.execute(query, (point['id'], |
|
408 | reporting_start_datetime_utc, |
|
409 | reporting_end_datetime_utc)) |
|
410 | rows = cursor_historical.fetchall() |
|
411 | ||
412 | if rows is not None and len(rows) > 0: |
|
413 | for row in rows: |
|
414 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
415 | timedelta(minutes=timezone_offset) |
|
416 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
417 | point_timestamps.append(current_datetime) |
|
418 | point_values.append(row[1]) |
|
419 | ||
420 | elif point['object_type'] == 'ENERGY_VALUE': |
|
421 | query = (" SELECT utc_date_time, actual_value " |
|
422 | " FROM tbl_energy_value " |
|
423 | " WHERE point_id = %s " |
|
424 | " AND utc_date_time BETWEEN %s AND %s " |
|
425 | " ORDER BY utc_date_time ") |
|
426 | cursor_historical.execute(query, (point['id'], |
|
427 | reporting_start_datetime_utc, |
|
428 | reporting_end_datetime_utc)) |
|
429 | rows = cursor_historical.fetchall() |
|
430 | ||
431 | if rows is not None and len(rows) > 0: |
|
432 | for row in rows: |
|
433 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
434 | timedelta(minutes=timezone_offset) |
|
435 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
436 | point_timestamps.append(current_datetime) |
|
437 | point_values.append(row[1]) |
|
438 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
439 | query = (" SELECT utc_date_time, actual_value " |
|
440 | " FROM tbl_digital_value " |
|
441 | " WHERE point_id = %s " |
|
442 | " AND utc_date_time BETWEEN %s AND %s ") |
|
443 | cursor_historical.execute(query, (point['id'], |
|
444 | reporting_start_datetime_utc, |
|
445 | reporting_end_datetime_utc)) |
|
446 | rows = cursor_historical.fetchall() |
|
447 | ||
448 | if rows is not None and len(rows) > 0: |
|
449 | for row in rows: |
|
450 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
451 | timedelta(minutes=timezone_offset) |
|
452 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
453 | point_timestamps.append(current_datetime) |
|
454 | point_values.append(row[1]) |
|
455 | ||
456 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
457 | parameters_data['timestamps'].append(point_timestamps) |
|
458 | parameters_data['values'].append(point_values) |
|
459 | ||
460 | ################################################################################################################ |
|
461 | # Step 12: construct the report |
|
462 | ################################################################################################################ |
|
463 | if cursor_system: |
|
464 | cursor_system.close() |
|
465 | if cnx_system: |
|
466 | cnx_system.disconnect() |
|
467 | ||
468 | if cursor_energy: |
|
469 | cursor_energy.close() |
|
470 | if cnx_energy: |
|
471 | cnx_energy.disconnect() |
|
472 | ||
473 | result = dict() |
|
474 | ||
475 | result['shopfloor'] = dict() |
|
476 | result['shopfloor']['name'] = shopfloor['name'] |
|
477 | result['shopfloor']['area'] = shopfloor['area'] |
|
478 | ||
479 | result['base_period'] = dict() |
|
480 | result['base_period']['names'] = list() |
|
481 | result['base_period']['units'] = list() |
|
482 | result['base_period']['timestamps'] = list() |
|
483 | result['base_period']['values'] = list() |
|
484 | result['base_period']['subtotals'] = list() |
|
485 | result['base_period']['subtotals_in_kgce'] = list() |
|
486 | result['base_period']['subtotals_in_kgco2e'] = list() |
|
487 | result['base_period']['total_in_kgce'] = Decimal(0.0) |
|
488 | result['base_period']['total_in_kgco2e'] = Decimal(0.0) |
|
489 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
490 | for energy_category_id in energy_category_set: |
|
491 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
492 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
493 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
494 | result['base_period']['values'].append(base[energy_category_id]['values']) |
|
495 | result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
|
496 | result['base_period']['subtotals_in_kgce'].append(base[energy_category_id]['subtotal_in_kgce']) |
|
497 | result['base_period']['subtotals_in_kgco2e'].append(base[energy_category_id]['subtotal_in_kgco2e']) |
|
498 | result['base_period']['total_in_kgce'] += base[energy_category_id]['subtotal_in_kgce'] |
|
499 | result['base_period']['total_in_kgco2e'] += base[energy_category_id]['subtotal_in_kgco2e'] |
|
500 | ||
501 | result['reporting_period'] = dict() |
|
502 | result['reporting_period']['names'] = list() |
|
503 | result['reporting_period']['energy_category_ids'] = list() |
|
504 | result['reporting_period']['units'] = list() |
|
505 | result['reporting_period']['timestamps'] = list() |
|
506 | result['reporting_period']['values'] = list() |
|
507 | result['reporting_period']['subtotals'] = list() |
|
508 | result['reporting_period']['subtotals_in_kgce'] = list() |
|
509 | result['reporting_period']['subtotals_in_kgco2e'] = list() |
|
510 | result['reporting_period']['subtotals_per_unit_area'] = list() |
|
511 | result['reporting_period']['toppeaks'] = list() |
|
512 | result['reporting_period']['onpeaks'] = list() |
|
513 | result['reporting_period']['midpeaks'] = list() |
|
514 | result['reporting_period']['offpeaks'] = list() |
|
515 | result['reporting_period']['increment_rates'] = list() |
|
516 | result['reporting_period']['total_in_kgce'] = Decimal(0.0) |
|
517 | result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) |
|
518 | result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) |
|
519 | result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) |
|
520 | ||
521 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
522 | for energy_category_id in energy_category_set: |
|
523 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
524 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
525 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
526 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
527 | result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
|
528 | result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
|
529 | result['reporting_period']['subtotals_in_kgce'].append( |
|
530 | reporting[energy_category_id]['subtotal_in_kgce']) |
|
531 | result['reporting_period']['subtotals_in_kgco2e'].append( |
|
532 | reporting[energy_category_id]['subtotal_in_kgco2e']) |
|
533 | result['reporting_period']['subtotals_per_unit_area'].append( |
|
534 | reporting[energy_category_id]['subtotal'] / shopfloor['area'] if shopfloor['area'] > 0.0 else None) |
|
535 | result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak']) |
|
536 | result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak']) |
|
537 | result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak']) |
|
538 | result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak']) |
|
539 | result['reporting_period']['increment_rates'].append( |
|
540 | (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
|
541 | base[energy_category_id]['subtotal'] |
|
542 | if base[energy_category_id]['subtotal'] > 0.0 else None) |
|
543 | result['reporting_period']['total_in_kgce'] += reporting[energy_category_id]['subtotal_in_kgce'] |
|
544 | result['reporting_period']['total_in_kgco2e'] += reporting[energy_category_id]['subtotal_in_kgco2e'] |
|
545 | ||
546 | result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ |
|
547 | result['reporting_period']['total_in_kgce'] / shopfloor['area'] if shopfloor['area'] > 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'] / shopfloor['area'] if shopfloor['area'] > 0.0 else None |
|
556 | ||
557 | result['reporting_period']['increment_rate_in_kgco2e'] = \ |
|
558 | (result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ |
|
559 | result['base_period']['total_in_kgco2e'] \ |
|
560 | if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None |
|
561 | ||
562 | result['parameters'] = { |
|
563 | "names": parameters_data['names'], |
|
564 | "timestamps": parameters_data['timestamps'], |
|
565 | "values": parameters_data['values'] |
|
566 | } |
|
567 | ||
568 | resp.body = json.dumps(result) |
|
569 |
@@ 10-566 (lines=557) @@ | ||
7 | from decimal import * |
|
8 | ||
9 | ||
10 | class Reporting: |
|
11 | @staticmethod |
|
12 | def __init__(): |
|
13 | pass |
|
14 | ||
15 | @staticmethod |
|
16 | def on_options(req, resp): |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | ||
19 | #################################################################################################################### |
|
20 | # PROCEDURES |
|
21 | # Step 1: valid parameters |
|
22 | # Step 2: query the store |
|
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 input |
|
27 | # Step 7: query reporting period energy input |
|
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 | store_id = req.params.get('storeid') |
|
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 store_id is None: |
|
46 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') |
|
47 | else: |
|
48 | store_id = str.strip(store_id) |
|
49 | if not store_id.isdigit() or int(store_id) <= 0: |
|
50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_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 store |
|
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_historical = mysql.connector.connect(**config.myems_historical_db) |
|
130 | cursor_historical = cnx_historical.cursor() |
|
131 | ||
132 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
133 | " FROM tbl_stores " |
|
134 | " WHERE id = %s ", (store_id,)) |
|
135 | row_store = cursor_system.fetchone() |
|
136 | if row_store is None: |
|
137 | if cursor_system: |
|
138 | cursor_system.close() |
|
139 | if cnx_system: |
|
140 | cnx_system.disconnect() |
|
141 | ||
142 | if cursor_energy: |
|
143 | cursor_energy.close() |
|
144 | if cnx_energy: |
|
145 | cnx_energy.disconnect() |
|
146 | ||
147 | if cnx_historical: |
|
148 | cnx_historical.close() |
|
149 | if cursor_historical: |
|
150 | cursor_historical.disconnect() |
|
151 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND') |
|
152 | ||
153 | store = dict() |
|
154 | store['id'] = row_store[0] |
|
155 | store['name'] = row_store[1] |
|
156 | store['area'] = row_store[2] |
|
157 | store['cost_center_id'] = row_store[3] |
|
158 | ||
159 | ################################################################################################################ |
|
160 | # Step 3: query energy categories |
|
161 | ################################################################################################################ |
|
162 | energy_category_set = set() |
|
163 | # query energy categories in base period |
|
164 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
165 | " FROM tbl_store_input_category_hourly " |
|
166 | " WHERE store_id = %s " |
|
167 | " AND start_datetime_utc >= %s " |
|
168 | " AND start_datetime_utc < %s ", |
|
169 | (store['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
170 | rows_energy_categories = cursor_energy.fetchall() |
|
171 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
172 | for row_energy_category in rows_energy_categories: |
|
173 | energy_category_set.add(row_energy_category[0]) |
|
174 | ||
175 | # query energy categories in reporting period |
|
176 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
177 | " FROM tbl_store_input_category_hourly " |
|
178 | " WHERE store_id = %s " |
|
179 | " AND start_datetime_utc >= %s " |
|
180 | " AND start_datetime_utc < %s ", |
|
181 | (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
182 | rows_energy_categories = cursor_energy.fetchall() |
|
183 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
184 | for row_energy_category in rows_energy_categories: |
|
185 | energy_category_set.add(row_energy_category[0]) |
|
186 | ||
187 | # query all energy categories in base period and reporting period |
|
188 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
189 | " FROM tbl_energy_categories " |
|
190 | " ORDER BY id ", ) |
|
191 | rows_energy_categories = cursor_system.fetchall() |
|
192 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
193 | if cursor_system: |
|
194 | cursor_system.close() |
|
195 | if cnx_system: |
|
196 | cnx_system.disconnect() |
|
197 | ||
198 | if cursor_energy: |
|
199 | cursor_energy.close() |
|
200 | if cnx_energy: |
|
201 | cnx_energy.disconnect() |
|
202 | ||
203 | if cnx_historical: |
|
204 | cnx_historical.close() |
|
205 | if cursor_historical: |
|
206 | cursor_historical.disconnect() |
|
207 | raise falcon.HTTPError(falcon.HTTP_404, |
|
208 | title='API.NOT_FOUND', |
|
209 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
210 | energy_category_dict = dict() |
|
211 | for row_energy_category in rows_energy_categories: |
|
212 | if row_energy_category[0] in energy_category_set: |
|
213 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
214 | "unit_of_measure": row_energy_category[2], |
|
215 | "kgce": row_energy_category[3], |
|
216 | "kgco2e": row_energy_category[4]} |
|
217 | ||
218 | ################################################################################################################ |
|
219 | # Step 4: query associated sensors |
|
220 | ################################################################################################################ |
|
221 | point_list = list() |
|
222 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
223 | " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, " |
|
224 | " tbl_points p, tbl_sensors_points sp " |
|
225 | " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id " |
|
226 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
227 | " ORDER BY p.id ", (store['id'],)) |
|
228 | rows_points = cursor_system.fetchall() |
|
229 | if rows_points is not None and len(rows_points) > 0: |
|
230 | for row in rows_points: |
|
231 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
232 | ||
233 | ################################################################################################################ |
|
234 | # Step 5: query associated points |
|
235 | ################################################################################################################ |
|
236 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
237 | " FROM tbl_stores s, tbl_stores_points sp, tbl_points p " |
|
238 | " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id " |
|
239 | " ORDER BY p.id ", (store['id'],)) |
|
240 | rows_points = cursor_system.fetchall() |
|
241 | if rows_points is not None and len(rows_points) > 0: |
|
242 | for row in rows_points: |
|
243 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
244 | ||
245 | ################################################################################################################ |
|
246 | # Step 6: query base period energy input |
|
247 | ################################################################################################################ |
|
248 | base = dict() |
|
249 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
250 | for energy_category_id in energy_category_set: |
|
251 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
252 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
253 | ||
254 | base[energy_category_id] = dict() |
|
255 | base[energy_category_id]['timestamps'] = list() |
|
256 | base[energy_category_id]['values'] = list() |
|
257 | base[energy_category_id]['subtotal'] = Decimal(0.0) |
|
258 | base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
259 | base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
260 | ||
261 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
262 | " FROM tbl_store_input_category_hourly " |
|
263 | " WHERE store_id = %s " |
|
264 | " AND energy_category_id = %s " |
|
265 | " AND start_datetime_utc >= %s " |
|
266 | " AND start_datetime_utc < %s " |
|
267 | " ORDER BY start_datetime_utc ", |
|
268 | (store['id'], |
|
269 | energy_category_id, |
|
270 | base_start_datetime_utc, |
|
271 | base_end_datetime_utc)) |
|
272 | rows_store_hourly = cursor_energy.fetchall() |
|
273 | ||
274 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
275 | base_start_datetime_utc, |
|
276 | base_end_datetime_utc, |
|
277 | period_type) |
|
278 | for row_store_periodically in rows_store_periodically: |
|
279 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
280 | timedelta(minutes=timezone_offset) |
|
281 | if period_type == 'hourly': |
|
282 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
283 | elif period_type == 'daily': |
|
284 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
285 | elif period_type == 'monthly': |
|
286 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
287 | elif period_type == 'yearly': |
|
288 | current_datetime = current_datetime_local.strftime('%Y') |
|
289 | ||
290 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
291 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
292 | base[energy_category_id]['values'].append(actual_value) |
|
293 | base[energy_category_id]['subtotal'] += actual_value |
|
294 | base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
295 | base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
296 | ||
297 | ################################################################################################################ |
|
298 | # Step 8: query reporting period energy input |
|
299 | ################################################################################################################ |
|
300 | reporting = dict() |
|
301 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
302 | for energy_category_id in energy_category_set: |
|
303 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
304 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
305 | ||
306 | reporting[energy_category_id] = dict() |
|
307 | reporting[energy_category_id]['timestamps'] = list() |
|
308 | reporting[energy_category_id]['values'] = list() |
|
309 | reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
|
310 | reporting[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
311 | reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
312 | reporting[energy_category_id]['toppeak'] = Decimal(0.0) |
|
313 | reporting[energy_category_id]['onpeak'] = Decimal(0.0) |
|
314 | reporting[energy_category_id]['midpeak'] = Decimal(0.0) |
|
315 | reporting[energy_category_id]['offpeak'] = Decimal(0.0) |
|
316 | ||
317 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
318 | " FROM tbl_store_input_category_hourly " |
|
319 | " WHERE store_id = %s " |
|
320 | " AND energy_category_id = %s " |
|
321 | " AND start_datetime_utc >= %s " |
|
322 | " AND start_datetime_utc < %s " |
|
323 | " ORDER BY start_datetime_utc ", |
|
324 | (store['id'], |
|
325 | energy_category_id, |
|
326 | reporting_start_datetime_utc, |
|
327 | reporting_end_datetime_utc)) |
|
328 | rows_store_hourly = cursor_energy.fetchall() |
|
329 | ||
330 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
331 | reporting_start_datetime_utc, |
|
332 | reporting_end_datetime_utc, |
|
333 | period_type) |
|
334 | for row_store_periodically in rows_store_periodically: |
|
335 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
336 | timedelta(minutes=timezone_offset) |
|
337 | if period_type == 'hourly': |
|
338 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
339 | elif period_type == 'daily': |
|
340 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
341 | elif period_type == 'monthly': |
|
342 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
343 | elif period_type == 'yearly': |
|
344 | current_datetime = current_datetime_local.strftime('%Y') |
|
345 | ||
346 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
347 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
348 | reporting[energy_category_id]['values'].append(actual_value) |
|
349 | reporting[energy_category_id]['subtotal'] += actual_value |
|
350 | reporting[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
351 | reporting[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
352 | ||
353 | energy_category_tariff_dict = utilities.get_energy_category_peak_types(store['cost_center_id'], |
|
354 | energy_category_id, |
|
355 | reporting_start_datetime_utc, |
|
356 | reporting_end_datetime_utc) |
|
357 | for row in rows_store_hourly: |
|
358 | peak_type = energy_category_tariff_dict.get(row[0], None) |
|
359 | if peak_type == 'toppeak': |
|
360 | reporting[energy_category_id]['toppeak'] += row[1] |
|
361 | elif peak_type == 'onpeak': |
|
362 | reporting[energy_category_id]['onpeak'] += row[1] |
|
363 | elif peak_type == 'midpeak': |
|
364 | reporting[energy_category_id]['midpeak'] += row[1] |
|
365 | elif peak_type == 'offpeak': |
|
366 | reporting[energy_category_id]['offpeak'] += row[1] |
|
367 | ||
368 | ################################################################################################################ |
|
369 | # Step 9: query tariff data |
|
370 | ################################################################################################################ |
|
371 | parameters_data = dict() |
|
372 | parameters_data['names'] = list() |
|
373 | parameters_data['timestamps'] = list() |
|
374 | parameters_data['values'] = list() |
|
375 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
376 | for energy_category_id in energy_category_set: |
|
377 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(store['cost_center_id'], |
|
378 | energy_category_id, |
|
379 | reporting_start_datetime_utc, |
|
380 | reporting_end_datetime_utc) |
|
381 | tariff_timestamp_list = list() |
|
382 | tariff_value_list = list() |
|
383 | for k, v in energy_category_tariff_dict.items(): |
|
384 | # convert k from utc to local |
|
385 | k = k + timedelta(minutes=timezone_offset) |
|
386 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
387 | tariff_value_list.append(v) |
|
388 | ||
389 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
|
390 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
391 | parameters_data['values'].append(tariff_value_list) |
|
392 | ||
393 | ################################################################################################################ |
|
394 | # Step 10: query associated sensors and points data |
|
395 | ################################################################################################################ |
|
396 | for point in point_list: |
|
397 | point_values = [] |
|
398 | point_timestamps = [] |
|
399 | if point['object_type'] == 'ANALOG_VALUE': |
|
400 | query = (" SELECT utc_date_time, actual_value " |
|
401 | " FROM tbl_analog_value " |
|
402 | " WHERE point_id = %s " |
|
403 | " AND utc_date_time BETWEEN %s AND %s " |
|
404 | " ORDER BY utc_date_time ") |
|
405 | cursor_historical.execute(query, (point['id'], |
|
406 | reporting_start_datetime_utc, |
|
407 | reporting_end_datetime_utc)) |
|
408 | rows = cursor_historical.fetchall() |
|
409 | ||
410 | if rows is not None and len(rows) > 0: |
|
411 | for row in rows: |
|
412 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
413 | timedelta(minutes=timezone_offset) |
|
414 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
415 | point_timestamps.append(current_datetime) |
|
416 | point_values.append(row[1]) |
|
417 | ||
418 | elif point['object_type'] == 'ENERGY_VALUE': |
|
419 | query = (" SELECT utc_date_time, actual_value " |
|
420 | " FROM tbl_energy_value " |
|
421 | " WHERE point_id = %s " |
|
422 | " AND utc_date_time BETWEEN %s AND %s " |
|
423 | " ORDER BY utc_date_time ") |
|
424 | cursor_historical.execute(query, (point['id'], |
|
425 | reporting_start_datetime_utc, |
|
426 | reporting_end_datetime_utc)) |
|
427 | rows = cursor_historical.fetchall() |
|
428 | ||
429 | if rows is not None and len(rows) > 0: |
|
430 | for row in rows: |
|
431 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
432 | timedelta(minutes=timezone_offset) |
|
433 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
434 | point_timestamps.append(current_datetime) |
|
435 | point_values.append(row[1]) |
|
436 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
437 | query = (" SELECT utc_date_time, actual_value " |
|
438 | " FROM tbl_digital_value " |
|
439 | " WHERE point_id = %s " |
|
440 | " AND utc_date_time BETWEEN %s AND %s ") |
|
441 | cursor_historical.execute(query, (point['id'], |
|
442 | reporting_start_datetime_utc, |
|
443 | reporting_end_datetime_utc)) |
|
444 | rows = cursor_historical.fetchall() |
|
445 | ||
446 | if rows is not None and len(rows) > 0: |
|
447 | for row in rows: |
|
448 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
449 | timedelta(minutes=timezone_offset) |
|
450 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
451 | point_timestamps.append(current_datetime) |
|
452 | point_values.append(row[1]) |
|
453 | ||
454 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
455 | parameters_data['timestamps'].append(point_timestamps) |
|
456 | parameters_data['values'].append(point_values) |
|
457 | ||
458 | ################################################################################################################ |
|
459 | # Step 12: construct the report |
|
460 | ################################################################################################################ |
|
461 | if cursor_system: |
|
462 | cursor_system.close() |
|
463 | if cnx_system: |
|
464 | cnx_system.disconnect() |
|
465 | ||
466 | if cursor_energy: |
|
467 | cursor_energy.close() |
|
468 | if cnx_energy: |
|
469 | cnx_energy.disconnect() |
|
470 | ||
471 | result = dict() |
|
472 | ||
473 | result['store'] = dict() |
|
474 | result['store']['name'] = store['name'] |
|
475 | result['store']['area'] = store['area'] |
|
476 | ||
477 | result['base_period'] = dict() |
|
478 | result['base_period']['names'] = list() |
|
479 | result['base_period']['units'] = list() |
|
480 | result['base_period']['timestamps'] = list() |
|
481 | result['base_period']['values'] = list() |
|
482 | result['base_period']['subtotals'] = list() |
|
483 | result['base_period']['subtotals_in_kgce'] = list() |
|
484 | result['base_period']['subtotals_in_kgco2e'] = list() |
|
485 | result['base_period']['total_in_kgce'] = Decimal(0.0) |
|
486 | result['base_period']['total_in_kgco2e'] = Decimal(0.0) |
|
487 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
488 | for energy_category_id in energy_category_set: |
|
489 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
490 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
491 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
492 | result['base_period']['values'].append(base[energy_category_id]['values']) |
|
493 | result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
|
494 | result['base_period']['subtotals_in_kgce'].append(base[energy_category_id]['subtotal_in_kgce']) |
|
495 | result['base_period']['subtotals_in_kgco2e'].append(base[energy_category_id]['subtotal_in_kgco2e']) |
|
496 | result['base_period']['total_in_kgce'] += base[energy_category_id]['subtotal_in_kgce'] |
|
497 | result['base_period']['total_in_kgco2e'] += base[energy_category_id]['subtotal_in_kgco2e'] |
|
498 | ||
499 | result['reporting_period'] = dict() |
|
500 | result['reporting_period']['names'] = list() |
|
501 | result['reporting_period']['energy_category_ids'] = list() |
|
502 | result['reporting_period']['units'] = list() |
|
503 | result['reporting_period']['timestamps'] = list() |
|
504 | result['reporting_period']['values'] = list() |
|
505 | result['reporting_period']['subtotals'] = list() |
|
506 | result['reporting_period']['subtotals_in_kgce'] = list() |
|
507 | result['reporting_period']['subtotals_in_kgco2e'] = list() |
|
508 | result['reporting_period']['subtotals_per_unit_area'] = list() |
|
509 | result['reporting_period']['toppeaks'] = list() |
|
510 | result['reporting_period']['onpeaks'] = list() |
|
511 | result['reporting_period']['midpeaks'] = list() |
|
512 | result['reporting_period']['offpeaks'] = list() |
|
513 | result['reporting_period']['increment_rates'] = list() |
|
514 | result['reporting_period']['total_in_kgce'] = Decimal(0.0) |
|
515 | result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) |
|
516 | result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) |
|
517 | result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) |
|
518 | ||
519 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
520 | for energy_category_id in energy_category_set: |
|
521 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
522 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
523 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
524 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
525 | result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
|
526 | result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
|
527 | result['reporting_period']['subtotals_in_kgce'].append( |
|
528 | reporting[energy_category_id]['subtotal_in_kgce']) |
|
529 | result['reporting_period']['subtotals_in_kgco2e'].append( |
|
530 | reporting[energy_category_id]['subtotal_in_kgco2e']) |
|
531 | result['reporting_period']['subtotals_per_unit_area'].append( |
|
532 | reporting[energy_category_id]['subtotal'] / store['area'] if store['area'] > 0.0 else None) |
|
533 | result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak']) |
|
534 | result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak']) |
|
535 | result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak']) |
|
536 | result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak']) |
|
537 | result['reporting_period']['increment_rates'].append( |
|
538 | (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
|
539 | base[energy_category_id]['subtotal'] |
|
540 | if base[energy_category_id]['subtotal'] > 0.0 else None) |
|
541 | result['reporting_period']['total_in_kgce'] += reporting[energy_category_id]['subtotal_in_kgce'] |
|
542 | result['reporting_period']['total_in_kgco2e'] += reporting[energy_category_id]['subtotal_in_kgco2e'] |
|
543 | ||
544 | result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ |
|
545 | result['reporting_period']['total_in_kgce'] / store['area'] if store['area'] > 0.0 else None |
|
546 | ||
547 | result['reporting_period']['increment_rate_in_kgce'] = \ |
|
548 | (result['reporting_period']['total_in_kgce'] - result['base_period']['total_in_kgce']) / \ |
|
549 | result['base_period']['total_in_kgce'] \ |
|
550 | if result['base_period']['total_in_kgce'] > Decimal(0.0) else None |
|
551 | ||
552 | result['reporting_period']['total_in_kgce_per_unit_area'] = \ |
|
553 | result['reporting_period']['total_in_kgco2e'] / store['area'] if store['area'] > 0.0 else None |
|
554 | ||
555 | result['reporting_period']['increment_rate_in_kgco2e'] = \ |
|
556 | (result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ |
|
557 | result['base_period']['total_in_kgco2e'] \ |
|
558 | if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None |
|
559 | ||
560 | result['parameters'] = { |
|
561 | "names": parameters_data['names'], |
|
562 | "timestamps": parameters_data['timestamps'], |
|
563 | "values": parameters_data['values'] |
|
564 | } |
|
565 | ||
566 | resp.body = json.dumps(result) |
|
567 |
@@ 10-566 (lines=557) @@ | ||
7 | from decimal import * |
|
8 | ||
9 | ||
10 | class Reporting: |
|
11 | @staticmethod |
|
12 | def __init__(): |
|
13 | pass |
|
14 | ||
15 | @staticmethod |
|
16 | def on_options(req, resp): |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | ||
19 | #################################################################################################################### |
|
20 | # PROCEDURES |
|
21 | # Step 1: valid parameters |
|
22 | # Step 2: query the tenant |
|
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 input |
|
27 | # Step 7: query reporting period energy input |
|
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 | tenant_id = req.params.get('tenantid') |
|
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 tenant_id is None: |
|
46 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') |
|
47 | else: |
|
48 | tenant_id = str.strip(tenant_id) |
|
49 | if not tenant_id.isdigit() or int(tenant_id) <= 0: |
|
50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_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 tenant |
|
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_historical = mysql.connector.connect(**config.myems_historical_db) |
|
130 | cursor_historical = cnx_historical.cursor() |
|
131 | ||
132 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
133 | " FROM tbl_tenants " |
|
134 | " WHERE id = %s ", (tenant_id,)) |
|
135 | row_tenant = cursor_system.fetchone() |
|
136 | if row_tenant is None: |
|
137 | if cursor_system: |
|
138 | cursor_system.close() |
|
139 | if cnx_system: |
|
140 | cnx_system.disconnect() |
|
141 | ||
142 | if cursor_energy: |
|
143 | cursor_energy.close() |
|
144 | if cnx_energy: |
|
145 | cnx_energy.disconnect() |
|
146 | ||
147 | if cnx_historical: |
|
148 | cnx_historical.close() |
|
149 | if cursor_historical: |
|
150 | cursor_historical.disconnect() |
|
151 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') |
|
152 | ||
153 | tenant = dict() |
|
154 | tenant['id'] = row_tenant[0] |
|
155 | tenant['name'] = row_tenant[1] |
|
156 | tenant['area'] = row_tenant[2] |
|
157 | tenant['cost_center_id'] = row_tenant[3] |
|
158 | ||
159 | ################################################################################################################ |
|
160 | # Step 3: query energy categories |
|
161 | ################################################################################################################ |
|
162 | energy_category_set = set() |
|
163 | # query energy categories in base period |
|
164 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
165 | " FROM tbl_tenant_input_category_hourly " |
|
166 | " WHERE tenant_id = %s " |
|
167 | " AND start_datetime_utc >= %s " |
|
168 | " AND start_datetime_utc < %s ", |
|
169 | (tenant['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
170 | rows_energy_categories = cursor_energy.fetchall() |
|
171 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
172 | for row_energy_category in rows_energy_categories: |
|
173 | energy_category_set.add(row_energy_category[0]) |
|
174 | ||
175 | # query energy categories in reporting period |
|
176 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
177 | " FROM tbl_tenant_input_category_hourly " |
|
178 | " WHERE tenant_id = %s " |
|
179 | " AND start_datetime_utc >= %s " |
|
180 | " AND start_datetime_utc < %s ", |
|
181 | (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
182 | rows_energy_categories = cursor_energy.fetchall() |
|
183 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
184 | for row_energy_category in rows_energy_categories: |
|
185 | energy_category_set.add(row_energy_category[0]) |
|
186 | ||
187 | # query all energy categories in base period and reporting period |
|
188 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
189 | " FROM tbl_energy_categories " |
|
190 | " ORDER BY id ", ) |
|
191 | rows_energy_categories = cursor_system.fetchall() |
|
192 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
193 | if cursor_system: |
|
194 | cursor_system.close() |
|
195 | if cnx_system: |
|
196 | cnx_system.disconnect() |
|
197 | ||
198 | if cursor_energy: |
|
199 | cursor_energy.close() |
|
200 | if cnx_energy: |
|
201 | cnx_energy.disconnect() |
|
202 | ||
203 | if cnx_historical: |
|
204 | cnx_historical.close() |
|
205 | if cursor_historical: |
|
206 | cursor_historical.disconnect() |
|
207 | raise falcon.HTTPError(falcon.HTTP_404, |
|
208 | title='API.NOT_FOUND', |
|
209 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
210 | energy_category_dict = dict() |
|
211 | for row_energy_category in rows_energy_categories: |
|
212 | if row_energy_category[0] in energy_category_set: |
|
213 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
214 | "unit_of_measure": row_energy_category[2], |
|
215 | "kgce": row_energy_category[3], |
|
216 | "kgco2e": row_energy_category[4]} |
|
217 | ||
218 | ################################################################################################################ |
|
219 | # Step 4: query associated sensors |
|
220 | ################################################################################################################ |
|
221 | point_list = list() |
|
222 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
223 | " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, " |
|
224 | " tbl_points p, tbl_sensors_points sp " |
|
225 | " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id " |
|
226 | " AND s.id = sp.sensor_id AND sp.point_id = p.id " |
|
227 | " ORDER BY p.id ", (tenant['id'], )) |
|
228 | rows_points = cursor_system.fetchall() |
|
229 | if rows_points is not None and len(rows_points) > 0: |
|
230 | for row in rows_points: |
|
231 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
232 | ||
233 | ################################################################################################################ |
|
234 | # Step 5: query associated points |
|
235 | ################################################################################################################ |
|
236 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
237 | " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
|
238 | " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id " |
|
239 | " ORDER BY p.id ", (tenant['id'], )) |
|
240 | rows_points = cursor_system.fetchall() |
|
241 | if rows_points is not None and len(rows_points) > 0: |
|
242 | for row in rows_points: |
|
243 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
244 | ||
245 | ################################################################################################################ |
|
246 | # Step 6: query base period energy input |
|
247 | ################################################################################################################ |
|
248 | base = dict() |
|
249 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
250 | for energy_category_id in energy_category_set: |
|
251 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
252 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
253 | ||
254 | base[energy_category_id] = dict() |
|
255 | base[energy_category_id]['timestamps'] = list() |
|
256 | base[energy_category_id]['values'] = list() |
|
257 | base[energy_category_id]['subtotal'] = Decimal(0.0) |
|
258 | base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
259 | base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
260 | ||
261 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
262 | " FROM tbl_tenant_input_category_hourly " |
|
263 | " WHERE tenant_id = %s " |
|
264 | " AND energy_category_id = %s " |
|
265 | " AND start_datetime_utc >= %s " |
|
266 | " AND start_datetime_utc < %s " |
|
267 | " ORDER BY start_datetime_utc ", |
|
268 | (tenant['id'], |
|
269 | energy_category_id, |
|
270 | base_start_datetime_utc, |
|
271 | base_end_datetime_utc)) |
|
272 | rows_tenant_hourly = cursor_energy.fetchall() |
|
273 | ||
274 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
275 | base_start_datetime_utc, |
|
276 | base_end_datetime_utc, |
|
277 | period_type) |
|
278 | for row_tenant_periodically in rows_tenant_periodically: |
|
279 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
280 | timedelta(minutes=timezone_offset) |
|
281 | if period_type == 'hourly': |
|
282 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
283 | elif period_type == 'daily': |
|
284 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
285 | elif period_type == 'monthly': |
|
286 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
287 | elif period_type == 'yearly': |
|
288 | current_datetime = current_datetime_local.strftime('%Y') |
|
289 | ||
290 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
291 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
292 | base[energy_category_id]['values'].append(actual_value) |
|
293 | base[energy_category_id]['subtotal'] += actual_value |
|
294 | base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
295 | base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
296 | ||
297 | ################################################################################################################ |
|
298 | # Step 8: query reporting period energy input |
|
299 | ################################################################################################################ |
|
300 | reporting = dict() |
|
301 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
302 | for energy_category_id in energy_category_set: |
|
303 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
304 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
305 | ||
306 | reporting[energy_category_id] = dict() |
|
307 | reporting[energy_category_id]['timestamps'] = list() |
|
308 | reporting[energy_category_id]['values'] = list() |
|
309 | reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
|
310 | reporting[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) |
|
311 | reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) |
|
312 | reporting[energy_category_id]['toppeak'] = Decimal(0.0) |
|
313 | reporting[energy_category_id]['onpeak'] = Decimal(0.0) |
|
314 | reporting[energy_category_id]['midpeak'] = Decimal(0.0) |
|
315 | reporting[energy_category_id]['offpeak'] = Decimal(0.0) |
|
316 | ||
317 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
318 | " FROM tbl_tenant_input_category_hourly " |
|
319 | " WHERE tenant_id = %s " |
|
320 | " AND energy_category_id = %s " |
|
321 | " AND start_datetime_utc >= %s " |
|
322 | " AND start_datetime_utc < %s " |
|
323 | " ORDER BY start_datetime_utc ", |
|
324 | (tenant['id'], |
|
325 | energy_category_id, |
|
326 | reporting_start_datetime_utc, |
|
327 | reporting_end_datetime_utc)) |
|
328 | rows_tenant_hourly = cursor_energy.fetchall() |
|
329 | ||
330 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
331 | reporting_start_datetime_utc, |
|
332 | reporting_end_datetime_utc, |
|
333 | period_type) |
|
334 | for row_tenant_periodically in rows_tenant_periodically: |
|
335 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
336 | timedelta(minutes=timezone_offset) |
|
337 | if period_type == 'hourly': |
|
338 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
339 | elif period_type == 'daily': |
|
340 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
341 | elif period_type == 'monthly': |
|
342 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
343 | elif period_type == 'yearly': |
|
344 | current_datetime = current_datetime_local.strftime('%Y') |
|
345 | ||
346 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
347 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
348 | reporting[energy_category_id]['values'].append(actual_value) |
|
349 | reporting[energy_category_id]['subtotal'] += actual_value |
|
350 | reporting[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce |
|
351 | reporting[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e |
|
352 | ||
353 | energy_category_tariff_dict = utilities.get_energy_category_peak_types(tenant['cost_center_id'], |
|
354 | energy_category_id, |
|
355 | reporting_start_datetime_utc, |
|
356 | reporting_end_datetime_utc) |
|
357 | for row in rows_tenant_hourly: |
|
358 | peak_type = energy_category_tariff_dict.get(row[0], None) |
|
359 | if peak_type == 'toppeak': |
|
360 | reporting[energy_category_id]['toppeak'] += row[1] |
|
361 | elif peak_type == 'onpeak': |
|
362 | reporting[energy_category_id]['onpeak'] += row[1] |
|
363 | elif peak_type == 'midpeak': |
|
364 | reporting[energy_category_id]['midpeak'] += row[1] |
|
365 | elif peak_type == 'offpeak': |
|
366 | reporting[energy_category_id]['offpeak'] += row[1] |
|
367 | ||
368 | ################################################################################################################ |
|
369 | # Step 9: query tariff data |
|
370 | ################################################################################################################ |
|
371 | parameters_data = dict() |
|
372 | parameters_data['names'] = list() |
|
373 | parameters_data['timestamps'] = list() |
|
374 | parameters_data['values'] = list() |
|
375 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
376 | for energy_category_id in energy_category_set: |
|
377 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'], |
|
378 | energy_category_id, |
|
379 | reporting_start_datetime_utc, |
|
380 | reporting_end_datetime_utc) |
|
381 | tariff_timestamp_list = list() |
|
382 | tariff_value_list = list() |
|
383 | for k, v in energy_category_tariff_dict.items(): |
|
384 | # convert k from utc to local |
|
385 | k = k + timedelta(minutes=timezone_offset) |
|
386 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
387 | tariff_value_list.append(v) |
|
388 | ||
389 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
|
390 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
391 | parameters_data['values'].append(tariff_value_list) |
|
392 | ||
393 | ################################################################################################################ |
|
394 | # Step 10: query associated sensors and points data |
|
395 | ################################################################################################################ |
|
396 | for point in point_list: |
|
397 | point_values = [] |
|
398 | point_timestamps = [] |
|
399 | if point['object_type'] == 'ANALOG_VALUE': |
|
400 | query = (" SELECT utc_date_time, actual_value " |
|
401 | " FROM tbl_analog_value " |
|
402 | " WHERE point_id = %s " |
|
403 | " AND utc_date_time BETWEEN %s AND %s " |
|
404 | " ORDER BY utc_date_time ") |
|
405 | cursor_historical.execute(query, (point['id'], |
|
406 | reporting_start_datetime_utc, |
|
407 | reporting_end_datetime_utc)) |
|
408 | rows = cursor_historical.fetchall() |
|
409 | ||
410 | if rows is not None and len(rows) > 0: |
|
411 | for row in rows: |
|
412 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
413 | timedelta(minutes=timezone_offset) |
|
414 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
415 | point_timestamps.append(current_datetime) |
|
416 | point_values.append(row[1]) |
|
417 | ||
418 | elif point['object_type'] == 'ENERGY_VALUE': |
|
419 | query = (" SELECT utc_date_time, actual_value " |
|
420 | " FROM tbl_energy_value " |
|
421 | " WHERE point_id = %s " |
|
422 | " AND utc_date_time BETWEEN %s AND %s " |
|
423 | " ORDER BY utc_date_time ") |
|
424 | cursor_historical.execute(query, (point['id'], |
|
425 | reporting_start_datetime_utc, |
|
426 | reporting_end_datetime_utc)) |
|
427 | rows = cursor_historical.fetchall() |
|
428 | ||
429 | if rows is not None and len(rows) > 0: |
|
430 | for row in rows: |
|
431 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
432 | timedelta(minutes=timezone_offset) |
|
433 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
434 | point_timestamps.append(current_datetime) |
|
435 | point_values.append(row[1]) |
|
436 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
437 | query = (" SELECT utc_date_time, actual_value " |
|
438 | " FROM tbl_digital_value " |
|
439 | " WHERE point_id = %s " |
|
440 | " AND utc_date_time BETWEEN %s AND %s ") |
|
441 | cursor_historical.execute(query, (point['id'], |
|
442 | reporting_start_datetime_utc, |
|
443 | reporting_end_datetime_utc)) |
|
444 | rows = cursor_historical.fetchall() |
|
445 | ||
446 | if rows is not None and len(rows) > 0: |
|
447 | for row in rows: |
|
448 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
449 | timedelta(minutes=timezone_offset) |
|
450 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
451 | point_timestamps.append(current_datetime) |
|
452 | point_values.append(row[1]) |
|
453 | ||
454 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
455 | parameters_data['timestamps'].append(point_timestamps) |
|
456 | parameters_data['values'].append(point_values) |
|
457 | ||
458 | ################################################################################################################ |
|
459 | # Step 12: construct the report |
|
460 | ################################################################################################################ |
|
461 | if cursor_system: |
|
462 | cursor_system.close() |
|
463 | if cnx_system: |
|
464 | cnx_system.disconnect() |
|
465 | ||
466 | if cursor_energy: |
|
467 | cursor_energy.close() |
|
468 | if cnx_energy: |
|
469 | cnx_energy.disconnect() |
|
470 | ||
471 | result = dict() |
|
472 | ||
473 | result['tenant'] = dict() |
|
474 | result['tenant']['name'] = tenant['name'] |
|
475 | result['tenant']['area'] = tenant['area'] |
|
476 | ||
477 | result['base_period'] = dict() |
|
478 | result['base_period']['names'] = list() |
|
479 | result['base_period']['units'] = list() |
|
480 | result['base_period']['timestamps'] = list() |
|
481 | result['base_period']['values'] = list() |
|
482 | result['base_period']['subtotals'] = list() |
|
483 | result['base_period']['subtotals_in_kgce'] = list() |
|
484 | result['base_period']['subtotals_in_kgco2e'] = list() |
|
485 | result['base_period']['total_in_kgce'] = Decimal(0.0) |
|
486 | result['base_period']['total_in_kgco2e'] = Decimal(0.0) |
|
487 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
488 | for energy_category_id in energy_category_set: |
|
489 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
490 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
491 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
492 | result['base_period']['values'].append(base[energy_category_id]['values']) |
|
493 | result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
|
494 | result['base_period']['subtotals_in_kgce'].append(base[energy_category_id]['subtotal_in_kgce']) |
|
495 | result['base_period']['subtotals_in_kgco2e'].append(base[energy_category_id]['subtotal_in_kgco2e']) |
|
496 | result['base_period']['total_in_kgce'] += base[energy_category_id]['subtotal_in_kgce'] |
|
497 | result['base_period']['total_in_kgco2e'] += base[energy_category_id]['subtotal_in_kgco2e'] |
|
498 | ||
499 | result['reporting_period'] = dict() |
|
500 | result['reporting_period']['names'] = list() |
|
501 | result['reporting_period']['energy_category_ids'] = list() |
|
502 | result['reporting_period']['units'] = list() |
|
503 | result['reporting_period']['timestamps'] = list() |
|
504 | result['reporting_period']['values'] = list() |
|
505 | result['reporting_period']['subtotals'] = list() |
|
506 | result['reporting_period']['subtotals_in_kgce'] = list() |
|
507 | result['reporting_period']['subtotals_in_kgco2e'] = list() |
|
508 | result['reporting_period']['subtotals_per_unit_area'] = list() |
|
509 | result['reporting_period']['toppeaks'] = list() |
|
510 | result['reporting_period']['onpeaks'] = list() |
|
511 | result['reporting_period']['midpeaks'] = list() |
|
512 | result['reporting_period']['offpeaks'] = list() |
|
513 | result['reporting_period']['increment_rates'] = list() |
|
514 | result['reporting_period']['total_in_kgce'] = Decimal(0.0) |
|
515 | result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) |
|
516 | result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) |
|
517 | result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) |
|
518 | ||
519 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
520 | for energy_category_id in energy_category_set: |
|
521 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
522 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
523 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
524 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
525 | result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
|
526 | result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
|
527 | result['reporting_period']['subtotals_in_kgce'].append( |
|
528 | reporting[energy_category_id]['subtotal_in_kgce']) |
|
529 | result['reporting_period']['subtotals_in_kgco2e'].append( |
|
530 | reporting[energy_category_id]['subtotal_in_kgco2e']) |
|
531 | result['reporting_period']['subtotals_per_unit_area'].append( |
|
532 | reporting[energy_category_id]['subtotal'] / tenant['area'] if tenant['area'] > 0.0 else None) |
|
533 | result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak']) |
|
534 | result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak']) |
|
535 | result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak']) |
|
536 | result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak']) |
|
537 | result['reporting_period']['increment_rates'].append( |
|
538 | (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
|
539 | base[energy_category_id]['subtotal'] |
|
540 | if base[energy_category_id]['subtotal'] > 0.0 else None) |
|
541 | result['reporting_period']['total_in_kgce'] += reporting[energy_category_id]['subtotal_in_kgce'] |
|
542 | result['reporting_period']['total_in_kgco2e'] += reporting[energy_category_id]['subtotal_in_kgco2e'] |
|
543 | ||
544 | result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ |
|
545 | result['reporting_period']['total_in_kgce'] / tenant['area'] if tenant['area'] > 0.0 else None |
|
546 | ||
547 | result['reporting_period']['increment_rate_in_kgce'] = \ |
|
548 | (result['reporting_period']['total_in_kgce'] - result['base_period']['total_in_kgce']) / \ |
|
549 | result['base_period']['total_in_kgce'] \ |
|
550 | if result['base_period']['total_in_kgce'] > Decimal(0.0) else None |
|
551 | ||
552 | result['reporting_period']['total_in_kgce_per_unit_area'] = \ |
|
553 | result['reporting_period']['total_in_kgco2e'] / tenant['area'] if tenant['area'] > 0.0 else None |
|
554 | ||
555 | result['reporting_period']['increment_rate_in_kgco2e'] = \ |
|
556 | (result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ |
|
557 | result['base_period']['total_in_kgco2e'] \ |
|
558 | if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None |
|
559 | ||
560 | result['parameters'] = { |
|
561 | "names": parameters_data['names'], |
|
562 | "timestamps": parameters_data['timestamps'], |
|
563 | "values": parameters_data['values'] |
|
564 | } |
|
565 | ||
566 | resp.body = json.dumps(result) |
|
567 |