Conditions | 57 |
Total Lines | 248 |
Code Lines | 171 |
Lines | 248 |
Ratio | 100 % |
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 equipment_billing_output_category.main() 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 time |
||
21 | View Code Duplication | def main(logger): |
|
|
|||
22 | |||
23 | while True: |
||
24 | # the outermost while loop |
||
25 | ################################################################################################################ |
||
26 | # Step 1: get all equipments |
||
27 | ################################################################################################################ |
||
28 | cnx_system_db = None |
||
29 | cursor_system_db = None |
||
30 | try: |
||
31 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
32 | cursor_system_db = cnx_system_db.cursor() |
||
33 | except Exception as e: |
||
34 | logger.error("Error in step 1.1 of equipment_billing_input_category " + str(e)) |
||
35 | if cursor_system_db: |
||
36 | cursor_system_db.close() |
||
37 | if cnx_system_db: |
||
38 | cnx_system_db.close() |
||
39 | # sleep and continue the outermost while loop |
||
40 | time.sleep(60) |
||
41 | continue |
||
42 | |||
43 | print("Connected to MyEMS System Database") |
||
44 | |||
45 | try: |
||
46 | cursor_system_db.execute(" SELECT id, name, cost_center_id " |
||
47 | " FROM tbl_equipments " |
||
48 | " ORDER BY id ") |
||
49 | rows_equipments = cursor_system_db.fetchall() |
||
50 | |||
51 | if rows_equipments is None or len(rows_equipments) == 0: |
||
52 | print("Step 1.2: There isn't any equipments. ") |
||
53 | if cursor_system_db: |
||
54 | cursor_system_db.close() |
||
55 | if cnx_system_db: |
||
56 | cnx_system_db.close() |
||
57 | # sleep and continue the outermost while loop |
||
58 | time.sleep(60) |
||
59 | continue |
||
60 | |||
61 | equipment_list = list() |
||
62 | for row in rows_equipments: |
||
63 | equipment_list.append({"id": row[0], "name": row[1], "cost_center_id": row[2]}) |
||
64 | |||
65 | except Exception as e: |
||
66 | logger.error("Error in step 1.2 of equipment_billing_output_category " + str(e)) |
||
67 | if cursor_system_db: |
||
68 | cursor_system_db.close() |
||
69 | if cnx_system_db: |
||
70 | cnx_system_db.close() |
||
71 | # sleep and continue the outermost while loop |
||
72 | time.sleep(60) |
||
73 | continue |
||
74 | |||
75 | print("Step 1.2: Got all equipments from MyEMS System Database") |
||
76 | |||
77 | cnx_energy_db = None |
||
78 | cursor_energy_db = None |
||
79 | try: |
||
80 | cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
||
81 | cursor_energy_db = cnx_energy_db.cursor() |
||
82 | except Exception as e: |
||
83 | logger.error("Error in step 1.3 of equipment_billing_output_category " + str(e)) |
||
84 | if cursor_energy_db: |
||
85 | cursor_energy_db.close() |
||
86 | if cnx_energy_db: |
||
87 | cnx_energy_db.close() |
||
88 | |||
89 | if cursor_system_db: |
||
90 | cursor_system_db.close() |
||
91 | if cnx_system_db: |
||
92 | cnx_system_db.close() |
||
93 | # sleep and continue the outermost while loop |
||
94 | time.sleep(60) |
||
95 | continue |
||
96 | |||
97 | print("Connected to MyEMS Energy Database") |
||
98 | |||
99 | cnx_billing_db = None |
||
100 | cursor_billing_db = None |
||
101 | try: |
||
102 | cnx_billing_db = mysql.connector.connect(**config.myems_billing_db) |
||
103 | cursor_billing_db = cnx_billing_db.cursor() |
||
104 | except Exception as e: |
||
105 | logger.error("Error in step 1.4 of equipment_billing_output_category " + str(e)) |
||
106 | if cursor_billing_db: |
||
107 | cursor_billing_db.close() |
||
108 | if cnx_billing_db: |
||
109 | cnx_billing_db.close() |
||
110 | |||
111 | if cursor_energy_db: |
||
112 | cursor_energy_db.close() |
||
113 | if cnx_energy_db: |
||
114 | cnx_energy_db.close() |
||
115 | |||
116 | if cursor_system_db: |
||
117 | cursor_system_db.close() |
||
118 | if cnx_system_db: |
||
119 | cnx_system_db.close() |
||
120 | # sleep and continue the outermost while loop |
||
121 | time.sleep(60) |
||
122 | continue |
||
123 | |||
124 | print("Connected to MyEMS Billing Database") |
||
125 | |||
126 | for equipment in equipment_list: |
||
127 | |||
128 | ############################################################################################################ |
||
129 | # Step 2: get the latest start_datetime_utc |
||
130 | ############################################################################################################ |
||
131 | print("Step 2: get the latest start_datetime_utc from billing database for " + equipment['name']) |
||
132 | try: |
||
133 | cursor_billing_db.execute(" SELECT MAX(start_datetime_utc) " |
||
134 | " FROM tbl_equipment_output_category_hourly " |
||
135 | " WHERE equipment_id = %s ", |
||
136 | (equipment['id'], )) |
||
137 | row_datetime = cursor_billing_db.fetchone() |
||
138 | start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S') |
||
139 | start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) |
||
140 | |||
141 | if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime): |
||
142 | # replace second and microsecond with 0 |
||
143 | # note: do not replace minute in case of calculating in half hourly |
||
144 | start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None) |
||
145 | # start from the next time slot |
||
146 | start_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
147 | |||
148 | print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19]) |
||
149 | except Exception as e: |
||
150 | logger.error("Error in step 2 of equipment_billing_output_category " + str(e)) |
||
151 | # break the for equipment loop |
||
152 | break |
||
153 | |||
154 | ############################################################################################################ |
||
155 | # Step 3: get all energy output data since the latest start_datetime_utc |
||
156 | ############################################################################################################ |
||
157 | print("Step 3: get all energy output data since the latest start_datetime_utc") |
||
158 | |||
159 | query = (" SELECT start_datetime_utc, energy_category_id, actual_value " |
||
160 | " FROM tbl_equipment_output_category_hourly " |
||
161 | " WHERE equipment_id = %s AND start_datetime_utc >= %s " |
||
162 | " ORDER BY id ") |
||
163 | cursor_energy_db.execute(query, (equipment['id'], start_datetime_utc, )) |
||
164 | rows_hourly = cursor_energy_db.fetchall() |
||
165 | |||
166 | if rows_hourly is None or len(rows_hourly) == 0: |
||
167 | print("Step 3: There isn't any energy output data to calculate. ") |
||
168 | # continue the for equipment loop |
||
169 | continue |
||
170 | |||
171 | energy_dict = dict() |
||
172 | energy_category_list = list() |
||
173 | end_datetime_utc = start_datetime_utc |
||
174 | for row_hourly in rows_hourly: |
||
175 | current_datetime_utc = row_hourly[0] |
||
176 | energy_category_id = row_hourly[1] |
||
177 | |||
178 | if energy_category_id not in energy_category_list: |
||
179 | energy_category_list.append(energy_category_id) |
||
180 | |||
181 | actual_value = row_hourly[2] |
||
182 | if energy_dict.get(current_datetime_utc) is None: |
||
183 | energy_dict[current_datetime_utc] = dict() |
||
184 | energy_dict[current_datetime_utc][energy_category_id] = actual_value |
||
185 | if current_datetime_utc > end_datetime_utc: |
||
186 | end_datetime_utc = current_datetime_utc |
||
187 | |||
188 | ############################################################################################################ |
||
189 | # Step 4: get tariffs |
||
190 | ############################################################################################################ |
||
191 | print("Step 4: get tariffs") |
||
192 | tariff_dict = dict() |
||
193 | for energy_category_id in energy_category_list: |
||
194 | tariff_dict[energy_category_id] = tariff.get_energy_category_tariffs(equipment['cost_center_id'], |
||
195 | energy_category_id, |
||
196 | start_datetime_utc, |
||
197 | end_datetime_utc) |
||
198 | ############################################################################################################ |
||
199 | # Step 5: calculate billing by multiplying energy with tariff |
||
200 | ############################################################################################################ |
||
201 | print("Step 5: calculate billing by multiplying energy with tariff") |
||
202 | billing_dict = dict() |
||
203 | |||
204 | if len(energy_dict) > 0: |
||
205 | for current_datetime_utc in energy_dict.keys(): |
||
206 | billing_dict[current_datetime_utc] = dict() |
||
207 | for energy_category_id in energy_category_list: |
||
208 | current_tariff = tariff_dict[energy_category_id].get(current_datetime_utc) |
||
209 | current_energy = energy_dict[current_datetime_utc].get(energy_category_id) |
||
210 | if current_tariff is not None \ |
||
211 | and isinstance(current_tariff, Decimal) \ |
||
212 | and current_energy is not None \ |
||
213 | and isinstance(current_energy, Decimal): |
||
214 | billing_dict[current_datetime_utc][energy_category_id] = \ |
||
215 | current_energy * current_tariff |
||
216 | |||
217 | if len(billing_dict[current_datetime_utc]) == 0: |
||
218 | del billing_dict[current_datetime_utc] |
||
219 | |||
220 | ############################################################################################################ |
||
221 | # Step 6: save billing data to billing database |
||
222 | ############################################################################################################ |
||
223 | print("Step 6: save billing data to billing database") |
||
224 | |||
225 | if len(billing_dict) > 0: |
||
226 | try: |
||
227 | add_values = (" INSERT INTO tbl_equipment_output_category_hourly " |
||
228 | " (equipment_id, " |
||
229 | " energy_category_id, " |
||
230 | " start_datetime_utc, " |
||
231 | " actual_value) " |
||
232 | " VALUES ") |
||
233 | |||
234 | for current_datetime_utc in billing_dict: |
||
235 | for energy_category_id in energy_category_list: |
||
236 | current_billing = billing_dict[current_datetime_utc].get(energy_category_id) |
||
237 | if current_billing is not None and isinstance(current_billing, Decimal): |
||
238 | add_values += " (" + str(equipment['id']) + "," |
||
239 | add_values += " " + str(energy_category_id) + "," |
||
240 | add_values += "'" + current_datetime_utc.isoformat()[0:19] + "'," |
||
241 | add_values += str(billing_dict[current_datetime_utc][energy_category_id]) + "), " |
||
242 | print("add_values:" + add_values) |
||
243 | # trim ", " at the end of string and then execute |
||
244 | cursor_billing_db.execute(add_values[:-2]) |
||
245 | cnx_billing_db.commit() |
||
246 | except Exception as e: |
||
247 | logger.error("Error in step 6 of equipment_billing_output_category " + str(e)) |
||
248 | # break the for equipment loop |
||
249 | break |
||
250 | |||
251 | # end of for equipment loop |
||
252 | if cnx_system_db: |
||
253 | cnx_system_db.close() |
||
254 | if cursor_system_db: |
||
255 | cursor_system_db.close() |
||
256 | |||
257 | if cnx_energy_db: |
||
258 | cnx_energy_db.close() |
||
259 | if cursor_energy_db: |
||
260 | cursor_energy_db.close() |
||
261 | |||
262 | if cnx_billing_db: |
||
263 | cnx_billing_db.close() |
||
264 | if cursor_billing_db: |
||
265 | cursor_billing_db.close() |
||
266 | print("go to sleep 300 seconds...") |
||
267 | time.sleep(300) |
||
268 | print("wake from sleep, and continue to work...") |
||
269 | # end of the outermost while loop |
||
270 |