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