Passed
Push — master ( e26986...50d9b5 )
by Guangyu
89:06 queued 11s
created

reports.storebatch   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 240
Duplicated Lines 5.83 %

Importance

Changes 0
Metric Value
wmc 43
eloc 160
dl 14
loc 240
rs 8.96
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Reporting.__init__() 0 3 1
A Reporting.on_options() 0 3 1
F Reporting.on_get() 14 211 41

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complexity

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

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

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

1
import falcon
2
import simplejson as json
3
import mysql.connector
4
import config
5
from anytree import Node, AnyNode, LevelOrderIter
6
from datetime import datetime, timedelta, timezone
7
from decimal import Decimal
8
import excelexporters.storebatch
9
10
11
class Reporting:
12
    @staticmethod
13
    def __init__():
14
        pass
15
16
    @staticmethod
17
    def on_options(req, resp):
18
        resp.status = falcon.HTTP_200
19
20
    ####################################################################################################################
21
    # PROCEDURES
22
    # Step 1: valid parameters
23
    # Step 2: build a space tree
24
    # Step 3: query all stores in the space tree
25
    # Step 4: query energy categories
26
    # Step 5: query reporting period energy input
27
    # Step 6: construct the report
28
    ####################################################################################################################
29
    @staticmethod
30
    def on_get(req, resp):
31
        print(req.params)
32
        space_id = req.params.get('spaceid')
33
        reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime')
34
        reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime')
35
36
        ################################################################################################################
37
        # Step 1: valid parameters
38
        ################################################################################################################
39
        if space_id is None:
40
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID')
41
        else:
42
            space_id = str.strip(space_id)
43
            if not space_id.isdigit() or int(space_id) <= 0:
44
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID')
45
            else:
46
                space_id = int(space_id)
47
48
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
49
        if config.utc_offset[0] == '-':
50
            timezone_offset = -timezone_offset
51
52
        if reporting_period_start_datetime_local is None:
53
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
54
                                   description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
55
        else:
56
            reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local)
57
            try:
58
                reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local,
59
                                                                 '%Y-%m-%dT%H:%M:%S')
60
            except ValueError:
61
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
62
                                       description="API.INVALID_REPORTING_PERIOD_START_DATETIME")
63
            reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \
64
                timedelta(minutes=timezone_offset)
65
66
        if reporting_period_end_datetime_local is None:
67
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
68
                                   description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
69
        else:
70
            reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local)
71
            try:
72
                reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local,
73
                                                               '%Y-%m-%dT%H:%M:%S')
74
            except ValueError:
75
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
76
                                       description="API.INVALID_REPORTING_PERIOD_END_DATETIME")
77
            reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \
78
                timedelta(minutes=timezone_offset)
79
80
        if reporting_start_datetime_utc >= reporting_end_datetime_utc:
81
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
82
                                   description='API.INVALID_REPORTING_PERIOD_END_DATETIME')
83
84
        cnx_system_db = mysql.connector.connect(**config.myems_system_db)
85
        cursor_system_db = cnx_system_db.cursor(dictionary=True)
86
87
        cursor_system_db.execute(" SELECT name "
88
                                 " FROM tbl_spaces "
89
                                 " WHERE id = %s ", (space_id,))
90
        row = cursor_system_db.fetchone()
91
92
        if row is None:
93
            if cursor_system_db:
94
                cursor_system_db.close()
95
            if cnx_system_db:
96
                cnx_system_db.disconnect()
97
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
98
                                   description='API.SPACE_NOT_FOUND')
99
        else:
100
            space_name = row['name']
101
102
        ################################################################################################################
103
        # Step 2: build a space tree
104
        ################################################################################################################
105
106
        query = (" SELECT id, name, parent_space_id "
107
                 " FROM tbl_spaces "
108
                 " ORDER BY id ")
109
        cursor_system_db.execute(query)
110
        rows_spaces = cursor_system_db.fetchall()
111
        node_dict = dict()
112
        if rows_spaces is not None and len(rows_spaces) > 0:
113
            for row in rows_spaces:
114
                parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None
115
                node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name'])
116
117
        ################################################################################################################
118
        # Step 3: query all stores in the space tree
119
        ################################################################################################################
120
        store_dict = dict()
121
        space_dict = dict()
122
123
        for node in LevelOrderIter(node_dict[space_id]):
124
            space_dict[node.id] = node.name
125
126
        cursor_system_db.execute(" SELECT store.id, store.name AS store_name, s.name AS space_name, "
127
                                 "        cc.name AS cost_center_name, store.description "
128
                                 " FROM tbl_spaces s, tbl_spaces_stores ss, tbl_stores store, tbl_cost_centers cc "
129
                                 " WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") "
130
                                 "       AND ss.space_id = s.id AND ss.store_id = store.id "
131
                                 "       AND store.cost_center_id = cc.id  ", )
132
        rows_stores = cursor_system_db.fetchall()
133
        if rows_stores is not None and len(rows_stores) > 0:
134
            for row in rows_stores:
135
                store_dict[row['id']] = {"store_name": row['store_name'],
136
                                         "space_name": row['space_name'],
137
                                         "cost_center_name": row['cost_center_name'],
138
                                         "description": row['description'],
139
                                         "values": list()}
140
141
        ################################################################################################################
142
        # Step 4: query energy categories
143
        ################################################################################################################
144
        cnx_energy_db = mysql.connector.connect(**config.myems_energy_db)
145
        cursor_energy_db = cnx_energy_db.cursor()
146
147
        # query energy categories in reporting period
148
        energy_category_set = set()
149
        cursor_energy_db.execute(" SELECT DISTINCT(energy_category_id) "
150
                                 " FROM tbl_store_input_category_hourly "
151
                                 " WHERE start_datetime_utc >= %s AND start_datetime_utc < %s ",
152
                                 (reporting_start_datetime_utc, reporting_end_datetime_utc))
153
        rows_energy_categories = cursor_energy_db.fetchall()
154
        if rows_energy_categories is not None or len(rows_energy_categories) > 0:
155
            for row_energy_category in rows_energy_categories:
156
                energy_category_set.add(row_energy_category[0])
157
158
        # query all energy categories
159
        cursor_system_db.execute(" SELECT id, name, unit_of_measure "
160
                                 " FROM tbl_energy_categories "
161
                                 " ORDER BY id ", )
162
        rows_energy_categories = cursor_system_db.fetchall()
163 View Code Duplication
        if rows_energy_categories is None or len(rows_energy_categories) == 0:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
164
            if cursor_system_db:
165
                cursor_system_db.close()
166
            if cnx_system_db:
167
                cnx_system_db.disconnect()
168
169
            if cursor_energy_db:
170
                cursor_energy_db.close()
171
            if cnx_energy_db:
172
                cnx_energy_db.disconnect()
173
174
            raise falcon.HTTPError(falcon.HTTP_404,
175
                                   title='API.NOT_FOUND',
176
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
177
        energy_category_list = list()
178
        for row_energy_category in rows_energy_categories:
179
            if row_energy_category['id'] in energy_category_set:
180
                energy_category_list.append({"id": row_energy_category['id'],
181
                                             "name": row_energy_category['name'],
182
                                             "unit_of_measure": row_energy_category['unit_of_measure']})
183
184
        ################################################################################################################
185
        # Step 5: query reporting period energy input
186
        ################################################################################################################
187
        for store_id in store_dict:
188
189
            cursor_energy_db.execute(" SELECT energy_category_id, SUM(actual_value) "
190
                                     " FROM tbl_store_input_category_hourly "
191
                                     " WHERE store_id = %s "
192
                                     "     AND start_datetime_utc >= %s "
193
                                     "     AND start_datetime_utc < %s "
194
                                     " GROUP BY energy_category_id ",
195
                                     (store_id,
196
                                      reporting_start_datetime_utc,
197
                                      reporting_end_datetime_utc))
198
            rows_store_energy = cursor_energy_db.fetchall()
199
            for energy_category in energy_category_list:
200
                subtotal = Decimal(0.0)
201
                for row_store_energy in rows_store_energy:
202
                    if energy_category['id'] == row_store_energy[0]:
203
                        subtotal = row_store_energy[1]
204
                        break
205
                store_dict[store_id]['values'].append(subtotal)
206
207
        if cursor_system_db:
208
            cursor_system_db.close()
209
        if cnx_system_db:
210
            cnx_system_db.disconnect()
211
212
        if cursor_energy_db:
213
            cursor_energy_db.close()
214
        if cnx_energy_db:
215
            cnx_energy_db.disconnect()
216
217
        ################################################################################################################
218
        # Step 6: construct the report
219
        ################################################################################################################
220
        store_list = list()
221
        for store_id, store in store_dict.items():
222
            store_list.append({
223
                "id": store_id,
224
                "store_name": store['store_name'],
225
                "space_name": store['space_name'],
226
                "cost_center_name": store['cost_center_name'],
227
                "description": store['description'],
228
                "values": store['values'],
229
            })
230
231
        result = {'stores': store_list,
232
                  'energycategories': energy_category_list}
233
234
        # export result to Excel file and then encode the file to base64 string
235
        result['excel_bytes_base64'] = excelexporters.storebatch.export(result,
236
                                                                        space_name,
237
                                                                        reporting_period_start_datetime_local,
238
                                                                        reporting_period_end_datetime_local)
239
        resp.body = json.dumps(result)
240