Passed
Push — master ( 88a5d8...7de4c1 )
by Guangyu
01:49 queued 11s
created

metercost   F

Complexity

Total Complexity 74

Size/Duplication

Total Lines 483
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 74
eloc 342
dl 0
loc 483
rs 2.48
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
F Reporting.on_get() 0 451 72
A Reporting.__init__() 0 3 1
A Reporting.on_options() 0 3 1

How to fix   Complexity   

Complexity

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

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

1
import falcon
2
import simplejson as json
3
import mysql.connector
4
import config
5
from datetime import datetime, timedelta, timezone
6
import utilities
7
from decimal import *
8
9
10
class Reporting:
11
    @staticmethod
12
    def __init__():
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp):
17
        resp.status = falcon.HTTP_200
18
19
    ####################################################################################################################
20
    # PROCEDURES
21
    # Step 1: valid parameters
22
    # Step 2: query the meter and energy category
23
    # Step 3: query associated points
24
    # Step 4: query base period energy consumption
25
    # Step 5: query base period energy cost
26
    # Step 6: query reporting period energy consumption
27
    # Step 7: query reporting period energy cost
28
    # Step 8: query tariff data
29
    # Step 9: query associated points data
30
    # Step 10: construct the report
31
    ####################################################################################################################
32
    @staticmethod
33
    def on_get(req, resp):
34
        print(req.params)
35
        meter_id = req.params.get('meterid')
36
        period_type = req.params.get('periodtype')
37
        base_period_start_datetime = req.params.get('baseperiodstartdatetime')
38
        base_period_end_datetime = req.params.get('baseperiodenddatetime')
39
        reporting_period_start_datetime = req.params.get('reportingperiodstartdatetime')
40
        reporting_period_end_datetime = req.params.get('reportingperiodenddatetime')
41
42
        ################################################################################################################
43
        # Step 1: valid parameters
44
        ################################################################################################################
45
        if meter_id is None:
46
            raise falcon.HTTPError(falcon.HTTP_400,
47
                                   title='API.BAD_REQUEST',
48
                                   description='API.INVALID_METER_ID')
49
        else:
50
            meter_id = str.strip(meter_id)
51
            if not meter_id.isdigit() or int(meter_id) <= 0:
52
                raise falcon.HTTPError(falcon.HTTP_400,
53
                                       title='API.BAD_REQUEST',
54
                                       description='API.INVALID_METER_ID')
55
56
        if period_type is None:
57
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
58
        else:
59
            period_type = str.strip(period_type)
60
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
61
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
62
63
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
64
        if config.utc_offset[0] == '-':
65
            timezone_offset = -timezone_offset
66
67
        base_start_datetime_utc = None
68
        if base_period_start_datetime is not None and len(str.strip(base_period_start_datetime)) > 0:
69
            base_period_start_datetime = str.strip(base_period_start_datetime)
70
            try:
71
                base_start_datetime_utc = datetime.strptime(base_period_start_datetime, '%Y-%m-%dT%H:%M:%S')
72
            except ValueError:
73
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
74
                                       description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME")
75
            base_start_datetime_utc = base_start_datetime_utc.replace(tzinfo=timezone.utc) - \
76
                timedelta(minutes=timezone_offset)
77
78
        base_end_datetime_utc = None
79
        if base_period_end_datetime is not None and len(str.strip(base_period_end_datetime)) > 0:
80
            base_period_end_datetime = str.strip(base_period_end_datetime)
81
            try:
82
                base_end_datetime_utc = datetime.strptime(base_period_end_datetime, '%Y-%m-%dT%H:%M:%S')
83
            except ValueError:
84
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
85
                                       description="API.INVALID_BASE_PERIOD_ENDS_DATETIME")
86
            base_end_datetime_utc = base_end_datetime_utc.replace(tzinfo=timezone.utc) - \
87
                timedelta(minutes=timezone_offset)
88
89
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
90
                base_start_datetime_utc >= base_end_datetime_utc:
91
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
92
                                   description='API.INVALID_BASE_PERIOD_ENDS_DATETIME')
93
94
        if reporting_period_start_datetime is None:
95
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
96
                                   description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
97
        else:
98
            reporting_period_start_datetime = str.strip(reporting_period_start_datetime)
99
            try:
100
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime, '%Y-%m-%dT%H:%M:%S')
101
            except ValueError:
102
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
103
                                       description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
104
            reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \
105
                timedelta(minutes=timezone_offset)
106
107
        if reporting_period_end_datetime is None:
108
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
109
                                   description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
110
        else:
111
            reporting_period_end_datetime = str.strip(reporting_period_end_datetime)
112
            try:
113
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime, '%Y-%m-%dT%H:%M:%S')
114
            except ValueError:
115
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
116
                                       description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
117
            reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \
118
                timedelta(minutes=timezone_offset)
119
120
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
121
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
122
                                   description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME')
123
124
        ################################################################################################################
125
        # Step 2: query the meter and energy category
126
        ################################################################################################################
127
        cnx_system = mysql.connector.connect(**config.myems_system_db)
128
        cursor_system = cnx_system.cursor()
129
130
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
131
        cursor_energy = cnx_energy.cursor()
132
133
        cnx_billing = mysql.connector.connect(**config.myems_billing_db)
134
        cursor_billing = cnx_billing.cursor()
135
136
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
137
        cursor_historical = cnx_historical.cursor()
138
139
        cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, "
140
                              "        ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e "
141
                              " FROM tbl_meters m, tbl_energy_categories ec "
142
                              " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id,))
143
        row_meter = cursor_system.fetchone()
144
        if row_meter is None:
145
            if cursor_system:
146
                cursor_system.close()
147
            if cnx_system:
148
                cnx_system.disconnect()
149
150
            if cursor_energy:
151
                cursor_energy.close()
152
            if cnx_energy:
153
                cnx_energy.disconnect()
154
155
            if cursor_billing:
156
                cursor_billing.close()
157
            if cnx_billing:
158
                cnx_billing.disconnect()
159
160
            if cursor_historical:
161
                cursor_historical.close()
162
            if cnx_historical:
163
                cnx_historical.disconnect()
164
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND')
165
166
        meter = dict()
167
        meter['id'] = row_meter[0]
168
        meter['name'] = row_meter[1]
169
        meter['cost_center_id'] = row_meter[2]
170
        meter['energy_category_id'] = row_meter[3]
171
        meter['energy_category_name'] = row_meter[4]
172
        meter['unit_of_measure'] = config.currency_unit
173
        meter['kgce'] = row_meter[6]
174
        meter['kgco2e'] = row_meter[7]
175
176
        ################################################################################################################
177
        # Step 3: query associated points
178
        ################################################################################################################
179
        point_list = list()
180
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
181
                              " FROM tbl_meters m, tbl_meters_points mp, tbl_points p "
182
                              " WHERE m.id = %s AND m.id = mp.meter_id AND mp.point_id = p.id "
183
                              " ORDER BY p.id ", (meter['id'],))
184
        rows_points = cursor_system.fetchall()
185
        if rows_points is not None and len(rows_points) > 0:
186
            for row in rows_points:
187
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
188
189
        ################################################################################################################
190
        # Step 4: query base period energy consumption
191
        ################################################################################################################
192
        query = (" SELECT start_datetime_utc, actual_value "
193
                 " FROM tbl_meter_hourly "
194
                 " WHERE meter_id = %s "
195
                 " AND start_datetime_utc >= %s "
196
                 " AND start_datetime_utc < %s "
197
                 " ORDER BY start_datetime_utc ")
198
        cursor_energy.execute(query, (meter['id'], base_start_datetime_utc, base_end_datetime_utc))
199
        rows_meter_hourly = cursor_energy.fetchall()
200
201
        rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
202
                                                                            base_start_datetime_utc,
203
                                                                            base_end_datetime_utc,
204
                                                                            period_type)
205
        base = dict()
206
        base['timestamps'] = list()
207
        base['values_in_category'] = list()
208
        base['total_in_category'] = Decimal(0.0)
209
        base['values_in_kgce'] = list()
210
        base['total_in_kgce'] = Decimal(0.0)
211
        base['values_in_kgco2e'] = list()
212
        base['total_in_kgco2e'] = Decimal(0.0)
213
214
        for row_meter_periodically in rows_meter_periodically:
215
            current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \
216
                                     timedelta(minutes=timezone_offset)
217
            if period_type == 'hourly':
218
                current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
219
            elif period_type == 'daily':
220
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
221
            elif period_type == 'monthly':
222
                current_datetime = current_datetime_local.strftime('%Y-%m')
223
            elif period_type == 'yearly':
224
                current_datetime = current_datetime_local.strftime('%Y')
225
226
            actual_value = Decimal(0.0) if row_meter_periodically[1] is None \
227
                else row_meter_periodically[1]
228
            base['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...
229
            base['values_in_kgce'].append(actual_value * meter['kgce'])
230
            base['total_in_kgce'] += actual_value * meter['kgce']
231
            base['values_in_kgco2e'].append(actual_value * meter['kgco2e'])
232
            base['total_in_kgco2e'] += actual_value * meter['kgco2e']
233
234
        ################################################################################################################
235
        # Step 5: query base period energy cost
236
        ################################################################################################################
237
        query = (" SELECT start_datetime_utc, actual_value "
238
                 " FROM tbl_meter_hourly "
239
                 " WHERE meter_id = %s "
240
                 " AND start_datetime_utc >= %s "
241
                 " AND start_datetime_utc < %s "
242
                 " ORDER BY start_datetime_utc ")
243
        cursor_billing.execute(query, (meter['id'], base_start_datetime_utc, base_end_datetime_utc))
244
        rows_meter_hourly = cursor_billing.fetchall()
245
246
        rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
247
                                                                            base_start_datetime_utc,
248
                                                                            base_end_datetime_utc,
249
                                                                            period_type)
250
251
        base['values_in_category'] = list()
252
        base['total_in_category'] = Decimal(0.0)
253
254
        for row_meter_periodically in rows_meter_periodically:
255
            actual_value = Decimal(0.0) if row_meter_periodically[1] is None \
256
                else row_meter_periodically[1]
257
            base['values_in_category'].append(actual_value)
258
            base['total_in_category'] += actual_value
259
260
        ################################################################################################################
261
        # Step 6: query reporting period energy consumption
262
        ################################################################################################################
263
        query = (" SELECT start_datetime_utc, actual_value "
264
                 " FROM tbl_meter_hourly "
265
                 " WHERE meter_id = %s "
266
                 " AND start_datetime_utc >= %s "
267
                 " AND start_datetime_utc < %s "
268
                 " ORDER BY start_datetime_utc ")
269
        cursor_energy.execute(query, (meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
270
        rows_meter_hourly = cursor_energy.fetchall()
271
272
        rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
273
                                                                            reporting_start_datetime_utc,
274
                                                                            reporting_end_datetime_utc,
275
                                                                            period_type)
276
        reporting = dict()
277
        reporting['timestamps'] = list()
278
        reporting['values_in_category'] = list()
279
        reporting['total_in_category'] = Decimal(0.0)
280
        reporting['values_in_kgce'] = list()
281
        reporting['total_in_kgce'] = Decimal(0.0)
282
        reporting['values_in_kgco2e'] = list()
283
        reporting['total_in_kgco2e'] = Decimal(0.0)
284
285
        for row_meter_periodically in rows_meter_periodically:
286
            current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \
287
                timedelta(minutes=timezone_offset)
288
            if period_type == 'hourly':
289
                current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
290
            elif period_type == 'daily':
291
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
292
            elif period_type == 'monthly':
293
                current_datetime = current_datetime_local.strftime('%Y-%m')
294
            elif period_type == 'yearly':
295
                current_datetime = current_datetime_local.strftime('%Y')
296
297
            actual_value = Decimal(0.0) if row_meter_periodically[1] is None \
298
                else row_meter_periodically[1]
299
300
            reporting['timestamps'].append(current_datetime)
301
            reporting['values_in_kgce'].append(actual_value * meter['kgce'])
302
            reporting['total_in_kgce'] += actual_value * meter['kgce']
303
            reporting['values_in_kgco2e'].append(actual_value * meter['kgco2e'])
304
            reporting['total_in_kgco2e'] += actual_value * meter['kgco2e']
305
306
        ################################################################################################################
307
        # Step 7: query reporting period energy cost
308
        ################################################################################################################
309
        query = (" SELECT start_datetime_utc, actual_value "
310
                 " FROM tbl_meter_hourly "
311
                 " WHERE meter_id = %s "
312
                 " AND start_datetime_utc >= %s "
313
                 " AND start_datetime_utc < %s "
314
                 " ORDER BY start_datetime_utc ")
315
        cursor_billing.execute(query, (meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
316
        rows_meter_hourly = cursor_billing.fetchall()
317
318
        rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
319
                                                                            reporting_start_datetime_utc,
320
                                                                            reporting_end_datetime_utc,
321
                                                                            period_type)
322
323
        for row_meter_periodically in rows_meter_periodically:
324
            actual_value = Decimal(0.0) if row_meter_periodically[1] is None \
325
                else row_meter_periodically[1]
326
327
            reporting['values_in_category'].append(actual_value)
328
            reporting['total_in_category'] += actual_value
329
330
        ################################################################################################################
331
        # Step 8: query tariff data
332
        ################################################################################################################
333
        parameters_data = dict()
334
        parameters_data['names'] = list()
335
        parameters_data['timestamps'] = list()
336
        parameters_data['values'] = list()
337
338
        tariff_dict = utilities.get_energy_category_tariffs(meter['cost_center_id'],
339
                                                            meter['energy_category_id'],
340
                                                            reporting_start_datetime_utc,
341
                                                            reporting_end_datetime_utc)
342
        tariff_timestamp_list = list()
343
        tariff_value_list = list()
344
        for k, v in tariff_dict.items():
345
            # convert k from utc to local
346
            k = k + timedelta(minutes=timezone_offset)
347
            tariff_timestamp_list.append(k.isoformat()[0:19])
348
            tariff_value_list.append(v)
349
350
        parameters_data['names'].append('TARIFF-' + meter['energy_category_name'])
351
        parameters_data['timestamps'].append(tariff_timestamp_list)
352
        parameters_data['values'].append(tariff_value_list)
353
354
        ################################################################################################################
355
        # Step 9: query associated points data
356
        ################################################################################################################
357
        for point in point_list:
358
            point_values = []
359
            point_timestamps = []
360
            if point['object_type'] == 'ANALOG_VALUE':
361
                query = (" SELECT utc_date_time, actual_value "
362
                         " FROM tbl_analog_value "
363
                         " WHERE point_id = %s "
364
                         "       AND utc_date_time BETWEEN %s AND %s "
365
                         " ORDER BY utc_date_time ")
366
                cursor_historical.execute(query, (point['id'],
367
                                                  reporting_start_datetime_utc,
368
                                                  reporting_end_datetime_utc))
369
                rows = cursor_historical.fetchall()
370
371
                if rows is not None and len(rows) > 0:
372
                    for row in rows:
373
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
374
                                                 timedelta(minutes=timezone_offset)
375
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
376
                        point_timestamps.append(current_datetime)
377
                        point_values.append(row[1])
378
379
            elif point['object_type'] == 'ENERGY_VALUE':
380
                query = (" SELECT utc_date_time, actual_value "
381
                         " FROM tbl_energy_value "
382
                         " WHERE point_id = %s "
383
                         "       AND utc_date_time BETWEEN %s AND %s "
384
                         " ORDER BY utc_date_time ")
385
                cursor_historical.execute(query, (point['id'],
386
                                                  reporting_start_datetime_utc,
387
                                                  reporting_end_datetime_utc))
388
                rows = cursor_historical.fetchall()
389
390
                if rows is not None and len(rows) > 0:
391
                    for row in rows:
392
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
393
                                                 timedelta(minutes=timezone_offset)
394
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
395
                        point_timestamps.append(current_datetime)
396
                        point_values.append(row[1])
397
            elif point['object_type'] == 'DIGITAL_VALUE':
398
                query = (" SELECT utc_date_time, actual_value "
399
                         " FROM tbl_digital_value "
400
                         " WHERE point_id = %s "
401
                         "       AND utc_date_time BETWEEN %s AND %s ")
402
                cursor_historical.execute(query, (point['id'],
403
                                                  reporting_start_datetime_utc,
404
                                                  reporting_end_datetime_utc))
405
                rows = cursor_historical.fetchall()
406
407
                if rows is not None and len(rows) > 0:
408
                    for row in rows:
409
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
410
                                                 timedelta(minutes=timezone_offset)
411
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
412
                        point_timestamps.append(current_datetime)
413
                        point_values.append(row[1])
414
415
            parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
416
            parameters_data['timestamps'].append(point_timestamps)
417
            parameters_data['values'].append(point_values)
418
419
        ################################################################################################################
420
        # Step 10: construct the report
421
        ################################################################################################################
422
        if cursor_system:
423
            cursor_system.close()
424
        if cnx_system:
425
            cnx_system.disconnect()
426
427
        if cursor_energy:
428
            cursor_energy.close()
429
        if cnx_energy:
430
            cnx_energy.disconnect()
431
432
        if cursor_billing:
433
            cursor_billing.close()
434
        if cnx_billing:
435
            cnx_billing.disconnect()
436
437
        if cursor_historical:
438
            cursor_historical.close()
439
        if cnx_historical:
440
            cnx_historical.disconnect()
441
        result = {
442
            "meter": {
443
                "cost_center_id": meter['cost_center_id'],
444
                "energy_category_id": meter['energy_category_id'],
445
                "energy_category_name": meter['energy_category_name'],
446
                "unit_of_measure": meter['unit_of_measure'],
447
                "kgce": meter['kgce'],
448
                "kgco2e": meter['kgco2e'],
449
            },
450
            "reporting_period": {
451
                "increment_rate":
452
                    (reporting['total_in_category']-base['total_in_category'])/base['total_in_category']
453
                    if base['total_in_category'] > 0 else None,
454
                "total_in_category": reporting['total_in_category'],
455
                "total_in_kgce": reporting['total_in_kgce'],
456
                "total_in_kgco2e": reporting['total_in_kgco2e'],
457
                "timestamps": [reporting['timestamps'],
458
                               reporting['timestamps'],
459
                               reporting['timestamps']],
460
                "values": [reporting['values_in_category'],
461
                           reporting['values_in_kgce'],
462
                           reporting['values_in_kgco2e']],
463
            },
464
            "base_period": {
465
                "total_in_category": base['total_in_category'],
466
                "total_in_kgce": base['total_in_kgce'],
467
                "total_in_kgco2e": base['total_in_kgco2e'],
468
                "timestamps": [base['timestamps'],
469
                               base['timestamps'],
470
                               base['timestamps']],
471
                "values": [base['values_in_category'],
472
                           base['values_in_kgce'],
473
                           base['values_in_kgco2e']],
474
            },
475
            "parameters": {
476
                "names": parameters_data['names'],
477
                "timestamps": parameters_data['timestamps'],
478
                "values": parameters_data['values']
479
            },
480
        }
481
482
        resp.body = json.dumps(result)
483