Code Duplication    Length = 522-524 lines in 3 locations

reports/shopfloorcost.py 1 location

@@ 10-533 (lines=524) @@
7
from decimal import *
8
9
10
class Reporting:
11
    @staticmethod
12
    def __init__():
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp):
17
        resp.status = falcon.HTTP_200
18
19
    ####################################################################################################################
20
    # PROCEDURES
21
    # Step 1: valid parameters
22
    # Step 2: query the shopfloor
23
    # Step 3: query energy categories
24
    # Step 4: query associated sensors
25
    # Step 5: query associated points
26
    # Step 6: query base period energy cost
27
    # Step 7: query reporting period energy cost
28
    # Step 8: query tariff data
29
    # Step 9: query associated sensors and points data
30
    # Step 10: construct the report
31
    ####################################################################################################################
32
    @staticmethod
33
    def on_get(req, resp):
34
        print(req.params)
35
        shopfloor_id = req.params.get('shopfloorid')
36
        period_type = req.params.get('periodtype')
37
        base_start_datetime_local = req.params.get('baseperiodstartdatetime')
38
        base_end_datetime_local = req.params.get('baseperiodenddatetime')
39
        reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime')
40
        reporting_end_datetime_local = req.params.get('reportingperiodenddatetime')
41
42
        ################################################################################################################
43
        # Step 1: valid parameters
44
        ################################################################################################################
45
        if shopfloor_id is None:
46
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SHOPFLOOR_ID')
47
        else:
48
            shopfloor_id = str.strip(shopfloor_id)
49
            if not shopfloor_id.isdigit() or int(shopfloor_id) <= 0:
50
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SHOPFLOOR_ID')
51
52
        if period_type is None:
53
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
54
        else:
55
            period_type = str.strip(period_type)
56
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
57
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
58
59
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
60
        if config.utc_offset[0] == '-':
61
            timezone_offset = -timezone_offset
62
63
        base_start_datetime_utc = None
64
        if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0:
65
            base_start_datetime_local = str.strip(base_start_datetime_local)
66
            try:
67
                base_start_datetime_utc = datetime.strptime(base_start_datetime_local,
68
                                                            '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
69
                    timedelta(minutes=timezone_offset)
70
            except ValueError:
71
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
72
                                       description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME")
73
74
        base_end_datetime_utc = None
75
        if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0:
76
            base_end_datetime_local = str.strip(base_end_datetime_local)
77
            try:
78
                base_end_datetime_utc = datetime.strptime(base_end_datetime_local,
79
                                                          '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
80
                    timedelta(minutes=timezone_offset)
81
            except ValueError:
82
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
83
                                       description="API.INVALID_BASE_PERIOD_ENDS_DATETIME")
84
85
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
86
                base_start_datetime_utc >= base_end_datetime_utc:
87
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
88
                                   description='API.INVALID_BASE_PERIOD_ENDS_DATETIME')
89
90
        if reporting_start_datetime_local is None:
91
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
92
                                   description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
93
        else:
94
            reporting_start_datetime_local = str.strip(reporting_start_datetime_local)
95
            try:
96
                reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local,
97
                                                                 '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
98
                    timedelta(minutes=timezone_offset)
99
            except ValueError:
100
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
101
                                       description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
102
103
        if reporting_end_datetime_local is None:
104
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
105
                                   description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
106
        else:
107
            reporting_end_datetime_local = str.strip(reporting_end_datetime_local)
108
            try:
109
                reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local,
110
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
111
                    timedelta(minutes=timezone_offset)
112
            except ValueError:
113
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
114
                                       description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
115
116
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
117
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
118
                                   description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME')
119
120
        ################################################################################################################
121
        # Step 2: query the shopfloor
122
        ################################################################################################################
123
        cnx_system = mysql.connector.connect(**config.myems_system_db)
124
        cursor_system = cnx_system.cursor()
125
126
        cnx_billing = mysql.connector.connect(**config.myems_billing_db)
127
        cursor_billing = cnx_billing.cursor()
128
129
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
130
        cursor_historical = cnx_historical.cursor()
131
132
        cursor_system.execute(" SELECT id, name, area, cost_center_id "
133
                              " FROM tbl_shopfloors "
134
                              " WHERE id = %s ", (shopfloor_id,))
135
        row_shopfloor = cursor_system.fetchone()
136
        if row_shopfloor is None:
137
            if cursor_system:
138
                cursor_system.close()
139
            if cnx_system:
140
                cnx_system.disconnect()
141
142
            if cursor_billing:
143
                cursor_billing.close()
144
            if cnx_billing:
145
                cnx_billing.disconnect()
146
147
            if cnx_historical:
148
                cnx_historical.close()
149
            if cursor_historical:
150
                cursor_historical.disconnect()
151
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SHOPFLOOR_NOT_FOUND')
152
153
        shopfloor = dict()
154
        shopfloor['id'] = row_shopfloor[0]
155
        shopfloor['name'] = row_shopfloor[1]
156
        shopfloor['area'] = row_shopfloor[2]
157
        shopfloor['cost_center_id'] = row_shopfloor[3]
158
159
        ################################################################################################################
160
        # Step 3: query energy categories
161
        ################################################################################################################
162
        energy_category_set = set()
163
        # query energy categories in base period
164
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
165
                               " FROM tbl_shopfloor_input_category_hourly "
166
                               " WHERE shopfloor_id = %s "
167
                               "     AND start_datetime_utc >= %s "
168
                               "     AND start_datetime_utc < %s ",
169
                               (shopfloor['id'], base_start_datetime_utc, base_end_datetime_utc))
170
        rows_energy_categories = cursor_billing.fetchall()
171
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
172
            for row_energy_category in rows_energy_categories:
173
                energy_category_set.add(row_energy_category[0])
174
175
        # query energy categories in reporting period
176
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
177
                               " FROM tbl_shopfloor_input_category_hourly "
178
                               " WHERE shopfloor_id = %s "
179
                               "     AND start_datetime_utc >= %s "
180
                               "     AND start_datetime_utc < %s ",
181
                               (shopfloor['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
182
        rows_energy_categories = cursor_billing.fetchall()
183
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
184
            for row_energy_category in rows_energy_categories:
185
                energy_category_set.add(row_energy_category[0])
186
187
        # query all energy categories in base period and reporting period
188
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
189
                              " FROM tbl_energy_categories "
190
                              " ORDER BY id ", )
191
        rows_energy_categories = cursor_system.fetchall()
192
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
193
            if cursor_system:
194
                cursor_system.close()
195
            if cnx_system:
196
                cnx_system.disconnect()
197
198
            if cursor_billing:
199
                cursor_billing.close()
200
            if cnx_billing:
201
                cnx_billing.disconnect()
202
203
            if cnx_historical:
204
                cnx_historical.close()
205
            if cursor_historical:
206
                cursor_historical.disconnect()
207
            raise falcon.HTTPError(falcon.HTTP_404,
208
                                   title='API.NOT_FOUND',
209
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
210
        energy_category_dict = dict()
211
        for row_energy_category in rows_energy_categories:
212
            if row_energy_category[0] in energy_category_set:
213
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
214
                                                                "unit_of_measure": row_energy_category[2],
215
                                                                "kgce": row_energy_category[3],
216
                                                                "kgco2e": row_energy_category[4]}
217
218
        ################################################################################################################
219
        # Step 4: query associated sensors
220
        ################################################################################################################
221
        point_list = list()
222
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
223
                              " FROM tbl_shopfloors st, tbl_sensors se, tbl_shopfloors_sensors ss, "
224
                              "      tbl_points p, tbl_sensors_points sp "
225
                              " WHERE st.id = %s AND st.id = ss.shopfloor_id AND ss.sensor_id = se.id "
226
                              "       AND se.id = sp.sensor_id AND sp.point_id = p.id "
227
                              " ORDER BY p.id ", (shopfloor['id'], ))
228
        rows_points = cursor_system.fetchall()
229
        if rows_points is not None and len(rows_points) > 0:
230
            for row in rows_points:
231
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
232
233
        ################################################################################################################
234
        # Step 5: query associated points
235
        ################################################################################################################
236
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
237
                              " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p "
238
                              " WHERE s.id = %s AND s.id = sp.shopfloor_id AND sp.point_id = p.id "
239
                              " ORDER BY p.id ", (shopfloor['id'], ))
240
        rows_points = cursor_system.fetchall()
241
        if rows_points is not None and len(rows_points) > 0:
242
            for row in rows_points:
243
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
244
245
        ################################################################################################################
246
        # Step 6: query base period energy cost
247
        ################################################################################################################
248
        base = dict()
249
        if energy_category_set is not None and len(energy_category_set) > 0:
250
            for energy_category_id in energy_category_set:
251
                base[energy_category_id] = dict()
252
                base[energy_category_id]['timestamps'] = list()
253
                base[energy_category_id]['values'] = list()
254
                base[energy_category_id]['subtotal'] = Decimal(0.0)
255
256
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
257
                                       " FROM tbl_shopfloor_input_category_hourly "
258
                                       " WHERE shopfloor_id = %s "
259
                                       "     AND energy_category_id = %s "
260
                                       "     AND start_datetime_utc >= %s "
261
                                       "     AND start_datetime_utc < %s "
262
                                       " ORDER BY start_datetime_utc ",
263
                                       (shopfloor['id'],
264
                                        energy_category_id,
265
                                        base_start_datetime_utc,
266
                                        base_end_datetime_utc))
267
                rows_shopfloor_hourly = cursor_billing.fetchall()
268
269
                rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly,
270
                                                                                        base_start_datetime_utc,
271
                                                                                        base_end_datetime_utc,
272
                                                                                        period_type)
273
                for row_shopfloor_periodically in rows_shopfloor_periodically:
274
                    current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \
275
                                             timedelta(minutes=timezone_offset)
276
                    if period_type == 'hourly':
277
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
278
                    elif period_type == 'daily':
279
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
280
                    elif period_type == 'monthly':
281
                        current_datetime = current_datetime_local.strftime('%Y-%m')
282
                    elif period_type == 'yearly':
283
                        current_datetime = current_datetime_local.strftime('%Y')
284
285
                    actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \
286
                        else row_shopfloor_periodically[1]
287
                    base[energy_category_id]['timestamps'].append(current_datetime)
288
                    base[energy_category_id]['values'].append(actual_value)
289
                    base[energy_category_id]['subtotal'] += actual_value
290
291
        ################################################################################################################
292
        # Step 7: query reporting period energy cost
293
        ################################################################################################################
294
        reporting = dict()
295
        if energy_category_set is not None and len(energy_category_set) > 0:
296
            for energy_category_id in energy_category_set:
297
                reporting[energy_category_id] = dict()
298
                reporting[energy_category_id]['timestamps'] = list()
299
                reporting[energy_category_id]['values'] = list()
300
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
301
                reporting[energy_category_id]['toppeak'] = Decimal(0.0)
302
                reporting[energy_category_id]['onpeak'] = Decimal(0.0)
303
                reporting[energy_category_id]['midpeak'] = Decimal(0.0)
304
                reporting[energy_category_id]['offpeak'] = Decimal(0.0)
305
306
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
307
                                       " FROM tbl_shopfloor_input_category_hourly "
308
                                       " WHERE shopfloor_id = %s "
309
                                       "     AND energy_category_id = %s "
310
                                       "     AND start_datetime_utc >= %s "
311
                                       "     AND start_datetime_utc < %s "
312
                                       " ORDER BY start_datetime_utc ",
313
                                       (shopfloor['id'],
314
                                        energy_category_id,
315
                                        reporting_start_datetime_utc,
316
                                        reporting_end_datetime_utc))
317
                rows_shopfloor_hourly = cursor_billing.fetchall()
318
319
                rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly,
320
                                                                                        reporting_start_datetime_utc,
321
                                                                                        reporting_end_datetime_utc,
322
                                                                                        period_type)
323
                for row_shopfloor_periodically in rows_shopfloor_periodically:
324
                    current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \
325
                                             timedelta(minutes=timezone_offset)
326
                    if period_type == 'hourly':
327
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
328
                    elif period_type == 'daily':
329
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
330
                    elif period_type == 'monthly':
331
                        current_datetime = current_datetime_local.strftime('%Y-%m')
332
                    elif period_type == 'yearly':
333
                        current_datetime = current_datetime_local.strftime('%Y')
334
335
                    actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \
336
                        else row_shopfloor_periodically[1]
337
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
338
                    reporting[energy_category_id]['values'].append(actual_value)
339
                    reporting[energy_category_id]['subtotal'] += actual_value
340
341
                energy_category_tariff_dict = utilities.get_energy_category_peak_types(shopfloor['cost_center_id'],
342
                                                                                       energy_category_id,
343
                                                                                       reporting_start_datetime_utc,
344
                                                                                       reporting_end_datetime_utc)
345
                for row in rows_shopfloor_hourly:
346
                    peak_type = energy_category_tariff_dict.get(row[0], None)
347
                    if peak_type == 'toppeak':
348
                        reporting[energy_category_id]['toppeak'] += row[1]
349
                    elif peak_type == 'onpeak':
350
                        reporting[energy_category_id]['onpeak'] += row[1]
351
                    elif peak_type == 'midpeak':
352
                        reporting[energy_category_id]['midpeak'] += row[1]
353
                    elif peak_type == 'offpeak':
354
                        reporting[energy_category_id]['offpeak'] += row[1]
355
356
        ################################################################################################################
357
        # Step 8: query tariff data
358
        ################################################################################################################
359
        parameters_data = dict()
360
        parameters_data['names'] = list()
361
        parameters_data['timestamps'] = list()
362
        parameters_data['values'] = list()
363
        if energy_category_set is not None and len(energy_category_set) > 0:
364
            for energy_category_id in energy_category_set:
365
                energy_category_tariff_dict = utilities.get_energy_category_tariffs(shopfloor['cost_center_id'],
366
                                                                                    energy_category_id,
367
                                                                                    reporting_start_datetime_utc,
368
                                                                                    reporting_end_datetime_utc)
369
                tariff_timestamp_list = list()
370
                tariff_value_list = list()
371
                for k, v in energy_category_tariff_dict.items():
372
                    # convert k from utc to local
373
                    k = k + timedelta(minutes=timezone_offset)
374
                    tariff_timestamp_list.append(k.isoformat()[0:19][0:19])
375
                    tariff_value_list.append(v)
376
377
                parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name'])
378
                parameters_data['timestamps'].append(tariff_timestamp_list)
379
                parameters_data['values'].append(tariff_value_list)
380
381
        ################################################################################################################
382
        # Step 9: query associated sensors and points data
383
        ################################################################################################################
384
        for point in point_list:
385
            point_values = []
386
            point_timestamps = []
387
            if point['object_type'] == 'ANALOG_VALUE':
388
                query = (" SELECT utc_date_time, actual_value "
389
                         " FROM tbl_analog_value "
390
                         " WHERE point_id = %s "
391
                         "       AND utc_date_time BETWEEN %s AND %s "
392
                         " ORDER BY utc_date_time ")
393
                cursor_historical.execute(query, (point['id'],
394
                                                  reporting_start_datetime_utc,
395
                                                  reporting_end_datetime_utc))
396
                rows = cursor_historical.fetchall()
397
398
                if rows is not None and len(rows) > 0:
399
                    for row in rows:
400
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
401
                                                 timedelta(minutes=timezone_offset)
402
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
403
                        point_timestamps.append(current_datetime)
404
                        point_values.append(row[1])
405
406
            elif point['object_type'] == 'ENERGY_VALUE':
407
                query = (" SELECT utc_date_time, actual_value "
408
                         " FROM tbl_energy_value "
409
                         " WHERE point_id = %s "
410
                         "       AND utc_date_time BETWEEN %s AND %s "
411
                         " ORDER BY utc_date_time ")
412
                cursor_historical.execute(query, (point['id'],
413
                                                  reporting_start_datetime_utc,
414
                                                  reporting_end_datetime_utc))
415
                rows = cursor_historical.fetchall()
416
417
                if rows is not None and len(rows) > 0:
418
                    for row in rows:
419
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
420
                                                 timedelta(minutes=timezone_offset)
421
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
422
                        point_timestamps.append(current_datetime)
423
                        point_values.append(row[1])
424
            elif point['object_type'] == 'DIGITAL_VALUE':
425
                query = (" SELECT utc_date_time, actual_value "
426
                         " FROM tbl_digital_value "
427
                         " WHERE point_id = %s "
428
                         "       AND utc_date_time BETWEEN %s AND %s ")
429
                cursor_historical.execute(query, (point['id'],
430
                                                  reporting_start_datetime_utc,
431
                                                  reporting_end_datetime_utc))
432
                rows = cursor_historical.fetchall()
433
434
                if rows is not None and len(rows) > 0:
435
                    for row in rows:
436
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
437
                                                 timedelta(minutes=timezone_offset)
438
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
439
                        point_timestamps.append(current_datetime)
440
                        point_values.append(row[1])
441
442
            parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
443
            parameters_data['timestamps'].append(point_timestamps)
444
            parameters_data['values'].append(point_values)
445
446
        ################################################################################################################
447
        # Step 10: construct the report
448
        ################################################################################################################
449
        if cursor_system:
450
            cursor_system.close()
451
        if cnx_system:
452
            cnx_system.disconnect()
453
454
        if cursor_billing:
455
            cursor_billing.close()
456
        if cnx_billing:
457
            cnx_billing.disconnect()
458
459
        result = dict()
460
461
        result['shopfloor'] = dict()
462
        result['shopfloor']['name'] = shopfloor['name']
463
        result['shopfloor']['area'] = shopfloor['area']
464
465
        result['base_period'] = dict()
466
        result['base_period']['names'] = list()
467
        result['base_period']['units'] = list()
468
        result['base_period']['timestamps'] = list()
469
        result['base_period']['values'] = list()
470
        result['base_period']['subtotals'] = list()
471
        result['base_period']['total'] = Decimal(0.0)
472
        if energy_category_set is not None and len(energy_category_set) > 0:
473
            for energy_category_id in energy_category_set:
474
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
475
                result['base_period']['units'].append(config.currency_unit)
476
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
477
                result['base_period']['values'].append(base[energy_category_id]['values'])
478
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
479
                result['base_period']['total'] += base[energy_category_id]['subtotal']
480
481
        result['reporting_period'] = dict()
482
        result['reporting_period']['names'] = list()
483
        result['reporting_period']['energy_category_ids'] = list()
484
        result['reporting_period']['units'] = list()
485
        result['reporting_period']['timestamps'] = list()
486
        result['reporting_period']['values'] = list()
487
        result['reporting_period']['subtotals'] = list()
488
        result['reporting_period']['subtotals_per_unit_area'] = list()
489
        result['reporting_period']['toppeaks'] = list()
490
        result['reporting_period']['onpeaks'] = list()
491
        result['reporting_period']['midpeaks'] = list()
492
        result['reporting_period']['offpeaks'] = list()
493
        result['reporting_period']['increment_rates'] = list()
494
        result['reporting_period']['total'] = Decimal(0.0)
495
        result['reporting_period']['total_per_unit_area'] = Decimal(0.0)
496
        result['reporting_period']['total_increment_rate'] = Decimal(0.0)
497
        result['reporting_period']['total_unit'] = config.currency_unit
498
499
        if energy_category_set is not None and len(energy_category_set) > 0:
500
            for energy_category_id in energy_category_set:
501
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
502
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
503
                result['reporting_period']['units'].append(config.currency_unit)
504
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
505
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
506
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
507
                result['reporting_period']['subtotals_per_unit_area'].append(
508
                    reporting[energy_category_id]['subtotal'] / shopfloor['area'] if shopfloor['area'] > 0.0 else None)
509
                result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak'])
510
                result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak'])
511
                result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak'])
512
                result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak'])
513
                result['reporting_period']['increment_rates'].append(
514
                    (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) /
515
                    base[energy_category_id]['subtotal']
516
                    if base[energy_category_id]['subtotal'] > 0.0 else None)
517
                result['reporting_period']['total'] += reporting[energy_category_id]['subtotal']
518
519
        result['reporting_period']['total_per_unit_area'] = \
520
            result['reporting_period']['total'] / shopfloor['area'] if shopfloor['area'] > 0.0 else None
521
522
        result['reporting_period']['total_increment_rate'] = \
523
            (result['reporting_period']['total'] - result['base_period']['total']) / \
524
            result['base_period']['total'] \
525
            if result['base_period']['total'] > Decimal(0.0) else None
526
527
        result['parameters'] = {
528
            "names": parameters_data['names'],
529
            "timestamps": parameters_data['timestamps'],
530
            "values": parameters_data['values']
531
        }
532
533
        resp.body = json.dumps(result)
534

reports/tenantcost.py 1 location

@@ 10-531 (lines=522) @@
7
from decimal import *
8
9
10
class Reporting:
11
    @staticmethod
12
    def __init__():
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp):
17
        resp.status = falcon.HTTP_200
18
19
    ####################################################################################################################
20
    # PROCEDURES
21
    # Step 1: valid parameters
22
    # Step 2: query the tenant
23
    # Step 3: query energy categories
24
    # Step 4: query associated sensors
25
    # Step 5: query associated points
26
    # Step 6: query base period energy cost
27
    # Step 7: query reporting period energy cost
28
    # Step 8: query tariff data
29
    # Step 9: query associated sensors and points data
30
    # Step 10: construct the report
31
    ####################################################################################################################
32
    @staticmethod
33
    def on_get(req, resp):
34
        print(req.params)
35
        tenant_id = req.params.get('tenantid')
36
        period_type = req.params.get('periodtype')
37
        base_start_datetime_local = req.params.get('baseperiodstartdatetime')
38
        base_end_datetime_local = req.params.get('baseperiodenddatetime')
39
        reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime')
40
        reporting_end_datetime_local = req.params.get('reportingperiodenddatetime')
41
42
        ################################################################################################################
43
        # Step 1: valid parameters
44
        ################################################################################################################
45
        if tenant_id is None:
46
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID')
47
        else:
48
            tenant_id = str.strip(tenant_id)
49
            if not tenant_id.isdigit() or int(tenant_id) <= 0:
50
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID')
51
52
        if period_type is None:
53
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
54
        else:
55
            period_type = str.strip(period_type)
56
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
57
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
58
59
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
60
        if config.utc_offset[0] == '-':
61
            timezone_offset = -timezone_offset
62
63
        base_start_datetime_utc = None
64
        if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0:
65
            base_start_datetime_local = str.strip(base_start_datetime_local)
66
            try:
67
                base_start_datetime_utc = datetime.strptime(base_start_datetime_local,
68
                                                            '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
69
                    timedelta(minutes=timezone_offset)
70
            except ValueError:
71
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
72
                                       description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME")
73
74
        base_end_datetime_utc = None
75
        if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0:
76
            base_end_datetime_local = str.strip(base_end_datetime_local)
77
            try:
78
                base_end_datetime_utc = datetime.strptime(base_end_datetime_local,
79
                                                          '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
80
                    timedelta(minutes=timezone_offset)
81
            except ValueError:
82
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
83
                                       description="API.INVALID_BASE_PERIOD_ENDS_DATETIME")
84
85
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
86
                base_start_datetime_utc >= base_end_datetime_utc:
87
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
88
                                   description='API.INVALID_BASE_PERIOD_ENDS_DATETIME')
89
90
        if reporting_start_datetime_local is None:
91
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
92
                                   description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
93
        else:
94
            reporting_start_datetime_local = str.strip(reporting_start_datetime_local)
95
            try:
96
                reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local,
97
                                                                 '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
98
                    timedelta(minutes=timezone_offset)
99
            except ValueError:
100
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
101
                                       description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
102
103
        if reporting_end_datetime_local is None:
104
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
105
                                   description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
106
        else:
107
            reporting_end_datetime_local = str.strip(reporting_end_datetime_local)
108
            try:
109
                reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local,
110
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
111
                    timedelta(minutes=timezone_offset)
112
            except ValueError:
113
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
114
                                       description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
115
116
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
117
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
118
                                   description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME')
119
120
        ################################################################################################################
121
        # Step 2: query the tenant
122
        ################################################################################################################
123
        cnx_system = mysql.connector.connect(**config.myems_system_db)
124
        cursor_system = cnx_system.cursor()
125
126
        cnx_billing = mysql.connector.connect(**config.myems_billing_db)
127
        cursor_billing = cnx_billing.cursor()
128
129
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
130
        cursor_historical = cnx_historical.cursor()
131
132
        cursor_system.execute(" SELECT id, name, area, cost_center_id "
133
                              " FROM tbl_tenants "
134
                              " WHERE id = %s ", (tenant_id,))
135
        row_tenant = cursor_system.fetchone()
136
        if row_tenant is None:
137
            if cursor_system:
138
                cursor_system.close()
139
            if cnx_system:
140
                cnx_system.disconnect()
141
142
            if cursor_billing:
143
                cursor_billing.close()
144
            if cnx_billing:
145
                cnx_billing.disconnect()
146
147
            if cnx_historical:
148
                cnx_historical.close()
149
            if cursor_historical:
150
                cursor_historical.disconnect()
151
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND')
152
153
        tenant = dict()
154
        tenant['id'] = row_tenant[0]
155
        tenant['name'] = row_tenant[1]
156
        tenant['area'] = row_tenant[2]
157
        tenant['cost_center_id'] = row_tenant[3]
158
159
        ################################################################################################################
160
        # Step 3: query energy categories
161
        ################################################################################################################
162
        energy_category_set = set()
163
        # query energy categories in base period
164
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
165
                               " FROM tbl_tenant_input_category_hourly "
166
                               " WHERE tenant_id = %s "
167
                               "     AND start_datetime_utc >= %s "
168
                               "     AND start_datetime_utc < %s ",
169
                               (tenant['id'], base_start_datetime_utc, base_end_datetime_utc))
170
        rows_energy_categories = cursor_billing.fetchall()
171
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
172
            for row_energy_category in rows_energy_categories:
173
                energy_category_set.add(row_energy_category[0])
174
175
        # query energy categories in reporting period
176
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
177
                               " FROM tbl_tenant_input_category_hourly "
178
                               " WHERE tenant_id = %s "
179
                               "     AND start_datetime_utc >= %s "
180
                               "     AND start_datetime_utc < %s ",
181
                               (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
182
        rows_energy_categories = cursor_billing.fetchall()
183
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
184
            for row_energy_category in rows_energy_categories:
185
                energy_category_set.add(row_energy_category[0])
186
187
        # query all energy categories in base period and reporting period
188
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
189
                              " FROM tbl_energy_categories "
190
                              " ORDER BY id ", )
191
        rows_energy_categories = cursor_system.fetchall()
192
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
193
            if cursor_system:
194
                cursor_system.close()
195
            if cnx_system:
196
                cnx_system.disconnect()
197
198
            if cursor_billing:
199
                cursor_billing.close()
200
            if cnx_billing:
201
                cnx_billing.disconnect()
202
203
            if cnx_historical:
204
                cnx_historical.close()
205
            if cursor_historical:
206
                cursor_historical.disconnect()
207
            raise falcon.HTTPError(falcon.HTTP_404,
208
                                   title='API.NOT_FOUND',
209
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
210
        energy_category_dict = dict()
211
        for row_energy_category in rows_energy_categories:
212
            if row_energy_category[0] in energy_category_set:
213
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
214
                                                                "unit_of_measure": row_energy_category[2],
215
                                                                "kgce": row_energy_category[3],
216
                                                                "kgco2e": row_energy_category[4]}
217
218
        ################################################################################################################
219
        # Step 4: query associated sensors
220
        ################################################################################################################
221
        point_list = list()
222
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
223
                              " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, "
224
                              "      tbl_points p, tbl_sensors_points sp "
225
                              " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id "
226
                              "       AND s.id = sp.sensor_id AND sp.point_id = p.id "
227
                              " ORDER BY p.id ", (tenant['id'], ))
228
        rows_points = cursor_system.fetchall()
229
        if rows_points is not None and len(rows_points) > 0:
230
            for row in rows_points:
231
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
232
233
        ################################################################################################################
234
        # Step 5: query associated points
235
        ################################################################################################################
236
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
237
                              " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p "
238
                              " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id "
239
                              " ORDER BY p.id ", (tenant['id'], ))
240
        rows_points = cursor_system.fetchall()
241
        if rows_points is not None and len(rows_points) > 0:
242
            for row in rows_points:
243
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
244
245
        ################################################################################################################
246
        # Step 6: query base period energy cost
247
        ################################################################################################################
248
        base = dict()
249
        if energy_category_set is not None and len(energy_category_set) > 0:
250
            for energy_category_id in energy_category_set:
251
                base[energy_category_id] = dict()
252
                base[energy_category_id]['timestamps'] = list()
253
                base[energy_category_id]['values'] = list()
254
                base[energy_category_id]['subtotal'] = Decimal(0.0)
255
256
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
257
                                       " FROM tbl_tenant_input_category_hourly "
258
                                       " WHERE tenant_id = %s "
259
                                       "     AND energy_category_id = %s "
260
                                       "     AND start_datetime_utc >= %s "
261
                                       "     AND start_datetime_utc < %s "
262
                                       " ORDER BY start_datetime_utc ",
263
                                       (tenant['id'],
264
                                        energy_category_id,
265
                                        base_start_datetime_utc,
266
                                        base_end_datetime_utc))
267
                rows_tenant_hourly = cursor_billing.fetchall()
268
269
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
270
                                                                                     base_start_datetime_utc,
271
                                                                                     base_end_datetime_utc,
272
                                                                                     period_type)
273
                for row_tenant_periodically in rows_tenant_periodically:
274
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
275
                                             timedelta(minutes=timezone_offset)
276
                    if period_type == 'hourly':
277
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
278
                    elif period_type == 'daily':
279
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
280
                    elif period_type == 'monthly':
281
                        current_datetime = current_datetime_local.strftime('%Y-%m')
282
                    elif period_type == 'yearly':
283
                        current_datetime = current_datetime_local.strftime('%Y')
284
285
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
286
                    base[energy_category_id]['timestamps'].append(current_datetime)
287
                    base[energy_category_id]['values'].append(actual_value)
288
                    base[energy_category_id]['subtotal'] += actual_value
289
290
        ################################################################################################################
291
        # Step 7: query reporting period energy cost
292
        ################################################################################################################
293
        reporting = dict()
294
        if energy_category_set is not None and len(energy_category_set) > 0:
295
            for energy_category_id in energy_category_set:
296
                reporting[energy_category_id] = dict()
297
                reporting[energy_category_id]['timestamps'] = list()
298
                reporting[energy_category_id]['values'] = list()
299
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
300
                reporting[energy_category_id]['toppeak'] = Decimal(0.0)
301
                reporting[energy_category_id]['onpeak'] = Decimal(0.0)
302
                reporting[energy_category_id]['midpeak'] = Decimal(0.0)
303
                reporting[energy_category_id]['offpeak'] = Decimal(0.0)
304
305
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
306
                                       " FROM tbl_tenant_input_category_hourly "
307
                                       " WHERE tenant_id = %s "
308
                                       "     AND energy_category_id = %s "
309
                                       "     AND start_datetime_utc >= %s "
310
                                       "     AND start_datetime_utc < %s "
311
                                       " ORDER BY start_datetime_utc ",
312
                                       (tenant['id'],
313
                                        energy_category_id,
314
                                        reporting_start_datetime_utc,
315
                                        reporting_end_datetime_utc))
316
                rows_tenant_hourly = cursor_billing.fetchall()
317
318
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
319
                                                                                     reporting_start_datetime_utc,
320
                                                                                     reporting_end_datetime_utc,
321
                                                                                     period_type)
322
                for row_tenant_periodically in rows_tenant_periodically:
323
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
324
                                             timedelta(minutes=timezone_offset)
325
                    if period_type == 'hourly':
326
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
327
                    elif period_type == 'daily':
328
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
329
                    elif period_type == 'monthly':
330
                        current_datetime = current_datetime_local.strftime('%Y-%m')
331
                    elif period_type == 'yearly':
332
                        current_datetime = current_datetime_local.strftime('%Y')
333
334
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
335
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
336
                    reporting[energy_category_id]['values'].append(actual_value)
337
                    reporting[energy_category_id]['subtotal'] += actual_value
338
339
                energy_category_tariff_dict = utilities.get_energy_category_peak_types(tenant['cost_center_id'],
340
                                                                                       energy_category_id,
341
                                                                                       reporting_start_datetime_utc,
342
                                                                                       reporting_end_datetime_utc)
343
                for row in rows_tenant_hourly:
344
                    peak_type = energy_category_tariff_dict.get(row[0], None)
345
                    if peak_type == 'toppeak':
346
                        reporting[energy_category_id]['toppeak'] += row[1]
347
                    elif peak_type == 'onpeak':
348
                        reporting[energy_category_id]['onpeak'] += row[1]
349
                    elif peak_type == 'midpeak':
350
                        reporting[energy_category_id]['midpeak'] += row[1]
351
                    elif peak_type == 'offpeak':
352
                        reporting[energy_category_id]['offpeak'] += row[1]
353
354
        ################################################################################################################
355
        # Step 8: query tariff data
356
        ################################################################################################################
357
        parameters_data = dict()
358
        parameters_data['names'] = list()
359
        parameters_data['timestamps'] = list()
360
        parameters_data['values'] = list()
361
        if energy_category_set is not None and len(energy_category_set) > 0:
362
            for energy_category_id in energy_category_set:
363
                energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'],
364
                                                                                    energy_category_id,
365
                                                                                    reporting_start_datetime_utc,
366
                                                                                    reporting_end_datetime_utc)
367
                tariff_timestamp_list = list()
368
                tariff_value_list = list()
369
                for k, v in energy_category_tariff_dict.items():
370
                    # convert k from utc to local
371
                    k = k + timedelta(minutes=timezone_offset)
372
                    tariff_timestamp_list.append(k.isoformat()[0:19][0:19])
373
                    tariff_value_list.append(v)
374
375
                parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name'])
376
                parameters_data['timestamps'].append(tariff_timestamp_list)
377
                parameters_data['values'].append(tariff_value_list)
378
379
        ################################################################################################################
380
        # Step 9: query associated sensors and points data
381
        ################################################################################################################
382
        for point in point_list:
383
            point_values = []
384
            point_timestamps = []
385
            if point['object_type'] == 'ANALOG_VALUE':
386
                query = (" SELECT utc_date_time, actual_value "
387
                         " FROM tbl_analog_value "
388
                         " WHERE point_id = %s "
389
                         "       AND utc_date_time BETWEEN %s AND %s "
390
                         " ORDER BY utc_date_time ")
391
                cursor_historical.execute(query, (point['id'],
392
                                                  reporting_start_datetime_utc,
393
                                                  reporting_end_datetime_utc))
394
                rows = cursor_historical.fetchall()
395
396
                if rows is not None and len(rows) > 0:
397
                    for row in rows:
398
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
399
                                                 timedelta(minutes=timezone_offset)
400
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
401
                        point_timestamps.append(current_datetime)
402
                        point_values.append(row[1])
403
404
            elif point['object_type'] == 'ENERGY_VALUE':
405
                query = (" SELECT utc_date_time, actual_value "
406
                         " FROM tbl_energy_value "
407
                         " WHERE point_id = %s "
408
                         "       AND utc_date_time BETWEEN %s AND %s "
409
                         " ORDER BY utc_date_time ")
410
                cursor_historical.execute(query, (point['id'],
411
                                                  reporting_start_datetime_utc,
412
                                                  reporting_end_datetime_utc))
413
                rows = cursor_historical.fetchall()
414
415
                if rows is not None and len(rows) > 0:
416
                    for row in rows:
417
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
418
                                                 timedelta(minutes=timezone_offset)
419
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
420
                        point_timestamps.append(current_datetime)
421
                        point_values.append(row[1])
422
            elif point['object_type'] == 'DIGITAL_VALUE':
423
                query = (" SELECT utc_date_time, actual_value "
424
                         " FROM tbl_digital_value "
425
                         " WHERE point_id = %s "
426
                         "       AND utc_date_time BETWEEN %s AND %s ")
427
                cursor_historical.execute(query, (point['id'],
428
                                                  reporting_start_datetime_utc,
429
                                                  reporting_end_datetime_utc))
430
                rows = cursor_historical.fetchall()
431
432
                if rows is not None and len(rows) > 0:
433
                    for row in rows:
434
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
435
                                                 timedelta(minutes=timezone_offset)
436
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
437
                        point_timestamps.append(current_datetime)
438
                        point_values.append(row[1])
439
440
            parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
441
            parameters_data['timestamps'].append(point_timestamps)
442
            parameters_data['values'].append(point_values)
443
444
        ################################################################################################################
445
        # Step 10: construct the report
446
        ################################################################################################################
447
        if cursor_system:
448
            cursor_system.close()
449
        if cnx_system:
450
            cnx_system.disconnect()
451
452
        if cursor_billing:
453
            cursor_billing.close()
454
        if cnx_billing:
455
            cnx_billing.disconnect()
456
457
        result = dict()
458
459
        result['tenant'] = dict()
460
        result['tenant']['name'] = tenant['name']
461
        result['tenant']['area'] = tenant['area']
462
463
        result['base_period'] = dict()
464
        result['base_period']['names'] = list()
465
        result['base_period']['units'] = list()
466
        result['base_period']['timestamps'] = list()
467
        result['base_period']['values'] = list()
468
        result['base_period']['subtotals'] = list()
469
        result['base_period']['total'] = Decimal(0.0)
470
        if energy_category_set is not None and len(energy_category_set) > 0:
471
            for energy_category_id in energy_category_set:
472
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
473
                result['base_period']['units'].append(config.currency_unit)
474
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
475
                result['base_period']['values'].append(base[energy_category_id]['values'])
476
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
477
                result['base_period']['total'] += base[energy_category_id]['subtotal']
478
479
        result['reporting_period'] = dict()
480
        result['reporting_period']['names'] = list()
481
        result['reporting_period']['energy_category_ids'] = list()
482
        result['reporting_period']['units'] = list()
483
        result['reporting_period']['timestamps'] = list()
484
        result['reporting_period']['values'] = list()
485
        result['reporting_period']['subtotals'] = list()
486
        result['reporting_period']['subtotals_per_unit_area'] = list()
487
        result['reporting_period']['toppeaks'] = list()
488
        result['reporting_period']['onpeaks'] = list()
489
        result['reporting_period']['midpeaks'] = list()
490
        result['reporting_period']['offpeaks'] = list()
491
        result['reporting_period']['increment_rates'] = list()
492
        result['reporting_period']['total'] = Decimal(0.0)
493
        result['reporting_period']['total_per_unit_area'] = Decimal(0.0)
494
        result['reporting_period']['total_increment_rate'] = Decimal(0.0)
495
        result['reporting_period']['total_unit'] = config.currency_unit
496
497
        if energy_category_set is not None and len(energy_category_set) > 0:
498
            for energy_category_id in energy_category_set:
499
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
500
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
501
                result['reporting_period']['units'].append(config.currency_unit)
502
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
503
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
504
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
505
                result['reporting_period']['subtotals_per_unit_area'].append(
506
                    reporting[energy_category_id]['subtotal'] / tenant['area'] if tenant['area'] > 0.0 else None)
507
                result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak'])
508
                result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak'])
509
                result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak'])
510
                result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak'])
511
                result['reporting_period']['increment_rates'].append(
512
                    (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) /
513
                    base[energy_category_id]['subtotal']
514
                    if base[energy_category_id]['subtotal'] > 0.0 else None)
515
                result['reporting_period']['total'] += reporting[energy_category_id]['subtotal']
516
517
        result['reporting_period']['total_per_unit_area'] = \
518
            result['reporting_period']['total'] / tenant['area'] if tenant['area'] > 0.0 else None
519
520
        result['reporting_period']['total_increment_rate'] = \
521
            (result['reporting_period']['total'] - result['base_period']['total']) / \
522
            result['base_period']['total'] \
523
            if result['base_period']['total'] > Decimal(0.0) else None
524
525
        result['parameters'] = {
526
            "names": parameters_data['names'],
527
            "timestamps": parameters_data['timestamps'],
528
            "values": parameters_data['values']
529
        }
530
531
        resp.body = json.dumps(result)
532

reports/storecost.py 1 location

@@ 10-531 (lines=522) @@
7
from decimal import *
8
9
10
class Reporting:
11
    @staticmethod
12
    def __init__():
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp):
17
        resp.status = falcon.HTTP_200
18
19
    ####################################################################################################################
20
    # PROCEDURES
21
    # Step 1: valid parameters
22
    # Step 2: query the store
23
    # Step 3: query energy categories
24
    # Step 4: query associated sensors
25
    # Step 5: query associated points
26
    # Step 6: query base period energy cost
27
    # Step 7: query reporting period energy cost
28
    # Step 8: query tariff data
29
    # Step 9: query associated sensors and points data
30
    # Step 10: construct the report
31
    ####################################################################################################################
32
    @staticmethod
33
    def on_get(req, resp):
34
        print(req.params)
35
        store_id = req.params.get('storeid')
36
        period_type = req.params.get('periodtype')
37
        base_start_datetime_local = req.params.get('baseperiodstartdatetime')
38
        base_end_datetime_local = req.params.get('baseperiodenddatetime')
39
        reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime')
40
        reporting_end_datetime_local = req.params.get('reportingperiodenddatetime')
41
42
        ################################################################################################################
43
        # Step 1: valid parameters
44
        ################################################################################################################
45
        if store_id is None:
46
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID')
47
        else:
48
            store_id = str.strip(store_id)
49
            if not store_id.isdigit() or int(store_id) <= 0:
50
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID')
51
52
        if period_type is None:
53
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
54
        else:
55
            period_type = str.strip(period_type)
56
            if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
57
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
58
59
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
60
        if config.utc_offset[0] == '-':
61
            timezone_offset = -timezone_offset
62
63
        base_start_datetime_utc = None
64
        if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0:
65
            base_start_datetime_local = str.strip(base_start_datetime_local)
66
            try:
67
                base_start_datetime_utc = datetime.strptime(base_start_datetime_local,
68
                                                            '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
69
                    timedelta(minutes=timezone_offset)
70
            except ValueError:
71
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
72
                                       description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME")
73
74
        base_end_datetime_utc = None
75
        if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0:
76
            base_end_datetime_local = str.strip(base_end_datetime_local)
77
            try:
78
                base_end_datetime_utc = datetime.strptime(base_end_datetime_local,
79
                                                          '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
80
                    timedelta(minutes=timezone_offset)
81
            except ValueError:
82
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
83
                                       description="API.INVALID_BASE_PERIOD_ENDS_DATETIME")
84
85
        if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \
86
                base_start_datetime_utc >= base_end_datetime_utc:
87
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
88
                                   description='API.INVALID_BASE_PERIOD_ENDS_DATETIME')
89
90
        if reporting_start_datetime_local is None:
91
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
92
                                   description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
93
        else:
94
            reporting_start_datetime_local = str.strip(reporting_start_datetime_local)
95
            try:
96
                reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local,
97
                                                                 '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
98
                    timedelta(minutes=timezone_offset)
99
            except ValueError:
100
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
101
                                       description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME")
102
103
        if reporting_end_datetime_local is None:
104
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
105
                                   description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
106
        else:
107
            reporting_end_datetime_local = str.strip(reporting_end_datetime_local)
108
            try:
109
                reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local,
110
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
111
                    timedelta(minutes=timezone_offset)
112
            except ValueError:
113
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
114
                                       description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME")
115
116
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
117
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
118
                                   description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME')
119
120
        ################################################################################################################
121
        # Step 2: query the store
122
        ################################################################################################################
123
        cnx_system = mysql.connector.connect(**config.myems_system_db)
124
        cursor_system = cnx_system.cursor()
125
126
        cnx_billing = mysql.connector.connect(**config.myems_billing_db)
127
        cursor_billing = cnx_billing.cursor()
128
129
        cnx_historical = mysql.connector.connect(**config.myems_historical_db)
130
        cursor_historical = cnx_historical.cursor()
131
132
        cursor_system.execute(" SELECT id, name, area, cost_center_id "
133
                              " FROM tbl_stores "
134
                              " WHERE id = %s ", (store_id,))
135
        row_store = cursor_system.fetchone()
136
        if row_store is None:
137
            if cursor_system:
138
                cursor_system.close()
139
            if cnx_system:
140
                cnx_system.disconnect()
141
142
            if cursor_billing:
143
                cursor_billing.close()
144
            if cnx_billing:
145
                cnx_billing.disconnect()
146
147
            if cnx_historical:
148
                cnx_historical.close()
149
            if cursor_historical:
150
                cursor_historical.disconnect()
151
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND')
152
153
        store = dict()
154
        store['id'] = row_store[0]
155
        store['name'] = row_store[1]
156
        store['area'] = row_store[2]
157
        store['cost_center_id'] = row_store[3]
158
159
        ################################################################################################################
160
        # Step 3: query energy categories
161
        ################################################################################################################
162
        energy_category_set = set()
163
        # query energy categories in base period
164
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
165
                               " FROM tbl_store_input_category_hourly "
166
                               " WHERE store_id = %s "
167
                               "     AND start_datetime_utc >= %s "
168
                               "     AND start_datetime_utc < %s ",
169
                               (store['id'], base_start_datetime_utc, base_end_datetime_utc))
170
        rows_energy_categories = cursor_billing.fetchall()
171
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
172
            for row_energy_category in rows_energy_categories:
173
                energy_category_set.add(row_energy_category[0])
174
175
        # query energy categories in reporting period
176
        cursor_billing.execute(" SELECT DISTINCT(energy_category_id) "
177
                               " FROM tbl_store_input_category_hourly "
178
                               " WHERE store_id = %s "
179
                               "     AND start_datetime_utc >= %s "
180
                               "     AND start_datetime_utc < %s ",
181
                               (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
182
        rows_energy_categories = cursor_billing.fetchall()
183
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
184
            for row_energy_category in rows_energy_categories:
185
                energy_category_set.add(row_energy_category[0])
186
187
        # query all energy categories in base period and reporting period
188
        cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e "
189
                              " FROM tbl_energy_categories "
190
                              " ORDER BY id ", )
191
        rows_energy_categories = cursor_system.fetchall()
192
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
193
            if cursor_system:
194
                cursor_system.close()
195
            if cnx_system:
196
                cnx_system.disconnect()
197
198
            if cursor_billing:
199
                cursor_billing.close()
200
            if cnx_billing:
201
                cnx_billing.disconnect()
202
203
            if cnx_historical:
204
                cnx_historical.close()
205
            if cursor_historical:
206
                cursor_historical.disconnect()
207
            raise falcon.HTTPError(falcon.HTTP_404,
208
                                   title='API.NOT_FOUND',
209
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
210
        energy_category_dict = dict()
211
        for row_energy_category in rows_energy_categories:
212
            if row_energy_category[0] in energy_category_set:
213
                energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1],
214
                                                                "unit_of_measure": row_energy_category[2],
215
                                                                "kgce": row_energy_category[3],
216
                                                                "kgco2e": row_energy_category[4]}
217
218
        ################################################################################################################
219
        # Step 4: query associated sensors
220
        ################################################################################################################
221
        point_list = list()
222
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
223
                              " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, "
224
                              "      tbl_points p, tbl_sensors_points sp "
225
                              " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id "
226
                              "       AND se.id = sp.sensor_id AND sp.point_id = p.id "
227
                              " ORDER BY p.id ", (store['id'], ))
228
        rows_points = cursor_system.fetchall()
229
        if rows_points is not None and len(rows_points) > 0:
230
            for row in rows_points:
231
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
232
233
        ################################################################################################################
234
        # Step 5: query associated points
235
        ################################################################################################################
236
        cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type  "
237
                              " FROM tbl_stores s, tbl_stores_points sp, tbl_points p "
238
                              " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id "
239
                              " ORDER BY p.id ", (store['id'], ))
240
        rows_points = cursor_system.fetchall()
241
        if rows_points is not None and len(rows_points) > 0:
242
            for row in rows_points:
243
                point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]})
244
245
        ################################################################################################################
246
        # Step 6: query base period energy cost
247
        ################################################################################################################
248
        base = dict()
249
        if energy_category_set is not None and len(energy_category_set) > 0:
250
            for energy_category_id in energy_category_set:
251
                base[energy_category_id] = dict()
252
                base[energy_category_id]['timestamps'] = list()
253
                base[energy_category_id]['values'] = list()
254
                base[energy_category_id]['subtotal'] = Decimal(0.0)
255
256
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
257
                                       " FROM tbl_store_input_category_hourly "
258
                                       " WHERE store_id = %s "
259
                                       "     AND energy_category_id = %s "
260
                                       "     AND start_datetime_utc >= %s "
261
                                       "     AND start_datetime_utc < %s "
262
                                       " ORDER BY start_datetime_utc ",
263
                                       (store['id'],
264
                                        energy_category_id,
265
                                        base_start_datetime_utc,
266
                                        base_end_datetime_utc))
267
                rows_store_hourly = cursor_billing.fetchall()
268
269
                rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly,
270
                                                                                    base_start_datetime_utc,
271
                                                                                    base_end_datetime_utc,
272
                                                                                    period_type)
273
                for row_store_periodically in rows_store_periodically:
274
                    current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \
275
                                             timedelta(minutes=timezone_offset)
276
                    if period_type == 'hourly':
277
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
278
                    elif period_type == 'daily':
279
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
280
                    elif period_type == 'monthly':
281
                        current_datetime = current_datetime_local.strftime('%Y-%m')
282
                    elif period_type == 'yearly':
283
                        current_datetime = current_datetime_local.strftime('%Y')
284
285
                    actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1]
286
                    base[energy_category_id]['timestamps'].append(current_datetime)
287
                    base[energy_category_id]['values'].append(actual_value)
288
                    base[energy_category_id]['subtotal'] += actual_value
289
290
        ################################################################################################################
291
        # Step 7: query reporting period energy cost
292
        ################################################################################################################
293
        reporting = dict()
294
        if energy_category_set is not None and len(energy_category_set) > 0:
295
            for energy_category_id in energy_category_set:
296
                reporting[energy_category_id] = dict()
297
                reporting[energy_category_id]['timestamps'] = list()
298
                reporting[energy_category_id]['values'] = list()
299
                reporting[energy_category_id]['subtotal'] = Decimal(0.0)
300
                reporting[energy_category_id]['toppeak'] = Decimal(0.0)
301
                reporting[energy_category_id]['onpeak'] = Decimal(0.0)
302
                reporting[energy_category_id]['midpeak'] = Decimal(0.0)
303
                reporting[energy_category_id]['offpeak'] = Decimal(0.0)
304
305
                cursor_billing.execute(" SELECT start_datetime_utc, actual_value "
306
                                       " FROM tbl_store_input_category_hourly "
307
                                       " WHERE store_id = %s "
308
                                       "     AND energy_category_id = %s "
309
                                       "     AND start_datetime_utc >= %s "
310
                                       "     AND start_datetime_utc < %s "
311
                                       " ORDER BY start_datetime_utc ",
312
                                       (store['id'],
313
                                        energy_category_id,
314
                                        reporting_start_datetime_utc,
315
                                        reporting_end_datetime_utc))
316
                rows_store_hourly = cursor_billing.fetchall()
317
318
                rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly,
319
                                                                                    reporting_start_datetime_utc,
320
                                                                                    reporting_end_datetime_utc,
321
                                                                                    period_type)
322
                for row_store_periodically in rows_store_periodically:
323
                    current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \
324
                                             timedelta(minutes=timezone_offset)
325
                    if period_type == 'hourly':
326
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
327
                    elif period_type == 'daily':
328
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
329
                    elif period_type == 'monthly':
330
                        current_datetime = current_datetime_local.strftime('%Y-%m')
331
                    elif period_type == 'yearly':
332
                        current_datetime = current_datetime_local.strftime('%Y')
333
334
                    actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1]
335
                    reporting[energy_category_id]['timestamps'].append(current_datetime)
336
                    reporting[energy_category_id]['values'].append(actual_value)
337
                    reporting[energy_category_id]['subtotal'] += actual_value
338
339
                energy_category_tariff_dict = utilities.get_energy_category_peak_types(store['cost_center_id'],
340
                                                                                       energy_category_id,
341
                                                                                       reporting_start_datetime_utc,
342
                                                                                       reporting_end_datetime_utc)
343
                for row in rows_store_hourly:
344
                    peak_type = energy_category_tariff_dict.get(row[0], None)
345
                    if peak_type == 'toppeak':
346
                        reporting[energy_category_id]['toppeak'] += row[1]
347
                    elif peak_type == 'onpeak':
348
                        reporting[energy_category_id]['onpeak'] += row[1]
349
                    elif peak_type == 'midpeak':
350
                        reporting[energy_category_id]['midpeak'] += row[1]
351
                    elif peak_type == 'offpeak':
352
                        reporting[energy_category_id]['offpeak'] += row[1]
353
354
        ################################################################################################################
355
        # Step 8: query tariff data
356
        ################################################################################################################
357
        parameters_data = dict()
358
        parameters_data['names'] = list()
359
        parameters_data['timestamps'] = list()
360
        parameters_data['values'] = list()
361
        if energy_category_set is not None and len(energy_category_set) > 0:
362
            for energy_category_id in energy_category_set:
363
                energy_category_tariff_dict = utilities.get_energy_category_tariffs(store['cost_center_id'],
364
                                                                                    energy_category_id,
365
                                                                                    reporting_start_datetime_utc,
366
                                                                                    reporting_end_datetime_utc)
367
                tariff_timestamp_list = list()
368
                tariff_value_list = list()
369
                for k, v in energy_category_tariff_dict.items():
370
                    # convert k from utc to local
371
                    k = k + timedelta(minutes=timezone_offset)
372
                    tariff_timestamp_list.append(k.isoformat()[0:19][0:19])
373
                    tariff_value_list.append(v)
374
375
                parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name'])
376
                parameters_data['timestamps'].append(tariff_timestamp_list)
377
                parameters_data['values'].append(tariff_value_list)
378
379
        ################################################################################################################
380
        # Step 9: query associated sensors and points data
381
        ################################################################################################################
382
        for point in point_list:
383
            point_values = []
384
            point_timestamps = []
385
            if point['object_type'] == 'ANALOG_VALUE':
386
                query = (" SELECT utc_date_time, actual_value "
387
                         " FROM tbl_analog_value "
388
                         " WHERE point_id = %s "
389
                         "       AND utc_date_time BETWEEN %s AND %s "
390
                         " ORDER BY utc_date_time ")
391
                cursor_historical.execute(query, (point['id'],
392
                                                  reporting_start_datetime_utc,
393
                                                  reporting_end_datetime_utc))
394
                rows = cursor_historical.fetchall()
395
396
                if rows is not None and len(rows) > 0:
397
                    for row in rows:
398
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
399
                                                 timedelta(minutes=timezone_offset)
400
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
401
                        point_timestamps.append(current_datetime)
402
                        point_values.append(row[1])
403
404
            elif point['object_type'] == 'ENERGY_VALUE':
405
                query = (" SELECT utc_date_time, actual_value "
406
                         " FROM tbl_energy_value "
407
                         " WHERE point_id = %s "
408
                         "       AND utc_date_time BETWEEN %s AND %s "
409
                         " ORDER BY utc_date_time ")
410
                cursor_historical.execute(query, (point['id'],
411
                                                  reporting_start_datetime_utc,
412
                                                  reporting_end_datetime_utc))
413
                rows = cursor_historical.fetchall()
414
415
                if rows is not None and len(rows) > 0:
416
                    for row in rows:
417
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
418
                                                 timedelta(minutes=timezone_offset)
419
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
420
                        point_timestamps.append(current_datetime)
421
                        point_values.append(row[1])
422
            elif point['object_type'] == 'DIGITAL_VALUE':
423
                query = (" SELECT utc_date_time, actual_value "
424
                         " FROM tbl_digital_value "
425
                         " WHERE point_id = %s "
426
                         "       AND utc_date_time BETWEEN %s AND %s ")
427
                cursor_historical.execute(query, (point['id'],
428
                                                  reporting_start_datetime_utc,
429
                                                  reporting_end_datetime_utc))
430
                rows = cursor_historical.fetchall()
431
432
                if rows is not None and len(rows) > 0:
433
                    for row in rows:
434
                        current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \
435
                                                 timedelta(minutes=timezone_offset)
436
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
437
                        point_timestamps.append(current_datetime)
438
                        point_values.append(row[1])
439
440
            parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')')
441
            parameters_data['timestamps'].append(point_timestamps)
442
            parameters_data['values'].append(point_values)
443
444
        ################################################################################################################
445
        # Step 10: construct the report
446
        ################################################################################################################
447
        if cursor_system:
448
            cursor_system.close()
449
        if cnx_system:
450
            cnx_system.disconnect()
451
452
        if cursor_billing:
453
            cursor_billing.close()
454
        if cnx_billing:
455
            cnx_billing.disconnect()
456
457
        result = dict()
458
459
        result['store'] = dict()
460
        result['store']['name'] = store['name']
461
        result['store']['area'] = store['area']
462
463
        result['base_period'] = dict()
464
        result['base_period']['names'] = list()
465
        result['base_period']['units'] = list()
466
        result['base_period']['timestamps'] = list()
467
        result['base_period']['values'] = list()
468
        result['base_period']['subtotals'] = list()
469
        result['base_period']['total'] = Decimal(0.0)
470
        if energy_category_set is not None and len(energy_category_set) > 0:
471
            for energy_category_id in energy_category_set:
472
                result['base_period']['names'].append(energy_category_dict[energy_category_id]['name'])
473
                result['base_period']['units'].append(config.currency_unit)
474
                result['base_period']['timestamps'].append(base[energy_category_id]['timestamps'])
475
                result['base_period']['values'].append(base[energy_category_id]['values'])
476
                result['base_period']['subtotals'].append(base[energy_category_id]['subtotal'])
477
                result['base_period']['total'] += base[energy_category_id]['subtotal']
478
479
        result['reporting_period'] = dict()
480
        result['reporting_period']['names'] = list()
481
        result['reporting_period']['energy_category_ids'] = list()
482
        result['reporting_period']['units'] = list()
483
        result['reporting_period']['timestamps'] = list()
484
        result['reporting_period']['values'] = list()
485
        result['reporting_period']['subtotals'] = list()
486
        result['reporting_period']['subtotals_per_unit_area'] = list()
487
        result['reporting_period']['toppeaks'] = list()
488
        result['reporting_period']['onpeaks'] = list()
489
        result['reporting_period']['midpeaks'] = list()
490
        result['reporting_period']['offpeaks'] = list()
491
        result['reporting_period']['increment_rates'] = list()
492
        result['reporting_period']['total'] = Decimal(0.0)
493
        result['reporting_period']['total_per_unit_area'] = Decimal(0.0)
494
        result['reporting_period']['total_increment_rate'] = Decimal(0.0)
495
        result['reporting_period']['total_unit'] = config.currency_unit
496
497
        if energy_category_set is not None and len(energy_category_set) > 0:
498
            for energy_category_id in energy_category_set:
499
                result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name'])
500
                result['reporting_period']['energy_category_ids'].append(energy_category_id)
501
                result['reporting_period']['units'].append(config.currency_unit)
502
                result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps'])
503
                result['reporting_period']['values'].append(reporting[energy_category_id]['values'])
504
                result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal'])
505
                result['reporting_period']['subtotals_per_unit_area'].append(
506
                    reporting[energy_category_id]['subtotal'] / store['area'] if store['area'] > 0.0 else None)
507
                result['reporting_period']['toppeaks'].append(reporting[energy_category_id]['toppeak'])
508
                result['reporting_period']['onpeaks'].append(reporting[energy_category_id]['onpeak'])
509
                result['reporting_period']['midpeaks'].append(reporting[energy_category_id]['midpeak'])
510
                result['reporting_period']['offpeaks'].append(reporting[energy_category_id]['offpeak'])
511
                result['reporting_period']['increment_rates'].append(
512
                    (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) /
513
                    base[energy_category_id]['subtotal']
514
                    if base[energy_category_id]['subtotal'] > 0.0 else None)
515
                result['reporting_period']['total'] += reporting[energy_category_id]['subtotal']
516
517
        result['reporting_period']['total_per_unit_area'] = \
518
            result['reporting_period']['total'] / store['area'] if store['area'] > 0.0 else None
519
520
        result['reporting_period']['total_increment_rate'] = \
521
            (result['reporting_period']['total'] - result['base_period']['total']) / \
522
            result['base_period']['total'] \
523
            if result['base_period']['total'] > Decimal(0.0) else None
524
525
        result['parameters'] = {
526
            "names": parameters_data['names'],
527
            "timestamps": parameters_data['timestamps'],
528
            "values": parameters_data['values']
529
        }
530
531
        resp.body = json.dumps(result)
532