Passed
Push — master ( c215ca...be0b67 )
by Guangyu
13:48 queued 12s
created

reports.offlinemeterenergy   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 364
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 63
eloc 275
dl 0
loc 364
rs 3.36
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Reporting.__init__() 0 4 1
A Reporting.on_options() 0 3 1
F Reporting.on_get() 0 331 61

How to fix   Complexity   

Complexity

Complex classes like reports.offlinemeterenergy 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
from datetime import datetime, timedelta, timezone
2
from decimal import Decimal
3
4
import gettext
5
import falcon
6
import mysql.connector
7
import simplejson as json
8
9
import config
10
import excelexporters.offlinemeterenergy
11
from core import utilities
12
13
14
class Reporting:
15
    @staticmethod
16
    def __init__():
17
        """Initializes Class"""
18
        pass
19
20
    @staticmethod
21
    def on_options(req, resp):
22
        resp.status = falcon.HTTP_200
23
24
    ####################################################################################################################
25
    # PROCEDURES
26
    # Step 1: valid parameters
27
    # Step 2: query the offline meter and energy category
28
    # Step 3: query base period energy consumption
29
    # Step 4: query reporting period energy consumption
30
    # Step 5: query tariff data
31
    # Step 6: construct the report
32
    ####################################################################################################################
33
    @staticmethod
34
    def on_get(req, resp):
35
        print(req.params)
36
        offline_meter_id = req.params.get('offlinemeterid')
37
        period_type = req.params.get('periodtype')
38
        base_period_start_datetime_local = req.params.get('baseperiodstartdatetime')
39
        base_period_end_datetime_local = req.params.get('baseperiodenddatetime')
40
        reporting_period_start_datetime = req.params.get('reportingperiodstartdatetime')
41
        reporting_period_end_datetime = req.params.get('reportingperiodenddatetime')
42
        language = req.params.get('language')
43
        quick_mode = req.params.get('quickmode')
44
45
        ################################################################################################################
46
        # Step 1: valid parameters
47
        ################################################################################################################
48
        if offline_meter_id is None:
49
            raise falcon.HTTPError(falcon.HTTP_400,
50
                                   title='API.BAD_REQUEST',
51
                                   description='API.INVALID_OFFLINE_METER_ID')
52
        else:
53
            offline_meter_id = str.strip(offline_meter_id)
54
            if not offline_meter_id.isdigit() or int(offline_meter_id) <= 0:
55
                raise falcon.HTTPError(falcon.HTTP_400,
56
                                       title='API.BAD_REQUEST',
57
                                       description='API.INVALID_OFFLINE_METER_ID')
58
59
        if period_type is None:
60
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
61
        else:
62
            period_type = str.strip(period_type)
63
            if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']:
64
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
65
66
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
67
        if config.utc_offset[0] == '-':
68
            timezone_offset = -timezone_offset
69
70
        base_start_datetime_utc = None
71
        if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0:
72
            base_period_start_datetime_local = str.strip(base_period_start_datetime_local)
73
            try:
74
                base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S')
75
            except ValueError:
76
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
77
                                       description="API.INVALID_BASE_PERIOD_START_DATETIME")
78
            base_start_datetime_utc = \
79
                base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
80
            # nomalize the start datetime
81
            if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30:
82
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
83
            else:
84
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
85
86
        base_end_datetime_utc = None
87
        if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0:
88
            base_period_end_datetime_local = str.strip(base_period_end_datetime_local)
89
            try:
90
                base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S')
91
            except ValueError:
92
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
93
                                       description="API.INVALID_BASE_PERIOD_END_DATETIME")
94
            base_end_datetime_utc = \
95
                base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
96
97
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
98
                base_start_datetime_utc >= base_end_datetime_utc:
99
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
100
                                   description='API.INVALID_BASE_PERIOD_END_DATETIME')
101
102
        if reporting_period_start_datetime is None:
103
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
104
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
105
        else:
106
            reporting_period_start_datetime = str.strip(reporting_period_start_datetime)
107
            try:
108
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime, '%Y-%m-%dT%H:%M:%S')
109
            except ValueError:
110
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
111
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
112
            reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \
113
                timedelta(minutes=timezone_offset)
114
115
        if reporting_period_end_datetime is None:
116
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
117
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
118
        else:
119
            reporting_period_end_datetime = str.strip(reporting_period_end_datetime)
120
            try:
121
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime, '%Y-%m-%dT%H:%M:%S')
122
            except ValueError:
123
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
124
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
125
            reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \
126
                timedelta(minutes=timezone_offset)
127
128
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
129
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
130
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
131
132
        # if turn quick mode on, do not return parameters data and excel file
133
        is_quick_mode = False
134
        if quick_mode is not None and \
135
                len(str.strip(quick_mode)) > 0 and \
136
                str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'):
137
            is_quick_mode = True
138
139
        locale_path = './i18n/'
140
        if language == 'zh_CN':
141
            trans = gettext.translation('myems', locale_path, languages=['zh_CN'])
142
        elif language == 'de':
143
            trans = gettext.translation('myems', locale_path, languages=['de'])
144
        elif language == 'en':
145
            trans = gettext.translation('myems', locale_path, languages=['en'])
146
        else:
147
            trans = gettext.translation('myems', locale_path, languages=['en'])
148
        trans.install()
149
        _ = trans.gettext
150
151
        ################################################################################################################
152
        # Step 2: query the offline meter and energy category
153
        ################################################################################################################
154
        cnx_system = mysql.connector.connect(**config.myems_system_db)
155
        cursor_system = cnx_system.cursor()
156
157
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
158
        cursor_energy = cnx_energy.cursor()
159
160
        cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, "
161
                              "        ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e "
162
                              " FROM tbl_offline_meters m, tbl_energy_categories ec "
163
                              " WHERE m.id = %s AND m.energy_category_id = ec.id ", (offline_meter_id,))
164
        row_offline_meter = cursor_system.fetchone()
165
        if row_offline_meter is None:
166
            if cursor_system:
167
                cursor_system.close()
168
            if cnx_system:
169
                cnx_system.close()
170
171
            if cursor_energy:
172
                cursor_energy.close()
173
            if cnx_energy:
174
                cnx_energy.close()
175
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND')
176
177
        offline_meter = dict()
178
        offline_meter['id'] = row_offline_meter[0]
179
        offline_meter['name'] = row_offline_meter[1]
180
        offline_meter['cost_center_id'] = row_offline_meter[2]
181
        offline_meter['energy_category_id'] = row_offline_meter[3]
182
        offline_meter['energy_category_name'] = row_offline_meter[4]
183
        offline_meter['unit_of_measure'] = row_offline_meter[5]
184
        offline_meter['kgce'] = row_offline_meter[6]
185
        offline_meter['kgco2e'] = row_offline_meter[7]
186
187
        ################################################################################################################
188
        # Step 3: query base period energy consumption
189
        ################################################################################################################
190
        query = (" SELECT start_datetime_utc, actual_value "
191
                 " FROM tbl_offline_meter_hourly "
192
                 " WHERE offline_meter_id = %s "
193
                 " AND start_datetime_utc >= %s "
194
                 " AND start_datetime_utc < %s "
195
                 " ORDER BY start_datetime_utc ")
196
        cursor_energy.execute(query, (offline_meter['id'], base_start_datetime_utc, base_end_datetime_utc))
197
        rows_offline_meter_hourly = cursor_energy.fetchall()
198
199
        rows_offline_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_offline_meter_hourly,
200
                                                                                    base_start_datetime_utc,
201
                                                                                    base_end_datetime_utc,
202
                                                                                    period_type)
203
        base = dict()
204
        base['timestamps'] = list()
205
        base['values'] = list()
206
        base['total_in_category'] = Decimal(0.0)
207
        base['total_in_kgce'] = Decimal(0.0)
208
        base['total_in_kgco2e'] = Decimal(0.0)
209
210
        for row_offline_meter_periodically in rows_offline_meter_periodically:
211
            current_datetime_local = row_offline_meter_periodically[0].replace(tzinfo=timezone.utc) + \
212
                                     timedelta(minutes=timezone_offset)
213
            if period_type == 'hourly':
214
                current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
215
            elif period_type == 'daily':
216
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
217
            elif period_type == 'weekly':
218
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
219
            elif period_type == 'monthly':
220
                current_datetime = current_datetime_local.strftime('%Y-%m')
221
            elif period_type == 'yearly':
222
                current_datetime = current_datetime_local.strftime('%Y')
223
224
            actual_value = Decimal(0.0) if row_offline_meter_periodically[1] is None \
225
                else row_offline_meter_periodically[1]
226
            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...
227
            base['values'].append(actual_value)
228
            base['total_in_category'] += actual_value
229
            base['total_in_kgce'] += actual_value * offline_meter['kgce']
230
            base['total_in_kgco2e'] += actual_value * offline_meter['kgco2e']
231
        ################################################################################################################
232
        # Step 4: query reporting period energy consumption
233
        ################################################################################################################
234
235
        query = (" SELECT start_datetime_utc, actual_value "
236
                 " FROM tbl_offline_meter_hourly "
237
                 " WHERE offline_meter_id = %s "
238
                 " AND start_datetime_utc >= %s "
239
                 " AND start_datetime_utc < %s "
240
                 " ORDER BY start_datetime_utc ")
241
        cursor_energy.execute(query, (offline_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
242
        rows_offline_meter_hourly = cursor_energy.fetchall()
243
244
        rows_offline_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_offline_meter_hourly,
245
                                                                                    reporting_start_datetime_utc,
246
                                                                                    reporting_end_datetime_utc,
247
                                                                                    period_type)
248
        reporting = dict()
249
        reporting['timestamps'] = list()
250
        reporting['values'] = list()
251
        reporting['rates'] = list()
252
        reporting['total_in_category'] = Decimal(0.0)
253
        reporting['total_in_kgce'] = Decimal(0.0)
254
        reporting['total_in_kgco2e'] = Decimal(0.0)
255
256
        for row_offline_meter_periodically in rows_offline_meter_periodically:
257
            current_datetime_local = row_offline_meter_periodically[0].replace(tzinfo=timezone.utc) + \
258
                                     timedelta(minutes=timezone_offset)
259
            if period_type == 'hourly':
260
                current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
261
            elif period_type == 'daily':
262
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
263
            elif period_type == 'weekly':
264
                current_datetime = current_datetime_local.strftime('%Y-%m-%d')
265
            elif period_type == 'monthly':
266
                current_datetime = current_datetime_local.strftime('%Y-%m')
267
            elif period_type == 'yearly':
268
                current_datetime = current_datetime_local.strftime('%Y')
269
270
            actual_value = Decimal(0.0) if row_offline_meter_periodically[1] is None \
271
                else row_offline_meter_periodically[1]
272
273
            reporting['timestamps'].append(current_datetime)
274
            reporting['values'].append(actual_value)
275
            reporting['total_in_category'] += actual_value
276
            reporting['total_in_kgce'] += actual_value * offline_meter['kgce']
277
            reporting['total_in_kgco2e'] += actual_value * offline_meter['kgco2e']
278
279
        for index, value in enumerate(reporting['values']):
280
            if index < len(base['values']) and base['values'][index] != 0 and value != 0:
281
                reporting['rates'].append((value - base['values'][index]) / base['values'][index])
282
            else:
283
                reporting['rates'].append(None)
284
285
        ################################################################################################################
286
        # Step 5: query tariff data
287
        ################################################################################################################
288
        parameters_data = dict()
289
        parameters_data['names'] = list()
290
        parameters_data['timestamps'] = list()
291
        parameters_data['values'] = list()
292
        if config.is_tariff_appended and not is_quick_mode:
293
            tariff_dict = utilities.get_energy_category_tariffs(offline_meter['cost_center_id'],
294
                                                                offline_meter['energy_category_id'],
295
                                                                reporting_start_datetime_utc,
296
                                                                reporting_end_datetime_utc)
297
            tariff_timestamp_list = list()
298
            tariff_value_list = list()
299
            for k, v in tariff_dict.items():
300
                # convert k from utc to local
301
                k = k + timedelta(minutes=timezone_offset)
302
                tariff_timestamp_list.append(k.isoformat()[0:19])
303
                tariff_value_list.append(v)
304
305
            parameters_data['names'].append(_('Tariff') + '-' + offline_meter['energy_category_name'])
306
            parameters_data['timestamps'].append(tariff_timestamp_list)
307
            parameters_data['values'].append(tariff_value_list)
308
309
        ################################################################################################################
310
        # Step 6: construct the report
311
        ################################################################################################################
312
        if cursor_system:
313
            cursor_system.close()
314
        if cnx_system:
315
            cnx_system.close()
316
317
        if cursor_energy:
318
            cursor_energy.close()
319
        if cnx_energy:
320
            cnx_energy.close()
321
322
        result = {"offline_meter": {
323
            "cost_center_id": offline_meter['cost_center_id'],
324
            "energy_category_id": offline_meter['energy_category_id'],
325
            "energy_category_name": offline_meter['energy_category_name'],
326
            "unit_of_measure": offline_meter['unit_of_measure'],
327
            "kgce": offline_meter['kgce'],
328
            "kgco2e": offline_meter['kgco2e'],
329
        }, "base_period": {
330
            "total_in_category": base['total_in_category'],
331
            "total_in_kgce": base['total_in_kgce'],
332
            "total_in_kgco2e": base['total_in_kgco2e'],
333
            "timestamps": base['timestamps'],
334
            "values": base['values'],
335
        }, "reporting_period": {
336
            "increment_rate":
337
                (reporting['total_in_category'] - base['total_in_category']) / base['total_in_category']
338
                if base['total_in_category'] > 0 else None,
339
            "total_in_category": reporting['total_in_category'],
340
            "total_in_kgce": reporting['total_in_kgce'],
341
            "total_in_kgco2e": reporting['total_in_kgco2e'],
342
            "timestamps": reporting['timestamps'],
343
            "values": reporting['values'],
344
            "rates": reporting['rates'],
345
        }, "parameters": {
346
            "names": parameters_data['names'],
347
            "timestamps": parameters_data['timestamps'],
348
            "values": parameters_data['values']
349
        }, 'excel_bytes_base64': None}
350
351
        # export result to Excel file and then encode the file to base64 string
352
        if not is_quick_mode:
353
            result['excel_bytes_base64'] = \
354
                excelexporters.offlinemeterenergy.export(result,
355
                                                         offline_meter['name'],
356
                                                         base_period_start_datetime_local,
357
                                                         base_period_end_datetime_local,
358
                                                         reporting_period_start_datetime,
359
                                                         reporting_period_end_datetime,
360
                                                         period_type,
361
                                                         language)
362
363
        resp.text = json.dumps(result)
364