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