Passed
Push — master ( ce9807...863545 )
by Guangyu
01:54 queued 11s
created

reports.distributionsystem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Reporting.__init__() 0 3 1
B Reporting.on_get() 0 60 7
A Reporting.on_options() 0 3 1
1
import falcon
2
import simplejson as json
3
import mysql.connector
4
from datetime import datetime, timedelta, timezone
5
from core import utilities
6
from decimal import Decimal
7
import config
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 distribution system
23
    # Step 3: query meters
24
    # Step 4: query reporting period energy input
25
    # Step 5: construct the report
26
    ####################################################################################################################
27
    @staticmethod
28
    def on_get(req, resp):
29
        print(req.params)
30
        reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime')
31
        reporting_end_datetime_local = req.params.get('reportingperiodenddatetime')
32
33
        ################################################################################################################
34
        # Step 1: valid parameters
35
        ################################################################################################################
36
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
37
        if config.utc_offset[0] == '-':
38
            timezone_offset = -timezone_offset
39
40
        if reporting_start_datetime_local is None:
41
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
42
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
43
        else:
44
            reporting_start_datetime_local = str.strip(reporting_start_datetime_local)
45
            try:
46
                reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local,
47
                                                                 '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
48
                    timedelta(minutes=timezone_offset)
49
            except ValueError:
50
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
51
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
52
53
        if reporting_end_datetime_local is None:
54
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
55
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
56
        else:
57
            reporting_end_datetime_local = str.strip(reporting_end_datetime_local)
58
            try:
59
                reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local,
60
                                                               '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
61
                    timedelta(minutes=timezone_offset)
62
            except ValueError:
63
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
64
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
65
66
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
67
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
68
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
69
        ################################################################################################################
70
        # Step 2: query the distribution system
71
        ################################################################################################################
72
73
        ################################################################################################################
74
        # Step 3: query meters
75
        ################################################################################################################
76
77
        ################################################################################################################
78
        # Step 4: query reporting period energy input
79
        ################################################################################################################
80
81
        ################################################################################################################
82
        # Step 5: construct the report
83
        ################################################################################################################
84
85
        result = {}
86
        resp.body = json.dumps(result)
87