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