Conditions | 121 |
Total Lines | 648 |
Code Lines | 487 |
Lines | 648 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like reports.storesaving.Reporting.on_get() 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 |
||
32 | @staticmethod |
||
33 | def on_get(req, resp): |
||
34 | print(req.params) |
||
35 | store_id = req.params.get('storeid') |
||
36 | period_type = req.params.get('periodtype') |
||
37 | base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
||
38 | base_end_datetime_local = req.params.get('baseperiodenddatetime') |
||
39 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
40 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
41 | |||
42 | ################################################################################################################ |
||
43 | # Step 1: valid parameters |
||
44 | ################################################################################################################ |
||
45 | if store_id is None: |
||
46 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') |
||
47 | else: |
||
48 | store_id = str.strip(store_id) |
||
49 | if not store_id.isdigit() or int(store_id) <= 0: |
||
50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') |
||
51 | |||
52 | if period_type is None: |
||
53 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
54 | else: |
||
55 | period_type = str.strip(period_type) |
||
56 | if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
||
57 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
58 | |||
59 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
60 | if config.utc_offset[0] == '-': |
||
61 | timezone_offset = -timezone_offset |
||
62 | |||
63 | base_start_datetime_utc = None |
||
64 | if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
||
65 | base_start_datetime_local = str.strip(base_start_datetime_local) |
||
66 | try: |
||
67 | base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
||
68 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
69 | timedelta(minutes=timezone_offset) |
||
70 | except ValueError: |
||
71 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
72 | description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") |
||
73 | |||
74 | base_end_datetime_utc = None |
||
75 | if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
||
76 | base_end_datetime_local = str.strip(base_end_datetime_local) |
||
77 | try: |
||
78 | base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
||
79 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
80 | timedelta(minutes=timezone_offset) |
||
81 | except ValueError: |
||
82 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
83 | description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") |
||
84 | |||
85 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
||
86 | base_start_datetime_utc >= base_end_datetime_utc: |
||
87 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
88 | description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') |
||
89 | |||
90 | if reporting_start_datetime_local is None: |
||
91 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
92 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
93 | else: |
||
94 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
||
95 | try: |
||
96 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
||
97 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
98 | timedelta(minutes=timezone_offset) |
||
99 | except ValueError: |
||
100 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
101 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
102 | |||
103 | if reporting_end_datetime_local is None: |
||
104 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
105 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
106 | else: |
||
107 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
||
108 | try: |
||
109 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
||
110 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
111 | timedelta(minutes=timezone_offset) |
||
112 | except ValueError: |
||
113 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
114 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
115 | |||
116 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
117 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
118 | description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
||
119 | |||
120 | ################################################################################################################ |
||
121 | # Step 2: query the store |
||
122 | ################################################################################################################ |
||
123 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
124 | cursor_system = cnx_system.cursor() |
||
125 | |||
126 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
||
127 | cursor_energy = cnx_energy.cursor() |
||
128 | |||
129 | cnx_energy_baseline = mysql.connector.connect(**config.myems_energy_baseline_db) |
||
130 | cursor_energy_baseline = cnx_energy_baseline.cursor() |
||
131 | |||
132 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
||
133 | cursor_historical = cnx_historical.cursor() |
||
134 | |||
135 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
||
136 | " FROM tbl_stores " |
||
137 | " WHERE id = %s ", (store_id,)) |
||
138 | row_store = cursor_system.fetchone() |
||
139 | if row_store is None: |
||
140 | if cursor_system: |
||
141 | cursor_system.close() |
||
142 | if cnx_system: |
||
143 | cnx_system.disconnect() |
||
144 | |||
145 | if cursor_energy: |
||
146 | cursor_energy.close() |
||
147 | if cnx_energy: |
||
148 | cnx_energy.disconnect() |
||
149 | |||
150 | if cursor_energy_baseline: |
||
151 | cursor_energy_baseline.close() |
||
152 | if cnx_energy_baseline: |
||
153 | cnx_energy_baseline.disconnect() |
||
154 | |||
155 | if cnx_historical: |
||
156 | cnx_historical.close() |
||
157 | if cursor_historical: |
||
158 | cursor_historical.disconnect() |
||
159 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND') |
||
160 | |||
161 | store = dict() |
||
162 | store['id'] = row_store[0] |
||
163 | store['name'] = row_store[1] |
||
164 | store['area'] = row_store[2] |
||
165 | store['cost_center_id'] = row_store[3] |
||
166 | |||
167 | ################################################################################################################ |
||
168 | # Step 3: query energy categories |
||
169 | ################################################################################################################ |
||
170 | energy_category_set = set() |
||
171 | # query energy categories in base period |
||
172 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
||
173 | " FROM tbl_store_input_category_hourly " |
||
174 | " WHERE store_id = %s " |
||
175 | " AND start_datetime_utc >= %s " |
||
176 | " AND start_datetime_utc < %s ", |
||
177 | (store['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
178 | rows_energy_categories = cursor_energy.fetchall() |
||
179 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
||
180 | for row_energy_category in rows_energy_categories: |
||
181 | energy_category_set.add(row_energy_category[0]) |
||
182 | |||
183 | # query energy categories in reporting period |
||
184 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
||
185 | " FROM tbl_store_input_category_hourly " |
||
186 | " WHERE store_id = %s " |
||
187 | " AND start_datetime_utc >= %s " |
||
188 | " AND start_datetime_utc < %s ", |
||
189 | (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
190 | rows_energy_categories = cursor_energy.fetchall() |
||
191 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
||
192 | for row_energy_category in rows_energy_categories: |
||
193 | energy_category_set.add(row_energy_category[0]) |
||
194 | |||
195 | # query all energy categories in base period and reporting period |
||
196 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
||
197 | " FROM tbl_energy_categories " |
||
198 | " ORDER BY id ", ) |
||
199 | rows_energy_categories = cursor_system.fetchall() |
||
200 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
||
201 | if cursor_system: |
||
202 | cursor_system.close() |
||
203 | if cnx_system: |
||
204 | cnx_system.disconnect() |
||
205 | |||
206 | if cursor_energy: |
||
207 | cursor_energy.close() |
||
208 | if cnx_energy: |
||
209 | cnx_energy.disconnect() |
||
210 | |||
211 | if cursor_energy_baseline: |
||
212 | cursor_energy_baseline.close() |
||
213 | if cnx_energy_baseline: |
||
214 | cnx_energy_baseline.disconnect() |
||
215 | |||
216 | if cnx_historical: |
||
217 | cnx_historical.close() |
||
218 | if cursor_historical: |
||
219 | cursor_historical.disconnect() |
||
220 | raise falcon.HTTPError(falcon.HTTP_404, |
||
221 | title='API.NOT_FOUND', |
||
222 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
223 | energy_category_dict = dict() |
||
224 | for row_energy_category in rows_energy_categories: |
||
225 | if row_energy_category[0] in energy_category_set: |
||
226 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
||
227 | "unit_of_measure": row_energy_category[2], |
||
228 | "kgce": row_energy_category[3], |
||
229 | "kgco2e": row_energy_category[4]} |
||
230 | |||
231 | ################################################################################################################ |
||
232 | # Step 4: query associated sensors |
||
233 | ################################################################################################################ |
||
234 | point_list = list() |
||
235 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
||
236 | " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, " |
||
237 | " tbl_points p, tbl_sensors_points sp " |
||
238 | " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id " |
||
239 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
||
240 | " ORDER BY p.id ", (store['id'],)) |
||
241 | rows_points = cursor_system.fetchall() |
||
242 | if rows_points is not None and len(rows_points) > 0: |
||
243 | for row in rows_points: |
||
244 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
||
245 | |||
246 | ################################################################################################################ |
||
247 | # Step 5: query associated points |
||
248 | ################################################################################################################ |
||
249 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
||
250 | " FROM tbl_stores s, tbl_stores_points sp, tbl_points p " |
||
251 | " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id " |
||
252 | " ORDER BY p.id ", (store['id'],)) |
||
253 | rows_points = cursor_system.fetchall() |
||
254 | if rows_points is not None and len(rows_points) > 0: |
||
255 | for row in rows_points: |
||
256 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
||
257 | |||
258 | ################################################################################################################ |
||
259 | # Step 6: query base period energy saving |
||
260 | ################################################################################################################ |
||
261 | base = dict() |
||
262 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
263 | for energy_category_id in energy_category_set: |
||
264 | kgce = energy_category_dict[energy_category_id]['kgce'] |
||
265 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
||
266 | |||
267 | base[energy_category_id] = dict() |
||
268 | base[energy_category_id]['timestamps'] = list() |
||
269 | base[energy_category_id]['values_baseline'] = list() |
||
270 | base[energy_category_id]['values_actual'] = list() |
||
271 | base[energy_category_id]['values_saving'] = list() |
||
272 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
||
273 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
||
274 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
||
275 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
||
276 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
||
277 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
||
278 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
||
279 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
||
280 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
||
281 | # query base period's energy baseline |
||
282 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
||
283 | " FROM tbl_store_input_category_hourly " |
||
284 | " WHERE store_id = %s " |
||
285 | " AND energy_category_id = %s " |
||
286 | " AND start_datetime_utc >= %s " |
||
287 | " AND start_datetime_utc < %s " |
||
288 | " ORDER BY start_datetime_utc ", |
||
289 | (store['id'], |
||
290 | energy_category_id, |
||
291 | base_start_datetime_utc, |
||
292 | base_end_datetime_utc)) |
||
293 | rows_store_hourly = cursor_energy_baseline.fetchall() |
||
294 | |||
295 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
||
296 | base_start_datetime_utc, |
||
297 | base_end_datetime_utc, |
||
298 | period_type) |
||
299 | for row_store_periodically in rows_store_periodically: |
||
300 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
301 | timedelta(minutes=timezone_offset) |
||
302 | if period_type == 'hourly': |
||
303 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
304 | elif period_type == 'daily': |
||
305 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
306 | elif period_type == 'monthly': |
||
307 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
308 | elif period_type == 'yearly': |
||
309 | current_datetime = current_datetime_local.strftime('%Y') |
||
310 | |||
311 | baseline_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
||
312 | base[energy_category_id]['timestamps'].append(current_datetime) |
||
313 | base[energy_category_id]['values_baseline'].append(baseline_value) |
||
314 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
||
315 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
||
316 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
||
317 | |||
318 | # query base period's energy actual |
||
319 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
||
320 | " FROM tbl_store_input_category_hourly " |
||
321 | " WHERE store_id = %s " |
||
322 | " AND energy_category_id = %s " |
||
323 | " AND start_datetime_utc >= %s " |
||
324 | " AND start_datetime_utc < %s " |
||
325 | " ORDER BY start_datetime_utc ", |
||
326 | (store['id'], |
||
327 | energy_category_id, |
||
328 | base_start_datetime_utc, |
||
329 | base_end_datetime_utc)) |
||
330 | rows_store_hourly = cursor_energy.fetchall() |
||
331 | |||
332 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
||
333 | base_start_datetime_utc, |
||
334 | base_end_datetime_utc, |
||
335 | period_type) |
||
336 | for row_store_periodically in rows_store_periodically: |
||
337 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
338 | timedelta(minutes=timezone_offset) |
||
339 | if period_type == 'hourly': |
||
340 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
341 | elif period_type == 'daily': |
||
342 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
343 | elif period_type == 'monthly': |
||
344 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
345 | elif period_type == 'yearly': |
||
346 | current_datetime = current_datetime_local.strftime('%Y') |
||
347 | |||
348 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
||
349 | base[energy_category_id]['values_actual'].append(actual_value) |
||
350 | base[energy_category_id]['subtotal_actual'] += actual_value |
||
351 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
||
352 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
||
353 | |||
354 | # calculate base period's energy savings |
||
355 | for i in range(len(base[energy_category_id]['values_baseline'])): |
||
356 | base[energy_category_id]['values_saving'].append( |
||
357 | base[energy_category_id]['values_baseline'][i] - |
||
358 | base[energy_category_id]['values_actual'][i]) |
||
359 | |||
360 | base[energy_category_id]['subtotal_saving'] = \ |
||
361 | base[energy_category_id]['subtotal_baseline'] - \ |
||
362 | base[energy_category_id]['subtotal_actual'] |
||
363 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
||
364 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
||
365 | base[energy_category_id]['subtotal_in_kgce_actual'] |
||
366 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
||
367 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
||
368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
||
369 | ################################################################################################################ |
||
370 | # Step 7: query reporting period energy saving |
||
371 | ################################################################################################################ |
||
372 | reporting = dict() |
||
373 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
374 | for energy_category_id in energy_category_set: |
||
375 | kgce = energy_category_dict[energy_category_id]['kgce'] |
||
376 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
||
377 | |||
378 | reporting[energy_category_id] = dict() |
||
379 | reporting[energy_category_id]['timestamps'] = list() |
||
380 | reporting[energy_category_id]['values_baseline'] = list() |
||
381 | reporting[energy_category_id]['values_actual'] = list() |
||
382 | reporting[energy_category_id]['values_saving'] = list() |
||
383 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
||
384 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
||
385 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
||
386 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
||
387 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
||
388 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
||
389 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
||
390 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
||
391 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
||
392 | # query reporting period's energy baseline |
||
393 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
||
394 | " FROM tbl_store_input_category_hourly " |
||
395 | " WHERE store_id = %s " |
||
396 | " AND energy_category_id = %s " |
||
397 | " AND start_datetime_utc >= %s " |
||
398 | " AND start_datetime_utc < %s " |
||
399 | " ORDER BY start_datetime_utc ", |
||
400 | (store['id'], |
||
401 | energy_category_id, |
||
402 | reporting_start_datetime_utc, |
||
403 | reporting_end_datetime_utc)) |
||
404 | rows_store_hourly = cursor_energy_baseline.fetchall() |
||
405 | |||
406 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
||
407 | reporting_start_datetime_utc, |
||
408 | reporting_end_datetime_utc, |
||
409 | period_type) |
||
410 | for row_store_periodically in rows_store_periodically: |
||
411 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
412 | timedelta(minutes=timezone_offset) |
||
413 | if period_type == 'hourly': |
||
414 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
415 | elif period_type == 'daily': |
||
416 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
417 | elif period_type == 'monthly': |
||
418 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
419 | elif period_type == 'yearly': |
||
420 | current_datetime = current_datetime_local.strftime('%Y') |
||
421 | |||
422 | baseline_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
||
423 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
||
424 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
||
425 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
||
426 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
||
427 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
||
428 | |||
429 | # query reporting period's energy actual |
||
430 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
||
431 | " FROM tbl_store_input_category_hourly " |
||
432 | " WHERE store_id = %s " |
||
433 | " AND energy_category_id = %s " |
||
434 | " AND start_datetime_utc >= %s " |
||
435 | " AND start_datetime_utc < %s " |
||
436 | " ORDER BY start_datetime_utc ", |
||
437 | (store['id'], |
||
438 | energy_category_id, |
||
439 | reporting_start_datetime_utc, |
||
440 | reporting_end_datetime_utc)) |
||
441 | rows_store_hourly = cursor_energy.fetchall() |
||
442 | |||
443 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
||
444 | reporting_start_datetime_utc, |
||
445 | reporting_end_datetime_utc, |
||
446 | period_type) |
||
447 | for row_store_periodically in rows_store_periodically: |
||
448 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
449 | timedelta(minutes=timezone_offset) |
||
450 | if period_type == 'hourly': |
||
451 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
452 | elif period_type == 'daily': |
||
453 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
454 | elif period_type == 'monthly': |
||
455 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
456 | elif period_type == 'yearly': |
||
457 | current_datetime = current_datetime_local.strftime('%Y') |
||
458 | |||
459 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
||
460 | reporting[energy_category_id]['values_actual'].append(actual_value) |
||
461 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
||
462 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
||
463 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
||
464 | |||
465 | # calculate reporting period's energy savings |
||
466 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
||
467 | reporting[energy_category_id]['values_saving'].append( |
||
468 | reporting[energy_category_id]['values_baseline'][i] - |
||
469 | reporting[energy_category_id]['values_actual'][i]) |
||
470 | |||
471 | reporting[energy_category_id]['subtotal_saving'] = \ |
||
472 | reporting[energy_category_id]['subtotal_baseline'] - \ |
||
473 | reporting[energy_category_id]['subtotal_actual'] |
||
474 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
||
475 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
||
476 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
||
477 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
||
478 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
||
479 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
||
480 | ################################################################################################################ |
||
481 | # Step 8: query tariff data |
||
482 | ################################################################################################################ |
||
483 | parameters_data = dict() |
||
484 | parameters_data['names'] = list() |
||
485 | parameters_data['timestamps'] = list() |
||
486 | parameters_data['values'] = list() |
||
487 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
488 | for energy_category_id in energy_category_set: |
||
489 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(store['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 9: query associated sensors and 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 10: 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['store'] = dict() |
||
591 | result['store']['name'] = store['name'] |
||
592 | result['store']['area'] = store['area'] |
||
593 | |||
594 | result['base_period'] = dict() |
||
595 | result['base_period']['names'] = list() |
||
596 | result['base_period']['units'] = list() |
||
597 | result['base_period']['timestamps'] = list() |
||
598 | result['base_period']['values_saving'] = list() |
||
599 | result['base_period']['subtotals_saving'] = list() |
||
600 | result['base_period']['subtotals_in_kgce_saving'] = list() |
||
601 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
||
602 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
||
603 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
||
604 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
605 | for energy_category_id in energy_category_set: |
||
606 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
607 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
||
608 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
||
609 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
||
610 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
||
611 | result['base_period']['subtotals_in_kgce_saving'].append( |
||
612 | base[energy_category_id]['subtotal_in_kgce_saving']) |
||
613 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
||
614 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
||
615 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
||
616 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
||
617 | |||
618 | result['reporting_period'] = dict() |
||
619 | result['reporting_period']['names'] = list() |
||
620 | result['reporting_period']['energy_category_ids'] = list() |
||
621 | result['reporting_period']['units'] = list() |
||
622 | result['reporting_period']['timestamps'] = list() |
||
623 | result['reporting_period']['values_saving'] = list() |
||
624 | result['reporting_period']['subtotals_saving'] = list() |
||
625 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
||
626 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
||
627 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
||
628 | result['reporting_period']['increment_rates_saving'] = list() |
||
629 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
||
630 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
||
631 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
||
632 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
||
633 | |||
634 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
635 | for energy_category_id in energy_category_set: |
||
636 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
637 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
||
638 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
||
639 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
||
640 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
||
641 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
||
642 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
||
643 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
||
644 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
||
645 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
||
646 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
||
647 | reporting[energy_category_id]['subtotal_saving'] / store['area'] if store['area'] > 0.0 else None) |
||
648 | result['reporting_period']['increment_rates_saving'].append( |
||
649 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
||
650 | base[energy_category_id]['subtotal_saving'] |
||
651 | if base[energy_category_id]['subtotal_saving'] > 0.0 else None) |
||
652 | result['reporting_period']['total_in_kgce_saving'] += \ |
||
653 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
||
654 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
||
655 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
||
656 | |||
657 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
||
658 | result['reporting_period']['total_in_kgce_saving'] / store['area'] if store['area'] > 0.0 else None |
||
659 | |||
660 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
||
661 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
||
662 | result['base_period']['total_in_kgce_saving'] \ |
||
663 | if result['base_period']['total_in_kgce_saving'] > Decimal(0.0) else None |
||
664 | |||
665 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
||
666 | result['reporting_period']['total_in_kgco2e_saving'] / store['area'] if store['area'] > 0.0 else None |
||
667 | |||
668 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
||
669 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
||
670 | result['base_period']['total_in_kgco2e_saving'] \ |
||
671 | if result['base_period']['total_in_kgco2e_saving'] > Decimal(0.0) else None |
||
672 | |||
673 | result['parameters'] = { |
||
674 | "names": parameters_data['names'], |
||
675 | "timestamps": parameters_data['timestamps'], |
||
676 | "values": parameters_data['values'] |
||
677 | } |
||
678 | |||
679 | resp.body = json.dumps(result) |
||
680 |