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