Code Duplication    Length = 660-661 lines in 2 locations

myems-api/reports/combinedequipmentcarbon.py 1 location

@@ 14-674 (lines=661) @@
11
from core.useractivity import access_control, api_key_control
12
13
14
class Reporting:
15
16
    def __init__(self):
17
        """"Initializes Reporting"""
18
        pass
19
20
    @staticmethod
21
    def on_options(req, resp):
22
        _ = req
23
        resp.status = falcon.HTTP_200
24
25
    ####################################################################################################################
26
    # PROCEDURES
27
    # Step 1: valid parameters
28
    # Step 2: query the combined equipment
29
    # Step 3: query energy items
30
    # Step 4: query associated points
31
    # Step 5: query associated equipments
32
    # Step 6: query base period energy carbon dioxide emissions
33
    # Step 7: query reporting period energy carbon dioxide emissions
34
    # Step 8: query tariff data
35
    # Step 9: query associated points data
36
    # Step 10: query associated equipments energy carbon dioxide emissions
37
    # Step 11: construct the report
38
    ####################################################################################################################
39
    @staticmethod
40
    def on_get(req, resp):
41
        if 'API-KEY' not in req.headers or \
42
                not isinstance(req.headers['API-KEY'], str) or \
43
                len(str.strip(req.headers['API-KEY'])) == 0:
44
            access_control(req)
45
        else:
46
            api_key_control(req)
47
        print(req.params)
48
        combined_equipment_id = req.params.get('combinedequipmentid')
49
        combined_equipment_uuid = req.params.get('combinedequipmentuuid')
50
        period_type = req.params.get('periodtype')
51
        base_period_start_datetime_local = req.params.get('baseperiodstartdatetime')
52
        base_period_end_datetime_local = req.params.get('baseperiodenddatetime')
53
        reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime')
54
        reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime')
55
        language = req.params.get('language')
56
        quick_mode = req.params.get('quickmode')
57
58
        ################################################################################################################
59
        # Step 1: valid parameters
60
        ################################################################################################################
61
        if combined_equipment_id is None and combined_equipment_uuid is None:
62
            raise falcon.HTTPError(status=falcon.HTTP_400,
63
                                   title='API.BAD_REQUEST',
64
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
65
66
        if combined_equipment_id is not None:
67
            combined_equipment_id = str.strip(combined_equipment_id)
68
            if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0:
69
                raise falcon.HTTPError(status=falcon.HTTP_400,
70
                                       title='API.BAD_REQUEST',
71
                                       description='API.INVALID_COMBINED_EQUIPMENT_ID')
72
73
        if combined_equipment_uuid is not None:
74
            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)
75
            match = regex.match(str.strip(combined_equipment_uuid))
76
            if not bool(match):
77
                raise falcon.HTTPError(status=falcon.HTTP_400,
78
                                       title='API.BAD_REQUEST',
79
                                       description='API.INVALID_COMBINED_EQUIPMENT_UUID')
80
81
        if period_type is None:
82
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
83
                                   description='API.INVALID_PERIOD_TYPE')
84
        else:
85
            period_type = str.strip(period_type)
86
            if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']:
87
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
88
                                       description='API.INVALID_PERIOD_TYPE')
89
90
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
91
        if config.utc_offset[0] == '-':
92
            timezone_offset = -timezone_offset
93
94
        base_start_datetime_utc = None
95
        if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0:
96
            base_period_start_datetime_local = str.strip(base_period_start_datetime_local)
97
            try:
98
                base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S')
99
            except ValueError:
100
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
101
                                       description="API.INVALID_BASE_PERIOD_START_DATETIME")
102
            base_start_datetime_utc = \
103
                base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
104
            # nomalize the start datetime
105
            if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30:
106
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
107
            else:
108
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
109
110
        base_end_datetime_utc = None
111
        if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0:
112
            base_period_end_datetime_local = str.strip(base_period_end_datetime_local)
113
            try:
114
                base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S')
115
            except ValueError:
116
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
117
                                       description="API.INVALID_BASE_PERIOD_END_DATETIME")
118
            base_end_datetime_utc = \
119
                base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
120
121
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
122
                base_start_datetime_utc >= base_end_datetime_utc:
123
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
124
                                   description='API.INVALID_BASE_PERIOD_END_DATETIME')
125
126
        if reporting_period_start_datetime_local is None:
127
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
128
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
129
        else:
130
            reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local)
131
            try:
132
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local,
133
                                                                 '%Y-%m-%dT%H:%M:%S')
134
            except ValueError:
135
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
136
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
137
            reporting_start_datetime_utc = \
138
                reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
139
            # nomalize the start datetime
140
            if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30:
141
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
142
            else:
143
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
144
145
        if reporting_period_end_datetime_local is None:
146
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
147
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
148
        else:
149
            reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local)
150
            try:
151
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local,
152
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
153
                                             timedelta(minutes=timezone_offset)
154
            except ValueError:
155
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
156
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
157
158
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
159
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
160
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
161
162
        # if turn quick mode on, do not return parameters data and excel file
163
        is_quick_mode = False
164
        if quick_mode is not None and \
165
                len(str.strip(quick_mode)) > 0 and \
166
                str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'):
167
            is_quick_mode = True
168
169
        trans = utilities.get_translation(language)
170
        trans.install()
171
        _ = trans.gettext
172
173
        ################################################################################################################
174
        # Step 2: query the combined equipment
175
        ################################################################################################################
176
        cnx_system = mysql.connector.connect(**config.myems_system_db)
177
        cursor_system = cnx_system.cursor()
178
179
        cnx_carbon = mysql.connector.connect(**config.myems_carbon_db)
180
        cursor_carbon = cnx_carbon.cursor()
181
182
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
183
        cursor_historical = cnx_historical.cursor()
184
185
        if combined_equipment_id is not None:
186
            cursor_system.execute(" SELECT id, name, cost_center_id "
187
                                  " FROM tbl_combined_equipments "
188
                                  " WHERE id = %s ", (combined_equipment_id,))
189
            row_combined_equipment = cursor_system.fetchone()
190
        elif combined_equipment_uuid is not None:
191
            cursor_system.execute(" SELECT id, name, cost_center_id "
192
                                  " FROM tbl_combined_equipments "
193
                                  " WHERE uuid = %s ", (combined_equipment_uuid,))
194
            row_combined_equipment = cursor_system.fetchone()
195
196
        if row_combined_equipment is None:
197
            if cursor_system:
198
                cursor_system.close()
199
            if cnx_system:
200
                cnx_system.close()
201
202
            if cursor_carbon:
203
                cursor_carbon.close()
204
            if cnx_carbon:
205
                cnx_carbon.close()
206
207
            if cursor_historical:
208
                cursor_historical.close()
209
            if cnx_historical:
210
                cnx_historical.close()
211
            raise falcon.HTTPError(status=falcon.HTTP_404,
212
                                   title='API.NOT_FOUND',
213
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
214
215
        combined_equipment = dict()
216
        combined_equipment['id'] = row_combined_equipment[0]
217
        combined_equipment['name'] = row_combined_equipment[1]
218
        combined_equipment['cost_center_id'] = row_combined_equipment[2]
219
220
        ################################################################################################################
221
        # Step 3: query energy categories
222
        ################################################################################################################
223
        energy_category_set = set()
224
        # query energy categories in base period
225
        cursor_carbon.execute(" SELECT DISTINCT(energy_category_id) "
226
                              " FROM tbl_combined_equipment_input_category_hourly "
227
                              " WHERE combined_equipment_id = %s "
228
                              "     AND start_datetime_utc >= %s "
229
                              "     AND start_datetime_utc < %s ",
230
                              (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc))
231
        rows_energy_categories = cursor_carbon.fetchall()
232
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
233
            for row_energy_category in rows_energy_categories:
234
                energy_category_set.add(row_energy_category[0])
235
236
        # query energy categories in reporting period
237
        cursor_carbon.execute(" SELECT DISTINCT(energy_category_id) "
238
                              " FROM tbl_combined_equipment_input_category_hourly "
239
                              " WHERE combined_equipment_id = %s "
240
                              "     AND start_datetime_utc >= %s "
241
                              "     AND start_datetime_utc < %s ",
242
                              (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
243
        rows_energy_categories = cursor_carbon.fetchall()
244
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
245
            for row_energy_category in rows_energy_categories:
246
                energy_category_set.add(row_energy_category[0])
247
248
        # query all energy categories in base period and reporting period
249
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
250
                              " FROM tbl_energy_categories "
251
                              " ORDER BY id ", )
252
        rows_energy_categories = cursor_system.fetchall()
253
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
254
            if cursor_system:
255
                cursor_system.close()
256
            if cnx_system:
257
                cnx_system.close()
258
259
            if cursor_carbon:
260
                cursor_carbon.close()
261
            if cnx_carbon:
262
                cnx_carbon.close()
263
264
            if cursor_historical:
265
                cursor_historical.close()
266
            if cnx_historical:
267
                cnx_historical.close()
268
            raise falcon.HTTPError(status=falcon.HTTP_404,
269
                                   title='API.NOT_FOUND',
270
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
271
        energy_category_dict = dict()
272
        for row_energy_category in rows_energy_categories:
273
            if row_energy_category[0] in energy_category_set:
274
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
275
                                                                "unit_of_measure": row_energy_category[2],
276
                                                                "kgce": row_energy_category[3],
277
                                                                "kgco2e": row_energy_category[4]}
278
279
        ################################################################################################################
280
        # Step 4: query associated points
281
        ################################################################################################################
282
        point_list = list()
283
        cursor_system.execute(" SELECT p.id, ep.name, p.units, p.object_type  "
284
                              " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p "
285
                              " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' "
286
                              "       AND ep.point_id = p.id "
287
                              " ORDER BY p.id ", (combined_equipment['id'],))
288
        rows_points = cursor_system.fetchall()
289
        if rows_points is not None and len(rows_points) > 0:
290
            for row in rows_points:
291
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
292
293
        ################################################################################################################
294
        # Step 5: query associated equipments
295
        ################################################################################################################
296
        associated_equipment_list = list()
297
        cursor_system.execute(" SELECT e.id, e.name "
298
                              " FROM tbl_equipments e,tbl_combined_equipments_equipments ee"
299
                              " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id"
300
                              " ORDER BY id ", (combined_equipment['id'],))
301
        rows_associated_equipments = cursor_system.fetchall()
302
        if rows_associated_equipments is not None and len(rows_associated_equipments) > 0:
303
            for row in rows_associated_equipments:
304
                associated_equipment_list.append({"id": row[0], "name": row[1]})
305
306
        ################################################################################################################
307
        # Step 6: query base period energy carbon dioxide emissions
308
        ################################################################################################################
309
        base = dict()
310
        if energy_category_set is not None and len(energy_category_set) > 0:
311
            for energy_category_id in energy_category_set:
312
                base[energy_category_id] = dict()
313
                base[energy_category_id]['timestamps'] = list()
314
                base[energy_category_id]['values'] = list()
315
                base[energy_category_id]['subtotal'] = Decimal(0.0)
316
317
                cursor_carbon.execute(" SELECT start_datetime_utc, actual_value "
318
                                      " FROM tbl_combined_equipment_input_category_hourly "
319
                                      " WHERE combined_equipment_id = %s "
320
                                      "     AND energy_category_id = %s "
321
                                      "     AND start_datetime_utc >= %s "
322
                                      "     AND start_datetime_utc < %s "
323
                                      " ORDER BY start_datetime_utc ",
324
                                      (combined_equipment['id'],
325
                                       energy_category_id,
326
                                       base_start_datetime_utc,
327
                                       base_end_datetime_utc))
328
                rows_combined_equipment_hourly = cursor_carbon.fetchall()
329
330
                rows_combined_equipment_periodically = \
331
                    utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly,
332
                                                              base_start_datetime_utc,
333
                                                              base_end_datetime_utc,
334
                                                              period_type)
335
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
336
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
337
                                             timedelta(minutes=timezone_offset)
338
                    if period_type == 'hourly':
339
                        current_datetime = current_datetime_local.isoformat()[0:19]
340
                    elif period_type == 'daily':
341
                        current_datetime = current_datetime_local.isoformat()[0:10]
342
                    elif period_type == 'weekly':
343
                        current_datetime = current_datetime_local.isoformat()[0:10]
344
                    elif period_type == 'monthly':
345
                        current_datetime = current_datetime_local.isoformat()[0:7]
346
                    elif period_type == 'yearly':
347
                        current_datetime = current_datetime_local.isoformat()[0:4]
348
349
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
350
                        else row_combined_equipment_periodically[1]
351
                    base[energy_category_id]['timestamps'].append(current_datetime)
352
                    base[energy_category_id]['values'].append(actual_value)
353
                    base[energy_category_id]['subtotal'] += actual_value
354
355
        ################################################################################################################
356
        # Step 7: query reporting period energy carbon dioxide emissions
357
        ################################################################################################################
358
        reporting = dict()
359
        if energy_category_set is not None and len(energy_category_set) > 0:
360
            for energy_category_id in energy_category_set:
361
                reporting[energy_category_id] = dict()
362
                reporting[energy_category_id]['timestamps'] = list()
363
                reporting[energy_category_id]['values'] = list()
364
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
365
                reporting[energy_category_id]['toppeak'] = Decimal(0.0)
366
                reporting[energy_category_id]['onpeak'] = Decimal(0.0)
367
                reporting[energy_category_id]['midpeak'] = Decimal(0.0)
368
                reporting[energy_category_id]['offpeak'] = Decimal(0.0)
369
                reporting[energy_category_id]['deep'] = Decimal(0.0)
370
371
                cursor_carbon.execute(" SELECT start_datetime_utc, actual_value "
372
                                      " FROM tbl_combined_equipment_input_category_hourly "
373
                                      " WHERE combined_equipment_id = %s "
374
                                      "     AND energy_category_id = %s "
375
                                      "     AND start_datetime_utc >= %s "
376
                                      "     AND start_datetime_utc < %s "
377
                                      " ORDER BY start_datetime_utc ",
378
                                      (combined_equipment['id'],
379
                                       energy_category_id,
380
                                       reporting_start_datetime_utc,
381
                                       reporting_end_datetime_utc))
382
                rows_combined_equipment_hourly = cursor_carbon.fetchall()
383
384
                rows_combined_equipment_periodically = \
385
                    utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly,
386
                                                              reporting_start_datetime_utc,
387
                                                              reporting_end_datetime_utc,
388
                                                              period_type)
389
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
390
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
391
                                             timedelta(minutes=timezone_offset)
392
                    if period_type == 'hourly':
393
                        current_datetime = current_datetime_local.isoformat()[0:19]
394
                    elif period_type == 'daily':
395
                        current_datetime = current_datetime_local.isoformat()[0:10]
396
                    elif period_type == 'weekly':
397
                        current_datetime = current_datetime_local.isoformat()[0:10]
398
                    elif period_type == 'monthly':
399
                        current_datetime = current_datetime_local.isoformat()[0:7]
400
                    elif period_type == 'yearly':
401
                        current_datetime = current_datetime_local.isoformat()[0:4]
402
403
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
404
                        else row_combined_equipment_periodically[1]
405
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
406
                    reporting[energy_category_id]['values'].append(actual_value)
407
                    reporting[energy_category_id]['subtotal'] += actual_value
408
409
                energy_category_tariff_dict = \
410
                    utilities.get_energy_category_peak_types(combined_equipment['cost_center_id'],
411
                                                             energy_category_id,
412
                                                             reporting_start_datetime_utc,
413
                                                             reporting_end_datetime_utc)
414
                for row in rows_combined_equipment_hourly:
415
                    peak_type = energy_category_tariff_dict.get(row[0], None)
416
                    if peak_type == 'toppeak':
417
                        reporting[energy_category_id]['toppeak'] += row[1]
418
                    elif peak_type == 'onpeak':
419
                        reporting[energy_category_id]['onpeak'] += row[1]
420
                    elif peak_type == 'midpeak':
421
                        reporting[energy_category_id]['midpeak'] += row[1]
422
                    elif peak_type == 'offpeak':
423
                        reporting[energy_category_id]['offpeak'] += row[1]
424
                    elif peak_type == 'deep':
425
                        reporting[energy_category_id]['deep'] += row[1]
426
427
        ################################################################################################################
428
        # Step 8: query tariff data
429
        ################################################################################################################
430
        parameters_data = dict()
431
        parameters_data['names'] = list()
432
        parameters_data['timestamps'] = list()
433
        parameters_data['values'] = list()
434
        if not is_quick_mode:
435
            if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0:
436
                for energy_category_id in energy_category_set:
437
                    energy_category_tariff_dict = \
438
                        utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'],
439
                                                              energy_category_id,
440
                                                              reporting_start_datetime_utc,
441
                                                              reporting_end_datetime_utc)
442
                    tariff_timestamp_list = list()
443
                    tariff_value_list = list()
444
                    for k, v in energy_category_tariff_dict.items():
445
                        # convert k from utc to local
446
                        k = k + timedelta(minutes=timezone_offset)
447
                        tariff_timestamp_list.append(k.isoformat()[0:19])
448
                        tariff_value_list.append(v)
449
450
                    parameters_data['names'].append(
451
                        _('Tariff') + '-' + energy_category_dict[energy_category_id]['name'])
452
                    parameters_data['timestamps'].append(tariff_timestamp_list)
453
                    parameters_data['values'].append(tariff_value_list)
454
455
        ################################################################################################################
456
        # Step 9: query associated points data
457
        ################################################################################################################
458
        if not is_quick_mode:
459
            for point in point_list:
460
                point_values = []
461
                point_timestamps = []
462
                if point['object_type'] == 'ENERGY_VALUE':
463
                    query = (" SELECT utc_date_time, actual_value "
464
                             " FROM tbl_energy_value "
465
                             " WHERE point_id = %s "
466
                             "       AND utc_date_time BETWEEN %s AND %s "
467
                             " ORDER BY utc_date_time ")
468
                    cursor_historical.execute(query, (point['id'],
469
                                                      reporting_start_datetime_utc,
470
                                                      reporting_end_datetime_utc))
471
                    rows = cursor_historical.fetchall()
472
473
                    if rows is not None and len(rows) > 0:
474
                        for row in rows:
475
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
476
                                                     timedelta(minutes=timezone_offset)
477
                            current_datetime = current_datetime_local.isoformat()[0:19]
478
                            point_timestamps.append(current_datetime)
479
                            point_values.append(row[1])
480
                elif point['object_type'] == 'ANALOG_VALUE':
481
                    query = (" SELECT utc_date_time, actual_value "
482
                             " FROM tbl_analog_value "
483
                             " WHERE point_id = %s "
484
                             "       AND utc_date_time BETWEEN %s AND %s "
485
                             " ORDER BY utc_date_time ")
486
                    cursor_historical.execute(query, (point['id'],
487
                                                      reporting_start_datetime_utc,
488
                                                      reporting_end_datetime_utc))
489
                    rows = cursor_historical.fetchall()
490
491
                    if rows is not None and len(rows) > 0:
492
                        for row in rows:
493
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
494
                                                     timedelta(minutes=timezone_offset)
495
                            current_datetime = current_datetime_local.isoformat()[0:19]
496
                            point_timestamps.append(current_datetime)
497
                            point_values.append(row[1])
498
                elif point['object_type'] == 'DIGITAL_VALUE':
499
                    query = (" SELECT utc_date_time, actual_value "
500
                             " FROM tbl_digital_value "
501
                             " WHERE point_id = %s "
502
                             "       AND utc_date_time BETWEEN %s AND %s "
503
                             " ORDER BY utc_date_time ")
504
                    cursor_historical.execute(query, (point['id'],
505
                                                      reporting_start_datetime_utc,
506
                                                      reporting_end_datetime_utc))
507
                    rows = cursor_historical.fetchall()
508
509
                    if rows is not None and len(rows) > 0:
510
                        for row in rows:
511
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
512
                                                     timedelta(minutes=timezone_offset)
513
                            current_datetime = current_datetime_local.isoformat()[0:19]
514
                            point_timestamps.append(current_datetime)
515
                            point_values.append(row[1])
516
517
                parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
518
                parameters_data['timestamps'].append(point_timestamps)
519
                parameters_data['values'].append(point_values)
520
521
        ################################################################################################################
522
        # Step 10: query associated equipments energy carbon dioxide emissions
523
        ################################################################################################################
524
        associated_equipment_data = dict()
525
526
        if energy_category_set is not None and len(energy_category_set) > 0:
527
            for energy_category_id in energy_category_set:
528
                associated_equipment_data[energy_category_id] = dict()
529
                associated_equipment_data[energy_category_id]['associated_equipment_names'] = list()
530
                associated_equipment_data[energy_category_id]['subtotals'] = list()
531
                for associated_equipment in associated_equipment_list:
532
                    associated_equipment_data[energy_category_id]['associated_equipment_names'].append(
533
                        associated_equipment['name'])
534
535
                    cursor_carbon.execute(" SELECT SUM(actual_value) "
536
                                          " FROM tbl_equipment_input_category_hourly "
537
                                          " WHERE equipment_id = %s "
538
                                          "     AND energy_category_id = %s "
539
                                          "     AND start_datetime_utc >= %s "
540
                                          "     AND start_datetime_utc < %s ",
541
                                          (associated_equipment['id'],
542
                                           energy_category_id,
543
                                           reporting_start_datetime_utc,
544
                                           reporting_end_datetime_utc))
545
                    row_subtotal = cursor_carbon.fetchone()
546
547
                    subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0]
548
                    associated_equipment_data[energy_category_id]['subtotals'].append(subtotal)
549
550
        ################################################################################################################
551
        # Step 11: construct the report
552
        ################################################################################################################
553
        if cursor_system:
554
            cursor_system.close()
555
        if cnx_system:
556
            cnx_system.close()
557
558
        if cursor_carbon:
559
            cursor_carbon.close()
560
        if cnx_carbon:
561
            cnx_carbon.close()
562
563
        if cursor_historical:
564
            cursor_historical.close()
565
        if cnx_historical:
566
            cnx_historical.close()
567
568
        result = dict()
569
570
        result['combined_equipment'] = dict()
571
        result['combined_equipment']['name'] = combined_equipment['name']
572
573
        result['base_period'] = dict()
574
        result['base_period']['names'] = list()
575
        result['base_period']['units'] = list()
576
        result['base_period']['timestamps'] = list()
577
        result['base_period']['values'] = list()
578
        result['base_period']['subtotals'] = list()
579
        result['base_period']['total'] = Decimal(0.0)
580
        if energy_category_set is not None and len(energy_category_set) > 0:
581
            for energy_category_id in energy_category_set:
582
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
583
                result['base_period']['units'].append('KG')
584
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
585
                result['base_period']['values'].append(base[energy_category_id]['values'])
586
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
587
                result['base_period']['total'] += base[energy_category_id]['subtotal']
588
589
        result['reporting_period'] = dict()
590
        result['reporting_period']['names'] = list()
591
        result['reporting_period']['energy_category_ids'] = list()
592
        result['reporting_period']['units'] = list()
593
        result['reporting_period']['timestamps'] = list()
594
        result['reporting_period']['values'] = list()
595
        result['reporting_period']['rates'] = list()
596
        result['reporting_period']['subtotals'] = list()
597
        result['reporting_period']['toppeaks'] = list()
598
        result['reporting_period']['onpeaks'] = list()
599
        result['reporting_period']['midpeaks'] = list()
600
        result['reporting_period']['offpeaks'] = list()
601
        result['reporting_period']['deeps'] = list()
602
        result['reporting_period']['increment_rates'] = list()
603
        result['reporting_period']['total'] = Decimal(0.0)
604
        result['reporting_period']['total_increment_rate'] = Decimal(0.0)
605
        result['reporting_period']['total_unit'] = 'KG'
606
607
        if energy_category_set is not None and len(energy_category_set) > 0:
608
            for energy_category_id in energy_category_set:
609
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
610
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
611
                result['reporting_period']['units'].append('KG')
612
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
613
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
614
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
615
                result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak'])
616
                result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak'])
617
                result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak'])
618
                result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak'])
619
                result['reporting_period']['deeps'].append(reporting[energy_category_id]['deep'])
620
                result['reporting_period']['increment_rates'].append(
621
                    (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) /
622
                    base[energy_category_id]['subtotal']
623
                    if base[energy_category_id]['subtotal'] > 0.0 else None)
624
                result['reporting_period']['total'] += reporting[energy_category_id]['subtotal']
625
626
                rate = list()
627
                for index, value in enumerate(reporting[energy_category_id]['values']):
628
                    if index < len(base[energy_category_id]['values']) \
629
                            and base[energy_category_id]['values'][index] != 0 and value != 0:
630
                        rate.append((value - base[energy_category_id]['values'][index])
631
                                    / base[energy_category_id]['values'][index])
632
                    else:
633
                        rate.append(None)
634
                result['reporting_period']['rates'].append(rate)
635
636
        result['reporting_period']['total_increment_rate'] = \
637
            (result['reporting_period']['total'] - result['base_period']['total']) / result['base_period']['total'] \
638
            if result['base_period']['total'] > Decimal(0.0) else None
639
640
        result['parameters'] = {
641
            "names": parameters_data['names'],
642
            "timestamps": parameters_data['timestamps'],
643
            "values": parameters_data['values']
644
        }
645
646
        result['associated_equipment'] = dict()
647
        result['associated_equipment']['energy_category_names'] = list()
648
        result['associated_equipment']['units'] = list()
649
        result['associated_equipment']['associated_equipment_names_array'] = list()
650
        result['associated_equipment']['subtotals_array'] = list()
651
        result['associated_equipment']['total_unit'] = 'KG'
652
        if energy_category_set is not None and len(energy_category_set) > 0:
653
            for energy_category_id in energy_category_set:
654
                result['associated_equipment']['energy_category_names'].append(
655
                    energy_category_dict[energy_category_id]['name'])
656
                result['associated_equipment']['units'].append('KG')
657
                result['associated_equipment']['associated_equipment_names_array'].append(
658
                    associated_equipment_data[energy_category_id]['associated_equipment_names'])
659
                result['associated_equipment']['subtotals_array'].append(
660
                    associated_equipment_data[energy_category_id]['subtotals'])
661
662
        # export result to Excel file and then encode the file to base64 string
663
        result['excel_bytes_base64'] = None
664
        if not is_quick_mode:
665
            result['excel_bytes_base64'] = \
666
                excelexporters.combinedequipmentcarbon.export(result,
667
                                                              combined_equipment['name'],
668
                                                              base_period_start_datetime_local,
669
                                                              base_period_end_datetime_local,
670
                                                              reporting_period_start_datetime_local,
671
                                                              reporting_period_end_datetime_local,
672
                                                              period_type,
673
                                                              language)
674
        resp.text = json.dumps(result)
675

myems-api/reports/combinedequipmentcost.py 1 location

@@ 13-672 (lines=660) @@
10
from core.useractivity import access_control, api_key_control
11
12
13
class Reporting:
14
    def __init__(self):
15
        """"Initializes Reporting"""
16
        pass
17
18
    @staticmethod
19
    def on_options(req, resp):
20
        _ = req
21
        resp.status = falcon.HTTP_200
22
23
    ####################################################################################################################
24
    # PROCEDURES
25
    # Step 1: valid parameters
26
    # Step 2: query the combined equipment
27
    # Step 3: query energy items
28
    # Step 4: query associated points
29
    # Step 5: query associated equipments
30
    # Step 6: query base period energy cost
31
    # Step 7: query reporting period energy cost
32
    # Step 8: query tariff data
33
    # Step 9: query associated points data
34
    # Step 10: query associated equipments energy cost
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_billing = mysql.connector.connect(**config.myems_billing_db)
178
        cursor_billing = cnx_billing.cursor()
179
180
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
181
        cursor_historical = cnx_historical.cursor()
182
183
        if combined_equipment_id is not None:
184
            cursor_system.execute(" SELECT id, name, cost_center_id "
185
                                  " FROM tbl_combined_equipments "
186
                                  " WHERE id = %s ", (combined_equipment_id,))
187
            row_combined_equipment = cursor_system.fetchone()
188
        elif combined_equipment_uuid is not None:
189
            cursor_system.execute(" SELECT id, name, cost_center_id "
190
                                  " FROM tbl_combined_equipments "
191
                                  " WHERE uuid = %s ", (combined_equipment_uuid,))
192
            row_combined_equipment = cursor_system.fetchone()
193
194
        if row_combined_equipment is None:
195
            if cursor_system:
196
                cursor_system.close()
197
            if cnx_system:
198
                cnx_system.close()
199
200
            if cursor_billing:
201
                cursor_billing.close()
202
            if cnx_billing:
203
                cnx_billing.close()
204
205
            if cursor_historical:
206
                cursor_historical.close()
207
            if cnx_historical:
208
                cnx_historical.close()
209
            raise falcon.HTTPError(status=falcon.HTTP_404,
210
                                   title='API.NOT_FOUND',
211
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
212
213
        combined_equipment = dict()
214
        combined_equipment['id'] = row_combined_equipment[0]
215
        combined_equipment['name'] = row_combined_equipment[1]
216
        combined_equipment['cost_center_id'] = row_combined_equipment[2]
217
218
        ################################################################################################################
219
        # Step 3: query energy categories
220
        ################################################################################################################
221
        energy_category_set = set()
222
        # query energy categories in base period
223
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
224
                               " FROM tbl_combined_equipment_input_category_hourly "
225
                               " WHERE combined_equipment_id = %s "
226
                               "     AND start_datetime_utc >= %s "
227
                               "     AND start_datetime_utc < %s ",
228
                               (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc))
229
        rows_energy_categories = cursor_billing.fetchall()
230
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
231
            for row_energy_category in rows_energy_categories:
232
                energy_category_set.add(row_energy_category[0])
233
234
        # query energy categories in reporting period
235
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
236
                               " FROM tbl_combined_equipment_input_category_hourly "
237
                               " WHERE combined_equipment_id = %s "
238
                               "     AND start_datetime_utc >= %s "
239
                               "     AND start_datetime_utc < %s ",
240
                               (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
241
        rows_energy_categories = cursor_billing.fetchall()
242
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
243
            for row_energy_category in rows_energy_categories:
244
                energy_category_set.add(row_energy_category[0])
245
246
        # query all energy categories in base period and reporting period
247
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
248
                              " FROM tbl_energy_categories "
249
                              " ORDER BY id ", )
250
        rows_energy_categories = cursor_system.fetchall()
251
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
252
            if cursor_system:
253
                cursor_system.close()
254
            if cnx_system:
255
                cnx_system.close()
256
257
            if cursor_billing:
258
                cursor_billing.close()
259
            if cnx_billing:
260
                cnx_billing.close()
261
262
            if cursor_historical:
263
                cursor_historical.close()
264
            if cnx_historical:
265
                cnx_historical.close()
266
            raise falcon.HTTPError(status=falcon.HTTP_404,
267
                                   title='API.NOT_FOUND',
268
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
269
        energy_category_dict = dict()
270
        for row_energy_category in rows_energy_categories:
271
            if row_energy_category[0] in energy_category_set:
272
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
273
                                                                "unit_of_measure": row_energy_category[2],
274
                                                                "kgce": row_energy_category[3],
275
                                                                "kgco2e": row_energy_category[4]}
276
277
        ################################################################################################################
278
        # Step 4: query associated points
279
        ################################################################################################################
280
        point_list = list()
281
        cursor_system.execute(" SELECT p.id, ep.name, p.units, p.object_type  "
282
                              " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p "
283
                              " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' "
284
                              "       AND ep.point_id = p.id "
285
                              " ORDER BY p.id ", (combined_equipment['id'],))
286
        rows_points = cursor_system.fetchall()
287
        if rows_points is not None and len(rows_points) > 0:
288
            for row in rows_points:
289
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
290
291
        ################################################################################################################
292
        # Step 5: query associated equipments
293
        ################################################################################################################
294
        associated_equipment_list = list()
295
        cursor_system.execute(" SELECT e.id, e.name "
296
                              " FROM tbl_equipments e,tbl_combined_equipments_equipments ee"
297
                              " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id"
298
                              " ORDER BY id ", (combined_equipment['id'],))
299
        rows_associated_equipments = cursor_system.fetchall()
300
        if rows_associated_equipments is not None and len(rows_associated_equipments) > 0:
301
            for row in rows_associated_equipments:
302
                associated_equipment_list.append({"id": row[0], "name": row[1]})
303
304
        ################################################################################################################
305
        # Step 6: query base period energy cost
306
        ################################################################################################################
307
        base = dict()
308
        if energy_category_set is not None and len(energy_category_set) > 0:
309
            for energy_category_id in energy_category_set:
310
                base[energy_category_id] = dict()
311
                base[energy_category_id]['timestamps'] = list()
312
                base[energy_category_id]['values'] = list()
313
                base[energy_category_id]['subtotal'] = Decimal(0.0)
314
315
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
316
                                       " FROM tbl_combined_equipment_input_category_hourly "
317
                                       " WHERE combined_equipment_id = %s "
318
                                       "     AND energy_category_id = %s "
319
                                       "     AND start_datetime_utc >= %s "
320
                                       "     AND start_datetime_utc < %s "
321
                                       " ORDER BY start_datetime_utc ",
322
                                       (combined_equipment['id'],
323
                                        energy_category_id,
324
                                        base_start_datetime_utc,
325
                                        base_end_datetime_utc))
326
                rows_combined_equipment_hourly = cursor_billing.fetchall()
327
328
                rows_combined_equipment_periodically = \
329
                    utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly,
330
                                                              base_start_datetime_utc,
331
                                                              base_end_datetime_utc,
332
                                                              period_type)
333
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
334
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
335
                                             timedelta(minutes=timezone_offset)
336
                    if period_type == 'hourly':
337
                        current_datetime = current_datetime_local.isoformat()[0:19]
338
                    elif period_type == 'daily':
339
                        current_datetime = current_datetime_local.isoformat()[0:10]
340
                    elif period_type == 'weekly':
341
                        current_datetime = current_datetime_local.isoformat()[0:10]
342
                    elif period_type == 'monthly':
343
                        current_datetime = current_datetime_local.isoformat()[0:7]
344
                    elif period_type == 'yearly':
345
                        current_datetime = current_datetime_local.isoformat()[0:4]
346
347
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
348
                        else row_combined_equipment_periodically[1]
349
                    base[energy_category_id]['timestamps'].append(current_datetime)
350
                    base[energy_category_id]['values'].append(actual_value)
351
                    base[energy_category_id]['subtotal'] += actual_value
352
353
        ################################################################################################################
354
        # Step 7: query reporting period energy cost
355
        ################################################################################################################
356
        reporting = dict()
357
        if energy_category_set is not None and len(energy_category_set) > 0:
358
            for energy_category_id in energy_category_set:
359
                reporting[energy_category_id] = dict()
360
                reporting[energy_category_id]['timestamps'] = list()
361
                reporting[energy_category_id]['values'] = list()
362
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
363
                reporting[energy_category_id]['toppeak'] = Decimal(0.0)
364
                reporting[energy_category_id]['onpeak'] = Decimal(0.0)
365
                reporting[energy_category_id]['midpeak'] = Decimal(0.0)
366
                reporting[energy_category_id]['offpeak'] = Decimal(0.0)
367
                reporting[energy_category_id]['deep'] = Decimal(0.0)
368
369
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
370
                                       " FROM tbl_combined_equipment_input_category_hourly "
371
                                       " WHERE combined_equipment_id = %s "
372
                                       "     AND energy_category_id = %s "
373
                                       "     AND start_datetime_utc >= %s "
374
                                       "     AND start_datetime_utc < %s "
375
                                       " ORDER BY start_datetime_utc ",
376
                                       (combined_equipment['id'],
377
                                        energy_category_id,
378
                                        reporting_start_datetime_utc,
379
                                        reporting_end_datetime_utc))
380
                rows_combined_equipment_hourly = cursor_billing.fetchall()
381
382
                rows_combined_equipment_periodically = \
383
                    utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly,
384
                                                              reporting_start_datetime_utc,
385
                                                              reporting_end_datetime_utc,
386
                                                              period_type)
387
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
388
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
389
                                             timedelta(minutes=timezone_offset)
390
                    if period_type == 'hourly':
391
                        current_datetime = current_datetime_local.isoformat()[0:19]
392
                    elif period_type == 'daily':
393
                        current_datetime = current_datetime_local.isoformat()[0:10]
394
                    elif period_type == 'weekly':
395
                        current_datetime = current_datetime_local.isoformat()[0:10]
396
                    elif period_type == 'monthly':
397
                        current_datetime = current_datetime_local.isoformat()[0:7]
398
                    elif period_type == 'yearly':
399
                        current_datetime = current_datetime_local.isoformat()[0:4]
400
401
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
402
                        else row_combined_equipment_periodically[1]
403
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
404
                    reporting[energy_category_id]['values'].append(actual_value)
405
                    reporting[energy_category_id]['subtotal'] += actual_value
406
407
                energy_category_tariff_dict = \
408
                    utilities.get_energy_category_peak_types(combined_equipment['cost_center_id'],
409
                                                             energy_category_id,
410
                                                             reporting_start_datetime_utc,
411
                                                             reporting_end_datetime_utc)
412
                for row in rows_combined_equipment_hourly:
413
                    peak_type = energy_category_tariff_dict.get(row[0], None)
414
                    if peak_type == 'toppeak':
415
                        reporting[energy_category_id]['toppeak'] += row[1]
416
                    elif peak_type == 'onpeak':
417
                        reporting[energy_category_id]['onpeak'] += row[1]
418
                    elif peak_type == 'midpeak':
419
                        reporting[energy_category_id]['midpeak'] += row[1]
420
                    elif peak_type == 'offpeak':
421
                        reporting[energy_category_id]['offpeak'] += row[1]
422
                    elif peak_type == 'deep':
423
                        reporting[energy_category_id]['deep'] += row[1]
424
425
        ################################################################################################################
426
        # Step 8: query tariff data
427
        ################################################################################################################
428
        parameters_data = dict()
429
        parameters_data['names'] = list()
430
        parameters_data['timestamps'] = list()
431
        parameters_data['values'] = list()
432
        if not is_quick_mode:
433
            if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0:
434
                for energy_category_id in energy_category_set:
435
                    energy_category_tariff_dict = \
436
                        utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'],
437
                                                              energy_category_id,
438
                                                              reporting_start_datetime_utc,
439
                                                              reporting_end_datetime_utc)
440
                    tariff_timestamp_list = list()
441
                    tariff_value_list = list()
442
                    for k, v in energy_category_tariff_dict.items():
443
                        # convert k from utc to local
444
                        k = k + timedelta(minutes=timezone_offset)
445
                        tariff_timestamp_list.append(k.isoformat()[0:19])
446
                        tariff_value_list.append(v)
447
448
                    parameters_data['names'].append(
449
                        _('Tariff') + '-' + energy_category_dict[energy_category_id]['name'])
450
                    parameters_data['timestamps'].append(tariff_timestamp_list)
451
                    parameters_data['values'].append(tariff_value_list)
452
453
        ################################################################################################################
454
        # Step 9: query associated points data
455
        ################################################################################################################
456
        if not is_quick_mode:
457
            for point in point_list:
458
                point_values = []
459
                point_timestamps = []
460
                if point['object_type'] == 'ENERGY_VALUE':
461
                    query = (" SELECT utc_date_time, actual_value "
462
                             " FROM tbl_energy_value "
463
                             " WHERE point_id = %s "
464
                             "       AND utc_date_time BETWEEN %s AND %s "
465
                             " ORDER BY utc_date_time ")
466
                    cursor_historical.execute(query, (point['id'],
467
                                                      reporting_start_datetime_utc,
468
                                                      reporting_end_datetime_utc))
469
                    rows = cursor_historical.fetchall()
470
471
                    if rows is not None and len(rows) > 0:
472
                        for row in rows:
473
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
474
                                                     timedelta(minutes=timezone_offset)
475
                            current_datetime = current_datetime_local.isoformat()[0:19]
476
                            point_timestamps.append(current_datetime)
477
                            point_values.append(row[1])
478
                elif point['object_type'] == 'ANALOG_VALUE':
479
                    query = (" SELECT utc_date_time, actual_value "
480
                             " FROM tbl_analog_value "
481
                             " WHERE point_id = %s "
482
                             "       AND utc_date_time BETWEEN %s AND %s "
483
                             " ORDER BY utc_date_time ")
484
                    cursor_historical.execute(query, (point['id'],
485
                                                      reporting_start_datetime_utc,
486
                                                      reporting_end_datetime_utc))
487
                    rows = cursor_historical.fetchall()
488
489
                    if rows is not None and len(rows) > 0:
490
                        for row in rows:
491
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
492
                                                     timedelta(minutes=timezone_offset)
493
                            current_datetime = current_datetime_local.isoformat()[0:19]
494
                            point_timestamps.append(current_datetime)
495
                            point_values.append(row[1])
496
                elif point['object_type'] == 'DIGITAL_VALUE':
497
                    query = (" SELECT utc_date_time, actual_value "
498
                             " FROM tbl_digital_value "
499
                             " WHERE point_id = %s "
500
                             "       AND utc_date_time BETWEEN %s AND %s "
501
                             " ORDER BY utc_date_time ")
502
                    cursor_historical.execute(query, (point['id'],
503
                                                      reporting_start_datetime_utc,
504
                                                      reporting_end_datetime_utc))
505
                    rows = cursor_historical.fetchall()
506
507
                    if rows is not None and len(rows) > 0:
508
                        for row in rows:
509
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
510
                                                     timedelta(minutes=timezone_offset)
511
                            current_datetime = current_datetime_local.isoformat()[0:19]
512
                            point_timestamps.append(current_datetime)
513
                            point_values.append(row[1])
514
515
                parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
516
                parameters_data['timestamps'].append(point_timestamps)
517
                parameters_data['values'].append(point_values)
518
519
        ################################################################################################################
520
        # Step 10: query associated equipments energy cost
521
        ################################################################################################################
522
        associated_equipment_data = dict()
523
524
        if energy_category_set is not None and len(energy_category_set) > 0:
525
            for energy_category_id in energy_category_set:
526
                associated_equipment_data[energy_category_id] = dict()
527
                associated_equipment_data[energy_category_id]['associated_equipment_names'] = list()
528
                associated_equipment_data[energy_category_id]['subtotals'] = list()
529
                for associated_equipment in associated_equipment_list:
530
                    associated_equipment_data[energy_category_id]['associated_equipment_names'].append(
531
                        associated_equipment['name'])
532
533
                    cursor_billing.execute(" SELECT SUM(actual_value) "
534
                                           " FROM tbl_equipment_input_category_hourly "
535
                                           " WHERE equipment_id = %s "
536
                                           "     AND energy_category_id = %s "
537
                                           "     AND start_datetime_utc >= %s "
538
                                           "     AND start_datetime_utc < %s ",
539
                                           (associated_equipment['id'],
540
                                            energy_category_id,
541
                                            reporting_start_datetime_utc,
542
                                            reporting_end_datetime_utc))
543
                    row_subtotal = cursor_billing.fetchone()
544
545
                    subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0]
546
                    associated_equipment_data[energy_category_id]['subtotals'].append(subtotal)
547
548
        ################################################################################################################
549
        # Step 11: construct the report
550
        ################################################################################################################
551
        if cursor_system:
552
            cursor_system.close()
553
        if cnx_system:
554
            cnx_system.close()
555
556
        if cursor_billing:
557
            cursor_billing.close()
558
        if cnx_billing:
559
            cnx_billing.close()
560
561
        if cursor_historical:
562
            cursor_historical.close()
563
        if cnx_historical:
564
            cnx_historical.close()
565
566
        result = dict()
567
568
        result['combined_equipment'] = dict()
569
        result['combined_equipment']['name'] = combined_equipment['name']
570
571
        result['base_period'] = dict()
572
        result['base_period']['names'] = list()
573
        result['base_period']['units'] = list()
574
        result['base_period']['timestamps'] = list()
575
        result['base_period']['values'] = list()
576
        result['base_period']['subtotals'] = list()
577
        result['base_period']['total'] = Decimal(0.0)
578
        if energy_category_set is not None and len(energy_category_set) > 0:
579
            for energy_category_id in energy_category_set:
580
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
581
                result['base_period']['units'].append(config.currency_unit)
582
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
583
                result['base_period']['values'].append(base[energy_category_id]['values'])
584
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
585
                result['base_period']['total'] += base[energy_category_id]['subtotal']
586
587
        result['reporting_period'] = dict()
588
        result['reporting_period']['names'] = list()
589
        result['reporting_period']['energy_category_ids'] = list()
590
        result['reporting_period']['units'] = list()
591
        result['reporting_period']['timestamps'] = list()
592
        result['reporting_period']['values'] = list()
593
        result['reporting_period']['rates'] = list()
594
        result['reporting_period']['subtotals'] = list()
595
        result['reporting_period']['toppeaks'] = list()
596
        result['reporting_period']['onpeaks'] = list()
597
        result['reporting_period']['midpeaks'] = list()
598
        result['reporting_period']['offpeaks'] = list()
599
        result['reporting_period']['deeps'] = list()
600
        result['reporting_period']['increment_rates'] = list()
601
        result['reporting_period']['total'] = Decimal(0.0)
602
        result['reporting_period']['total_increment_rate'] = Decimal(0.0)
603
        result['reporting_period']['total_unit'] = config.currency_unit
604
605
        if energy_category_set is not None and len(energy_category_set) > 0:
606
            for energy_category_id in energy_category_set:
607
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
608
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
609
                result['reporting_period']['units'].append(config.currency_unit)
610
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
611
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
612
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
613
                result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak'])
614
                result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak'])
615
                result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak'])
616
                result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak'])
617
                result['reporting_period']['deeps'].append(reporting[energy_category_id]['deep'])
618
                result['reporting_period']['increment_rates'].append(
619
                    (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) /
620
                    base[energy_category_id]['subtotal']
621
                    if base[energy_category_id]['subtotal'] > 0.0 else None)
622
                result['reporting_period']['total'] += reporting[energy_category_id]['subtotal']
623
624
                rate = list()
625
                for index, value in enumerate(reporting[energy_category_id]['values']):
626
                    if index < len(base[energy_category_id]['values']) \
627
                            and base[energy_category_id]['values'][index] != 0 and value != 0:
628
                        rate.append((value - base[energy_category_id]['values'][index])
629
                                    / base[energy_category_id]['values'][index])
630
                    else:
631
                        rate.append(None)
632
                result['reporting_period']['rates'].append(rate)
633
634
        result['reporting_period']['total_increment_rate'] = \
635
            (result['reporting_period']['total'] - result['base_period']['total']) / result['base_period']['total'] \
636
            if result['base_period']['total'] > Decimal(0.0) else None
637
638
        result['parameters'] = {
639
            "names": parameters_data['names'],
640
            "timestamps": parameters_data['timestamps'],
641
            "values": parameters_data['values']
642
        }
643
644
        result['associated_equipment'] = dict()
645
        result['associated_equipment']['energy_category_names'] = list()
646
        result['associated_equipment']['units'] = list()
647
        result['associated_equipment']['associated_equipment_names_array'] = list()
648
        result['associated_equipment']['subtotals_array'] = list()
649
        result['associated_equipment']['total_unit'] = config.currency_unit
650
        if energy_category_set is not None and len(energy_category_set) > 0:
651
            for energy_category_id in energy_category_set:
652
                result['associated_equipment']['energy_category_names'].append(
653
                    energy_category_dict[energy_category_id]['name'])
654
                result['associated_equipment']['units'].append(config.currency_unit)
655
                result['associated_equipment']['associated_equipment_names_array'].append(
656
                    associated_equipment_data[energy_category_id]['associated_equipment_names'])
657
                result['associated_equipment']['subtotals_array'].append(
658
                    associated_equipment_data[energy_category_id]['subtotals'])
659
660
        # export result to Excel file and then encode the file to base64 string
661
        result['excel_bytes_base64'] = None
662
        if not is_quick_mode:
663
            result['excel_bytes_base64'] = \
664
                excelexporters.combinedequipmentcost.export(result,
665
                                                            combined_equipment['name'],
666
                                                            base_period_start_datetime_local,
667
                                                            base_period_end_datetime_local,
668
                                                            reporting_period_start_datetime_local,
669
                                                            reporting_period_end_datetime_local,
670
                                                            period_type,
671
                                                            language)
672
        resp.text = json.dumps(result)
673