Reporting.on_get()   F
last analyzed

Complexity

Conditions 137

Size

Total Lines 676
Code Lines 518

Duplication

Lines 72
Ratio 10.65 %

Importance

Changes 0
Metric Value
eloc 518
dl 72
loc 676
rs 0
c 0
b 0
f 0
cc 137
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.combinedequipmentstatistics.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
from datetime import datetime, timedelta, timezone
3
from decimal import Decimal
4
import falcon
5
import mysql.connector
6
import simplejson as json
7
import config
8
import excelexporters.combinedequipmentstatistics
9
from core import utilities
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 categories
28
    # Step 4: query associated points
29
    # Step 5: query associated equipments
30
    # Step 6: query base period energy input
31
    # Step 7: query reporting period energy input
32
    # Step 8: query tariff data
33
    # Step 9: query associated points data
34
    # Step 10: query associated equipments energy input
35
    # Step 11: construct the report
36
    ####################################################################################################################
37
    @staticmethod
38
    def on_get(req, resp):
39
        if 'API-KEY' not in req.headers or \
40
                not isinstance(req.headers['API-KEY'], str) or \
41
                len(str.strip(req.headers['API-KEY'])) == 0:
42
            access_control(req)
43
        else:
44
            api_key_control(req)
45
        print(req.params)
46
        combined_equipment_id = req.params.get('combinedequipmentid')
47
        combined_equipment_uuid = req.params.get('combinedequipmentuuid')
48
        period_type = req.params.get('periodtype')
49
        base_period_start_datetime_local = req.params.get('baseperiodstartdatetime')
50
        base_period_end_datetime_local = req.params.get('baseperiodenddatetime')
51
        reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime')
52
        reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime')
53
        language = req.params.get('language')
54
        quick_mode = req.params.get('quickmode')
55
56
        ################################################################################################################
57
        # Step 1: valid parameters
58
        ################################################################################################################
59
        if combined_equipment_id is None and combined_equipment_uuid is None:
60
            raise falcon.HTTPError(status=falcon.HTTP_400,
61
                                   title='API.BAD_REQUEST',
62
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
63
64
        if combined_equipment_id is not None:
65
            combined_equipment_id = str.strip(combined_equipment_id)
66
            if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0:
67
                raise falcon.HTTPError(status=falcon.HTTP_400,
68
                                       title='API.BAD_REQUEST',
69
                                       description='API.INVALID_COMBINED_EQUIPMENT_ID')
70
71
        if combined_equipment_uuid is not None:
72
            regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I)
73
            match = regex.match(str.strip(combined_equipment_uuid))
74
            if not bool(match):
75
                raise falcon.HTTPError(status=falcon.HTTP_400,
76
                                       title='API.BAD_REQUEST',
77
                                       description='API.INVALID_COMBINED_EQUIPMENT_UUID')
78
79
        if period_type is None:
80
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
81
                                   description='API.INVALID_PERIOD_TYPE')
82
        else:
83
            period_type = str.strip(period_type)
84
            if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']:
85
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
86
                                       description='API.INVALID_PERIOD_TYPE')
87
88
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
89
        if config.utc_offset[0] == '-':
90
            timezone_offset = -timezone_offset
91
92
        base_start_datetime_utc = None
93
        if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0:
94
            base_period_start_datetime_local = str.strip(base_period_start_datetime_local)
95
            try:
96
                base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S')
97
            except ValueError:
98
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
99
                                       description="API.INVALID_BASE_PERIOD_START_DATETIME")
100
            base_start_datetime_utc = \
101
                base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
102
            # nomalize the start datetime
103
            if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30:
104
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
105
            else:
106
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
107
108
        base_end_datetime_utc = None
109
        if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0:
110
            base_period_end_datetime_local = str.strip(base_period_end_datetime_local)
111
            try:
112
                base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S')
113
            except ValueError:
114
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
115
                                       description="API.INVALID_BASE_PERIOD_END_DATETIME")
116
            base_end_datetime_utc = \
117
                base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
118
119
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
120
                base_start_datetime_utc >= base_end_datetime_utc:
121
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
122
                                   description='API.INVALID_BASE_PERIOD_END_DATETIME')
123
124
        if reporting_period_start_datetime_local is None:
125
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
126
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
127
        else:
128
            reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local)
129
            try:
130
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local,
131
                                                                 '%Y-%m-%dT%H:%M:%S')
132
            except ValueError:
133
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
134
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
135
            reporting_start_datetime_utc = \
136
                reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
137
            # nomalize the start datetime
138
            if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30:
139
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
140
            else:
141
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
142
143
        if reporting_period_end_datetime_local is None:
144
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
145
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
146
        else:
147
            reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local)
148
            try:
149
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local,
150
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
151
                                             timedelta(minutes=timezone_offset)
152
            except ValueError:
153
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
154
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
155
156
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
157
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
158
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
159
160
        # if turn quick mode on, do not return parameters data and excel file
161
        is_quick_mode = False
162
        if quick_mode is not None and \
163
                len(str.strip(quick_mode)) > 0 and \
164
                str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'):
165
            is_quick_mode = True
166
167
        trans = utilities.get_translation(language)
168
        trans.install()
169
        _ = trans.gettext
170
171
        ################################################################################################################
172
        # Step 2: query the combined equipment
173
        ################################################################################################################
174
        cnx_system = mysql.connector.connect(**config.myems_system_db)
175
        cursor_system = cnx_system.cursor()
176
177
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
178
        cursor_energy = cnx_energy.cursor()
179
180
        cnx_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 View Code Duplication
        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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
195
            if cursor_system:
196
                cursor_system.close()
197
            if cnx_system:
198
                cnx_system.close()
199
200
            if cursor_energy:
201
                cursor_energy.close()
202
            if cnx_energy:
203
                cnx_energy.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_energy.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_energy.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_energy.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_energy.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_energy:
258
                cursor_energy.close()
259
            if cnx_energy:
260
                cnx_energy.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 input
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
                base[energy_category_id]['mean'] = None
315
                base[energy_category_id]['median'] = None
316
                base[energy_category_id]['minimum'] = None
317
                base[energy_category_id]['maximum'] = None
318
                base[energy_category_id]['stdev'] = None
319
                base[energy_category_id]['variance'] = None
320
321
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
322
                                      " FROM tbl_combined_equipment_input_category_hourly "
323
                                      " WHERE combined_equipment_id = %s "
324
                                      "     AND energy_category_id = %s "
325
                                      "     AND start_datetime_utc >= %s "
326
                                      "     AND start_datetime_utc < %s "
327
                                      " ORDER BY start_datetime_utc ",
328
                                      (combined_equipment['id'],
329
                                       energy_category_id,
330
                                       base_start_datetime_utc,
331
                                       base_end_datetime_utc))
332
                rows_combined_equipment_hourly = cursor_energy.fetchall()
333
334
                rows_combined_equipment_periodically, \
335
                    base[energy_category_id]['mean'], \
336
                    base[energy_category_id]['median'], \
337
                    base[energy_category_id]['minimum'], \
338
                    base[energy_category_id]['maximum'], \
339
                    base[energy_category_id]['stdev'], \
340
                    base[energy_category_id]['variance'] = \
341
                    utilities.statistics_hourly_data_by_period(rows_combined_equipment_hourly,
342
                                                               base_start_datetime_utc,
343
                                                               base_end_datetime_utc,
344
                                                               period_type)
345
346
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
347
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
348
                                             timedelta(minutes=timezone_offset)
349
                    if period_type == 'hourly':
350
                        current_datetime = current_datetime_local.isoformat()[0:19]
351
                    elif period_type == 'daily':
352
                        current_datetime = current_datetime_local.isoformat()[0:10]
353
                    elif period_type == 'weekly':
354
                        current_datetime = current_datetime_local.isoformat()[0:10]
355
                    elif period_type == 'monthly':
356
                        current_datetime = current_datetime_local.isoformat()[0:7]
357
                    elif period_type == 'yearly':
358
                        current_datetime = current_datetime_local.isoformat()[0:4]
359
360
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
361
                        else row_combined_equipment_periodically[1]
362
                    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...
363
                    base[energy_category_id]['values'].append(actual_value)
364
                    base[energy_category_id]['subtotal'] += actual_value
365
366
        ################################################################################################################
367
        # Step 7: query reporting period energy input
368
        ################################################################################################################
369
        reporting = dict()
370
        if energy_category_set is not None and len(energy_category_set) > 0:
371
            for energy_category_id in energy_category_set:
372
                reporting[energy_category_id] = dict()
373
                reporting[energy_category_id]['timestamps'] = list()
374
                reporting[energy_category_id]['values'] = list()
375
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
376
                reporting[energy_category_id]['mean'] = None
377
                reporting[energy_category_id]['median'] = None
378
                reporting[energy_category_id]['minimum'] = None
379
                reporting[energy_category_id]['maximum'] = None
380
                reporting[energy_category_id]['stdev'] = None
381
                reporting[energy_category_id]['variance'] = None
382
383
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
384
                                      " FROM tbl_combined_equipment_input_category_hourly "
385
                                      " WHERE combined_equipment_id = %s "
386
                                      "     AND energy_category_id = %s "
387
                                      "     AND start_datetime_utc >= %s "
388
                                      "     AND start_datetime_utc < %s "
389
                                      " ORDER BY start_datetime_utc ",
390
                                      (combined_equipment['id'],
391
                                       energy_category_id,
392
                                       reporting_start_datetime_utc,
393
                                       reporting_end_datetime_utc))
394
                rows_combined_equipment_hourly = cursor_energy.fetchall()
395
396
                rows_combined_equipment_periodically, \
397
                    reporting[energy_category_id]['mean'], \
398
                    reporting[energy_category_id]['median'], \
399
                    reporting[energy_category_id]['minimum'], \
400
                    reporting[energy_category_id]['maximum'], \
401
                    reporting[energy_category_id]['stdev'], \
402
                    reporting[energy_category_id]['variance'] = \
403
                    utilities.statistics_hourly_data_by_period(rows_combined_equipment_hourly,
404
                                                               reporting_start_datetime_utc,
405
                                                               reporting_end_datetime_utc,
406
                                                               period_type)
407
408
                for row_combined_equipment_periodically in rows_combined_equipment_periodically:
409
                    current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \
410
                                             timedelta(minutes=timezone_offset)
411
                    if period_type == 'hourly':
412
                        current_datetime = current_datetime_local.isoformat()[0:19]
413
                    elif period_type == 'daily':
414
                        current_datetime = current_datetime_local.isoformat()[0:10]
415
                    elif period_type == 'weekly':
416
                        current_datetime = current_datetime_local.isoformat()[0:10]
417
                    elif period_type == 'monthly':
418
                        current_datetime = current_datetime_local.isoformat()[0:7]
419
                    elif period_type == 'yearly':
420
                        current_datetime = current_datetime_local.isoformat()[0:4]
421
422
                    actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \
423
                        else row_combined_equipment_periodically[1]
424
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
425
                    reporting[energy_category_id]['values'].append(actual_value)
426
                    reporting[energy_category_id]['subtotal'] += actual_value
427
428
        ################################################################################################################
429
        # Step 8: query tariff data
430
        ################################################################################################################
431
        parameters_data = dict()
432
        parameters_data['names'] = list()
433
        parameters_data['timestamps'] = list()
434
        parameters_data['values'] = list()
435
        if not is_quick_mode:
436
            if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0:
437
                for energy_category_id in energy_category_set:
438
                    energy_category_tariff_dict = \
439
                        utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'],
440
                                                              energy_category_id,
441
                                                              reporting_start_datetime_utc,
442
                                                              reporting_end_datetime_utc)
443
                    tariff_timestamp_list = list()
444
                    tariff_value_list = list()
445
                    for k, v in energy_category_tariff_dict.items():
446
                        # convert k from utc to local
447
                        k = k + timedelta(minutes=timezone_offset)
448
                        tariff_timestamp_list.append(k.isoformat()[0:19])
449
                        tariff_value_list.append(v)
450
451
                    parameters_data['names'].append(
452
                        _('Tariff') + '-' + energy_category_dict[energy_category_id]['name'])
453
                    parameters_data['timestamps'].append(tariff_timestamp_list)
454
                    parameters_data['values'].append(tariff_value_list)
455
456
        ################################################################################################################
457
        # Step 9: query associated points data
458
        ################################################################################################################
459
        if not is_quick_mode:
460
            for point in point_list:
461
                point_values = []
462
                point_timestamps = []
463
                if point['object_type'] == 'ENERGY_VALUE':
464
                    query = (" SELECT utc_date_time, actual_value "
465
                             " FROM tbl_energy_value "
466
                             " WHERE point_id = %s "
467
                             "       AND utc_date_time BETWEEN %s AND %s "
468
                             " ORDER BY utc_date_time ")
469
                    cursor_historical.execute(query, (point['id'],
470
                                                      reporting_start_datetime_utc,
471
                                                      reporting_end_datetime_utc))
472
                    rows = cursor_historical.fetchall()
473
474
                    if rows is not None and len(rows) > 0:
475
                        for row in rows:
476
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
477
                                                     timedelta(minutes=timezone_offset)
478
                            current_datetime = current_datetime_local.isoformat()[0:19]
479
                            point_timestamps.append(current_datetime)
480
                            point_values.append(row[1])
481
                elif point['object_type'] == 'ANALOG_VALUE':
482
                    query = (" SELECT utc_date_time, actual_value "
483
                             " FROM tbl_analog_value "
484
                             " WHERE point_id = %s "
485
                             "       AND utc_date_time BETWEEN %s AND %s "
486
                             " ORDER BY utc_date_time ")
487
                    cursor_historical.execute(query, (point['id'],
488
                                                      reporting_start_datetime_utc,
489
                                                      reporting_end_datetime_utc))
490
                    rows = cursor_historical.fetchall()
491
492
                    if rows is not None and len(rows) > 0:
493
                        for row in rows:
494
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
495
                                                     timedelta(minutes=timezone_offset)
496
                            current_datetime = current_datetime_local.isoformat()[0:19]
497
                            point_timestamps.append(current_datetime)
498
                            point_values.append(row[1])
499
                elif point['object_type'] == 'DIGITAL_VALUE':
500
                    query = (" SELECT utc_date_time, actual_value "
501
                             " FROM tbl_digital_value "
502
                             " WHERE point_id = %s "
503
                             "       AND utc_date_time BETWEEN %s AND %s "
504
                             " ORDER BY utc_date_time ")
505
                    cursor_historical.execute(query, (point['id'],
506
                                                      reporting_start_datetime_utc,
507
                                                      reporting_end_datetime_utc))
508
                    rows = cursor_historical.fetchall()
509
510
                    if rows is not None and len(rows) > 0:
511
                        for row in rows:
512
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
513
                                                     timedelta(minutes=timezone_offset)
514
                            current_datetime = current_datetime_local.isoformat()[0:19]
515
                            point_timestamps.append(current_datetime)
516
                            point_values.append(row[1])
517
518
                parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
519
                parameters_data['timestamps'].append(point_timestamps)
520
                parameters_data['values'].append(point_values)
521
522
        ################################################################################################################
523
        # Step 10: query associated equipments energy input
524
        ################################################################################################################
525
        associated_equipment_data = dict()
526
527
        if energy_category_set is not None and len(energy_category_set) > 0:
528
            for energy_category_id in energy_category_set:
529
                associated_equipment_data[energy_category_id] = dict()
530
                associated_equipment_data[energy_category_id]['associated_equipment_names'] = list()
531
                associated_equipment_data[energy_category_id]['subtotals'] = list()
532
                for associated_equipment in associated_equipment_list:
533
                    associated_equipment_data[energy_category_id]['associated_equipment_names'].append(
534
                        associated_equipment['name'])
535
536
                    cursor_energy.execute(" SELECT SUM(actual_value) "
537
                                          " FROM tbl_equipment_input_category_hourly "
538
                                          " WHERE equipment_id = %s "
539
                                          "     AND energy_category_id = %s "
540
                                          "     AND start_datetime_utc >= %s "
541
                                          "     AND start_datetime_utc < %s ",
542
                                          (associated_equipment['id'],
543
                                           energy_category_id,
544
                                           reporting_start_datetime_utc,
545
                                           reporting_end_datetime_utc))
546
                    row_subtotal = cursor_energy.fetchone()
547
548
                    subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0]
549
                    associated_equipment_data[energy_category_id]['subtotals'].append(subtotal)
550
551
        ################################################################################################################
552
        # Step 11: construct the report
553
        ################################################################################################################
554
        if cursor_system:
555
            cursor_system.close()
556
        if cnx_system:
557
            cnx_system.close()
558
559
        if cursor_energy:
560
            cursor_energy.close()
561
        if cnx_energy:
562
            cnx_energy.close()
563
564
        if cursor_historical:
565
            cursor_historical.close()
566
        if cnx_historical:
567
            cnx_historical.close()
568
569
        result = dict()
570
571
        result['combined_equipment'] = dict()
572
        result['combined_equipment']['name'] = combined_equipment['name']
573
574
        result['base_period'] = dict()
575
        result['base_period']['names'] = list()
576
        result['base_period']['units'] = list()
577
        result['base_period']['timestamps'] = list()
578
        result['base_period']['values'] = list()
579
        result['base_period']['subtotals'] = list()
580
        result['base_period']['means'] = list()
581
        result['base_period']['medians'] = list()
582
        result['base_period']['minimums'] = list()
583
        result['base_period']['maximums'] = list()
584
        result['base_period']['stdevs'] = list()
585
        result['base_period']['variances'] = list()
586
587
        if energy_category_set is not None and len(energy_category_set) > 0:
588
            for energy_category_id in energy_category_set:
589
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
590
                result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
591
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
592
                result['base_period']['values'].append(base[energy_category_id]['values'])
593
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
594
                result['base_period']['means'].append(base[energy_category_id]['mean'])
595
                result['base_period']['medians'].append(base[energy_category_id]['median'])
596
                result['base_period']['minimums'].append(base[energy_category_id]['minimum'])
597
                result['base_period']['maximums'].append(base[energy_category_id]['maximum'])
598
                result['base_period']['stdevs'].append(base[energy_category_id]['stdev'])
599
                result['base_period']['variances'].append(base[energy_category_id]['variance'])
600
601
        result['reporting_period'] = dict()
602
        result['reporting_period']['names'] = list()
603
        result['reporting_period']['energy_category_ids'] = list()
604
        result['reporting_period']['units'] = list()
605
        result['reporting_period']['timestamps'] = list()
606
        result['reporting_period']['values'] = list()
607
        result['reporting_period']['rates'] = list()
608
        result['reporting_period']['subtotals'] = list()
609
        result['reporting_period']['means'] = list()
610
        result['reporting_period']['means_increment_rate'] = list()
611
        result['reporting_period']['medians'] = list()
612
        result['reporting_period']['medians_increment_rate'] = list()
613
        result['reporting_period']['minimums'] = list()
614
        result['reporting_period']['minimums_increment_rate'] = list()
615
        result['reporting_period']['maximums'] = list()
616
        result['reporting_period']['maximums_increment_rate'] = list()
617
        result['reporting_period']['stdevs'] = list()
618
        result['reporting_period']['stdevs_increment_rate'] = list()
619
        result['reporting_period']['variances'] = list()
620
        result['reporting_period']['variances_increment_rate'] = list()
621
622 View Code Duplication
        if energy_category_set is not None and len(energy_category_set) > 0:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
623
            for energy_category_id in energy_category_set:
624
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
625
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
626
                result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
627
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
628
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
629
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
630
                result['reporting_period']['means'].append(reporting[energy_category_id]['mean'])
631
                result['reporting_period']['means_increment_rate'].append(
632
                    (reporting[energy_category_id]['mean'] - base[energy_category_id]['mean']) /
633
                    base[energy_category_id]['mean'] if (base[energy_category_id]['mean'] is not None and
634
                                                         base[energy_category_id]['mean'] > Decimal(0.0))
635
                    else None)
636
                result['reporting_period']['medians'].append(reporting[energy_category_id]['median'])
637
                result['reporting_period']['medians_increment_rate'].append(
638
                    (reporting[energy_category_id]['median'] - base[energy_category_id]['median']) /
639
                    base[energy_category_id]['median'] if (base[energy_category_id]['median'] is not None and
640
                                                           base[energy_category_id]['median'] > Decimal(0.0))
641
                    else None)
642
                result['reporting_period']['minimums'].append(reporting[energy_category_id]['minimum'])
643
                result['reporting_period']['minimums_increment_rate'].append(
644
                    (reporting[energy_category_id]['minimum'] - base[energy_category_id]['minimum']) /
645
                    base[energy_category_id]['minimum'] if (base[energy_category_id]['minimum'] is not None and
646
                                                            base[energy_category_id]['minimum'] > Decimal(0.0))
647
                    else None)
648
                result['reporting_period']['maximums'].append(reporting[energy_category_id]['maximum'])
649
                result['reporting_period']['maximums_increment_rate'].append(
650
                    (reporting[energy_category_id]['maximum'] - base[energy_category_id]['maximum']) /
651
                    base[energy_category_id]['maximum'] if (base[energy_category_id]['maximum'] is not None and
652
                                                            base[energy_category_id]['maximum'] > Decimal(0.0))
653
                    else None)
654
                result['reporting_period']['stdevs'].append(reporting[energy_category_id]['stdev'])
655
                result['reporting_period']['stdevs_increment_rate'].append(
656
                    (reporting[energy_category_id]['stdev'] - base[energy_category_id]['stdev']) /
657
                    base[energy_category_id]['stdev'] if (base[energy_category_id]['stdev'] is not None and
658
                                                          base[energy_category_id]['stdev'] > Decimal(0.0))
659
                    else None)
660
                result['reporting_period']['variances'].append(reporting[energy_category_id]['variance'])
661
                result['reporting_period']['variances_increment_rate'].append(
662
                    (reporting[energy_category_id]['variance'] - base[energy_category_id]['variance']) /
663
                    base[energy_category_id]['variance'] if (base[energy_category_id]['variance'] is not None and
664
                                                             base[energy_category_id]['variance'] > Decimal(0.0))
665
                    else None)
666
667
                rate = list()
668
                for index, value in enumerate(reporting[energy_category_id]['values']):
669
                    if index < len(base[energy_category_id]['values']) \
670
                            and base[energy_category_id]['values'][index] != 0 and value != 0:
671
                        rate.append((value - base[energy_category_id]['values'][index])
672
                                    / base[energy_category_id]['values'][index])
673
                    else:
674
                        rate.append(None)
675
                result['reporting_period']['rates'].append(rate)
676
677
        result['parameters'] = {
678
            "names": parameters_data['names'],
679
            "timestamps": parameters_data['timestamps'],
680
            "values": parameters_data['values']
681
        }
682
683
        result['associated_equipment'] = dict()
684
        result['associated_equipment']['energy_category_names'] = list()
685
        result['associated_equipment']['units'] = list()
686
        result['associated_equipment']['associated_equipment_names_array'] = list()
687
        result['associated_equipment']['subtotals_array'] = list()
688
        if energy_category_set is not None and len(energy_category_set) > 0:
689
            for energy_category_id in energy_category_set:
690
                result['associated_equipment']['energy_category_names'].append(
691
                    energy_category_dict[energy_category_id]['name'])
692
                result['associated_equipment']['units'].append(
693
                    energy_category_dict[energy_category_id]['unit_of_measure'])
694
                result['associated_equipment']['associated_equipment_names_array'].append(
695
                    associated_equipment_data[energy_category_id]['associated_equipment_names'])
696
                result['associated_equipment']['subtotals_array'].append(
697
                    associated_equipment_data[energy_category_id]['subtotals'])
698
699
        # export result to Excel file and then encode the file to base64 string
700
        result['excel_bytes_base64'] = None
701
        if not is_quick_mode:
702
            result['excel_bytes_base64'] = \
703
                excelexporters.combinedequipmentstatistics.export(result,
704
                                                                  combined_equipment['name'],
705
                                                                  base_period_start_datetime_local,
706
                                                                  base_period_end_datetime_local,
707
                                                                  reporting_period_start_datetime_local,
708
                                                                  reporting_period_end_datetime_local,
709
                                                                  period_type,
710
                                                                  language)
711
712
        resp.text = json.dumps(result)
713