1 | import re |
||
2 | from datetime import datetime, timedelta, timezone |
||
3 | from decimal import Decimal |
||
4 | import falcon |
||
5 | import mysql.connector |
||
6 | import simplejson as json |
||
7 | import config |
||
8 | import excelexporters.virtualmeterenergy |
||
9 | from core import utilities |
||
10 | from core.useractivity import access_control, api_key_control |
||
11 | |||
12 | |||
13 | class Reporting: |
||
14 | def __init__(self): |
||
15 | """"Initializes Reporting""" |
||
16 | pass |
||
17 | |||
18 | @staticmethod |
||
19 | def on_options(req, resp): |
||
20 | _ = req |
||
21 | resp.status = falcon.HTTP_200 |
||
22 | |||
23 | #################################################################################################################### |
||
24 | # PROCEDURES |
||
25 | # Step 1: valid parameters |
||
26 | # Step 2: query the virtual meter and energy category |
||
27 | # Step 3: query base period energy consumption |
||
28 | # Step 4: query reporting period energy consumption |
||
29 | # Step 5: query tariff data |
||
30 | # Step 6: construct the report |
||
31 | #################################################################################################################### |
||
32 | @staticmethod |
||
33 | def on_get(req, resp): |
||
34 | if 'API-KEY' not in req.headers or \ |
||
35 | not isinstance(req.headers['API-KEY'], str) or \ |
||
36 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
37 | access_control(req) |
||
38 | else: |
||
39 | api_key_control(req) |
||
40 | print(req.params) |
||
41 | virtual_meter_id = req.params.get('virtualmeterid') |
||
42 | virtual_meter_uuid = req.params.get('virtualmeteruuid') |
||
43 | period_type = req.params.get('periodtype') |
||
44 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
||
45 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
||
46 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
47 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
48 | language = req.params.get('language') |
||
49 | quick_mode = req.params.get('quickmode') |
||
50 | |||
51 | ################################################################################################################ |
||
52 | # Step 1: valid parameters |
||
53 | ################################################################################################################ |
||
54 | if virtual_meter_id is None and virtual_meter_uuid is None: |
||
55 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
56 | title='API.BAD_REQUEST', |
||
57 | description='API.INVALID_VIRTUAL_METER_ID') |
||
58 | |||
59 | if virtual_meter_id is not None: |
||
60 | virtual_meter_id = str.strip(virtual_meter_id) |
||
61 | if not virtual_meter_id.isdigit() or int(virtual_meter_id) <= 0: |
||
62 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
63 | title='API.BAD_REQUEST', |
||
64 | description='API.INVALID_VIRTUAL_METER_ID') |
||
65 | |||
66 | if virtual_meter_uuid is not None: |
||
67 | 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) |
||
68 | match = regex.match(str.strip(virtual_meter_uuid)) |
||
69 | if not bool(match): |
||
70 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
71 | title='API.BAD_REQUEST', |
||
72 | description='API.INVALID_VIRTUAL_METER_UUID') |
||
73 | |||
74 | if period_type is None: |
||
75 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
76 | description='API.INVALID_PERIOD_TYPE') |
||
77 | else: |
||
78 | period_type = str.strip(period_type) |
||
79 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
||
80 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
81 | description='API.INVALID_PERIOD_TYPE') |
||
82 | |||
83 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
84 | if config.utc_offset[0] == '-': |
||
85 | timezone_offset = -timezone_offset |
||
86 | |||
87 | base_start_datetime_utc = None |
||
88 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
||
89 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
||
90 | try: |
||
91 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
||
92 | except ValueError: |
||
93 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
94 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
||
95 | base_start_datetime_utc = \ |
||
96 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
97 | # nomalize the start datetime |
||
98 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
||
99 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
||
100 | else: |
||
101 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
||
102 | |||
103 | base_end_datetime_utc = None |
||
104 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
||
105 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
||
106 | try: |
||
107 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
||
108 | except ValueError: |
||
109 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
110 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
||
111 | base_end_datetime_utc = \ |
||
112 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
113 | |||
114 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
||
115 | base_start_datetime_utc >= base_end_datetime_utc: |
||
116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
117 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
||
118 | |||
119 | if reporting_period_start_datetime_local is None: |
||
120 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
121 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
122 | else: |
||
123 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
||
124 | try: |
||
125 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
||
126 | '%Y-%m-%dT%H:%M:%S') |
||
127 | except ValueError: |
||
128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
129 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
130 | reporting_start_datetime_utc = \ |
||
131 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
132 | # nomalize the start datetime |
||
133 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
||
134 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
||
135 | else: |
||
136 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
||
137 | |||
138 | if reporting_period_end_datetime_local is None: |
||
139 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
140 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
141 | else: |
||
142 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
||
143 | try: |
||
144 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
||
145 | '%Y-%m-%dT%H:%M:%S') |
||
146 | except ValueError: |
||
147 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
148 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
149 | reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
150 | timedelta(minutes=timezone_offset) |
||
151 | |||
152 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
154 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
||
155 | |||
156 | # if turn quick mode on, do not return parameters data and excel file |
||
157 | is_quick_mode = False |
||
158 | if quick_mode is not None and \ |
||
159 | len(str.strip(quick_mode)) > 0 and \ |
||
160 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
||
161 | is_quick_mode = True |
||
162 | |||
163 | trans = utilities.get_translation(language) |
||
164 | trans.install() |
||
165 | _ = trans.gettext |
||
166 | |||
167 | ################################################################################################################ |
||
168 | # Step 2: query the virtual meter and energy category |
||
169 | ################################################################################################################ |
||
170 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
171 | cursor_system = cnx_system.cursor() |
||
172 | |||
173 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
||
174 | cursor_energy = cnx_energy.cursor() |
||
175 | |||
176 | cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " |
||
177 | " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " |
||
178 | " FROM tbl_virtual_meters m, tbl_energy_categories ec " |
||
179 | " WHERE m.id = %s AND m.energy_category_id = ec.id ", (virtual_meter_id,)) |
||
180 | row_virtual_meter = cursor_system.fetchone() |
||
181 | if row_virtual_meter is None: |
||
182 | if cursor_system: |
||
183 | cursor_system.close() |
||
184 | if cnx_system: |
||
185 | cnx_system.close() |
||
186 | |||
187 | if cursor_energy: |
||
188 | cursor_energy.close() |
||
189 | if cnx_energy: |
||
190 | cnx_energy.close() |
||
191 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
192 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
193 | |||
194 | virtual_meter = dict() |
||
195 | virtual_meter['id'] = row_virtual_meter[0] |
||
196 | virtual_meter['name'] = row_virtual_meter[1] |
||
197 | virtual_meter['cost_center_id'] = row_virtual_meter[2] |
||
198 | virtual_meter['energy_category_id'] = row_virtual_meter[3] |
||
199 | virtual_meter['energy_category_name'] = row_virtual_meter[4] |
||
200 | virtual_meter['unit_of_measure'] = row_virtual_meter[5] |
||
201 | virtual_meter['kgce'] = row_virtual_meter[6] |
||
202 | virtual_meter['kgco2e'] = row_virtual_meter[7] |
||
203 | |||
204 | ################################################################################################################ |
||
205 | # Step 3: query base period energy consumption |
||
206 | ################################################################################################################ |
||
207 | query = (" SELECT start_datetime_utc, actual_value " |
||
208 | " FROM tbl_virtual_meter_hourly " |
||
209 | " WHERE virtual_meter_id = %s " |
||
210 | " AND start_datetime_utc >= %s " |
||
211 | " AND start_datetime_utc < %s " |
||
212 | " ORDER BY start_datetime_utc ") |
||
213 | cursor_energy.execute(query, (virtual_meter['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
214 | rows_virtual_meter_hourly = cursor_energy.fetchall() |
||
215 | |||
216 | rows_virtual_meter_periodically = \ |
||
217 | utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
218 | base_start_datetime_utc, |
||
219 | base_end_datetime_utc, |
||
220 | period_type) |
||
221 | base = dict() |
||
222 | base['timestamps'] = list() |
||
223 | base['values'] = list() |
||
224 | base['total_in_category'] = Decimal(0.0) |
||
225 | base['total_in_kgce'] = Decimal(0.0) |
||
226 | base['total_in_kgco2e'] = Decimal(0.0) |
||
227 | |||
228 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
229 | current_datetime_local = row_virtual_meter_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
230 | timedelta(minutes=timezone_offset) |
||
231 | if period_type == 'hourly': |
||
232 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
233 | elif period_type == 'daily': |
||
234 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
235 | elif period_type == 'weekly': |
||
236 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
237 | elif period_type == 'monthly': |
||
238 | current_datetime = current_datetime_local.isoformat()[0:7] |
||
239 | elif period_type == 'yearly': |
||
240 | current_datetime = current_datetime_local.isoformat()[0:4] |
||
241 | |||
242 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
243 | else row_virtual_meter_periodically[1] |
||
244 | base['timestamps'].append(current_datetime) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
245 | base['values'].append(actual_value) |
||
246 | base['total_in_category'] += actual_value |
||
247 | base['total_in_kgce'] += actual_value * virtual_meter['kgce'] |
||
248 | base['total_in_kgco2e'] += actual_value * virtual_meter['kgco2e'] |
||
249 | |||
250 | ################################################################################################################ |
||
251 | # Step 3: query reporting period energy consumption |
||
252 | ################################################################################################################ |
||
253 | query = (" SELECT start_datetime_utc, actual_value " |
||
254 | " FROM tbl_virtual_meter_hourly " |
||
255 | " WHERE virtual_meter_id = %s " |
||
256 | " AND start_datetime_utc >= %s " |
||
257 | " AND start_datetime_utc < %s " |
||
258 | " ORDER BY start_datetime_utc ") |
||
259 | cursor_energy.execute(query, (virtual_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
260 | rows_virtual_meter_hourly = cursor_energy.fetchall() |
||
261 | |||
262 | rows_virtual_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
263 | reporting_start_datetime_utc, |
||
264 | reporting_end_datetime_utc, |
||
265 | period_type) |
||
266 | reporting = dict() |
||
267 | reporting['timestamps'] = list() |
||
268 | reporting['values'] = list() |
||
269 | reporting['rates'] = list() |
||
270 | reporting['total_in_category'] = Decimal(0.0) |
||
271 | reporting['total_in_kgce'] = Decimal(0.0) |
||
272 | reporting['total_in_kgco2e'] = Decimal(0.0) |
||
273 | |||
274 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
275 | current_datetime_local = row_virtual_meter_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
276 | timedelta(minutes=timezone_offset) |
||
277 | if period_type == 'hourly': |
||
278 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
279 | elif period_type == 'daily': |
||
280 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
281 | elif period_type == 'weekly': |
||
282 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
283 | elif period_type == 'monthly': |
||
284 | current_datetime = current_datetime_local.isoformat()[0:7] |
||
285 | elif period_type == 'yearly': |
||
286 | current_datetime = current_datetime_local.isoformat()[0:4] |
||
287 | |||
288 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
289 | else row_virtual_meter_periodically[1] |
||
290 | |||
291 | reporting['timestamps'].append(current_datetime) |
||
292 | reporting['values'].append(actual_value) |
||
293 | reporting['total_in_category'] += actual_value |
||
294 | reporting['total_in_kgce'] += actual_value * virtual_meter['kgce'] |
||
295 | reporting['total_in_kgco2e'] += actual_value * virtual_meter['kgco2e'] |
||
296 | |||
297 | for index, value in enumerate(reporting['values']): |
||
298 | if index < len(base['values']) and base['values'][index] != 0 and value != 0: |
||
299 | reporting['rates'].append((value - base['values'][index]) / base['values'][index]) |
||
300 | else: |
||
301 | reporting['rates'].append(None) |
||
302 | |||
303 | ################################################################################################################ |
||
304 | # Step 5: query tariff data |
||
305 | ################################################################################################################ |
||
306 | parameters_data = dict() |
||
307 | parameters_data['names'] = list() |
||
308 | parameters_data['timestamps'] = list() |
||
309 | parameters_data['values'] = list() |
||
310 | if config.is_tariff_appended and not is_quick_mode: |
||
311 | tariff_dict = utilities.get_energy_category_tariffs(virtual_meter['cost_center_id'], |
||
312 | virtual_meter['energy_category_id'], |
||
313 | reporting_start_datetime_utc, |
||
314 | reporting_end_datetime_utc) |
||
315 | tariff_timestamp_list = list() |
||
316 | tariff_value_list = list() |
||
317 | for k, v in tariff_dict.items(): |
||
318 | # convert k from utc to local |
||
319 | k = k + timedelta(minutes=timezone_offset) |
||
320 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
||
321 | tariff_value_list.append(v) |
||
322 | |||
323 | parameters_data['names'].append(_('Tariff') + '-' + virtual_meter['energy_category_name']) |
||
324 | parameters_data['timestamps'].append(tariff_timestamp_list) |
||
325 | parameters_data['values'].append(tariff_value_list) |
||
326 | |||
327 | ################################################################################################################ |
||
328 | # Step 6: construct the report |
||
329 | ################################################################################################################ |
||
330 | if cursor_system: |
||
331 | cursor_system.close() |
||
332 | if cnx_system: |
||
333 | cnx_system.close() |
||
334 | |||
335 | if cursor_energy: |
||
336 | cursor_energy.close() |
||
337 | if cnx_energy: |
||
338 | cnx_energy.close() |
||
339 | |||
340 | result = { |
||
341 | "virtual_meter": { |
||
342 | "cost_center_id": virtual_meter['cost_center_id'], |
||
343 | "energy_category_id": virtual_meter['energy_category_id'], |
||
344 | "energy_category_name": virtual_meter['energy_category_name'], |
||
345 | "unit_of_measure": virtual_meter['unit_of_measure'], |
||
346 | "kgce": virtual_meter['kgce'], |
||
347 | "kgco2e": virtual_meter['kgco2e'], |
||
348 | }, |
||
349 | "base_period": { |
||
350 | "total_in_category": base['total_in_category'], |
||
351 | "total_in_kgce": base['total_in_kgce'], |
||
352 | "total_in_kgco2e": base['total_in_kgco2e'], |
||
353 | "timestamps": base['timestamps'], |
||
354 | "values": base['values'], |
||
355 | }, |
||
356 | "reporting_period": { |
||
357 | "increment_rate": |
||
358 | (reporting['total_in_category'] - base['total_in_category']) / base['total_in_category'] |
||
359 | if base['total_in_category'] != Decimal(0.0) else None, |
||
360 | "total_in_category": reporting['total_in_category'], |
||
361 | "total_in_kgce": reporting['total_in_kgce'], |
||
362 | "total_in_kgco2e": reporting['total_in_kgco2e'], |
||
363 | "timestamps": reporting['timestamps'], |
||
364 | "values": reporting['values'], |
||
365 | "rates": reporting['rates'], |
||
366 | }, |
||
367 | "parameters": { |
||
368 | "names": parameters_data['names'], |
||
369 | "timestamps": parameters_data['timestamps'], |
||
370 | "values": parameters_data['values'] |
||
371 | }, |
||
372 | } |
||
373 | |||
374 | # export result to Excel file and then encode the file to base64 string |
||
375 | if not is_quick_mode: |
||
376 | result['excel_bytes_base64'] = \ |
||
377 | excelexporters.virtualmeterenergy.export(result, |
||
378 | virtual_meter['name'], |
||
379 | base_period_start_datetime_local, |
||
380 | base_period_end_datetime_local, |
||
381 | reporting_period_start_datetime_local, |
||
382 | reporting_period_end_datetime_local, |
||
383 | period_type, |
||
384 | language) |
||
385 | |||
386 | resp.text = json.dumps(result) |
||
387 |