reports.metersubmetersbalance.Reporting.on_get()   F
last analyzed

Complexity

Conditions 50

Size

Total Lines 312
Code Lines 215

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 50
eloc 215
nop 2
dl 0
loc 312
rs 0
c 0
b 0
f 0

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.metersubmetersbalance.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 falcon
2
import simplejson as json
3
import mysql.connector
4
import config
5
from datetime import datetime, timedelta, timezone
6
from core import utilities
7
from decimal import Decimal
8
import excelexporters.metersubmetersbalance
9
10
11
class Reporting:
12
    @staticmethod
13
    def __init__():
14
        pass
15
16
    @staticmethod
17
    def on_options(req, resp):
18
        resp.status = falcon.HTTP_200
19
20
    ####################################################################################################################
21
    # PROCEDURES
22
    # Step 1: valid parameters
23
    # Step 2: query the master meter and energy category
24
    # Step 3: query associated submeters
25
    # Step 4: query reporting period master meter energy consumption
26
    # Step 5: query reporting period submeters energy consumption
27
    # Step 6: calculate reporting period difference between master meter and submeters
28
    # Step 7: query submeter values as parameter data
29
    # Step 8: construct the report
30
    ####################################################################################################################
31
    @staticmethod
32
    def on_get(req, resp):
33
        print(req.params)
34
        meter_id = req.params.get('meterid')
35
        period_type = req.params.get('periodtype')
36
        reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime')
37
        reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime')
38
39
        ################################################################################################################
40
        # Step 1: valid parameters
41
        ################################################################################################################
42
        if meter_id is None:
43
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID')
44
        else:
45
            meter_id = str.strip(meter_id)
46
            if not meter_id.isdigit() or int(meter_id) <= 0:
47
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID')
48
49
        if period_type is None:
50
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
51
        else:
52
            period_type = str.strip(period_type)
53
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
54
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
55
56
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
57
        if config.utc_offset[0] == '-':
58
            timezone_offset = -timezone_offset
59
60
        if reporting_period_start_datetime_local is None:
61
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
62
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
63
        else:
64
            reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local)
65
            try:
66
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local,
67
                                                                 '%Y-%m-%dT%H:%M:%S')
68
            except ValueError:
69
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
70
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
71
            reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \
72
                timedelta(minutes=timezone_offset)
73
74
        if reporting_period_end_datetime_local is None:
75
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
76
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
77
        else:
78
            reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local)
79
            try:
80
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local,
81
                                                               '%Y-%m-%dT%H:%M:%S')
82
            except ValueError:
83
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
84
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
85
            reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \
86
                timedelta(minutes=timezone_offset)
87
88
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
89
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
90
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
91
92
        ################################################################################################################
93
        # Step 2: query the meter and energy category
94
        ################################################################################################################
95
        cnx_system = mysql.connector.connect(**config.myems_system_db)
96
        cursor_system = cnx_system.cursor()
97
98
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
99
        cursor_energy = cnx_energy.cursor()
100
101
        cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, "
102
                              "        ec.name, ec.unit_of_measure "
103
                              " FROM tbl_meters m, tbl_energy_categories ec "
104
                              " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id,))
105
        row_meter = cursor_system.fetchone()
106
        if row_meter is None:
107
            if cursor_system:
108
                cursor_system.close()
109
            if cnx_system:
110
                cnx_system.disconnect()
111
112
            if cursor_energy:
113
                cursor_energy.close()
114
            if cnx_energy:
115
                cnx_energy.disconnect()
116
117
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND')
118
119
        master_meter = dict()
120
        master_meter['id'] = row_meter[0]
121
        master_meter['name'] = row_meter[1]
122
        master_meter['cost_center_id'] = row_meter[2]
123
        master_meter['energy_category_id'] = row_meter[3]
124
        master_meter['energy_category_name'] = row_meter[4]
125
        master_meter['unit_of_measure'] = row_meter[5]
126
127
        ################################################################################################################
128
        # Step 3: query associated submeters
129
        ################################################################################################################
130
        submeter_list = list()
131
        submeter_id_set = set()
132
133
        cursor_system.execute(" SELECT id, name, energy_category_id "
134
                              " FROM tbl_meters "
135
                              " WHERE master_meter_id = %s ",
136
                              (master_meter['id'],))
137
        rows_meters = cursor_system.fetchall()
138
139
        if rows_meters is not None and len(rows_meters) > 0:
140
            for row in rows_meters:
141
                submeter_list.append({"id": row[0],
142
                                      "name": row[1],
143
                                      "energy_category_id": row[2]})
144
                submeter_id_set.add(row[0])
145
146
        ################################################################################################################
147
        # Step 4: query reporting period master meter energy consumption
148
        ################################################################################################################
149
        reporting = dict()
150
        reporting['master_meter_total_in_category'] = Decimal(0.0)
151
        reporting['submeters_total_in_category'] = Decimal(0.0)
152
        reporting['total_difference_in_category'] = Decimal(0.0)
153
        reporting['percentage_difference'] = Decimal(0.0)
154
        reporting['timestamps'] = list()
155
        reporting['master_meter_values'] = list()
156
        reporting['submeters_values'] = list()
157
        reporting['difference_values'] = list()
158
159
        parameters_data = dict()
160
        parameters_data['names'] = list()
161
        parameters_data['timestamps'] = list()
162
        parameters_data['values'] = list()
163
164
        query = (" SELECT start_datetime_utc, actual_value "
165
                 " FROM tbl_meter_hourly "
166
                 " WHERE meter_id = %s "
167
                 " AND start_datetime_utc >= %s "
168
                 " AND start_datetime_utc < %s "
169
                 " ORDER BY start_datetime_utc ")
170
        cursor_energy.execute(query, (master_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
171
        rows_meter_hourly = cursor_energy.fetchall()
172
173
        rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
174
                                                                            reporting_start_datetime_utc,
175
                                                                            reporting_end_datetime_utc,
176
                                                                            period_type)
177
178
        for row_meter_periodically in rows_meter_periodically:
179
            current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \
180
                                     timedelta(minutes=timezone_offset)
181
            if period_type == 'hourly':
182
                current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
183
            elif period_type == 'daily':
184
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
185
            elif period_type == 'monthly':
186
                current_datetime = current_datetime_local.strftime('%Y-%m')
187
            elif period_type == 'yearly':
188
                current_datetime = current_datetime_local.strftime('%Y')
189
190
            actual_value = Decimal(0.0) if row_meter_periodically[1] is None else row_meter_periodically[1]
191
192
            reporting['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...
193
            reporting['master_meter_values'].append(actual_value)
194
            reporting['master_meter_total_in_category'] += actual_value
195
196
        # add master meter values to parameter data
197
        parameters_data['names'].append(master_meter['name'])
198
        parameters_data['timestamps'].append(reporting['timestamps'])
199
        parameters_data['values'].append(reporting['master_meter_values'])
200
201
        ################################################################################################################
202
        # Step 5: query reporting period submeters energy consumption
203
        ################################################################################################################
204
        if len(submeter_list) > 0:
205
            query = (" SELECT start_datetime_utc, SUM(actual_value) "
206
                     " FROM tbl_meter_hourly "
207
                     " WHERE meter_id IN ( " + ', '.join(map(str, submeter_id_set)) + ") "
208
                     " AND start_datetime_utc >= %s "
209
                     " AND start_datetime_utc < %s "
210
                     " GROUP BY start_datetime_utc "
211
                     " ORDER BY start_datetime_utc ")
212
            cursor_energy.execute(query, (reporting_start_datetime_utc, reporting_end_datetime_utc))
213
            rows_submeters_hourly = cursor_energy.fetchall()
214
215
            rows_submeters_periodically = utilities.aggregate_hourly_data_by_period(rows_submeters_hourly,
216
                                                                                    reporting_start_datetime_utc,
217
                                                                                    reporting_end_datetime_utc,
218
                                                                                    period_type)
219
220
            for row_submeters_periodically in rows_submeters_periodically:
221
                current_datetime_local = row_submeters_periodically[0].replace(tzinfo=timezone.utc) + \
222
                                         timedelta(minutes=timezone_offset)
223
                if period_type == 'hourly':
224
                    current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
225
                elif period_type == 'daily':
226
                    current_datetime = current_datetime_local.strftime('%Y-%m-%d')
227
                elif period_type == 'monthly':
228
                    current_datetime = current_datetime_local.strftime('%Y-%m')
229
                elif period_type == 'yearly':
230
                    current_datetime = current_datetime_local.strftime('%Y')
231
232
                actual_value = Decimal(0.0) if row_submeters_periodically[1] is None else row_submeters_periodically[1]
233
234
                reporting['submeters_values'].append(actual_value)
235
                reporting['submeters_total_in_category'] += actual_value
236
237
        ################################################################################################################
238
        # Step 6: calculate reporting period difference between master meter and submeters
239
        ################################################################################################################
240
        if len(submeter_list) > 0:
241
            for i in range(len(reporting['timestamps'])):
242
                reporting['difference_values'].append(reporting['master_meter_values'][i] -
243
                                                      reporting['submeters_values'][i])
244
        else:
245
            for i in range(len(reporting['timestamps'])):
246
                reporting['difference_values'].append(reporting['master_meter_values'][i])
247
248
        reporting['total_difference_in_category'] = \
249
            reporting['master_meter_total_in_category'] - reporting['submeters_total_in_category']
250
251
        if abs(reporting['master_meter_total_in_category']) > Decimal(0.0):
252
            reporting['percentage_difference'] = \
253
                reporting['total_difference_in_category'] / reporting['master_meter_total_in_category']
254
        elif abs(reporting['master_meter_total_in_category']) == Decimal(0.0) and \
255
                abs(reporting['submeters_total_in_category']) > Decimal(0.0):
256
            reporting['percentage_difference'] = Decimal(-1.0)
257
258
        ################################################################################################################
259
        # Step 7: query submeter values as parameter data
260
        ################################################################################################################
261
        for submeter in submeter_list:
262
            submeter_timestamps = list()
263
            submeter_values = list()
264
265
            query = (" SELECT start_datetime_utc, actual_value "
266
                     " FROM tbl_meter_hourly "
267
                     " WHERE meter_id = %s "
268
                     " AND start_datetime_utc >= %s "
269
                     " AND start_datetime_utc < %s "
270
                     " ORDER BY start_datetime_utc ")
271
            cursor_energy.execute(query, (submeter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
272
            rows_meter_hourly = cursor_energy.fetchall()
273
274
            rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
275
                                                                                reporting_start_datetime_utc,
276
                                                                                reporting_end_datetime_utc,
277
                                                                                period_type)
278
279
            for row_meter_periodically in rows_meter_periodically:
280
                current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \
281
                                         timedelta(minutes=timezone_offset)
282
                if period_type == 'hourly':
283
                    current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
284
                elif period_type == 'daily':
285
                    current_datetime = current_datetime_local.strftime('%Y-%m-%d')
286
                elif period_type == 'monthly':
287
                    current_datetime = current_datetime_local.strftime('%Y-%m')
288
                elif period_type == 'yearly':
289
                    current_datetime = current_datetime_local.strftime('%Y')
290
291
                actual_value = Decimal(0.0) if row_meter_periodically[1] is None else row_meter_periodically[1]
292
293
                submeter_timestamps.append(current_datetime)
294
                submeter_values.append(actual_value)
295
296
            parameters_data['names'].append(submeter['name'])
297
            parameters_data['timestamps'].append(submeter_timestamps)
298
            parameters_data['values'].append(submeter_values)
299
300
        ################################################################################################################
301
        # Step 8: construct the report
302
        ################################################################################################################
303
        if cursor_system:
304
            cursor_system.close()
305
        if cnx_system:
306
            cnx_system.disconnect()
307
308
        if cursor_energy:
309
            cursor_energy.close()
310
        if cnx_energy:
311
            cnx_energy.disconnect()
312
313
        result = {
314
            "meter": {
315
                "cost_center_id": master_meter['cost_center_id'],
316
                "energy_category_id": master_meter['energy_category_id'],
317
                "energy_category_name": master_meter['energy_category_name'],
318
                "unit_of_measure": master_meter['unit_of_measure'],
319
            },
320
            "reporting_period": {
321
                "master_meter_consumption_in_category": reporting['master_meter_total_in_category'],
322
                "submeters_consumption_in_category": reporting['submeters_total_in_category'],
323
                "difference_in_category": reporting['total_difference_in_category'],
324
                "percentage_difference": reporting['percentage_difference'],
325
                "timestamps": reporting['timestamps'],
326
                "difference_values": reporting['difference_values'],
327
            },
328
            "parameters": {
329
                "names": parameters_data['names'],
330
                "timestamps": parameters_data['timestamps'],
331
                "values": parameters_data['values']
332
            },
333
        }
334
335
        # export result to Excel file and then encode the file to base64 string
336
        result['excel_bytes_base64'] = excelexporters.metersubmetersbalance.export(result,
337
                                              master_meter['name'],
338
                                              reporting_period_start_datetime_local,
339
                                              reporting_period_end_datetime_local,
340
                                              period_type)
341
342
        resp.body = json.dumps(result)
343