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

meterenergy   F

Complexity

Total Complexity 66

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 66
eloc 307
dl 0
loc 419
rs 3.12
c 0
b 0
f 0

3 Methods

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

How to fix   Complexity   

Complexity

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