reports.tenantstatistics   F
last analyzed

Complexity

Total Complexity 109

Size/Duplication

Total Lines 620
Duplicated Lines 98.23 %

Importance

Changes 0
Metric Value
eloc 473
dl 609
loc 620
rs 2
c 0
b 0
f 0
wmc 109

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Reporting.on_options() 3 3 1
A Reporting.__init__() 3 3 1
F Reporting.on_get() 587 587 107

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like reports.tenantstatistics 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.tenantstatistics
9
10
11 View Code Duplication
class Reporting:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 tenant
24
    # Step 3: query energy categories
25
    # Step 4: query associated sensors
26
    # Step 5: query associated points
27
    # Step 6: query base period energy input
28
    # Step 7: query reporting period energy input
29
    # Step 8: query tariff data
30
    # Step 9: query associated sensors and points data
31
    # Step 10: construct the report
32
    ####################################################################################################################
33
    @staticmethod
34
    def on_get(req, resp):
35
        print(req.params)
36
        tenant_id = req.params.get('tenantid')
37
        period_type = req.params.get('periodtype')
38
        base_start_datetime_local = req.params.get('baseperiodstartdatetime')
39
        base_end_datetime_local = req.params.get('baseperiodenddatetime')
40
        reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime')
41
        reporting_end_datetime_local = req.params.get('reportingperiodenddatetime')
42
43
        ################################################################################################################
44
        # Step 1: valid parameters
45
        ################################################################################################################
46
        if tenant_id is None:
47
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID')
48
        else:
49
            tenant_id = str.strip(tenant_id)
50
            if not tenant_id.isdigit() or int(tenant_id) <= 0:
51
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID')
52
53
        if period_type is None:
54
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
55
        else:
56
            period_type = str.strip(period_type)
57
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
58
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
59
60
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
61
        if config.utc_offset[0] == '-':
62
            timezone_offset = -timezone_offset
63
64
        base_start_datetime_utc = None
65
        if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0:
66
            base_start_datetime_local = str.strip(base_start_datetime_local)
67
            try:
68
                base_start_datetime_utc = datetime.strptime(base_start_datetime_local,
69
                                                            '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
70
                                          timedelta(minutes=timezone_offset)
71
            except ValueError:
72
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
73
                                       description="API.INVALID_BASE_PERIOD_START_DATETIME")
74
75
        base_end_datetime_utc = None
76
        if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0:
77
            base_end_datetime_local = str.strip(base_end_datetime_local)
78
            try:
79
                base_end_datetime_utc = datetime.strptime(base_end_datetime_local,
80
                                                          '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
81
                                        timedelta(minutes=timezone_offset)
82
            except ValueError:
83
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
84
                                       description="API.INVALID_BASE_PERIOD_END_DATETIME")
85
86
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
87
                base_start_datetime_utc >= base_end_datetime_utc:
88
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
89
                                   description='API.INVALID_BASE_PERIOD_END_DATETIME')
90
91
        if reporting_start_datetime_local is None:
92
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
93
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
94
        else:
95
            reporting_start_datetime_local = str.strip(reporting_start_datetime_local)
96
            try:
97
                reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local,
98
                                                                 '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
99
                                               timedelta(minutes=timezone_offset)
100
            except ValueError:
101
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
102
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
103
104
        if reporting_end_datetime_local is None:
105
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
106
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
107
        else:
108
            reporting_end_datetime_local = str.strip(reporting_end_datetime_local)
109
            try:
110
                reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local,
111
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
112
                                             timedelta(minutes=timezone_offset)
113
            except ValueError:
114
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
115
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
116
117
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
118
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
119
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
120
121
        ################################################################################################################
122
        # Step 2: query the tenant
123
        ################################################################################################################
124
        cnx_system = mysql.connector.connect(**config.myems_system_db)
125
        cursor_system = cnx_system.cursor()
126
127
        cnx_energy = mysql.connector.connect(**config.myems_energy_db)
128
        cursor_energy = cnx_energy.cursor()
129
130
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
131
        cursor_historical = cnx_historical.cursor()
132
133
        cursor_system.execute(" SELECT id, name, area, cost_center_id "
134
                              " FROM tbl_tenants "
135
                              " WHERE id = %s ", (tenant_id,))
136
        row_tenant = cursor_system.fetchone()
137
        if row_tenant is None:
138
            if cursor_system:
139
                cursor_system.close()
140
            if cnx_system:
141
                cnx_system.disconnect()
142
143
            if cursor_energy:
144
                cursor_energy.close()
145
            if cnx_energy:
146
                cnx_energy.disconnect()
147
148
            if cnx_historical:
149
                cnx_historical.close()
150
            if cursor_historical:
151
                cursor_historical.disconnect()
152
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND')
153
154
        tenant = dict()
155
        tenant['id'] = row_tenant[0]
156
        tenant['name'] = row_tenant[1]
157
        tenant['area'] = row_tenant[2]
158
        tenant['cost_center_id'] = row_tenant[3]
159
160
        ################################################################################################################
161
        # Step 3: query energy categories
162
        ################################################################################################################
163
        energy_category_set = set()
164
        # query energy categories in base period
165
        cursor_energy.execute(" SELECT DISTINCT(energy_category_id) "
166
                              " FROM tbl_tenant_input_category_hourly "
167
                              " WHERE tenant_id = %s "
168
                              "     AND start_datetime_utc >= %s "
169
                              "     AND start_datetime_utc < %s ",
170
                              (tenant['id'], base_start_datetime_utc, base_end_datetime_utc))
171
        rows_energy_categories = cursor_energy.fetchall()
172
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
173
            for row_energy_category in rows_energy_categories:
174
                energy_category_set.add(row_energy_category[0])
175
176
        # query energy categories in reporting period
177
        cursor_energy.execute(" SELECT DISTINCT(energy_category_id) "
178
                              " FROM tbl_tenant_input_category_hourly "
179
                              " WHERE tenant_id = %s "
180
                              "     AND start_datetime_utc >= %s "
181
                              "     AND start_datetime_utc < %s ",
182
                              (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
183
        rows_energy_categories = cursor_energy.fetchall()
184
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
185
            for row_energy_category in rows_energy_categories:
186
                energy_category_set.add(row_energy_category[0])
187
188
        # query all energy categories in base period and reporting period
189
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
190
                              " FROM tbl_energy_categories "
191
                              " ORDER BY id ", )
192
        rows_energy_categories = cursor_system.fetchall()
193
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
194
            if cursor_system:
195
                cursor_system.close()
196
            if cnx_system:
197
                cnx_system.disconnect()
198
199
            if cursor_energy:
200
                cursor_energy.close()
201
            if cnx_energy:
202
                cnx_energy.disconnect()
203
204
            if cnx_historical:
205
                cnx_historical.close()
206
            if cursor_historical:
207
                cursor_historical.disconnect()
208
            raise falcon.HTTPError(falcon.HTTP_404,
209
                                   title='API.NOT_FOUND',
210
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
211
        energy_category_dict = dict()
212
        for row_energy_category in rows_energy_categories:
213
            if row_energy_category[0] in energy_category_set:
214
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
215
                                                                "unit_of_measure": row_energy_category[2],
216
                                                                "kgce": row_energy_category[3],
217
                                                                "kgco2e": row_energy_category[4]}
218
219
        ################################################################################################################
220
        # Step 4: query associated sensors
221
        ################################################################################################################
222
        point_list = list()
223
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
224
                              " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, "
225
                              "      tbl_points p, tbl_sensors_points sp "
226
                              " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id "
227
                              "       AND s.id = sp.sensor_id AND sp.point_id = p.id "
228
                              " ORDER BY p.id ", (tenant['id'],))
229
        rows_points = cursor_system.fetchall()
230
        if rows_points is not None and len(rows_points) > 0:
231
            for row in rows_points:
232
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
233
234
        ################################################################################################################
235
        # Step 5: query associated points
236
        ################################################################################################################
237
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
238
                              " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p "
239
                              " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id "
240
                              " ORDER BY p.id ", (tenant['id'],))
241
        rows_points = cursor_system.fetchall()
242
        if rows_points is not None and len(rows_points) > 0:
243
            for row in rows_points:
244
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
245
246
        ################################################################################################################
247
        # Step 6: query base period energy input
248
        ################################################################################################################
249
        base = dict()
250
        if energy_category_set is not None and len(energy_category_set) > 0:
251
            for energy_category_id in energy_category_set:
252
                base[energy_category_id] = dict()
253
                base[energy_category_id]['timestamps'] = list()
254
                base[energy_category_id]['values'] = list()
255
                base[energy_category_id]['subtotal'] = Decimal(0.0)
256
                base[energy_category_id]['mean'] = None
257
                base[energy_category_id]['median'] = None
258
                base[energy_category_id]['minimum'] = None
259
                base[energy_category_id]['maximum'] = None
260
                base[energy_category_id]['stdev'] = None
261
                base[energy_category_id]['variance'] = None
262
263
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
264
                                      " FROM tbl_tenant_input_category_hourly "
265
                                      " WHERE tenant_id = %s "
266
                                      "     AND energy_category_id = %s "
267
                                      "     AND start_datetime_utc >= %s "
268
                                      "     AND start_datetime_utc < %s "
269
                                      " ORDER BY start_datetime_utc ",
270
                                      (tenant['id'],
271
                                       energy_category_id,
272
                                       base_start_datetime_utc,
273
                                       base_end_datetime_utc))
274
                rows_tenant_hourly = cursor_energy.fetchall()
275
276
                rows_tenant_periodically, \
277
                    base[energy_category_id]['mean'], \
278
                    base[energy_category_id]['median'], \
279
                    base[energy_category_id]['minimum'], \
280
                    base[energy_category_id]['maximum'], \
281
                    base[energy_category_id]['stdev'], \
282
                    base[energy_category_id]['variance'] = \
283
                    utilities.statistics_hourly_data_by_period(rows_tenant_hourly,
284
                                                               base_start_datetime_utc,
285
                                                               base_end_datetime_utc,
286
                                                               period_type)
287
288
                for row_tenant_periodically in rows_tenant_periodically:
289
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
290
                                             timedelta(minutes=timezone_offset)
291
                    if period_type == 'hourly':
292
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
293
                    elif period_type == 'daily':
294
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
295
                    elif period_type == 'monthly':
296
                        current_datetime = current_datetime_local.strftime('%Y-%m')
297
                    elif period_type == 'yearly':
298
                        current_datetime = current_datetime_local.strftime('%Y')
299
300
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
301
                    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...
302
                    base[energy_category_id]['values'].append(actual_value)
303
                    base[energy_category_id]['subtotal'] += actual_value
304
305
        ################################################################################################################
306
        # Step 7: query reporting period energy input
307
        ################################################################################################################
308
        reporting = dict()
309
        if energy_category_set is not None and len(energy_category_set) > 0:
310
            for energy_category_id in energy_category_set:
311
                reporting[energy_category_id] = dict()
312
                reporting[energy_category_id]['timestamps'] = list()
313
                reporting[energy_category_id]['values'] = list()
314
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
315
                reporting[energy_category_id]['mean'] = None
316
                reporting[energy_category_id]['median'] = None
317
                reporting[energy_category_id]['minimum'] = None
318
                reporting[energy_category_id]['maximum'] = None
319
                reporting[energy_category_id]['stdev'] = None
320
                reporting[energy_category_id]['variance'] = None
321
322
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
323
                                      " FROM tbl_tenant_input_category_hourly "
324
                                      " WHERE tenant_id = %s "
325
                                      "     AND energy_category_id = %s "
326
                                      "     AND start_datetime_utc >= %s "
327
                                      "     AND start_datetime_utc < %s "
328
                                      " ORDER BY start_datetime_utc ",
329
                                      (tenant['id'],
330
                                       energy_category_id,
331
                                       reporting_start_datetime_utc,
332
                                       reporting_end_datetime_utc))
333
                rows_tenant_hourly = cursor_energy.fetchall()
334
335
                rows_tenant_periodically, \
336
                    reporting[energy_category_id]['mean'], \
337
                    reporting[energy_category_id]['median'], \
338
                    reporting[energy_category_id]['minimum'], \
339
                    reporting[energy_category_id]['maximum'], \
340
                    reporting[energy_category_id]['stdev'], \
341
                    reporting[energy_category_id]['variance'] = \
342
                    utilities.statistics_hourly_data_by_period(rows_tenant_hourly,
343
                                                               reporting_start_datetime_utc,
344
                                                               reporting_end_datetime_utc,
345
                                                               period_type)
346
347
                for row_tenant_periodically in rows_tenant_periodically:
348
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
349
                                             timedelta(minutes=timezone_offset)
350
                    if period_type == 'hourly':
351
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
352
                    elif period_type == 'daily':
353
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
354
                    elif period_type == 'monthly':
355
                        current_datetime = current_datetime_local.strftime('%Y-%m')
356
                    elif period_type == 'yearly':
357
                        current_datetime = current_datetime_local.strftime('%Y')
358
359
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
360
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
361
                    reporting[energy_category_id]['values'].append(actual_value)
362
                    reporting[energy_category_id]['subtotal'] += actual_value
363
364
        ################################################################################################################
365
        # Step 8: query tariff data
366
        ################################################################################################################
367
        parameters_data = dict()
368
        parameters_data['names'] = list()
369
        parameters_data['timestamps'] = list()
370
        parameters_data['values'] = list()
371
        if energy_category_set is not None and len(energy_category_set) > 0:
372
            for energy_category_id in energy_category_set:
373
                energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'],
374
                                                                                    energy_category_id,
375
                                                                                    reporting_start_datetime_utc,
376
                                                                                    reporting_end_datetime_utc)
377
                tariff_timestamp_list = list()
378
                tariff_value_list = list()
379
                for k, v in energy_category_tariff_dict.items():
380
                    # convert k from utc to local
381
                    k = k + timedelta(minutes=timezone_offset)
382
                    tariff_timestamp_list.append(k.isoformat()[0:19][0:19])
383
                    tariff_value_list.append(v)
384
385
                parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name'])
386
                parameters_data['timestamps'].append(tariff_timestamp_list)
387
                parameters_data['values'].append(tariff_value_list)
388
389
        ################################################################################################################
390
        # Step 9: query associated sensors and points data
391
        ################################################################################################################
392
        for point in point_list:
393
            point_values = []
394
            point_timestamps = []
395
            if point['object_type'] == 'ANALOG_VALUE':
396
                query = (" SELECT utc_date_time, actual_value "
397
                         " FROM tbl_analog_value "
398
                         " WHERE point_id = %s "
399
                         "       AND utc_date_time BETWEEN %s AND %s "
400
                         " ORDER BY utc_date_time ")
401
                cursor_historical.execute(query, (point['id'],
402
                                                  reporting_start_datetime_utc,
403
                                                  reporting_end_datetime_utc))
404
                rows = cursor_historical.fetchall()
405
406
                if rows is not None and len(rows) > 0:
407
                    for row in rows:
408
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
409
                                                 timedelta(minutes=timezone_offset)
410
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
411
                        point_timestamps.append(current_datetime)
412
                        point_values.append(row[1])
413
414
            elif point['object_type'] == 'ENERGY_VALUE':
415
                query = (" SELECT utc_date_time, actual_value "
416
                         " FROM tbl_energy_value "
417
                         " WHERE point_id = %s "
418
                         "       AND utc_date_time BETWEEN %s AND %s "
419
                         " ORDER BY utc_date_time ")
420
                cursor_historical.execute(query, (point['id'],
421
                                                  reporting_start_datetime_utc,
422
                                                  reporting_end_datetime_utc))
423
                rows = cursor_historical.fetchall()
424
425
                if rows is not None and len(rows) > 0:
426
                    for row in rows:
427
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
428
                                                 timedelta(minutes=timezone_offset)
429
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
430
                        point_timestamps.append(current_datetime)
431
                        point_values.append(row[1])
432
            elif point['object_type'] == 'DIGITAL_VALUE':
433
                query = (" SELECT utc_date_time, actual_value "
434
                         " FROM tbl_digital_value "
435
                         " WHERE point_id = %s "
436
                         "       AND utc_date_time BETWEEN %s AND %s ")
437
                cursor_historical.execute(query, (point['id'],
438
                                                  reporting_start_datetime_utc,
439
                                                  reporting_end_datetime_utc))
440
                rows = cursor_historical.fetchall()
441
442
                if rows is not None and len(rows) > 0:
443
                    for row in rows:
444
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
445
                                                 timedelta(minutes=timezone_offset)
446
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
447
                        point_timestamps.append(current_datetime)
448
                        point_values.append(row[1])
449
450
            parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
451
            parameters_data['timestamps'].append(point_timestamps)
452
            parameters_data['values'].append(point_values)
453
454
        ################################################################################################################
455
        # Step 10: construct the report
456
        ################################################################################################################
457
        if cursor_system:
458
            cursor_system.close()
459
        if cnx_system:
460
            cnx_system.disconnect()
461
462
        if cursor_energy:
463
            cursor_energy.close()
464
        if cnx_energy:
465
            cnx_energy.disconnect()
466
467
        result = dict()
468
469
        result['tenant'] = dict()
470
        result['tenant']['name'] = tenant['name']
471
        result['tenant']['area'] = tenant['area']
472
473
        result['base_period'] = dict()
474
        result['base_period']['names'] = list()
475
        result['base_period']['units'] = list()
476
        result['base_period']['timestamps'] = list()
477
        result['base_period']['values'] = list()
478
        result['base_period']['subtotals'] = list()
479
        result['base_period']['means'] = list()
480
        result['base_period']['medians'] = list()
481
        result['base_period']['minimums'] = list()
482
        result['base_period']['maximums'] = list()
483
        result['base_period']['stdevs'] = list()
484
        result['base_period']['variances'] = list()
485
486
        if energy_category_set is not None and len(energy_category_set) > 0:
487
            for energy_category_id in energy_category_set:
488
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
489
                result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
490
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
491
                result['base_period']['values'].append(base[energy_category_id]['values'])
492
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
493
                result['base_period']['means'].append(base[energy_category_id]['mean'])
494
                result['base_period']['medians'].append(base[energy_category_id]['median'])
495
                result['base_period']['minimums'].append(base[energy_category_id]['minimum'])
496
                result['base_period']['maximums'].append(base[energy_category_id]['maximum'])
497
                result['base_period']['stdevs'].append(base[energy_category_id]['stdev'])
498
                result['base_period']['variances'].append(base[energy_category_id]['variance'])
499
500
        result['reporting_period'] = dict()
501
        result['reporting_period']['names'] = list()
502
        result['reporting_period']['energy_category_ids'] = list()
503
        result['reporting_period']['units'] = list()
504
        result['reporting_period']['timestamps'] = list()
505
        result['reporting_period']['values'] = list()
506
        result['reporting_period']['subtotals'] = list()
507
        result['reporting_period']['means'] = list()
508
        result['reporting_period']['means_per_unit_area'] = list()
509
        result['reporting_period']['means_increment_rate'] = list()
510
        result['reporting_period']['medians'] = list()
511
        result['reporting_period']['medians_per_unit_area'] = list()
512
        result['reporting_period']['medians_increment_rate'] = list()
513
        result['reporting_period']['minimums'] = list()
514
        result['reporting_period']['minimums_per_unit_area'] = list()
515
        result['reporting_period']['minimums_increment_rate'] = list()
516
        result['reporting_period']['maximums'] = list()
517
        result['reporting_period']['maximums_per_unit_area'] = list()
518
        result['reporting_period']['maximums_increment_rate'] = list()
519
        result['reporting_period']['stdevs'] = list()
520
        result['reporting_period']['stdevs_per_unit_area'] = list()
521
        result['reporting_period']['stdevs_increment_rate'] = list()
522
        result['reporting_period']['variances'] = list()
523
        result['reporting_period']['variances_per_unit_area'] = list()
524
        result['reporting_period']['variances_increment_rate'] = list()
525
526
        if energy_category_set is not None and len(energy_category_set) > 0:
527
            for energy_category_id in energy_category_set:
528
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
529
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
530
                result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure'])
531
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
532
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
533
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
534
                result['reporting_period']['means'].append(reporting[energy_category_id]['mean'])
535
                result['reporting_period']['means_per_unit_area'].append(
536
                    reporting[energy_category_id]['mean'] / tenant['area']
537
                    if reporting[energy_category_id]['mean'] is not None and
538
                    tenant['area'] is not None and
539
                    tenant['area'] > Decimal(0.0)
540
                    else None)
541
                result['reporting_period']['means_increment_rate'].append(
542
                    (reporting[energy_category_id]['mean'] - base[energy_category_id]['mean']) /
543
                    base[energy_category_id]['mean'] if (base[energy_category_id]['mean'] is not None and
544
                                                         base[energy_category_id]['mean'] > Decimal(0.0))
545
                    else None)
546
                result['reporting_period']['medians'].append(reporting[energy_category_id]['median'])
547
                result['reporting_period']['medians_per_unit_area'].append(
548
                    reporting[energy_category_id]['median'] / tenant['area']
549
                    if reporting[energy_category_id]['median'] is not None and
550
                    tenant['area'] is not None and
551
                    tenant['area'] > Decimal(0.0)
552
                    else None)
553
                result['reporting_period']['medians_increment_rate'].append(
554
                    (reporting[energy_category_id]['median'] - base[energy_category_id]['median']) /
555
                    base[energy_category_id]['median'] if (base[energy_category_id]['median'] is not None and
556
                                                           base[energy_category_id]['median'] > Decimal(0.0))
557
                    else None)
558
                result['reporting_period']['minimums'].append(reporting[energy_category_id]['minimum'])
559
                result['reporting_period']['minimums_per_unit_area'].append(
560
                    reporting[energy_category_id]['minimum'] / tenant['area']
561
                    if reporting[energy_category_id]['minimum'] is not None and
562
                    tenant['area'] is not None and
563
                    tenant['area'] > Decimal(0.0)
564
                    else None)
565
                result['reporting_period']['minimums_increment_rate'].append(
566
                    (reporting[energy_category_id]['minimum'] - base[energy_category_id]['minimum']) /
567
                    base[energy_category_id]['minimum'] if (base[energy_category_id]['minimum'] is not None and
568
                                                            base[energy_category_id]['minimum'] > Decimal(0.0))
569
                    else None)
570
                result['reporting_period']['maximums'].append(reporting[energy_category_id]['maximum'])
571
                result['reporting_period']['maximums_per_unit_area'].append(
572
                    reporting[energy_category_id]['maximum'] / tenant['area']
573
                    if reporting[energy_category_id]['maximum'] is not None and
574
                    tenant['area'] is not None and
575
                    tenant['area'] > Decimal(0.0)
576
                    else None)
577
                result['reporting_period']['maximums_increment_rate'].append(
578
                    (reporting[energy_category_id]['maximum'] - base[energy_category_id]['maximum']) /
579
                    base[energy_category_id]['maximum'] if (base[energy_category_id]['maximum'] is not None and
580
                                                            base[energy_category_id]['maximum'] > Decimal(0.0))
581
                    else None)
582
                result['reporting_period']['stdevs'].append(reporting[energy_category_id]['stdev'])
583
                result['reporting_period']['stdevs_per_unit_area'].append(
584
                    reporting[energy_category_id]['stdev'] / tenant['area']
585
                    if reporting[energy_category_id]['stdev'] is not None and
586
                    tenant['area'] is not None and
587
                    tenant['area'] > Decimal(0.0)
588
                    else None)
589
                result['reporting_period']['stdevs_increment_rate'].append(
590
                    (reporting[energy_category_id]['stdev'] - base[energy_category_id]['stdev']) /
591
                    base[energy_category_id]['stdev'] if (base[energy_category_id]['stdev'] is not None and
592
                                                          base[energy_category_id]['stdev'] > Decimal(0.0))
593
                    else None)
594
                result['reporting_period']['variances'].append(reporting[energy_category_id]['variance'])
595
                result['reporting_period']['variances_per_unit_area'].append(
596
                    reporting[energy_category_id]['variance'] / tenant['area']
597
                    if reporting[energy_category_id]['variance'] is not None and
598
                    tenant['area'] is not None and
599
                    tenant['area'] > Decimal(0.0)
600
                    else None)
601
                result['reporting_period']['variances_increment_rate'].append(
602
                    (reporting[energy_category_id]['variance'] - base[energy_category_id]['variance']) /
603
                    base[energy_category_id]['variance'] if (base[energy_category_id]['variance'] is not None and
604
                                                             base[energy_category_id]['variance'] > Decimal(0.0))
605
                    else None)
606
607
        result['parameters'] = {
608
            "names": parameters_data['names'],
609
            "timestamps": parameters_data['timestamps'],
610
            "values": parameters_data['values']
611
        }
612
        # export result to Excel file and then encode the file to base64 string
613
        result['excel_bytes_base64'] = excelexporters.tenantstatistics.export(result,
614
                                                                              tenant['name'],
615
                                                                              reporting_start_datetime_local,
616
                                                                              reporting_end_datetime_local,
617
                                                                              period_type)
618
619
        resp.body = json.dumps(result)
620