@@ 13-862 (lines=850) @@ | ||
10 | from core.useractivity import access_control, api_key_control |
|
11 | ||
12 | ||
13 | class Reporting: |
|
14 | @staticmethod |
|
15 | def __init__(): |
|
16 | """"Initializes Reporting""" |
|
17 | pass |
|
18 | ||
19 | @staticmethod |
|
20 | def on_options(req, resp): |
|
21 | resp.status = falcon.HTTP_200 |
|
22 | ||
23 | #################################################################################################################### |
|
24 | # PROCEDURES |
|
25 | # Step 1: valid parameters |
|
26 | # Step 2: query the combined equipment |
|
27 | # Step 3: query energy categories |
|
28 | # Step 4: query associated points |
|
29 | # Step 5: query associated equipments |
|
30 | # Step 6: query base period energy saving |
|
31 | # Step 7: query reporting period energy saving |
|
32 | # Step 8: query tariff data |
|
33 | # Step 9: query associated points data |
|
34 | # Step 10: query associated equipments energy saving |
|
35 | # Step 11: construct the report |
|
36 | #################################################################################################################### |
|
37 | @staticmethod |
|
38 | def on_get(req, resp): |
|
39 | if 'API-KEY' not in req.headers or \ |
|
40 | not isinstance(req.headers['API-KEY'], str) or \ |
|
41 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
42 | access_control(req) |
|
43 | else: |
|
44 | api_key_control(req) |
|
45 | print(req.params) |
|
46 | combined_equipment_id = req.params.get('combinedequipmentid') |
|
47 | combined_equipment_uuid = req.params.get('combinedequipmentuuid') |
|
48 | period_type = req.params.get('periodtype') |
|
49 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
50 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
51 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
52 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
53 | language = req.params.get('language') |
|
54 | quick_mode = req.params.get('quickmode') |
|
55 | ||
56 | ################################################################################################################ |
|
57 | # Step 1: valid parameters |
|
58 | ################################################################################################################ |
|
59 | if combined_equipment_id is None and combined_equipment_uuid is None: |
|
60 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
61 | title='API.BAD_REQUEST', |
|
62 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
63 | ||
64 | if combined_equipment_id is not None: |
|
65 | combined_equipment_id = str.strip(combined_equipment_id) |
|
66 | if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0: |
|
67 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
68 | title='API.BAD_REQUEST', |
|
69 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
70 | ||
71 | if combined_equipment_uuid is not None: |
|
72 | regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) |
|
73 | match = regex.match(str.strip(combined_equipment_uuid)) |
|
74 | if not bool(match): |
|
75 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
76 | title='API.BAD_REQUEST', |
|
77 | description='API.INVALID_COMBINED_EQUIPMENT_UUID') |
|
78 | ||
79 | if period_type is None: |
|
80 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
81 | description='API.INVALID_PERIOD_TYPE') |
|
82 | else: |
|
83 | period_type = str.strip(period_type) |
|
84 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
85 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
86 | description='API.INVALID_PERIOD_TYPE') |
|
87 | ||
88 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
89 | if config.utc_offset[0] == '-': |
|
90 | timezone_offset = -timezone_offset |
|
91 | ||
92 | base_start_datetime_utc = None |
|
93 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
94 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
95 | try: |
|
96 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
97 | except ValueError: |
|
98 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
99 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
100 | base_start_datetime_utc = \ |
|
101 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
102 | # nomalize the start datetime |
|
103 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
104 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
105 | else: |
|
106 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
107 | ||
108 | base_end_datetime_utc = None |
|
109 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
110 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
111 | try: |
|
112 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
113 | except ValueError: |
|
114 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
115 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
116 | base_end_datetime_utc = \ |
|
117 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
118 | ||
119 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
120 | base_start_datetime_utc >= base_end_datetime_utc: |
|
121 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
122 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
123 | ||
124 | if reporting_period_start_datetime_local is None: |
|
125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
126 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
127 | else: |
|
128 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
129 | try: |
|
130 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
131 | '%Y-%m-%dT%H:%M:%S') |
|
132 | except ValueError: |
|
133 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
134 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
135 | reporting_start_datetime_utc = \ |
|
136 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
137 | # nomalize the start datetime |
|
138 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
139 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
140 | else: |
|
141 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
142 | ||
143 | if reporting_period_end_datetime_local is None: |
|
144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
145 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
146 | else: |
|
147 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
148 | try: |
|
149 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
150 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
151 | timedelta(minutes=timezone_offset) |
|
152 | except ValueError: |
|
153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
154 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
155 | ||
156 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
158 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
159 | ||
160 | # if turn quick mode on, do not return parameters data and excel file |
|
161 | is_quick_mode = False |
|
162 | if quick_mode is not None and \ |
|
163 | len(str.strip(quick_mode)) > 0 and \ |
|
164 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
165 | is_quick_mode = True |
|
166 | ||
167 | trans = utilities.get_translation(language) |
|
168 | trans.install() |
|
169 | _ = trans.gettext |
|
170 | ||
171 | ################################################################################################################ |
|
172 | # Step 2: query the combined equipment |
|
173 | ################################################################################################################ |
|
174 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
175 | cursor_system = cnx_system.cursor() |
|
176 | ||
177 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
178 | cursor_energy = cnx_energy.cursor() |
|
179 | ||
180 | cnx_energy_baseline = mysql.connector.connect(**config.myems_energy_baseline_db) |
|
181 | cursor_energy_baseline = cnx_energy_baseline.cursor() |
|
182 | ||
183 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
184 | cursor_historical = cnx_historical.cursor() |
|
185 | ||
186 | if combined_equipment_id is not None: |
|
187 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
188 | " FROM tbl_combined_equipments " |
|
189 | " WHERE id = %s ", (combined_equipment_id,)) |
|
190 | row_combined_equipment = cursor_system.fetchone() |
|
191 | elif combined_equipment_uuid is not None: |
|
192 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
193 | " FROM tbl_combined_equipments " |
|
194 | " WHERE uuid = %s ", (combined_equipment_uuid,)) |
|
195 | row_combined_equipment = cursor_system.fetchone() |
|
196 | ||
197 | if row_combined_equipment is None: |
|
198 | if cursor_system: |
|
199 | cursor_system.close() |
|
200 | if cnx_system: |
|
201 | cnx_system.close() |
|
202 | ||
203 | if cursor_energy: |
|
204 | cursor_energy.close() |
|
205 | if cnx_energy: |
|
206 | cnx_energy.close() |
|
207 | ||
208 | if cursor_energy_baseline: |
|
209 | cursor_energy_baseline.close() |
|
210 | if cnx_energy_baseline: |
|
211 | cnx_energy_baseline.close() |
|
212 | ||
213 | if cursor_historical: |
|
214 | cursor_historical.close() |
|
215 | if cnx_historical: |
|
216 | cnx_historical.close() |
|
217 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
218 | title='API.NOT_FOUND', |
|
219 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
220 | ||
221 | combined_equipment = dict() |
|
222 | combined_equipment['id'] = row_combined_equipment[0] |
|
223 | combined_equipment['name'] = row_combined_equipment[1] |
|
224 | combined_equipment['cost_center_id'] = row_combined_equipment[2] |
|
225 | ||
226 | ################################################################################################################ |
|
227 | # Step 3: query energy categories |
|
228 | ################################################################################################################ |
|
229 | energy_category_set = set() |
|
230 | # query energy categories in base period |
|
231 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
232 | " FROM tbl_combined_equipment_input_category_hourly " |
|
233 | " WHERE combined_equipment_id = %s " |
|
234 | " AND start_datetime_utc >= %s " |
|
235 | " AND start_datetime_utc < %s ", |
|
236 | (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
237 | rows_energy_categories = cursor_energy.fetchall() |
|
238 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
239 | for row_energy_category in rows_energy_categories: |
|
240 | energy_category_set.add(row_energy_category[0]) |
|
241 | ||
242 | # query energy categories in reporting period |
|
243 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
244 | " FROM tbl_combined_equipment_input_category_hourly " |
|
245 | " WHERE combined_equipment_id = %s " |
|
246 | " AND start_datetime_utc >= %s " |
|
247 | " AND start_datetime_utc < %s ", |
|
248 | (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
249 | rows_energy_categories = cursor_energy.fetchall() |
|
250 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
251 | for row_energy_category in rows_energy_categories: |
|
252 | energy_category_set.add(row_energy_category[0]) |
|
253 | ||
254 | # query all energy categories in base period and reporting period |
|
255 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
256 | " FROM tbl_energy_categories " |
|
257 | " ORDER BY id ", ) |
|
258 | rows_energy_categories = cursor_system.fetchall() |
|
259 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
260 | if cursor_system: |
|
261 | cursor_system.close() |
|
262 | if cnx_system: |
|
263 | cnx_system.close() |
|
264 | ||
265 | if cursor_energy: |
|
266 | cursor_energy.close() |
|
267 | if cnx_energy: |
|
268 | cnx_energy.close() |
|
269 | ||
270 | if cursor_energy_baseline: |
|
271 | cursor_energy_baseline.close() |
|
272 | if cnx_energy_baseline: |
|
273 | cnx_energy_baseline.close() |
|
274 | ||
275 | if cursor_historical: |
|
276 | cursor_historical.close() |
|
277 | if cnx_historical: |
|
278 | cnx_historical.close() |
|
279 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
280 | title='API.NOT_FOUND', |
|
281 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
282 | energy_category_dict = dict() |
|
283 | for row_energy_category in rows_energy_categories: |
|
284 | if row_energy_category[0] in energy_category_set: |
|
285 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
286 | "unit_of_measure": row_energy_category[2], |
|
287 | "kgce": row_energy_category[3], |
|
288 | "kgco2e": row_energy_category[4]} |
|
289 | ||
290 | ################################################################################################################ |
|
291 | # Step 4: query associated points |
|
292 | ################################################################################################################ |
|
293 | point_list = list() |
|
294 | cursor_system.execute(" SELECT p.id, ep.name, p.units, p.object_type " |
|
295 | " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " |
|
296 | " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " |
|
297 | " AND ep.point_id = p.id " |
|
298 | " ORDER BY p.id ", (combined_equipment['id'],)) |
|
299 | rows_points = cursor_system.fetchall() |
|
300 | if rows_points is not None and len(rows_points) > 0: |
|
301 | for row in rows_points: |
|
302 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
303 | ||
304 | ################################################################################################################ |
|
305 | # Step 5: query associated equipments |
|
306 | ################################################################################################################ |
|
307 | associated_equipment_list = list() |
|
308 | cursor_system.execute(" SELECT e.id, e.name " |
|
309 | " FROM tbl_equipments e,tbl_combined_equipments_equipments ee" |
|
310 | " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id" |
|
311 | " ORDER BY id ", (combined_equipment['id'],)) |
|
312 | rows_associated_equipments = cursor_system.fetchall() |
|
313 | if rows_associated_equipments is not None and len(rows_associated_equipments) > 0: |
|
314 | for row in rows_associated_equipments: |
|
315 | associated_equipment_list.append({"id": row[0], "name": row[1]}) |
|
316 | ||
317 | ################################################################################################################ |
|
318 | # Step 6: query base period energy saving |
|
319 | ################################################################################################################ |
|
320 | base = dict() |
|
321 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
322 | for energy_category_id in energy_category_set: |
|
323 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
324 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
325 | ||
326 | base[energy_category_id] = dict() |
|
327 | base[energy_category_id]['timestamps'] = list() |
|
328 | base[energy_category_id]['values_baseline'] = list() |
|
329 | base[energy_category_id]['values_actual'] = list() |
|
330 | base[energy_category_id]['values_saving'] = list() |
|
331 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
332 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
333 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
334 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
335 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
336 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
337 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
338 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
339 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
340 | # query base period's energy baseline |
|
341 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
342 | " FROM tbl_combined_equipment_input_category_hourly " |
|
343 | " WHERE combined_equipment_id = %s " |
|
344 | " AND energy_category_id = %s " |
|
345 | " AND start_datetime_utc >= %s " |
|
346 | " AND start_datetime_utc < %s " |
|
347 | " ORDER BY start_datetime_utc ", |
|
348 | (combined_equipment['id'], |
|
349 | energy_category_id, |
|
350 | base_start_datetime_utc, |
|
351 | base_end_datetime_utc)) |
|
352 | rows_combined_equipment_hourly = cursor_energy_baseline.fetchall() |
|
353 | ||
354 | rows_combined_equipment_periodically = \ |
|
355 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
356 | base_start_datetime_utc, |
|
357 | base_end_datetime_utc, |
|
358 | period_type) |
|
359 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
360 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
361 | timedelta(minutes=timezone_offset) |
|
362 | if period_type == 'hourly': |
|
363 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
364 | elif period_type == 'daily': |
|
365 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
366 | elif period_type == 'weekly': |
|
367 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
368 | elif period_type == 'monthly': |
|
369 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
370 | elif period_type == 'yearly': |
|
371 | current_datetime = current_datetime_local.strftime('%Y') |
|
372 | ||
373 | baseline_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
374 | else row_combined_equipment_periodically[1] |
|
375 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
376 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
377 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
378 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
379 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
380 | ||
381 | # query base period's energy actual |
|
382 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
383 | " FROM tbl_combined_equipment_input_category_hourly " |
|
384 | " WHERE combined_equipment_id = %s " |
|
385 | " AND energy_category_id = %s " |
|
386 | " AND start_datetime_utc >= %s " |
|
387 | " AND start_datetime_utc < %s " |
|
388 | " ORDER BY start_datetime_utc ", |
|
389 | (combined_equipment['id'], |
|
390 | energy_category_id, |
|
391 | base_start_datetime_utc, |
|
392 | base_end_datetime_utc)) |
|
393 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
394 | ||
395 | rows_combined_equipment_periodically = \ |
|
396 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
397 | base_start_datetime_utc, |
|
398 | base_end_datetime_utc, |
|
399 | period_type) |
|
400 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
401 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
402 | timedelta(minutes=timezone_offset) |
|
403 | if period_type == 'hourly': |
|
404 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
405 | elif period_type == 'daily': |
|
406 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
407 | elif period_type == 'weekly': |
|
408 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
409 | elif period_type == 'monthly': |
|
410 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
411 | elif period_type == 'yearly': |
|
412 | current_datetime = current_datetime_local.strftime('%Y') |
|
413 | ||
414 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
415 | else row_combined_equipment_periodically[1] |
|
416 | base[energy_category_id]['values_actual'].append(actual_value) |
|
417 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
418 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
419 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
420 | ||
421 | # calculate base period's energy savings |
|
422 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
423 | base[energy_category_id]['values_saving'].append( |
|
424 | base[energy_category_id]['values_baseline'][i] - |
|
425 | base[energy_category_id]['values_actual'][i]) |
|
426 | ||
427 | base[energy_category_id]['subtotal_saving'] = \ |
|
428 | base[energy_category_id]['subtotal_baseline'] - \ |
|
429 | base[energy_category_id]['subtotal_actual'] |
|
430 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
431 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
432 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
433 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
434 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
435 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
436 | ################################################################################################################ |
|
437 | # Step 7: query reporting period energy saving |
|
438 | ################################################################################################################ |
|
439 | reporting = dict() |
|
440 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
441 | for energy_category_id in energy_category_set: |
|
442 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
443 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
444 | ||
445 | reporting[energy_category_id] = dict() |
|
446 | reporting[energy_category_id]['timestamps'] = list() |
|
447 | reporting[energy_category_id]['values_baseline'] = list() |
|
448 | reporting[energy_category_id]['values_actual'] = list() |
|
449 | reporting[energy_category_id]['values_saving'] = list() |
|
450 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
451 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
452 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
453 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
454 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
455 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
456 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
457 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
458 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
459 | # query reporting period's energy baseline |
|
460 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
461 | " FROM tbl_combined_equipment_input_category_hourly " |
|
462 | " WHERE combined_equipment_id = %s " |
|
463 | " AND energy_category_id = %s " |
|
464 | " AND start_datetime_utc >= %s " |
|
465 | " AND start_datetime_utc < %s " |
|
466 | " ORDER BY start_datetime_utc ", |
|
467 | (combined_equipment['id'], |
|
468 | energy_category_id, |
|
469 | reporting_start_datetime_utc, |
|
470 | reporting_end_datetime_utc)) |
|
471 | rows_combined_equipment_hourly = cursor_energy_baseline.fetchall() |
|
472 | ||
473 | rows_combined_equipment_periodically = \ |
|
474 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
475 | reporting_start_datetime_utc, |
|
476 | reporting_end_datetime_utc, |
|
477 | period_type) |
|
478 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
479 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
480 | timedelta(minutes=timezone_offset) |
|
481 | if period_type == 'hourly': |
|
482 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
483 | elif period_type == 'daily': |
|
484 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
485 | elif period_type == 'weekly': |
|
486 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
487 | elif period_type == 'monthly': |
|
488 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
489 | elif period_type == 'yearly': |
|
490 | current_datetime = current_datetime_local.strftime('%Y') |
|
491 | ||
492 | baseline_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
493 | else row_combined_equipment_periodically[1] |
|
494 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
495 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
|
496 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
|
497 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
498 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
499 | ||
500 | # query reporting period's energy actual |
|
501 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
502 | " FROM tbl_combined_equipment_input_category_hourly " |
|
503 | " WHERE combined_equipment_id = %s " |
|
504 | " AND energy_category_id = %s " |
|
505 | " AND start_datetime_utc >= %s " |
|
506 | " AND start_datetime_utc < %s " |
|
507 | " ORDER BY start_datetime_utc ", |
|
508 | (combined_equipment['id'], |
|
509 | energy_category_id, |
|
510 | reporting_start_datetime_utc, |
|
511 | reporting_end_datetime_utc)) |
|
512 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
513 | ||
514 | rows_combined_equipment_periodically = \ |
|
515 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
516 | reporting_start_datetime_utc, |
|
517 | reporting_end_datetime_utc, |
|
518 | period_type) |
|
519 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
520 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
521 | timedelta(minutes=timezone_offset) |
|
522 | if period_type == 'hourly': |
|
523 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
524 | elif period_type == 'daily': |
|
525 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
526 | elif period_type == 'weekly': |
|
527 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
528 | elif period_type == 'monthly': |
|
529 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
530 | elif period_type == 'yearly': |
|
531 | current_datetime = current_datetime_local.strftime('%Y') |
|
532 | ||
533 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
534 | else row_combined_equipment_periodically[1] |
|
535 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
536 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
537 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
538 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
539 | ||
540 | # calculate reporting period's energy savings |
|
541 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
|
542 | reporting[energy_category_id]['values_saving'].append( |
|
543 | reporting[energy_category_id]['values_baseline'][i] - |
|
544 | reporting[energy_category_id]['values_actual'][i]) |
|
545 | ||
546 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
547 | reporting[energy_category_id]['subtotal_baseline'] - \ |
|
548 | reporting[energy_category_id]['subtotal_actual'] |
|
549 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
550 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
551 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
552 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
553 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
554 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
555 | ################################################################################################################ |
|
556 | # Step 8: query tariff data |
|
557 | ################################################################################################################ |
|
558 | parameters_data = dict() |
|
559 | parameters_data['names'] = list() |
|
560 | parameters_data['timestamps'] = list() |
|
561 | parameters_data['values'] = list() |
|
562 | if not is_quick_mode: |
|
563 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0: |
|
564 | for energy_category_id in energy_category_set: |
|
565 | energy_category_tariff_dict = \ |
|
566 | utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'], |
|
567 | energy_category_id, |
|
568 | reporting_start_datetime_utc, |
|
569 | reporting_end_datetime_utc) |
|
570 | tariff_timestamp_list = list() |
|
571 | tariff_value_list = list() |
|
572 | for k, v in energy_category_tariff_dict.items(): |
|
573 | # convert k from utc to local |
|
574 | k = k + timedelta(minutes=timezone_offset) |
|
575 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
576 | tariff_value_list.append(v) |
|
577 | ||
578 | parameters_data['names'].append( |
|
579 | _('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
580 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
581 | parameters_data['values'].append(tariff_value_list) |
|
582 | ||
583 | ################################################################################################################ |
|
584 | # Step 9: query associated points data |
|
585 | ################################################################################################################ |
|
586 | if not is_quick_mode: |
|
587 | for point in point_list: |
|
588 | point_values = [] |
|
589 | point_timestamps = [] |
|
590 | if point['object_type'] == 'ENERGY_VALUE': |
|
591 | query = (" SELECT utc_date_time, actual_value " |
|
592 | " FROM tbl_energy_value " |
|
593 | " WHERE point_id = %s " |
|
594 | " AND utc_date_time BETWEEN %s AND %s " |
|
595 | " ORDER BY utc_date_time ") |
|
596 | cursor_historical.execute(query, (point['id'], |
|
597 | reporting_start_datetime_utc, |
|
598 | reporting_end_datetime_utc)) |
|
599 | rows = cursor_historical.fetchall() |
|
600 | ||
601 | if rows is not None and len(rows) > 0: |
|
602 | for row in rows: |
|
603 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
604 | timedelta(minutes=timezone_offset) |
|
605 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
606 | point_timestamps.append(current_datetime) |
|
607 | point_values.append(row[1]) |
|
608 | elif point['object_type'] == 'ANALOG_VALUE': |
|
609 | query = (" SELECT utc_date_time, actual_value " |
|
610 | " FROM tbl_analog_value " |
|
611 | " WHERE point_id = %s " |
|
612 | " AND utc_date_time BETWEEN %s AND %s " |
|
613 | " ORDER BY utc_date_time ") |
|
614 | cursor_historical.execute(query, (point['id'], |
|
615 | reporting_start_datetime_utc, |
|
616 | reporting_end_datetime_utc)) |
|
617 | rows = cursor_historical.fetchall() |
|
618 | ||
619 | if rows is not None and len(rows) > 0: |
|
620 | for row in rows: |
|
621 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
622 | timedelta(minutes=timezone_offset) |
|
623 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
624 | point_timestamps.append(current_datetime) |
|
625 | point_values.append(row[1]) |
|
626 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
627 | query = (" SELECT utc_date_time, actual_value " |
|
628 | " FROM tbl_digital_value " |
|
629 | " WHERE point_id = %s " |
|
630 | " AND utc_date_time BETWEEN %s AND %s " |
|
631 | " ORDER BY utc_date_time ") |
|
632 | cursor_historical.execute(query, (point['id'], |
|
633 | reporting_start_datetime_utc, |
|
634 | reporting_end_datetime_utc)) |
|
635 | rows = cursor_historical.fetchall() |
|
636 | ||
637 | if rows is not None and len(rows) > 0: |
|
638 | for row in rows: |
|
639 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
640 | timedelta(minutes=timezone_offset) |
|
641 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
642 | point_timestamps.append(current_datetime) |
|
643 | point_values.append(row[1]) |
|
644 | ||
645 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
646 | parameters_data['timestamps'].append(point_timestamps) |
|
647 | parameters_data['values'].append(point_values) |
|
648 | ||
649 | ################################################################################################################ |
|
650 | # Step 10: query associated equipments energy saving |
|
651 | ################################################################################################################ |
|
652 | associated_equipment_data = dict() |
|
653 | ||
654 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
655 | for energy_category_id in energy_category_set: |
|
656 | associated_equipment_data[energy_category_id] = dict() |
|
657 | associated_equipment_data[energy_category_id]['associated_equipment_names'] = list() |
|
658 | associated_equipment_data[energy_category_id]['subtotal_saving'] = list() |
|
659 | ||
660 | for associated_equipment in associated_equipment_list: |
|
661 | subtotal_baseline = Decimal(0.0) |
|
662 | subtotal_actual = Decimal(0.0) |
|
663 | associated_equipment_data[energy_category_id]['associated_equipment_names'].append( |
|
664 | associated_equipment['name']) |
|
665 | ||
666 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
667 | " FROM tbl_equipment_input_category_hourly " |
|
668 | " WHERE equipment_id = %s " |
|
669 | " AND energy_category_id = %s " |
|
670 | " AND start_datetime_utc >= %s " |
|
671 | " AND start_datetime_utc < %s " |
|
672 | " ORDER BY start_datetime_utc ", |
|
673 | (associated_equipment['id'], |
|
674 | energy_category_id, |
|
675 | reporting_start_datetime_utc, |
|
676 | reporting_end_datetime_utc)) |
|
677 | rows_associated_equipment_hourly = cursor_energy_baseline.fetchall() |
|
678 | ||
679 | rows_associated_equipment_periodically = \ |
|
680 | utilities.aggregate_hourly_data_by_period(rows_associated_equipment_hourly, |
|
681 | reporting_start_datetime_utc, |
|
682 | reporting_end_datetime_utc, |
|
683 | period_type) |
|
684 | ||
685 | for row_associated_equipment_periodically in rows_associated_equipment_periodically: |
|
686 | baseline_value = Decimal(0.0) if row_associated_equipment_periodically[1] is None \ |
|
687 | else row_associated_equipment_periodically[1] |
|
688 | subtotal_baseline += baseline_value |
|
689 | ||
690 | # query reporting period's energy actual |
|
691 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
692 | " FROM tbl_equipment_input_category_hourly " |
|
693 | " WHERE equipment_id = %s " |
|
694 | " AND energy_category_id = %s " |
|
695 | " AND start_datetime_utc >= %s " |
|
696 | " AND start_datetime_utc < %s " |
|
697 | " ORDER BY start_datetime_utc ", |
|
698 | (associated_equipment['id'], |
|
699 | energy_category_id, |
|
700 | reporting_start_datetime_utc, |
|
701 | reporting_end_datetime_utc)) |
|
702 | rows_associated_equipment_hourly = cursor_energy.fetchall() |
|
703 | ||
704 | rows_associated_equipment_periodically = \ |
|
705 | utilities.aggregate_hourly_data_by_period(rows_associated_equipment_hourly, |
|
706 | reporting_start_datetime_utc, |
|
707 | reporting_end_datetime_utc, |
|
708 | period_type) |
|
709 | ||
710 | for row_associated_equipment_periodically in rows_associated_equipment_periodically: |
|
711 | actual_value = Decimal(0.0) if row_associated_equipment_periodically[1] is None \ |
|
712 | else row_associated_equipment_periodically[1] |
|
713 | subtotal_actual += actual_value |
|
714 | ||
715 | associated_equipment_data[energy_category_id]['subtotal_saving'].append( |
|
716 | subtotal_baseline - subtotal_actual) |
|
717 | ||
718 | ################################################################################################################ |
|
719 | # Step 11: construct the report |
|
720 | ################################################################################################################ |
|
721 | if cursor_system: |
|
722 | cursor_system.close() |
|
723 | if cnx_system: |
|
724 | cnx_system.close() |
|
725 | ||
726 | if cursor_energy: |
|
727 | cursor_energy.close() |
|
728 | if cnx_energy: |
|
729 | cnx_energy.close() |
|
730 | ||
731 | if cursor_energy_baseline: |
|
732 | cursor_energy_baseline.close() |
|
733 | if cnx_energy_baseline: |
|
734 | cnx_energy_baseline.close() |
|
735 | ||
736 | if cursor_historical: |
|
737 | cursor_historical.close() |
|
738 | if cnx_historical: |
|
739 | cnx_historical.close() |
|
740 | ||
741 | result = dict() |
|
742 | ||
743 | result['combined_equipment'] = dict() |
|
744 | result['combined_equipment']['name'] = combined_equipment['name'] |
|
745 | ||
746 | result['base_period'] = dict() |
|
747 | result['base_period']['names'] = list() |
|
748 | result['base_period']['units'] = list() |
|
749 | result['base_period']['timestamps'] = list() |
|
750 | result['base_period']['values_saving'] = list() |
|
751 | result['base_period']['subtotals_saving'] = list() |
|
752 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
753 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
754 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
755 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
756 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
757 | for energy_category_id in energy_category_set: |
|
758 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
759 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
760 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
761 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
762 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
763 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
764 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
765 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
766 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
767 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
768 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
769 | ||
770 | result['reporting_period'] = dict() |
|
771 | result['reporting_period']['names'] = list() |
|
772 | result['reporting_period']['energy_category_ids'] = list() |
|
773 | result['reporting_period']['units'] = list() |
|
774 | result['reporting_period']['timestamps'] = list() |
|
775 | result['reporting_period']['values_saving'] = list() |
|
776 | result['reporting_period']['rates_saving'] = list() |
|
777 | result['reporting_period']['subtotals_saving'] = list() |
|
778 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
779 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
780 | result['reporting_period']['increment_rates_saving'] = list() |
|
781 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
782 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
783 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
784 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
785 | ||
786 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
787 | for energy_category_id in energy_category_set: |
|
788 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
789 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
790 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
791 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
792 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
793 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
794 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
795 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
796 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
797 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
798 | result['reporting_period']['increment_rates_saving'].append( |
|
799 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
800 | base[energy_category_id]['subtotal_saving'] |
|
801 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
802 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
803 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
804 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
805 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
806 | ||
807 | rate = list() |
|
808 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
809 | if index < len(base[energy_category_id]['values_saving']) \ |
|
810 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
811 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
812 | / base[energy_category_id]['values_saving'][index]) |
|
813 | else: |
|
814 | rate.append(None) |
|
815 | result['reporting_period']['rates_saving'].append(rate) |
|
816 | ||
817 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
818 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
819 | result['base_period']['total_in_kgce_saving'] \ |
|
820 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
821 | ||
822 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
823 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
824 | result['base_period']['total_in_kgco2e_saving'] \ |
|
825 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
826 | ||
827 | result['parameters'] = { |
|
828 | "names": parameters_data['names'], |
|
829 | "timestamps": parameters_data['timestamps'], |
|
830 | "values": parameters_data['values'] |
|
831 | } |
|
832 | ||
833 | result['associated_equipment'] = dict() |
|
834 | result['associated_equipment']['energy_category_names'] = list() |
|
835 | result['associated_equipment']['units'] = list() |
|
836 | result['associated_equipment']['associated_equipment_names_array'] = list() |
|
837 | result['associated_equipment']['subtotals_saving_array'] = list() |
|
838 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
839 | for energy_category_id in energy_category_set: |
|
840 | result['associated_equipment']['energy_category_names'].append( |
|
841 | energy_category_dict[energy_category_id]['name']) |
|
842 | result['associated_equipment']['units'].append( |
|
843 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
844 | result['associated_equipment']['associated_equipment_names_array'].append( |
|
845 | associated_equipment_data[energy_category_id]['associated_equipment_names']) |
|
846 | result['associated_equipment']['subtotals_saving_array'].append( |
|
847 | associated_equipment_data[energy_category_id]['subtotal_saving']) |
|
848 | ||
849 | # export result to Excel file and then encode the file to base64 string |
|
850 | result['excel_bytes_base64'] = None |
|
851 | if not is_quick_mode: |
|
852 | result['excel_bytes_base64'] = \ |
|
853 | excelexporters.combinedequipmentsaving.export(result, |
|
854 | combined_equipment['name'], |
|
855 | base_period_start_datetime_local, |
|
856 | base_period_end_datetime_local, |
|
857 | reporting_period_start_datetime_local, |
|
858 | reporting_period_end_datetime_local, |
|
859 | period_type, |
|
860 | language) |
|
861 | ||
862 | resp.text = json.dumps(result) |
|
863 |
@@ 13-862 (lines=850) @@ | ||
10 | from core.useractivity import access_control, api_key_control |
|
11 | ||
12 | ||
13 | class Reporting: |
|
14 | @staticmethod |
|
15 | def __init__(): |
|
16 | """"Initializes Reporting""" |
|
17 | pass |
|
18 | ||
19 | @staticmethod |
|
20 | def on_options(req, resp): |
|
21 | resp.status = falcon.HTTP_200 |
|
22 | ||
23 | #################################################################################################################### |
|
24 | # PROCEDURES |
|
25 | # Step 1: valid parameters |
|
26 | # Step 2: query the combined equipment |
|
27 | # Step 3: query energy categories |
|
28 | # Step 4: query associated points |
|
29 | # Step 5: query associated equipments |
|
30 | # Step 6: query base period energy saving |
|
31 | # Step 7: query reporting period energy saving |
|
32 | # Step 8: query tariff data |
|
33 | # Step 9: query associated points data |
|
34 | # Step 10: query associated equipments energy saving |
|
35 | # Step 11: construct the report |
|
36 | #################################################################################################################### |
|
37 | @staticmethod |
|
38 | def on_get(req, resp): |
|
39 | if 'API-KEY' not in req.headers or \ |
|
40 | not isinstance(req.headers['API-KEY'], str) or \ |
|
41 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
42 | access_control(req) |
|
43 | else: |
|
44 | api_key_control(req) |
|
45 | print(req.params) |
|
46 | combined_equipment_id = req.params.get('combinedequipmentid') |
|
47 | combined_equipment_uuid = req.params.get('combinedequipmentuuid') |
|
48 | period_type = req.params.get('periodtype') |
|
49 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
50 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
51 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
52 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
53 | language = req.params.get('language') |
|
54 | quick_mode = req.params.get('quickmode') |
|
55 | ||
56 | ################################################################################################################ |
|
57 | # Step 1: valid parameters |
|
58 | ################################################################################################################ |
|
59 | if combined_equipment_id is None and combined_equipment_uuid is None: |
|
60 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
61 | title='API.BAD_REQUEST', |
|
62 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
63 | ||
64 | if combined_equipment_id is not None: |
|
65 | combined_equipment_id = str.strip(combined_equipment_id) |
|
66 | if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0: |
|
67 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
68 | title='API.BAD_REQUEST', |
|
69 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
70 | ||
71 | if combined_equipment_uuid is not None: |
|
72 | regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) |
|
73 | match = regex.match(str.strip(combined_equipment_uuid)) |
|
74 | if not bool(match): |
|
75 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
76 | title='API.BAD_REQUEST', |
|
77 | description='API.INVALID_COMBINED_EQUIPMENT_UUID') |
|
78 | ||
79 | if period_type is None: |
|
80 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
81 | description='API.INVALID_PERIOD_TYPE') |
|
82 | else: |
|
83 | period_type = str.strip(period_type) |
|
84 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
85 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
86 | description='API.INVALID_PERIOD_TYPE') |
|
87 | ||
88 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
89 | if config.utc_offset[0] == '-': |
|
90 | timezone_offset = -timezone_offset |
|
91 | ||
92 | base_start_datetime_utc = None |
|
93 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
94 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
95 | try: |
|
96 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
97 | except ValueError: |
|
98 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
99 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
100 | base_start_datetime_utc = \ |
|
101 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
102 | # nomalize the start datetime |
|
103 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
104 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
105 | else: |
|
106 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
107 | ||
108 | base_end_datetime_utc = None |
|
109 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
110 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
111 | try: |
|
112 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
113 | except ValueError: |
|
114 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
115 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
116 | base_end_datetime_utc = \ |
|
117 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
118 | ||
119 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
120 | base_start_datetime_utc >= base_end_datetime_utc: |
|
121 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
122 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
123 | ||
124 | if reporting_period_start_datetime_local is None: |
|
125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
126 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
127 | else: |
|
128 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
129 | try: |
|
130 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
131 | '%Y-%m-%dT%H:%M:%S') |
|
132 | except ValueError: |
|
133 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
134 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
135 | reporting_start_datetime_utc = \ |
|
136 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
137 | # nomalize the start datetime |
|
138 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
139 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
140 | else: |
|
141 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
142 | ||
143 | if reporting_period_end_datetime_local is None: |
|
144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
145 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
146 | else: |
|
147 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
148 | try: |
|
149 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
150 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
151 | timedelta(minutes=timezone_offset) |
|
152 | except ValueError: |
|
153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
154 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
155 | ||
156 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
158 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
159 | ||
160 | # if turn quick mode on, do not return parameters data and excel file |
|
161 | is_quick_mode = False |
|
162 | if quick_mode is not None and \ |
|
163 | len(str.strip(quick_mode)) > 0 and \ |
|
164 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
165 | is_quick_mode = True |
|
166 | ||
167 | trans = utilities.get_translation(language) |
|
168 | trans.install() |
|
169 | _ = trans.gettext |
|
170 | ||
171 | ################################################################################################################ |
|
172 | # Step 2: query the combined equipment |
|
173 | ################################################################################################################ |
|
174 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
175 | cursor_system = cnx_system.cursor() |
|
176 | ||
177 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
178 | cursor_energy = cnx_energy.cursor() |
|
179 | ||
180 | cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db) |
|
181 | cursor_energy_plan = cnx_energy_plan.cursor() |
|
182 | ||
183 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
184 | cursor_historical = cnx_historical.cursor() |
|
185 | ||
186 | if combined_equipment_id is not None: |
|
187 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
188 | " FROM tbl_combined_equipments " |
|
189 | " WHERE id = %s ", (combined_equipment_id,)) |
|
190 | row_combined_equipment = cursor_system.fetchone() |
|
191 | elif combined_equipment_uuid is not None: |
|
192 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
193 | " FROM tbl_combined_equipments " |
|
194 | " WHERE uuid = %s ", (combined_equipment_uuid,)) |
|
195 | row_combined_equipment = cursor_system.fetchone() |
|
196 | ||
197 | if row_combined_equipment is None: |
|
198 | if cursor_system: |
|
199 | cursor_system.close() |
|
200 | if cnx_system: |
|
201 | cnx_system.close() |
|
202 | ||
203 | if cursor_energy: |
|
204 | cursor_energy.close() |
|
205 | if cnx_energy: |
|
206 | cnx_energy.close() |
|
207 | ||
208 | if cursor_energy_plan: |
|
209 | cursor_energy_plan.close() |
|
210 | if cnx_energy_plan: |
|
211 | cnx_energy_plan.close() |
|
212 | ||
213 | if cursor_historical: |
|
214 | cursor_historical.close() |
|
215 | if cnx_historical: |
|
216 | cnx_historical.close() |
|
217 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
218 | title='API.NOT_FOUND', |
|
219 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
220 | ||
221 | combined_equipment = dict() |
|
222 | combined_equipment['id'] = row_combined_equipment[0] |
|
223 | combined_equipment['name'] = row_combined_equipment[1] |
|
224 | combined_equipment['cost_center_id'] = row_combined_equipment[2] |
|
225 | ||
226 | ################################################################################################################ |
|
227 | # Step 3: query energy categories |
|
228 | ################################################################################################################ |
|
229 | energy_category_set = set() |
|
230 | # query energy categories in base period |
|
231 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
232 | " FROM tbl_combined_equipment_input_category_hourly " |
|
233 | " WHERE combined_equipment_id = %s " |
|
234 | " AND start_datetime_utc >= %s " |
|
235 | " AND start_datetime_utc < %s ", |
|
236 | (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
237 | rows_energy_categories = cursor_energy.fetchall() |
|
238 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
239 | for row_energy_category in rows_energy_categories: |
|
240 | energy_category_set.add(row_energy_category[0]) |
|
241 | ||
242 | # query energy categories in reporting period |
|
243 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
244 | " FROM tbl_combined_equipment_input_category_hourly " |
|
245 | " WHERE combined_equipment_id = %s " |
|
246 | " AND start_datetime_utc >= %s " |
|
247 | " AND start_datetime_utc < %s ", |
|
248 | (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
249 | rows_energy_categories = cursor_energy.fetchall() |
|
250 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
251 | for row_energy_category in rows_energy_categories: |
|
252 | energy_category_set.add(row_energy_category[0]) |
|
253 | ||
254 | # query all energy categories in base period and reporting period |
|
255 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
256 | " FROM tbl_energy_categories " |
|
257 | " ORDER BY id ", ) |
|
258 | rows_energy_categories = cursor_system.fetchall() |
|
259 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
260 | if cursor_system: |
|
261 | cursor_system.close() |
|
262 | if cnx_system: |
|
263 | cnx_system.close() |
|
264 | ||
265 | if cursor_energy: |
|
266 | cursor_energy.close() |
|
267 | if cnx_energy: |
|
268 | cnx_energy.close() |
|
269 | ||
270 | if cursor_energy_plan: |
|
271 | cursor_energy_plan.close() |
|
272 | if cnx_energy_plan: |
|
273 | cnx_energy_plan.close() |
|
274 | ||
275 | if cursor_historical: |
|
276 | cursor_historical.close() |
|
277 | if cnx_historical: |
|
278 | cnx_historical.close() |
|
279 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
280 | title='API.NOT_FOUND', |
|
281 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
282 | energy_category_dict = dict() |
|
283 | for row_energy_category in rows_energy_categories: |
|
284 | if row_energy_category[0] in energy_category_set: |
|
285 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
286 | "unit_of_measure": row_energy_category[2], |
|
287 | "kgce": row_energy_category[3], |
|
288 | "kgco2e": row_energy_category[4]} |
|
289 | ||
290 | ################################################################################################################ |
|
291 | # Step 4: query associated points |
|
292 | ################################################################################################################ |
|
293 | point_list = list() |
|
294 | cursor_system.execute(" SELECT p.id, ep.name, p.units, p.object_type " |
|
295 | " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " |
|
296 | " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " |
|
297 | " AND ep.point_id = p.id " |
|
298 | " ORDER BY p.id ", (combined_equipment['id'],)) |
|
299 | rows_points = cursor_system.fetchall() |
|
300 | if rows_points is not None and len(rows_points) > 0: |
|
301 | for row in rows_points: |
|
302 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
303 | ||
304 | ################################################################################################################ |
|
305 | # Step 5: query associated equipments |
|
306 | ################################################################################################################ |
|
307 | associated_equipment_list = list() |
|
308 | cursor_system.execute(" SELECT e.id, e.name " |
|
309 | " FROM tbl_equipments e,tbl_combined_equipments_equipments ee" |
|
310 | " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id" |
|
311 | " ORDER BY id ", (combined_equipment['id'],)) |
|
312 | rows_associated_equipments = cursor_system.fetchall() |
|
313 | if rows_associated_equipments is not None and len(rows_associated_equipments) > 0: |
|
314 | for row in rows_associated_equipments: |
|
315 | associated_equipment_list.append({"id": row[0], "name": row[1]}) |
|
316 | ||
317 | ################################################################################################################ |
|
318 | # Step 6: query base period energy saving |
|
319 | ################################################################################################################ |
|
320 | base = dict() |
|
321 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
322 | for energy_category_id in energy_category_set: |
|
323 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
324 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
325 | ||
326 | base[energy_category_id] = dict() |
|
327 | base[energy_category_id]['timestamps'] = list() |
|
328 | base[energy_category_id]['values_plan'] = list() |
|
329 | base[energy_category_id]['values_actual'] = list() |
|
330 | base[energy_category_id]['values_saving'] = list() |
|
331 | base[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
332 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
333 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
334 | base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
335 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
336 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
337 | base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
338 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
339 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
340 | # query base period's energy plan |
|
341 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
342 | " FROM tbl_combined_equipment_input_category_hourly " |
|
343 | " WHERE combined_equipment_id = %s " |
|
344 | " AND energy_category_id = %s " |
|
345 | " AND start_datetime_utc >= %s " |
|
346 | " AND start_datetime_utc < %s " |
|
347 | " ORDER BY start_datetime_utc ", |
|
348 | (combined_equipment['id'], |
|
349 | energy_category_id, |
|
350 | base_start_datetime_utc, |
|
351 | base_end_datetime_utc)) |
|
352 | rows_combined_equipment_hourly = cursor_energy_plan.fetchall() |
|
353 | ||
354 | rows_combined_equipment_periodically = \ |
|
355 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
356 | base_start_datetime_utc, |
|
357 | base_end_datetime_utc, |
|
358 | period_type) |
|
359 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
360 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
361 | timedelta(minutes=timezone_offset) |
|
362 | if period_type == 'hourly': |
|
363 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
364 | elif period_type == 'daily': |
|
365 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
366 | elif period_type == 'weekly': |
|
367 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
368 | elif period_type == 'monthly': |
|
369 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
370 | elif period_type == 'yearly': |
|
371 | current_datetime = current_datetime_local.strftime('%Y') |
|
372 | ||
373 | plan_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
374 | else row_combined_equipment_periodically[1] |
|
375 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
376 | base[energy_category_id]['values_plan'].append(plan_value) |
|
377 | base[energy_category_id]['subtotal_plan'] += plan_value |
|
378 | base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
379 | base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
380 | ||
381 | # query base period's energy actual |
|
382 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
383 | " FROM tbl_combined_equipment_input_category_hourly " |
|
384 | " WHERE combined_equipment_id = %s " |
|
385 | " AND energy_category_id = %s " |
|
386 | " AND start_datetime_utc >= %s " |
|
387 | " AND start_datetime_utc < %s " |
|
388 | " ORDER BY start_datetime_utc ", |
|
389 | (combined_equipment['id'], |
|
390 | energy_category_id, |
|
391 | base_start_datetime_utc, |
|
392 | base_end_datetime_utc)) |
|
393 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
394 | ||
395 | rows_combined_equipment_periodically = \ |
|
396 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
397 | base_start_datetime_utc, |
|
398 | base_end_datetime_utc, |
|
399 | period_type) |
|
400 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
401 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
402 | timedelta(minutes=timezone_offset) |
|
403 | if period_type == 'hourly': |
|
404 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
405 | elif period_type == 'daily': |
|
406 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
407 | elif period_type == 'weekly': |
|
408 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
409 | elif period_type == 'monthly': |
|
410 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
411 | elif period_type == 'yearly': |
|
412 | current_datetime = current_datetime_local.strftime('%Y') |
|
413 | ||
414 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
415 | else row_combined_equipment_periodically[1] |
|
416 | base[energy_category_id]['values_actual'].append(actual_value) |
|
417 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
418 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
419 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
420 | ||
421 | # calculate base period's energy savings |
|
422 | for i in range(len(base[energy_category_id]['values_plan'])): |
|
423 | base[energy_category_id]['values_saving'].append( |
|
424 | base[energy_category_id]['values_plan'][i] - |
|
425 | base[energy_category_id]['values_actual'][i]) |
|
426 | ||
427 | base[energy_category_id]['subtotal_saving'] = \ |
|
428 | base[energy_category_id]['subtotal_plan'] - \ |
|
429 | base[energy_category_id]['subtotal_actual'] |
|
430 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
431 | base[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
432 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
433 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
434 | base[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
435 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
436 | ################################################################################################################ |
|
437 | # Step 7: query reporting period energy saving |
|
438 | ################################################################################################################ |
|
439 | reporting = dict() |
|
440 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
441 | for energy_category_id in energy_category_set: |
|
442 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
443 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
444 | ||
445 | reporting[energy_category_id] = dict() |
|
446 | reporting[energy_category_id]['timestamps'] = list() |
|
447 | reporting[energy_category_id]['values_plan'] = list() |
|
448 | reporting[energy_category_id]['values_actual'] = list() |
|
449 | reporting[energy_category_id]['values_saving'] = list() |
|
450 | reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
451 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
452 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
453 | reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
454 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
455 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
456 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
457 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
458 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
459 | # query reporting period's energy plan |
|
460 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
461 | " FROM tbl_combined_equipment_input_category_hourly " |
|
462 | " WHERE combined_equipment_id = %s " |
|
463 | " AND energy_category_id = %s " |
|
464 | " AND start_datetime_utc >= %s " |
|
465 | " AND start_datetime_utc < %s " |
|
466 | " ORDER BY start_datetime_utc ", |
|
467 | (combined_equipment['id'], |
|
468 | energy_category_id, |
|
469 | reporting_start_datetime_utc, |
|
470 | reporting_end_datetime_utc)) |
|
471 | rows_combined_equipment_hourly = cursor_energy_plan.fetchall() |
|
472 | ||
473 | rows_combined_equipment_periodically = \ |
|
474 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
475 | reporting_start_datetime_utc, |
|
476 | reporting_end_datetime_utc, |
|
477 | period_type) |
|
478 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
479 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
480 | timedelta(minutes=timezone_offset) |
|
481 | if period_type == 'hourly': |
|
482 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
483 | elif period_type == 'daily': |
|
484 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
485 | elif period_type == 'weekly': |
|
486 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
487 | elif period_type == 'monthly': |
|
488 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
489 | elif period_type == 'yearly': |
|
490 | current_datetime = current_datetime_local.strftime('%Y') |
|
491 | ||
492 | plan_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
493 | else row_combined_equipment_periodically[1] |
|
494 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
495 | reporting[energy_category_id]['values_plan'].append(plan_value) |
|
496 | reporting[energy_category_id]['subtotal_plan'] += plan_value |
|
497 | reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
498 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
499 | ||
500 | # query reporting period's energy actual |
|
501 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
502 | " FROM tbl_combined_equipment_input_category_hourly " |
|
503 | " WHERE combined_equipment_id = %s " |
|
504 | " AND energy_category_id = %s " |
|
505 | " AND start_datetime_utc >= %s " |
|
506 | " AND start_datetime_utc < %s " |
|
507 | " ORDER BY start_datetime_utc ", |
|
508 | (combined_equipment['id'], |
|
509 | energy_category_id, |
|
510 | reporting_start_datetime_utc, |
|
511 | reporting_end_datetime_utc)) |
|
512 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
513 | ||
514 | rows_combined_equipment_periodically = \ |
|
515 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
516 | reporting_start_datetime_utc, |
|
517 | reporting_end_datetime_utc, |
|
518 | period_type) |
|
519 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
520 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
521 | timedelta(minutes=timezone_offset) |
|
522 | if period_type == 'hourly': |
|
523 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
524 | elif period_type == 'daily': |
|
525 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
526 | elif period_type == 'weekly': |
|
527 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
528 | elif period_type == 'monthly': |
|
529 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
530 | elif period_type == 'yearly': |
|
531 | current_datetime = current_datetime_local.strftime('%Y') |
|
532 | ||
533 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
534 | else row_combined_equipment_periodically[1] |
|
535 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
536 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
537 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
538 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
539 | ||
540 | # calculate reporting period's energy savings |
|
541 | for i in range(len(reporting[energy_category_id]['values_plan'])): |
|
542 | reporting[energy_category_id]['values_saving'].append( |
|
543 | reporting[energy_category_id]['values_plan'][i] - |
|
544 | reporting[energy_category_id]['values_actual'][i]) |
|
545 | ||
546 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
547 | reporting[energy_category_id]['subtotal_plan'] - \ |
|
548 | reporting[energy_category_id]['subtotal_actual'] |
|
549 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
550 | reporting[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
551 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
552 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
553 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
554 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
555 | ################################################################################################################ |
|
556 | # Step 8: query tariff data |
|
557 | ################################################################################################################ |
|
558 | parameters_data = dict() |
|
559 | parameters_data['names'] = list() |
|
560 | parameters_data['timestamps'] = list() |
|
561 | parameters_data['values'] = list() |
|
562 | if not is_quick_mode: |
|
563 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0: |
|
564 | for energy_category_id in energy_category_set: |
|
565 | energy_category_tariff_dict = \ |
|
566 | utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'], |
|
567 | energy_category_id, |
|
568 | reporting_start_datetime_utc, |
|
569 | reporting_end_datetime_utc) |
|
570 | tariff_timestamp_list = list() |
|
571 | tariff_value_list = list() |
|
572 | for k, v in energy_category_tariff_dict.items(): |
|
573 | # convert k from utc to local |
|
574 | k = k + timedelta(minutes=timezone_offset) |
|
575 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
576 | tariff_value_list.append(v) |
|
577 | ||
578 | parameters_data['names'].append( |
|
579 | _('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
580 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
581 | parameters_data['values'].append(tariff_value_list) |
|
582 | ||
583 | ################################################################################################################ |
|
584 | # Step 9: query associated points data |
|
585 | ################################################################################################################ |
|
586 | if not is_quick_mode: |
|
587 | for point in point_list: |
|
588 | point_values = [] |
|
589 | point_timestamps = [] |
|
590 | if point['object_type'] == 'ENERGY_VALUE': |
|
591 | query = (" SELECT utc_date_time, actual_value " |
|
592 | " FROM tbl_energy_value " |
|
593 | " WHERE point_id = %s " |
|
594 | " AND utc_date_time BETWEEN %s AND %s " |
|
595 | " ORDER BY utc_date_time ") |
|
596 | cursor_historical.execute(query, (point['id'], |
|
597 | reporting_start_datetime_utc, |
|
598 | reporting_end_datetime_utc)) |
|
599 | rows = cursor_historical.fetchall() |
|
600 | ||
601 | if rows is not None and len(rows) > 0: |
|
602 | for row in rows: |
|
603 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
604 | timedelta(minutes=timezone_offset) |
|
605 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
606 | point_timestamps.append(current_datetime) |
|
607 | point_values.append(row[1]) |
|
608 | elif point['object_type'] == 'ANALOG_VALUE': |
|
609 | query = (" SELECT utc_date_time, actual_value " |
|
610 | " FROM tbl_analog_value " |
|
611 | " WHERE point_id = %s " |
|
612 | " AND utc_date_time BETWEEN %s AND %s " |
|
613 | " ORDER BY utc_date_time ") |
|
614 | cursor_historical.execute(query, (point['id'], |
|
615 | reporting_start_datetime_utc, |
|
616 | reporting_end_datetime_utc)) |
|
617 | rows = cursor_historical.fetchall() |
|
618 | ||
619 | if rows is not None and len(rows) > 0: |
|
620 | for row in rows: |
|
621 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
622 | timedelta(minutes=timezone_offset) |
|
623 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
624 | point_timestamps.append(current_datetime) |
|
625 | point_values.append(row[1]) |
|
626 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
627 | query = (" SELECT utc_date_time, actual_value " |
|
628 | " FROM tbl_digital_value " |
|
629 | " WHERE point_id = %s " |
|
630 | " AND utc_date_time BETWEEN %s AND %s " |
|
631 | " ORDER BY utc_date_time ") |
|
632 | cursor_historical.execute(query, (point['id'], |
|
633 | reporting_start_datetime_utc, |
|
634 | reporting_end_datetime_utc)) |
|
635 | rows = cursor_historical.fetchall() |
|
636 | ||
637 | if rows is not None and len(rows) > 0: |
|
638 | for row in rows: |
|
639 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
640 | timedelta(minutes=timezone_offset) |
|
641 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
642 | point_timestamps.append(current_datetime) |
|
643 | point_values.append(row[1]) |
|
644 | ||
645 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
646 | parameters_data['timestamps'].append(point_timestamps) |
|
647 | parameters_data['values'].append(point_values) |
|
648 | ||
649 | ################################################################################################################ |
|
650 | # Step 10: query associated equipments energy saving |
|
651 | ################################################################################################################ |
|
652 | associated_equipment_data = dict() |
|
653 | ||
654 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
655 | for energy_category_id in energy_category_set: |
|
656 | associated_equipment_data[energy_category_id] = dict() |
|
657 | associated_equipment_data[energy_category_id]['associated_equipment_names'] = list() |
|
658 | associated_equipment_data[energy_category_id]['subtotal_saving'] = list() |
|
659 | ||
660 | for associated_equipment in associated_equipment_list: |
|
661 | subtotal_plan = Decimal(0.0) |
|
662 | subtotal_actual = Decimal(0.0) |
|
663 | associated_equipment_data[energy_category_id]['associated_equipment_names'].append( |
|
664 | associated_equipment['name']) |
|
665 | ||
666 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
667 | " FROM tbl_equipment_input_category_hourly " |
|
668 | " WHERE equipment_id = %s " |
|
669 | " AND energy_category_id = %s " |
|
670 | " AND start_datetime_utc >= %s " |
|
671 | " AND start_datetime_utc < %s " |
|
672 | " ORDER BY start_datetime_utc ", |
|
673 | (associated_equipment['id'], |
|
674 | energy_category_id, |
|
675 | reporting_start_datetime_utc, |
|
676 | reporting_end_datetime_utc)) |
|
677 | rows_associated_equipment_hourly = cursor_energy_plan.fetchall() |
|
678 | ||
679 | rows_associated_equipment_periodically = \ |
|
680 | utilities.aggregate_hourly_data_by_period(rows_associated_equipment_hourly, |
|
681 | reporting_start_datetime_utc, |
|
682 | reporting_end_datetime_utc, |
|
683 | period_type) |
|
684 | ||
685 | for row_associated_equipment_periodically in rows_associated_equipment_periodically: |
|
686 | plan_value = Decimal(0.0) if row_associated_equipment_periodically[1] is None \ |
|
687 | else row_associated_equipment_periodically[1] |
|
688 | subtotal_plan += plan_value |
|
689 | ||
690 | # query reporting period's energy actual |
|
691 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
692 | " FROM tbl_equipment_input_category_hourly " |
|
693 | " WHERE equipment_id = %s " |
|
694 | " AND energy_category_id = %s " |
|
695 | " AND start_datetime_utc >= %s " |
|
696 | " AND start_datetime_utc < %s " |
|
697 | " ORDER BY start_datetime_utc ", |
|
698 | (associated_equipment['id'], |
|
699 | energy_category_id, |
|
700 | reporting_start_datetime_utc, |
|
701 | reporting_end_datetime_utc)) |
|
702 | rows_associated_equipment_hourly = cursor_energy.fetchall() |
|
703 | ||
704 | rows_associated_equipment_periodically = \ |
|
705 | utilities.aggregate_hourly_data_by_period(rows_associated_equipment_hourly, |
|
706 | reporting_start_datetime_utc, |
|
707 | reporting_end_datetime_utc, |
|
708 | period_type) |
|
709 | ||
710 | for row_associated_equipment_periodically in rows_associated_equipment_periodically: |
|
711 | actual_value = Decimal(0.0) if row_associated_equipment_periodically[1] is None \ |
|
712 | else row_associated_equipment_periodically[1] |
|
713 | subtotal_actual += actual_value |
|
714 | ||
715 | associated_equipment_data[energy_category_id]['subtotal_saving'].append( |
|
716 | subtotal_plan - subtotal_actual) |
|
717 | ||
718 | ################################################################################################################ |
|
719 | # Step 11: construct the report |
|
720 | ################################################################################################################ |
|
721 | if cursor_system: |
|
722 | cursor_system.close() |
|
723 | if cnx_system: |
|
724 | cnx_system.close() |
|
725 | ||
726 | if cursor_energy: |
|
727 | cursor_energy.close() |
|
728 | if cnx_energy: |
|
729 | cnx_energy.close() |
|
730 | ||
731 | if cursor_energy_plan: |
|
732 | cursor_energy_plan.close() |
|
733 | if cnx_energy_plan: |
|
734 | cnx_energy_plan.close() |
|
735 | ||
736 | if cursor_historical: |
|
737 | cursor_historical.close() |
|
738 | if cnx_historical: |
|
739 | cnx_historical.close() |
|
740 | ||
741 | result = dict() |
|
742 | ||
743 | result['combined_equipment'] = dict() |
|
744 | result['combined_equipment']['name'] = combined_equipment['name'] |
|
745 | ||
746 | result['base_period'] = dict() |
|
747 | result['base_period']['names'] = list() |
|
748 | result['base_period']['units'] = list() |
|
749 | result['base_period']['timestamps'] = list() |
|
750 | result['base_period']['values_saving'] = list() |
|
751 | result['base_period']['subtotals_saving'] = list() |
|
752 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
753 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
754 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
755 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
756 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
757 | for energy_category_id in energy_category_set: |
|
758 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
759 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
760 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
761 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
762 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
763 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
764 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
765 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
766 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
767 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
768 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
769 | ||
770 | result['reporting_period'] = dict() |
|
771 | result['reporting_period']['names'] = list() |
|
772 | result['reporting_period']['energy_category_ids'] = list() |
|
773 | result['reporting_period']['units'] = list() |
|
774 | result['reporting_period']['timestamps'] = list() |
|
775 | result['reporting_period']['values_saving'] = list() |
|
776 | result['reporting_period']['rates_saving'] = list() |
|
777 | result['reporting_period']['subtotals_saving'] = list() |
|
778 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
779 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
780 | result['reporting_period']['increment_rates_saving'] = list() |
|
781 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
782 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
783 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
784 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
785 | ||
786 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
787 | for energy_category_id in energy_category_set: |
|
788 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
789 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
790 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
791 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
792 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
793 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
794 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
795 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
796 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
797 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
798 | result['reporting_period']['increment_rates_saving'].append( |
|
799 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
800 | base[energy_category_id]['subtotal_saving'] |
|
801 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
802 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
803 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
804 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
805 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
806 | ||
807 | rate = list() |
|
808 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
809 | if index < len(base[energy_category_id]['values_saving']) \ |
|
810 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
811 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
812 | / base[energy_category_id]['values_saving'][index]) |
|
813 | else: |
|
814 | rate.append(None) |
|
815 | result['reporting_period']['rates_saving'].append(rate) |
|
816 | ||
817 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
818 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
819 | result['base_period']['total_in_kgce_saving'] \ |
|
820 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
821 | ||
822 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
823 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
824 | result['base_period']['total_in_kgco2e_saving'] \ |
|
825 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
826 | ||
827 | result['parameters'] = { |
|
828 | "names": parameters_data['names'], |
|
829 | "timestamps": parameters_data['timestamps'], |
|
830 | "values": parameters_data['values'] |
|
831 | } |
|
832 | ||
833 | result['associated_equipment'] = dict() |
|
834 | result['associated_equipment']['energy_category_names'] = list() |
|
835 | result['associated_equipment']['units'] = list() |
|
836 | result['associated_equipment']['associated_equipment_names_array'] = list() |
|
837 | result['associated_equipment']['subtotals_saving_array'] = list() |
|
838 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
839 | for energy_category_id in energy_category_set: |
|
840 | result['associated_equipment']['energy_category_names'].append( |
|
841 | energy_category_dict[energy_category_id]['name']) |
|
842 | result['associated_equipment']['units'].append( |
|
843 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
844 | result['associated_equipment']['associated_equipment_names_array'].append( |
|
845 | associated_equipment_data[energy_category_id]['associated_equipment_names']) |
|
846 | result['associated_equipment']['subtotals_saving_array'].append( |
|
847 | associated_equipment_data[energy_category_id]['subtotal_saving']) |
|
848 | ||
849 | # export result to Excel file and then encode the file to base64 string |
|
850 | result['excel_bytes_base64'] = None |
|
851 | if not is_quick_mode: |
|
852 | result['excel_bytes_base64'] = \ |
|
853 | excelexporters.combinedequipmentsaving.export(result, |
|
854 | combined_equipment['name'], |
|
855 | base_period_start_datetime_local, |
|
856 | base_period_end_datetime_local, |
|
857 | reporting_period_start_datetime_local, |
|
858 | reporting_period_end_datetime_local, |
|
859 | period_type, |
|
860 | language) |
|
861 | ||
862 | resp.text = json.dumps(result) |
|
863 |