Conditions | 28 |
Total Lines | 160 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like reports.photovoltaicpowerstationitemdashboard.Reporting.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import re |
||
29 | @staticmethod |
||
30 | def on_get(req, resp): |
||
31 | if 'API-KEY' not in req.headers or \ |
||
32 | not isinstance(req.headers['API-KEY'], str) or \ |
||
33 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
34 | access_control(req) |
||
35 | else: |
||
36 | api_key_control(req) |
||
37 | # this procedure accepts energy storage power station id or uuid |
||
38 | photovoltaic_power_station_id = req.params.get('id') |
||
39 | photovoltaic_power_station_uuid = req.params.get('uuid') |
||
40 | |||
41 | ################################################################################################################ |
||
42 | # Step 1: valid parameters |
||
43 | ################################################################################################################ |
||
44 | if photovoltaic_power_station_id is None and photovoltaic_power_station_uuid is None: |
||
45 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
46 | description='API.INVALID_photovoltaic_POWER_STATION_ID') |
||
47 | |||
48 | if photovoltaic_power_station_id is not None: |
||
49 | photovoltaic_power_station_id = str.strip(photovoltaic_power_station_id) |
||
50 | if not photovoltaic_power_station_id.isdigit() or int(photovoltaic_power_station_id) <= 0: |
||
51 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
52 | description='API.INVALID_photovoltaic_POWER_STATION_ID') |
||
53 | |||
54 | if photovoltaic_power_station_uuid is not None: |
||
55 | regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) |
||
56 | match = regex.match(str.strip(photovoltaic_power_station_uuid)) |
||
57 | if not bool(match): |
||
58 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
59 | description='API.INVALID_photovoltaic_POWER_STATION_UUID') |
||
60 | |||
61 | ################################################################################################################ |
||
62 | # Step 2: query the energy storage power station |
||
63 | ################################################################################################################ |
||
64 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
65 | cursor_system_db = cnx_system_db.cursor() |
||
66 | # Get Spaces associated with energy storage power stations |
||
67 | query = (" SELECT se.photovoltaic_power_station_id, s.name " |
||
68 | " FROM tbl_spaces s, tbl_spaces_photovoltaic_power_stations se " |
||
69 | " WHERE se.space_id = s.id ") |
||
70 | cursor_system_db.execute(query) |
||
71 | rows_spaces = cursor_system_db.fetchall() |
||
72 | |||
73 | space_dict = dict() |
||
74 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
75 | for row in rows_spaces: |
||
76 | space_dict[row[0]] = row[1] |
||
77 | print(space_dict) |
||
78 | # Get energy storage power station |
||
79 | if photovoltaic_power_station_id is not None: |
||
80 | query = (" SELECT id, name, uuid, " |
||
81 | " address, latitude, longitude, rated_capacity, rated_power, " |
||
82 | " contact_id, cost_center_id " |
||
83 | " FROM tbl_photovoltaic_power_stations " |
||
84 | " WHERE id = %s ") |
||
85 | cursor_system_db.execute(query, (photovoltaic_power_station_id,)) |
||
86 | row = cursor_system_db.fetchone() |
||
87 | elif photovoltaic_power_station_uuid is not None: |
||
88 | query = (" SELECT id, name, uuid, " |
||
89 | " address, latitude, longitude, rated_capacity, rated_power, " |
||
90 | " contact_id, cost_center_id " |
||
91 | " FROM tbl_photovoltaic_power_stations " |
||
92 | " WHERE uuid = %s ") |
||
93 | cursor_system_db.execute(query, (photovoltaic_power_station_uuid,)) |
||
94 | row = cursor_system_db.fetchone() |
||
95 | |||
96 | if row is None: |
||
|
|||
97 | cursor_system_db.close() |
||
98 | cnx_system_db.close() |
||
99 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
100 | description='API.photovoltaic_POWER_STATION_NOT_FOUND') |
||
101 | else: |
||
102 | photovoltaic_power_station_id = row[0] |
||
103 | photovoltaic_power_station = { |
||
104 | "id": row[0], |
||
105 | "name": row[1], |
||
106 | "uuid": row[2], |
||
107 | "address": row[3], |
||
108 | "space_name": space_dict.get(row[0]), |
||
109 | "latitude": row[4], |
||
110 | "longitude": row[5], |
||
111 | "rated_capacity": row[6], |
||
112 | "rated_power": row[7] |
||
113 | } |
||
114 | |||
115 | ################################################################################################################ |
||
116 | # Step 3: query generation energy data |
||
117 | ################################################################################################################ |
||
118 | cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
||
119 | cursor_energy_db = cnx_energy_db.cursor() |
||
120 | |||
121 | cnx_billing_db = mysql.connector.connect(**config.myems_billing_db) |
||
122 | cursor_billing_db = cnx_billing_db.cursor() |
||
123 | |||
124 | cnx_carbon_db = mysql.connector.connect(**config.myems_billing_db) |
||
125 | cursor_carbon_db = cnx_carbon_db.cursor() |
||
126 | |||
127 | query = (" SELECT SUM(actual_value) " |
||
128 | " FROM tbl_photovoltaic_power_station_generation_hourly " |
||
129 | " WHERE photovoltaic_power_station_id = %s ") |
||
130 | cursor_energy_db.execute(query, (photovoltaic_power_station_id, )) |
||
131 | row = cursor_energy_db.fetchone() |
||
132 | total_generation_energy = Decimal(0.0) |
||
133 | if row is not None: |
||
134 | total_generation_energy = row[0] |
||
135 | |||
136 | ################################################################################################################ |
||
137 | # Step 5: query generation billing data |
||
138 | ################################################################################################################ |
||
139 | query = (" SELECT SUM(actual_value) " |
||
140 | " FROM tbl_photovoltaic_power_station_generation_hourly " |
||
141 | " WHERE photovoltaic_power_station_id = %s ") |
||
142 | cursor_billing_db.execute(query, (photovoltaic_power_station_id, )) |
||
143 | row = cursor_billing_db.fetchone() |
||
144 | total_generation_billing = Decimal(0.0) |
||
145 | if row is not None: |
||
146 | total_generation_billing = row[0] |
||
147 | |||
148 | ################################################################################################################ |
||
149 | # Step 7: query generation carbon data |
||
150 | ################################################################################################################ |
||
151 | query = (" SELECT SUM(actual_value) " |
||
152 | " FROM tbl_photovoltaic_power_station_generation_hourly " |
||
153 | " WHERE photovoltaic_power_station_id = %s ") |
||
154 | cursor_carbon_db.execute(query, (photovoltaic_power_station_id, )) |
||
155 | row = cursor_carbon_db.fetchone() |
||
156 | total_generation_carbon = Decimal(0.0) |
||
157 | if row is not None: |
||
158 | total_generation_carbon = row[0] |
||
159 | |||
160 | ################################################################################################################ |
||
161 | # Step 7: construct the report |
||
162 | ################################################################################################################ |
||
163 | if cursor_system_db: |
||
164 | cursor_system_db.close() |
||
165 | if cnx_system_db: |
||
166 | cnx_system_db.close() |
||
167 | |||
168 | if cursor_energy_db: |
||
169 | cursor_energy_db.close() |
||
170 | if cnx_energy_db: |
||
171 | cnx_energy_db.close() |
||
172 | |||
173 | if cursor_billing_db: |
||
174 | cursor_billing_db.close() |
||
175 | if cnx_billing_db: |
||
176 | cnx_billing_db.close() |
||
177 | |||
178 | if cursor_carbon_db: |
||
179 | cursor_carbon_db.close() |
||
180 | if cnx_carbon_db: |
||
181 | cnx_carbon_db.close() |
||
182 | |||
183 | result = dict() |
||
184 | result['photovoltaic_power_station'] = photovoltaic_power_station |
||
185 | result['total_generation_energy'] = total_generation_energy |
||
186 | result['total_generation_billing'] = total_generation_billing |
||
187 | result['total_generation_carbon'] = total_generation_carbon |
||
188 | resp.text = json.dumps(result) |
||
189 |