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

Complexity

Conditions 151

Size

Total Lines 741
Code Lines 564

Duplication

Lines 741
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 564
dl 741
loc 741
rs 0
c 0
b 0
f 0
cc 151
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

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

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

1
import re
2
from datetime import datetime, timedelta, timezone
3
from decimal import Decimal
4
import falcon
5
import mysql.connector
6
import simplejson as json
7
import config
8
import excelexporters.tenantplan
9
from core import utilities
10
from core.useractivity import access_control, api_key_control
11
12
13 View Code Duplication
class Reporting:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    @staticmethod
15
    def __init__():
16
        """"Initializes Reporting"""
17
        pass
18
19
    @staticmethod
20
    def on_options(req, resp):
21
        resp.status = falcon.HTTP_200
22
23
    ####################################################################################################################
24
    # PROCEDURES
25
    # Step 1: valid parameters
26
    # Step 2: query the tenant
27
    # Step 3: query energy categories
28
    # Step 4: query associated sensors
29
    # Step 5: query associated points
30
    # Step 6: query base period energy saving
31
    # Step 7: query reporting period energy saving
32
    # Step 8: query tariff data
33
    # Step 9: query associated sensors and points data
34
    # Step 10: construct the report
35
    ####################################################################################################################
36
    @staticmethod
37
    def on_get(req, resp):
38
        if 'API-KEY' not in req.headers or \
39
                not isinstance(req.headers['API-KEY'], str) or \
40
                len(str.strip(req.headers['API-KEY'])) == 0:
41
            access_control(req)
42
        else:
43
            api_key_control(req)
44
        print(req.params)
45
        tenant_id = req.params.get('tenantid')
46
        tenant_uuid = req.params.get('tenantuuid')
47
        period_type = req.params.get('periodtype')
48
        base_period_start_datetime_local = req.params.get('baseperiodstartdatetime')
49
        base_period_end_datetime_local = req.params.get('baseperiodenddatetime')
50
        reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime')
51
        reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime')
52
        language = req.params.get('language')
53
        quick_mode = req.params.get('quickmode')
54
55
        ################################################################################################################
56
        # Step 1: valid parameters
57
        ################################################################################################################
58
        if tenant_id is None and tenant_uuid is None:
59
            raise falcon.HTTPError(status=falcon.HTTP_400,
60
                                   title='API.BAD_REQUEST',
61
                                   description='API.INVALID_TENANT_ID')
62
63
        if tenant_id is not None:
64
            tenant_id = str.strip(tenant_id)
65
            if not tenant_id.isdigit() or int(tenant_id) <= 0:
66
                raise falcon.HTTPError(status=falcon.HTTP_400,
67
                                       title='API.BAD_REQUEST',
68
                                       description='API.INVALID_TENANT_ID')
69
70
        if tenant_uuid is not None:
71
            regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I)
72
            match = regex.match(str.strip(tenant_uuid))
73
            if not bool(match):
74
                raise falcon.HTTPError(status=falcon.HTTP_400,
75
                                       title='API.BAD_REQUEST',
76
                                       description='API.INVALID_TENANT_UUID')
77
78
        if period_type is None:
79
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
80
                                   description='API.INVALID_PERIOD_TYPE')
81
        else:
82
            period_type = str.strip(period_type)
83
            if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']:
84
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
85
                                       description='API.INVALID_PERIOD_TYPE')
86
87
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
88
        if config.utc_offset[0] == '-':
89
            timezone_offset = -timezone_offset
90
91
        base_start_datetime_utc = None
92
        if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0:
93
            base_period_start_datetime_local = str.strip(base_period_start_datetime_local)
94
            try:
95
                base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S')
96
            except ValueError:
97
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
98
                                       description="API.INVALID_BASE_PERIOD_START_DATETIME")
99
            base_start_datetime_utc = \
100
                base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
101
            # nomalize the start datetime
102
            if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30:
103
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
104
            else:
105
                base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
106
107
        base_end_datetime_utc = None
108
        if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0:
109
            base_period_end_datetime_local = str.strip(base_period_end_datetime_local)
110
            try:
111
                base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S')
112
            except ValueError:
113
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
114
                                       description="API.INVALID_BASE_PERIOD_END_DATETIME")
115
            base_end_datetime_utc = \
116
                base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
117
118
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
119
                base_start_datetime_utc >= base_end_datetime_utc:
120
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
121
                                   description='API.INVALID_BASE_PERIOD_END_DATETIME')
122
123
        if reporting_period_start_datetime_local is None:
124
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
125
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
126
        else:
127
            reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local)
128
            try:
129
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local,
130
                                                                 '%Y-%m-%dT%H:%M:%S')
131
            except ValueError:
132
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
133
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
134
            reporting_start_datetime_utc = \
135
                reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset)
136
            # nomalize the start datetime
137
            if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30:
138
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0)
139
            else:
140
                reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0)
141
142
        if reporting_period_end_datetime_local is None:
143
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
144
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
145
        else:
146
            reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local)
147
            try:
148
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local,
149
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
150
                                             timedelta(minutes=timezone_offset)
151
            except ValueError:
152
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
153
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
154
155
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
156
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
157
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
158
159
        # if turn quick mode on, do not return parameters data and excel file
160
        is_quick_mode = False
161
        if quick_mode is not None and \
162
                len(str.strip(quick_mode)) > 0 and \
163
                str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'):
164
            is_quick_mode = True
165
166
        trans = utilities.get_translation(language)
167
        trans.install()
168
        _ = trans.gettext
169
170
        ################################################################################################################
171
        # Step 2: query the tenant
172
        ################################################################################################################
173
        cnx_system = mysql.connector.connect(**config.myems_system_db)
174
        cursor_system = cnx_system.cursor()
175
176
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
177
        cursor_energy = cnx_energy.cursor()
178
179
        cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db)
180
        cursor_energy_plan = cnx_energy_plan.cursor()
181
182
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
183
        cursor_historical = cnx_historical.cursor()
184
185
        if tenant_id is not None:
186
            cursor_system.execute(" SELECT id, name, area, cost_center_id "
187
                                  " FROM tbl_tenants "
188
                                  " WHERE id = %s ", (tenant_id,))
189
            row_tenant = cursor_system.fetchone()
190
        elif tenant_uuid is not None:
191
            cursor_system.execute(" SELECT id, name, area, cost_center_id "
192
                                  " FROM tbl_tenants "
193
                                  " WHERE uuid = %s ", (tenant_uuid,))
194
            row_tenant = cursor_system.fetchone()
195
196
        if row_tenant is None:
0 ignored issues
show
introduced by
The variable row_tenant does not seem to be defined for all execution paths.
Loading history...
197
            if cursor_system:
198
                cursor_system.close()
199
            if cnx_system:
200
                cnx_system.close()
201
202
            if cursor_energy:
203
                cursor_energy.close()
204
            if cnx_energy:
205
                cnx_energy.close()
206
207
            if cursor_energy_plan:
208
                cursor_energy_plan.close()
209
            if cnx_energy_plan:
210
                cnx_energy_plan.close()
211
212
            if cursor_historical:
213
                cursor_historical.close()
214
            if cnx_historical:
215
                cnx_historical.close()
216
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND')
217
218
        tenant = dict()
219
        tenant['id'] = row_tenant[0]
220
        tenant['name'] = row_tenant[1]
221
        tenant['area'] = row_tenant[2]
222
        tenant['cost_center_id'] = row_tenant[3]
223
224
        ################################################################################################################
225
        # Step 3: query energy categories
226
        ################################################################################################################
227
        energy_category_set = set()
228
        # query energy categories in base period
229
        cursor_energy.execute(" SELECT DISTINCT(energy_category_id) "
230
                              " FROM tbl_tenant_input_category_hourly "
231
                              " WHERE tenant_id = %s "
232
                              "     AND start_datetime_utc >= %s "
233
                              "     AND start_datetime_utc < %s ",
234
                              (tenant['id'], base_start_datetime_utc, base_end_datetime_utc))
235
        rows_energy_categories = cursor_energy.fetchall()
236
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
237
            for row_energy_category in rows_energy_categories:
238
                energy_category_set.add(row_energy_category[0])
239
240
        # query energy categories in reporting period
241
        cursor_energy.execute(" SELECT DISTINCT(energy_category_id) "
242
                              " FROM tbl_tenant_input_category_hourly "
243
                              " WHERE tenant_id = %s "
244
                              "     AND start_datetime_utc >= %s "
245
                              "     AND start_datetime_utc < %s ",
246
                              (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
247
        rows_energy_categories = cursor_energy.fetchall()
248
        if rows_energy_categories is not None and len(rows_energy_categories) > 0:
249
            for row_energy_category in rows_energy_categories:
250
                energy_category_set.add(row_energy_category[0])
251
252
        # query all energy categories in base period and reporting period
253
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
254
                              " FROM tbl_energy_categories "
255
                              " ORDER BY id ", )
256
        rows_energy_categories = cursor_system.fetchall()
257
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
258
            if cursor_system:
259
                cursor_system.close()
260
            if cnx_system:
261
                cnx_system.close()
262
263
            if cursor_energy:
264
                cursor_energy.close()
265
            if cnx_energy:
266
                cnx_energy.close()
267
268
            if cursor_energy_plan:
269
                cursor_energy_plan.close()
270
            if cnx_energy_plan:
271
                cnx_energy_plan.close()
272
273
            if cursor_historical:
274
                cursor_historical.close()
275
            if cnx_historical:
276
                cnx_historical.close()
277
            raise falcon.HTTPError(status=falcon.HTTP_404,
278
                                   title='API.NOT_FOUND',
279
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
280
        energy_category_dict = dict()
281
        for row_energy_category in rows_energy_categories:
282
            if row_energy_category[0] in energy_category_set:
283
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
284
                                                                "unit_of_measure": row_energy_category[2],
285
                                                                "kgce": row_energy_category[3],
286
                                                                "kgco2e": row_energy_category[4]}
287
288
        ################################################################################################################
289
        # Step 4: query associated sensors
290
        ################################################################################################################
291
        point_list = list()
292
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
293
                              " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, "
294
                              "      tbl_points p, tbl_sensors_points sp "
295
                              " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id "
296
                              "       AND s.id = sp.sensor_id AND sp.point_id = p.id "
297
                              " ORDER BY p.id ", (tenant['id'],))
298
        rows_points = cursor_system.fetchall()
299
        if rows_points is not None and len(rows_points) > 0:
300
            for row in rows_points:
301
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
302
303
        ################################################################################################################
304
        # Step 5: query associated points
305
        ################################################################################################################
306
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
307
                              " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p "
308
                              " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id "
309
                              " ORDER BY p.id ", (tenant['id'],))
310
        rows_points = cursor_system.fetchall()
311
        if rows_points is not None and len(rows_points) > 0:
312
            for row in rows_points:
313
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
314
315
        ################################################################################################################
316
        # Step 6: query base period energy saving
317
        ################################################################################################################
318
        base = dict()
319
        if energy_category_set is not None and len(energy_category_set) > 0:
320
            for energy_category_id in energy_category_set:
321
                kgce = energy_category_dict[energy_category_id]['kgce']
322
                kgco2e = energy_category_dict[energy_category_id]['kgco2e']
323
324
                base[energy_category_id] = dict()
325
                base[energy_category_id]['timestamps'] = list()
326
                base[energy_category_id]['values_plan'] = list()
327
                base[energy_category_id]['values_actual'] = list()
328
                base[energy_category_id]['values_saving'] = list()
329
                base[energy_category_id]['subtotal_plan'] = Decimal(0.0)
330
                base[energy_category_id]['subtotal_actual'] = Decimal(0.0)
331
                base[energy_category_id]['subtotal_saving'] = Decimal(0.0)
332
                base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0)
333
                base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0)
334
                base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0)
335
                base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0)
336
                base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0)
337
                base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0)
338
                # query base period's energy plan
339
                cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value "
340
                                           " FROM tbl_tenant_input_category_hourly "
341
                                           " WHERE tenant_id = %s "
342
                                           "     AND energy_category_id = %s "
343
                                           "     AND start_datetime_utc >= %s "
344
                                           "     AND start_datetime_utc < %s "
345
                                           " ORDER BY start_datetime_utc ",
346
                                           (tenant['id'],
347
                                            energy_category_id,
348
                                            base_start_datetime_utc,
349
                                            base_end_datetime_utc))
350
                rows_tenant_hourly = cursor_energy_plan.fetchall()
351
352
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
353
                                                                                     base_start_datetime_utc,
354
                                                                                     base_end_datetime_utc,
355
                                                                                     period_type)
356
                for row_tenant_periodically in rows_tenant_periodically:
357
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
358
                                             timedelta(minutes=timezone_offset)
359
                    if period_type == 'hourly':
360
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
361
                    elif period_type == 'daily':
362
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
363
                    elif period_type == 'weekly':
364
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
365
                    elif period_type == 'monthly':
366
                        current_datetime = current_datetime_local.strftime('%Y-%m')
367
                    elif period_type == 'yearly':
368
                        current_datetime = current_datetime_local.strftime('%Y')
369
370
                    plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
371
                    base[energy_category_id]['timestamps'].append(current_datetime)
0 ignored issues
show
introduced by
The variable current_datetime does not seem to be defined for all execution paths.
Loading history...
372
                    base[energy_category_id]['values_plan'].append(plan_value)
373
                    base[energy_category_id]['subtotal_plan'] += plan_value
374
                    base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce
375
                    base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e
376
377
                # query base period's energy actual
378
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
379
                                      " FROM tbl_tenant_input_category_hourly "
380
                                      " WHERE tenant_id = %s "
381
                                      "     AND energy_category_id = %s "
382
                                      "     AND start_datetime_utc >= %s "
383
                                      "     AND start_datetime_utc < %s "
384
                                      " ORDER BY start_datetime_utc ",
385
                                      (tenant['id'],
386
                                       energy_category_id,
387
                                       base_start_datetime_utc,
388
                                       base_end_datetime_utc))
389
                rows_tenant_hourly = cursor_energy.fetchall()
390
391
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
392
                                                                                     base_start_datetime_utc,
393
                                                                                     base_end_datetime_utc,
394
                                                                                     period_type)
395
                for row_tenant_periodically in rows_tenant_periodically:
396
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
397
                                             timedelta(minutes=timezone_offset)
398
                    if period_type == 'hourly':
399
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
400
                    elif period_type == 'daily':
401
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
402
                    elif period_type == 'weekly':
403
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
404
                    elif period_type == 'monthly':
405
                        current_datetime = current_datetime_local.strftime('%Y-%m')
406
                    elif period_type == 'yearly':
407
                        current_datetime = current_datetime_local.strftime('%Y')
408
409
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
410
                    base[energy_category_id]['values_actual'].append(actual_value)
411
                    base[energy_category_id]['subtotal_actual'] += actual_value
412
                    base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce
413
                    base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e
414
415
                # calculate base period's energy saving
416
                for i in range(len(base[energy_category_id]['values_plan'])):
417
                    base[energy_category_id]['values_saving'].append(
418
                        base[energy_category_id]['values_plan'][i] -
419
                        base[energy_category_id]['values_actual'][i])
420
421
                base[energy_category_id]['subtotal_saving'] = \
422
                    base[energy_category_id]['subtotal_plan'] - \
423
                    base[energy_category_id]['subtotal_actual']
424
                base[energy_category_id]['subtotal_in_kgce_saving'] = \
425
                    base[energy_category_id]['subtotal_in_kgce_plan'] - \
426
                    base[energy_category_id]['subtotal_in_kgce_actual']
427
                base[energy_category_id]['subtotal_in_kgco2e_saving'] = \
428
                    base[energy_category_id]['subtotal_in_kgco2e_plan'] - \
429
                    base[energy_category_id]['subtotal_in_kgco2e_actual']
430
        ################################################################################################################
431
        # Step 7: query reporting period energy saving
432
        ################################################################################################################
433
        reporting = dict()
434
        if energy_category_set is not None and len(energy_category_set) > 0:
435
            for energy_category_id in energy_category_set:
436
                kgce = energy_category_dict[energy_category_id]['kgce']
437
                kgco2e = energy_category_dict[energy_category_id]['kgco2e']
438
439
                reporting[energy_category_id] = dict()
440
                reporting[energy_category_id]['timestamps'] = list()
441
                reporting[energy_category_id]['values_plan'] = list()
442
                reporting[energy_category_id]['values_actual'] = list()
443
                reporting[energy_category_id]['values_saving'] = list()
444
                reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0)
445
                reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0)
446
                reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0)
447
                reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0)
448
                reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0)
449
                reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0)
450
                reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0)
451
                reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0)
452
                reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0)
453
                # query reporting period's energy plan
454
                cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value "
455
                                           " FROM tbl_tenant_input_category_hourly "
456
                                           " WHERE tenant_id = %s "
457
                                           "     AND energy_category_id = %s "
458
                                           "     AND start_datetime_utc >= %s "
459
                                           "     AND start_datetime_utc < %s "
460
                                           " ORDER BY start_datetime_utc ",
461
                                           (tenant['id'],
462
                                            energy_category_id,
463
                                            reporting_start_datetime_utc,
464
                                            reporting_end_datetime_utc))
465
                rows_tenant_hourly = cursor_energy_plan.fetchall()
466
467
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
468
                                                                                     reporting_start_datetime_utc,
469
                                                                                     reporting_end_datetime_utc,
470
                                                                                     period_type)
471
                for row_tenant_periodically in rows_tenant_periodically:
472
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
473
                                             timedelta(minutes=timezone_offset)
474
                    if period_type == 'hourly':
475
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
476
                    elif period_type == 'daily':
477
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
478
                    elif period_type == 'weekly':
479
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
480
                    elif period_type == 'monthly':
481
                        current_datetime = current_datetime_local.strftime('%Y-%m')
482
                    elif period_type == 'yearly':
483
                        current_datetime = current_datetime_local.strftime('%Y')
484
485
                    plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
486
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
487
                    reporting[energy_category_id]['values_plan'].append(plan_value)
488
                    reporting[energy_category_id]['subtotal_plan'] += plan_value
489
                    reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce
490
                    reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e
491
492
                # query reporting period's energy actual
493
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
494
                                      " FROM tbl_tenant_input_category_hourly "
495
                                      " WHERE tenant_id = %s "
496
                                      "     AND energy_category_id = %s "
497
                                      "     AND start_datetime_utc >= %s "
498
                                      "     AND start_datetime_utc < %s "
499
                                      " ORDER BY start_datetime_utc ",
500
                                      (tenant['id'],
501
                                       energy_category_id,
502
                                       reporting_start_datetime_utc,
503
                                       reporting_end_datetime_utc))
504
                rows_tenant_hourly = cursor_energy.fetchall()
505
506
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
507
                                                                                     reporting_start_datetime_utc,
508
                                                                                     reporting_end_datetime_utc,
509
                                                                                     period_type)
510
                for row_tenant_periodically in rows_tenant_periodically:
511
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
512
                                             timedelta(minutes=timezone_offset)
513
                    if period_type == 'hourly':
514
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
515
                    elif period_type == 'daily':
516
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
517
                    elif period_type == 'weekly':
518
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
519
                    elif period_type == 'monthly':
520
                        current_datetime = current_datetime_local.strftime('%Y-%m')
521
                    elif period_type == 'yearly':
522
                        current_datetime = current_datetime_local.strftime('%Y')
523
524
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
525
                    reporting[energy_category_id]['values_actual'].append(actual_value)
526
                    reporting[energy_category_id]['subtotal_actual'] += actual_value
527
                    reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce
528
                    reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e
529
530
                # calculate reporting period's energy savings
531
                for i in range(len(reporting[energy_category_id]['values_plan'])):
532
                    reporting[energy_category_id]['values_saving'].append(
533
                        reporting[energy_category_id]['values_plan'][i] -
534
                        reporting[energy_category_id]['values_actual'][i])
535
536
                reporting[energy_category_id]['subtotal_saving'] = \
537
                    reporting[energy_category_id]['subtotal_plan'] - \
538
                    reporting[energy_category_id]['subtotal_actual']
539
                reporting[energy_category_id]['subtotal_in_kgce_saving'] = \
540
                    reporting[energy_category_id]['subtotal_in_kgce_plan'] - \
541
                    reporting[energy_category_id]['subtotal_in_kgce_actual']
542
                reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \
543
                    reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \
544
                    reporting[energy_category_id]['subtotal_in_kgco2e_actual']
545
        ################################################################################################################
546
        # Step 8: query tariff data
547
        ################################################################################################################
548
        parameters_data = dict()
549
        parameters_data['names'] = list()
550
        parameters_data['timestamps'] = list()
551
        parameters_data['values'] = list()
552
        if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \
553
                and not is_quick_mode:
554
            for energy_category_id in energy_category_set:
555
                energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'],
556
                                                                                    energy_category_id,
557
                                                                                    reporting_start_datetime_utc,
558
                                                                                    reporting_end_datetime_utc)
559
                tariff_timestamp_list = list()
560
                tariff_value_list = list()
561
                for k, v in energy_category_tariff_dict.items():
562
                    # convert k from utc to local
563
                    k = k + timedelta(minutes=timezone_offset)
564
                    tariff_timestamp_list.append(k.isoformat()[0:19][0:19])
565
                    tariff_value_list.append(v)
566
567
                parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name'])
568
                parameters_data['timestamps'].append(tariff_timestamp_list)
569
                parameters_data['values'].append(tariff_value_list)
570
571
        ################################################################################################################
572
        # Step 9: query associated sensors and points data
573
        ################################################################################################################
574
        if not is_quick_mode:
575
            for point in point_list:
576
                point_values = []
577
                point_timestamps = []
578
                if point['object_type'] == 'ENERGY_VALUE':
579
                    query = (" SELECT utc_date_time, actual_value "
580
                             " FROM tbl_energy_value "
581
                             " WHERE point_id = %s "
582
                             "       AND utc_date_time BETWEEN %s AND %s "
583
                             " ORDER BY utc_date_time ")
584
                    cursor_historical.execute(query, (point['id'],
585
                                                      reporting_start_datetime_utc,
586
                                                      reporting_end_datetime_utc))
587
                    rows = cursor_historical.fetchall()
588
589
                    if rows is not None and len(rows) > 0:
590
                        for row in rows:
591
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
592
                                                     timedelta(minutes=timezone_offset)
593
                            current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
594
                            point_timestamps.append(current_datetime)
595
                            point_values.append(row[1])
596
                elif point['object_type'] == 'ANALOG_VALUE':
597
                    query = (" SELECT utc_date_time, actual_value "
598
                             " FROM tbl_analog_value "
599
                             " WHERE point_id = %s "
600
                             "       AND utc_date_time BETWEEN %s AND %s "
601
                             " ORDER BY utc_date_time ")
602
                    cursor_historical.execute(query, (point['id'],
603
                                                      reporting_start_datetime_utc,
604
                                                      reporting_end_datetime_utc))
605
                    rows = cursor_historical.fetchall()
606
607
                    if rows is not None and len(rows) > 0:
608
                        for row in rows:
609
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
610
                                                     timedelta(minutes=timezone_offset)
611
                            current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
612
                            point_timestamps.append(current_datetime)
613
                            point_values.append(row[1])
614
                elif point['object_type'] == 'DIGITAL_VALUE':
615
                    query = (" SELECT utc_date_time, actual_value "
616
                             " FROM tbl_digital_value "
617
                             " WHERE point_id = %s "
618
                             "       AND utc_date_time BETWEEN %s AND %s "
619
                             " ORDER BY utc_date_time ")
620
                    cursor_historical.execute(query, (point['id'],
621
                                                      reporting_start_datetime_utc,
622
                                                      reporting_end_datetime_utc))
623
                    rows = cursor_historical.fetchall()
624
625
                    if rows is not None and len(rows) > 0:
626
                        for row in rows:
627
                            current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
628
                                                     timedelta(minutes=timezone_offset)
629
                            current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
630
                            point_timestamps.append(current_datetime)
631
                            point_values.append(row[1])
632
633
                parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
634
                parameters_data['timestamps'].append(point_timestamps)
635
                parameters_data['values'].append(point_values)
636
637
        ################################################################################################################
638
        # Step 10: construct the report
639
        ################################################################################################################
640
        if cursor_system:
641
            cursor_system.close()
642
        if cnx_system:
643
            cnx_system.close()
644
645
        if cursor_energy:
646
            cursor_energy.close()
647
        if cnx_energy:
648
            cnx_energy.close()
649
650
        if cursor_energy_plan:
651
            cursor_energy_plan.close()
652
        if cnx_energy_plan:
653
            cnx_energy_plan.close()
654
655
        if cursor_historical:
656
            cursor_historical.close()
657
        if cnx_historical:
658
            cnx_historical.close()
659
660
        result = dict()
661
662
        result['tenant'] = dict()
663
        result['tenant']['name'] = tenant['name']
664
        result['tenant']['area'] = tenant['area']
665
666
        result['base_period'] = dict()
667
        result['base_period']['names'] = list()
668
        result['base_period']['units'] = list()
669
        result['base_period']['timestamps'] = list()
670
        result['base_period']['values_saving'] = list()
671
        result['base_period']['subtotals_saving'] = list()
672
        result['base_period']['subtotals_in_kgce_saving'] = list()
673
        result['base_period']['subtotals_in_kgco2e_saving'] = list()
674
        result['base_period']['total_in_kgce_saving'] = Decimal(0.0)
675
        result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0)
676
        if energy_category_set is not None and len(energy_category_set) > 0:
677
            for energy_category_id in energy_category_set:
678
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
679
                result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
680
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
681
                result['base_period']['values_saving'].append(base[energy_category_id]['values_saving'])
682
                result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving'])
683
                result['base_period']['subtotals_in_kgce_saving'].append(
684
                    base[energy_category_id]['subtotal_in_kgce_saving'])
685
                result['base_period']['subtotals_in_kgco2e_saving'].append(
686
                    base[energy_category_id]['subtotal_in_kgco2e_saving'])
687
                result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving']
688
                result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving']
689
690
        result['reporting_period'] = dict()
691
        result['reporting_period']['names'] = list()
692
        result['reporting_period']['energy_category_ids'] = list()
693
        result['reporting_period']['units'] = list()
694
        result['reporting_period']['timestamps'] = list()
695
        result['reporting_period']['values_saving'] = list()
696
        result['reporting_period']['rates_saving'] = list()
697
        result['reporting_period']['subtotals_saving'] = list()
698
        result['reporting_period']['subtotals_in_kgce_saving'] = list()
699
        result['reporting_period']['subtotals_in_kgco2e_saving'] = list()
700
        result['reporting_period']['subtotals_per_unit_area_saving'] = list()
701
        result['reporting_period']['increment_rates_saving'] = list()
702
        result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0)
703
        result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0)
704
        result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0)
705
        result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0)
706
707
        if energy_category_set is not None and len(energy_category_set) > 0:
708
            for energy_category_id in energy_category_set:
709
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
710
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
711
                result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
712
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
713
                result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving'])
714
                result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving'])
715
                result['reporting_period']['subtotals_in_kgce_saving'].append(
716
                    reporting[energy_category_id]['subtotal_in_kgce_saving'])
717
                result['reporting_period']['subtotals_in_kgco2e_saving'].append(
718
                    reporting[energy_category_id]['subtotal_in_kgco2e_saving'])
719
                result['reporting_period']['subtotals_per_unit_area_saving'].append(
720
                    reporting[energy_category_id]['subtotal_saving'] / tenant['area']
721
                    if tenant['area'] != Decimal(0.0) else None)
722
                result['reporting_period']['increment_rates_saving'].append(
723
                    (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) /
724
                    base[energy_category_id]['subtotal_saving']
725
                    if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None)
726
                result['reporting_period']['total_in_kgce_saving'] += \
727
                    reporting[energy_category_id]['subtotal_in_kgce_saving']
728
                result['reporting_period']['total_in_kgco2e_saving'] += \
729
                    reporting[energy_category_id]['subtotal_in_kgco2e_saving']
730
731
                rate = list()
732
                for index, value in enumerate(reporting[energy_category_id]['values_saving']):
733
                    if index < len(base[energy_category_id]['values_saving']) \
734
                            and base[energy_category_id]['values_saving'][index] != Decimal(0.0) \
735
                            and value != Decimal(0.0):
736
                        rate.append((value - base[energy_category_id]['values_saving'][index])
737
                                    / base[energy_category_id]['values_saving'][index])
738
                    else:
739
                        rate.append(None)
740
                result['reporting_period']['rates_saving'].append(rate)
741
742
        result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \
743
            result['reporting_period']['total_in_kgce_saving'] / tenant['area'] \
744
            if tenant['area'] != Decimal(0.0) else None
745
746
        result['reporting_period']['increment_rate_in_kgce_saving'] = \
747
            (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \
748
            result['base_period']['total_in_kgce_saving'] \
749
            if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None
750
751
        result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \
752
            result['reporting_period']['total_in_kgco2e_saving'] / tenant['area'] if tenant['area'] > 0.0 else None
753
754
        result['reporting_period']['increment_rate_in_kgco2e_saving'] = \
755
            (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \
756
            result['base_period']['total_in_kgco2e_saving'] \
757
            if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None
758
759
        result['parameters'] = {
760
            "names": parameters_data['names'],
761
            "timestamps": parameters_data['timestamps'],
762
            "values": parameters_data['values']
763
        }
764
765
        # export result to Excel file and then encode the file to base64 string
766
        if not is_quick_mode:
767
            result['excel_bytes_base64'] = excelexporters.tenantplan.export(result,
768
                                                                            tenant['name'],
769
                                                                            base_period_start_datetime_local,
770
                                                                            base_period_end_datetime_local,
771
                                                                            reporting_period_start_datetime_local,
772
                                                                            reporting_period_end_datetime_local,
773
                                                                            period_type,
774
                                                                            language)
775
776
        resp.text = json.dumps(result)
777