Passed
Push — master ( ef3fde...570acf )
by Guangyu
06:33 queued 12s
created

reports.combinedequipmentcarbon.Reporting.on_get()   F

Complexity

Conditions 120

Size

Total Lines 580
Code Lines 433

Duplication

Lines 580
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 433
dl 580
loc 580
rs 0
c 0
b 0
f 0
cc 120
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like reports.combinedequipmentcarbon.Reporting.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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