| Total Complexity | 54 |
| Total Lines | 375 |
| Duplicated Lines | 97.33 % |
| 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 reports.virtualmetercost 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 datetime import datetime, timedelta, timezone |
||
| 6 | import utilities |
||
| 7 | from decimal import * |
||
| 8 | |||
| 9 | |||
| 10 | View Code Duplication | 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 virtual meter and energy category |
||
| 23 | # Step 3: query base period energy consumption |
||
| 24 | # Step 4: query base period energy cost |
||
| 25 | # Step 5: query reporting period energy consumption |
||
| 26 | # Step 6: query reporting period energy cost |
||
| 27 | # Step 7: query tariff data |
||
| 28 | # Step 8: construct the report |
||
| 29 | #################################################################################################################### |
||
| 30 | @staticmethod |
||
| 31 | def on_get(req, resp): |
||
| 32 | print(req.params) |
||
| 33 | virtual_meter_id = req.params.get('virtualmeterid') |
||
| 34 | period_type = req.params.get('periodtype') |
||
| 35 | base_period_begins_datetime = req.params.get('baseperiodstartdatetime') |
||
| 36 | base_period_ends_datetime = req.params.get('baseperiodenddatetime') |
||
| 37 | reporting_period_begins_datetime = req.params.get('reportingperiodstartdatetime') |
||
| 38 | reporting_period_ends_datetime = req.params.get('reportingperiodenddatetime') |
||
| 39 | |||
| 40 | ################################################################################################################ |
||
| 41 | # Step 1: valid parameters |
||
| 42 | ################################################################################################################ |
||
| 43 | if virtual_meter_id is None: |
||
| 44 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 45 | title='API.BAD_REQUEST', |
||
| 46 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 47 | else: |
||
| 48 | virtual_meter_id = str.strip(virtual_meter_id) |
||
| 49 | if not virtual_meter_id.isdigit() or int(virtual_meter_id) <= 0: |
||
| 50 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 51 | title='API.BAD_REQUEST', |
||
| 52 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 53 | |||
| 54 | if period_type is None: |
||
| 55 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
| 56 | else: |
||
| 57 | period_type = str.strip(period_type) |
||
| 58 | if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
||
| 59 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
| 60 | |||
| 61 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 62 | if config.utc_offset[0] == '-': |
||
| 63 | timezone_offset = -timezone_offset |
||
| 64 | |||
| 65 | base_start_datetime_utc = None |
||
| 66 | if base_period_begins_datetime is not None and len(str.strip(base_period_begins_datetime)) > 0: |
||
| 67 | base_period_begins_datetime = str.strip(base_period_begins_datetime) |
||
| 68 | try: |
||
| 69 | base_start_datetime_utc = datetime.strptime(base_period_begins_datetime, '%Y-%m-%dT%H:%M:%S') |
||
| 70 | except ValueError: |
||
| 71 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 72 | description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") |
||
| 73 | base_start_datetime_utc = base_start_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
| 74 | timedelta(minutes=timezone_offset) |
||
| 75 | |||
| 76 | base_end_datetime_utc = None |
||
| 77 | if base_period_ends_datetime is not None and len(str.strip(base_period_ends_datetime)) > 0: |
||
| 78 | base_period_ends_datetime = str.strip(base_period_ends_datetime) |
||
| 79 | try: |
||
| 80 | base_end_datetime_utc = datetime.strptime(base_period_ends_datetime, '%Y-%m-%dT%H:%M:%S') |
||
| 81 | except ValueError: |
||
| 82 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 83 | description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") |
||
| 84 | base_end_datetime_utc = base_end_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
| 85 | timedelta(minutes=timezone_offset) |
||
| 86 | |||
| 87 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
||
| 88 | base_start_datetime_utc >= base_end_datetime_utc: |
||
| 89 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 90 | description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') |
||
| 91 | |||
| 92 | if reporting_period_begins_datetime is None: |
||
| 93 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 94 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
| 95 | else: |
||
| 96 | reporting_period_begins_datetime = str.strip(reporting_period_begins_datetime) |
||
| 97 | try: |
||
| 98 | reporting_start_datetime_utc = datetime.strptime(reporting_period_begins_datetime, '%Y-%m-%dT%H:%M:%S') |
||
| 99 | except ValueError: |
||
| 100 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 101 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
| 102 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
| 103 | timedelta(minutes=timezone_offset) |
||
| 104 | |||
| 105 | if reporting_period_ends_datetime is None: |
||
| 106 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 107 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
| 108 | else: |
||
| 109 | reporting_period_ends_datetime = str.strip(reporting_period_ends_datetime) |
||
| 110 | try: |
||
| 111 | reporting_end_datetime_utc = datetime.strptime(reporting_period_ends_datetime, '%Y-%m-%dT%H:%M:%S') |
||
| 112 | except ValueError: |
||
| 113 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 114 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
| 115 | reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
| 116 | timedelta(minutes=timezone_offset) |
||
| 117 | |||
| 118 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
| 119 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 120 | description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
||
| 121 | |||
| 122 | ################################################################################################################ |
||
| 123 | # Step 2: query the virtual meter and energy category |
||
| 124 | ################################################################################################################ |
||
| 125 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
| 126 | cursor_system = cnx_system.cursor() |
||
| 127 | |||
| 128 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
||
| 129 | cursor_energy = cnx_energy.cursor() |
||
| 130 | |||
| 131 | cnx_billing = mysql.connector.connect(**config.myems_billing_db) |
||
| 132 | cursor_billing = cnx_billing.cursor() |
||
| 133 | |||
| 134 | cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " |
||
| 135 | " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " |
||
| 136 | " FROM tbl_virtual_meters m, tbl_energy_categories ec " |
||
| 137 | " WHERE m.id = %s AND m.energy_category_id = ec.id ", (virtual_meter_id,)) |
||
| 138 | row_virtual_meter = cursor_system.fetchone() |
||
| 139 | if row_virtual_meter is None: |
||
| 140 | if cursor_system: |
||
| 141 | cursor_system.close() |
||
| 142 | if cnx_system: |
||
| 143 | cnx_system.disconnect() |
||
| 144 | |||
| 145 | if cursor_energy: |
||
| 146 | cursor_energy.close() |
||
| 147 | if cnx_energy: |
||
| 148 | cnx_energy.disconnect() |
||
| 149 | |||
| 150 | if cursor_billing: |
||
| 151 | cursor_billing.close() |
||
| 152 | if cnx_billing: |
||
| 153 | cnx_billing.disconnect() |
||
| 154 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 155 | |||
| 156 | virtual_meter = dict() |
||
| 157 | virtual_meter['id'] = row_virtual_meter[0] |
||
| 158 | virtual_meter['name'] = row_virtual_meter[1] |
||
| 159 | virtual_meter['cost_center_id'] = row_virtual_meter[2] |
||
| 160 | virtual_meter['energy_category_id'] = row_virtual_meter[3] |
||
| 161 | virtual_meter['energy_category_name'] = row_virtual_meter[4] |
||
| 162 | virtual_meter['unit_of_measure'] = config.currency_unit |
||
| 163 | virtual_meter['kgce'] = row_virtual_meter[6] |
||
| 164 | virtual_meter['kgco2e'] = row_virtual_meter[7] |
||
| 165 | |||
| 166 | ################################################################################################################ |
||
| 167 | # Step 3: query base period energy consumption |
||
| 168 | ################################################################################################################ |
||
| 169 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 170 | " FROM tbl_virtual_meter_hourly " |
||
| 171 | " WHERE virtual_meter_id = %s " |
||
| 172 | " AND start_datetime_utc >= %s " |
||
| 173 | " AND start_datetime_utc < %s " |
||
| 174 | " ORDER BY start_datetime_utc ") |
||
| 175 | cursor_energy.execute(query, (virtual_meter['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
| 176 | rows_virtual_meter_hourly = cursor_energy.fetchall() |
||
| 177 | |||
| 178 | rows_virtual_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
| 179 | base_start_datetime_utc, |
||
| 180 | base_end_datetime_utc, |
||
| 181 | period_type) |
||
| 182 | base = dict() |
||
| 183 | base['timestamps'] = list() |
||
| 184 | base['values'] = list() |
||
| 185 | base['total_in_category'] = Decimal(0.0) |
||
| 186 | base['total_in_kgce'] = Decimal(0.0) |
||
| 187 | base['total_in_kgco2e'] = Decimal(0.0) |
||
| 188 | |||
| 189 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
| 190 | current_datetime_local = row_virtual_meter_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 191 | timedelta(minutes=timezone_offset) |
||
| 192 | if period_type == 'hourly': |
||
| 193 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 194 | elif period_type == 'daily': |
||
| 195 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
| 196 | elif period_type == 'monthly': |
||
| 197 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
| 198 | elif period_type == 'yearly': |
||
| 199 | current_datetime = current_datetime_local.strftime('%Y') |
||
| 200 | |||
| 201 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
| 202 | else row_virtual_meter_periodically[1] |
||
| 203 | base['timestamps'].append(current_datetime) |
||
| 204 | base['total_in_kgce'] += actual_value * virtual_meter['kgce'] |
||
| 205 | base['total_in_kgco2e'] += actual_value * virtual_meter['kgco2e'] |
||
| 206 | |||
| 207 | ################################################################################################################ |
||
| 208 | # Step 4: query base period energy cost |
||
| 209 | ################################################################################################################ |
||
| 210 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 211 | " FROM tbl_virtual_meter_hourly " |
||
| 212 | " WHERE virtual_meter_id = %s " |
||
| 213 | " AND start_datetime_utc >= %s " |
||
| 214 | " AND start_datetime_utc < %s " |
||
| 215 | " ORDER BY start_datetime_utc ") |
||
| 216 | cursor_billing.execute(query, (virtual_meter['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
| 217 | rows_virtual_meter_hourly = cursor_billing.fetchall() |
||
| 218 | |||
| 219 | rows_virtual_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
| 220 | base_start_datetime_utc, |
||
| 221 | base_end_datetime_utc, |
||
| 222 | period_type) |
||
| 223 | |||
| 224 | base['values'] = list() |
||
| 225 | base['total_in_category'] = Decimal(0.0) |
||
| 226 | |||
| 227 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
| 228 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
| 229 | else row_virtual_meter_periodically[1] |
||
| 230 | base['values'].append(actual_value) |
||
| 231 | base['total_in_category'] += actual_value |
||
| 232 | |||
| 233 | ################################################################################################################ |
||
| 234 | # Step 5: query reporting period energy consumption |
||
| 235 | ################################################################################################################ |
||
| 236 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 237 | " FROM tbl_virtual_meter_hourly " |
||
| 238 | " WHERE virtual_meter_id = %s " |
||
| 239 | " AND start_datetime_utc >= %s " |
||
| 240 | " AND start_datetime_utc < %s " |
||
| 241 | " ORDER BY start_datetime_utc ") |
||
| 242 | cursor_billing.execute(query, (virtual_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
| 243 | rows_virtual_meter_hourly = cursor_billing.fetchall() |
||
| 244 | |||
| 245 | rows_virtual_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
| 246 | reporting_start_datetime_utc, |
||
| 247 | reporting_end_datetime_utc, |
||
| 248 | period_type) |
||
| 249 | reporting = dict() |
||
| 250 | reporting['timestamps'] = list() |
||
| 251 | reporting['values'] = list() |
||
| 252 | reporting['total_in_category'] = Decimal(0.0) |
||
| 253 | reporting['total_in_kgce'] = Decimal(0.0) |
||
| 254 | reporting['total_in_kgco2e'] = Decimal(0.0) |
||
| 255 | |||
| 256 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
| 257 | current_datetime_local = row_virtual_meter_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 258 | timedelta(minutes=timezone_offset) |
||
| 259 | if period_type == 'hourly': |
||
| 260 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 261 | elif period_type == 'daily': |
||
| 262 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
| 263 | elif period_type == 'monthly': |
||
| 264 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
| 265 | elif period_type == 'yearly': |
||
| 266 | current_datetime = current_datetime_local.strftime('%Y') |
||
| 267 | |||
| 268 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
| 269 | else row_virtual_meter_periodically[1] |
||
| 270 | |||
| 271 | reporting['timestamps'].append(current_datetime) |
||
| 272 | reporting['total_in_kgce'] += actual_value * virtual_meter['kgce'] |
||
| 273 | reporting['total_in_kgco2e'] += actual_value * virtual_meter['kgco2e'] |
||
| 274 | |||
| 275 | ################################################################################################################ |
||
| 276 | # Step 6: query reporting period energy cost |
||
| 277 | ################################################################################################################ |
||
| 278 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 279 | " FROM tbl_virtual_meter_hourly " |
||
| 280 | " WHERE virtual_meter_id = %s " |
||
| 281 | " AND start_datetime_utc >= %s " |
||
| 282 | " AND start_datetime_utc < %s " |
||
| 283 | " ORDER BY start_datetime_utc ") |
||
| 284 | cursor_billing.execute(query, (virtual_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
| 285 | rows_virtual_meter_hourly = cursor_billing.fetchall() |
||
| 286 | |||
| 287 | rows_virtual_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_virtual_meter_hourly, |
||
| 288 | reporting_start_datetime_utc, |
||
| 289 | reporting_end_datetime_utc, |
||
| 290 | period_type) |
||
| 291 | |||
| 292 | for row_virtual_meter_periodically in rows_virtual_meter_periodically: |
||
| 293 | actual_value = Decimal(0.0) if row_virtual_meter_periodically[1] is None \ |
||
| 294 | else row_virtual_meter_periodically[1] |
||
| 295 | |||
| 296 | reporting['values'].append(actual_value) |
||
| 297 | reporting['total_in_category'] += actual_value |
||
| 298 | |||
| 299 | ################################################################################################################ |
||
| 300 | # Step 7: query tariff data |
||
| 301 | ################################################################################################################ |
||
| 302 | parameters_data = dict() |
||
| 303 | parameters_data['names'] = list() |
||
| 304 | parameters_data['timestamps'] = list() |
||
| 305 | parameters_data['values'] = list() |
||
| 306 | |||
| 307 | tariff_dict = utilities.get_energy_category_tariffs(virtual_meter['cost_center_id'], |
||
| 308 | virtual_meter['energy_category_id'], |
||
| 309 | reporting_start_datetime_utc, |
||
| 310 | reporting_end_datetime_utc) |
||
| 311 | tariff_timestamp_list = list() |
||
| 312 | tariff_value_list = list() |
||
| 313 | for k, v in tariff_dict.items(): |
||
| 314 | # convert k from utc to local |
||
| 315 | k = k + timedelta(minutes=timezone_offset) |
||
| 316 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
||
| 317 | tariff_value_list.append(v) |
||
| 318 | |||
| 319 | parameters_data['names'].append('TARIFF-' + virtual_meter['energy_category_name']) |
||
| 320 | parameters_data['timestamps'].append(tariff_timestamp_list) |
||
| 321 | parameters_data['values'].append(tariff_value_list) |
||
| 322 | |||
| 323 | ################################################################################################################ |
||
| 324 | # Step 8: construct the report |
||
| 325 | ################################################################################################################ |
||
| 326 | if cursor_system: |
||
| 327 | cursor_system.close() |
||
| 328 | if cnx_system: |
||
| 329 | cnx_system.disconnect() |
||
| 330 | |||
| 331 | if cursor_energy: |
||
| 332 | cursor_energy.close() |
||
| 333 | if cnx_energy: |
||
| 334 | cnx_energy.disconnect() |
||
| 335 | |||
| 336 | if cursor_billing: |
||
| 337 | cursor_billing.close() |
||
| 338 | if cnx_billing: |
||
| 339 | cnx_billing.disconnect() |
||
| 340 | |||
| 341 | result = { |
||
| 342 | "virtual_meter": { |
||
| 343 | "cost_center_id": virtual_meter['cost_center_id'], |
||
| 344 | "energy_category_id": virtual_meter['energy_category_id'], |
||
| 345 | "energy_category_name": virtual_meter['energy_category_name'], |
||
| 346 | "unit_of_measure": config.currency_unit, |
||
| 347 | "kgce": virtual_meter['kgce'], |
||
| 348 | "kgco2e": virtual_meter['kgco2e'], |
||
| 349 | }, |
||
| 350 | "base_period": { |
||
| 351 | "total_in_category": base['total_in_category'], |
||
| 352 | "total_in_kgce": base['total_in_kgce'], |
||
| 353 | "total_in_kgco2e": base['total_in_kgco2e'], |
||
| 354 | "timestamps": base['timestamps'], |
||
| 355 | "values": base['values'], |
||
| 356 | }, |
||
| 357 | "reporting_period": { |
||
| 358 | "increment_rate": |
||
| 359 | (reporting['total_in_category']-base['total_in_category'])/base['total_in_category'] |
||
| 360 | if base['total_in_category'] > 0 else None, |
||
| 361 | "total_in_category": reporting['total_in_category'], |
||
| 362 | "total_in_kgce": reporting['total_in_kgce'], |
||
| 363 | "total_in_kgco2e": reporting['total_in_kgco2e'], |
||
| 364 | "timestamps": reporting['timestamps'], |
||
| 365 | "values": reporting['values'], |
||
| 366 | }, |
||
| 367 | "parameters": { |
||
| 368 | "names": parameters_data['names'], |
||
| 369 | "timestamps": parameters_data['timestamps'], |
||
| 370 | "values": parameters_data['values'] |
||
| 371 | }, |
||
| 372 | } |
||
| 373 | |||
| 374 | resp.body = json.dumps(result) |
||
| 375 |