| Total Complexity | 124 |
| Total Lines | 609 |
| Duplicated Lines | 3.78 % |
| 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.equipmentincome 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 | """ |
||
| 2 | Equipment Income Report API |
||
| 3 | |||
| 4 | This module provides REST API endpoints for generating equipment income reports. |
||
| 5 | It analyzes revenue generation from equipment operations, providing insights |
||
| 6 | into financial performance and income optimization opportunities. |
||
| 7 | |||
| 8 | Key Features: |
||
| 9 | - Equipment income analysis |
||
| 10 | - Base period vs reporting period comparison |
||
| 11 | - Income breakdown by energy categories |
||
| 12 | - Revenue optimization insights |
||
| 13 | - Excel export functionality |
||
| 14 | - Financial performance metrics |
||
| 15 | |||
| 16 | Report Components: |
||
| 17 | - Equipment income summary |
||
| 18 | - Base period comparison data |
||
| 19 | - Income breakdown by energy categories |
||
| 20 | - Revenue optimization recommendations |
||
| 21 | - Financial performance indicators |
||
| 22 | - Income trends and patterns |
||
| 23 | |||
| 24 | The module uses Falcon framework for REST API and includes: |
||
| 25 | - Database queries for income data |
||
| 26 | - Revenue calculation algorithms |
||
| 27 | - Financial analysis tools |
||
| 28 | - Excel export via excelexporters |
||
| 29 | - Multi-language support |
||
| 30 | - User authentication and authorization |
||
| 31 | """ |
||
| 32 | |||
| 33 | import re |
||
| 34 | from datetime import datetime, timedelta, timezone |
||
| 35 | from decimal import Decimal |
||
| 36 | import falcon |
||
| 37 | import mysql.connector |
||
| 38 | import simplejson as json |
||
| 39 | import config |
||
| 40 | import excelexporters.equipmentincome |
||
| 41 | from core import utilities |
||
| 42 | from core.useractivity import access_control, api_key_control |
||
| 43 | |||
| 44 | |||
| 45 | class Reporting: |
||
| 46 | def __init__(self): |
||
| 47 | """"Initializes Reporting""" |
||
| 48 | pass |
||
| 49 | |||
| 50 | @staticmethod |
||
| 51 | def on_options(req, resp): |
||
| 52 | _ = req |
||
| 53 | resp.status = falcon.HTTP_200 |
||
| 54 | |||
| 55 | #################################################################################################################### |
||
| 56 | # PROCEDURES |
||
| 57 | # Step 1: valid parameters |
||
| 58 | # Step 2: query the equipment |
||
| 59 | # Step 3: query energy categories |
||
| 60 | # Step 4: query associated points |
||
| 61 | # Step 5: query base period energy income |
||
| 62 | # Step 6: query reporting period energy income |
||
| 63 | # Step 7: query tariff data |
||
| 64 | # Step 8: query associated points data |
||
| 65 | # Step 9: construct the report |
||
| 66 | #################################################################################################################### |
||
| 67 | @staticmethod |
||
| 68 | def on_get(req, resp): |
||
| 69 | if 'API-KEY' not in req.headers or \ |
||
| 70 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 71 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 72 | access_control(req) |
||
| 73 | else: |
||
| 74 | api_key_control(req) |
||
| 75 | print(req.params) |
||
| 76 | equipment_id = req.params.get('equipmentid') |
||
| 77 | equipment_uuid = req.params.get('equipmentuuid') |
||
| 78 | period_type = req.params.get('periodtype') |
||
| 79 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
||
| 80 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
||
| 81 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
| 82 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
| 83 | language = req.params.get('language') |
||
| 84 | quick_mode = req.params.get('quickmode') |
||
| 85 | |||
| 86 | ################################################################################################################ |
||
| 87 | # Step 1: valid parameters |
||
| 88 | ################################################################################################################ |
||
| 89 | if equipment_id is None and equipment_uuid is None: |
||
| 90 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 91 | title='API.BAD_REQUEST', |
||
| 92 | description='API.INVALID_EQUIPMENT_ID') |
||
| 93 | |||
| 94 | if equipment_id is not None: |
||
| 95 | equipment_id = str.strip(equipment_id) |
||
| 96 | if not equipment_id.isdigit() or int(equipment_id) <= 0: |
||
| 97 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 98 | title='API.BAD_REQUEST', |
||
| 99 | description='API.INVALID_EQUIPMENT_ID') |
||
| 100 | |||
| 101 | if equipment_uuid is not None: |
||
| 102 | 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) |
||
| 103 | match = regex.match(str.strip(equipment_uuid)) |
||
| 104 | if not bool(match): |
||
| 105 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 106 | title='API.BAD_REQUEST', |
||
| 107 | description='API.INVALID_EQUIPMENT_UUID') |
||
| 108 | |||
| 109 | if period_type is None: |
||
| 110 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 111 | description='API.INVALID_PERIOD_TYPE') |
||
| 112 | else: |
||
| 113 | period_type = str.strip(period_type) |
||
| 114 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
||
| 115 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 116 | description='API.INVALID_PERIOD_TYPE') |
||
| 117 | |||
| 118 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 119 | if config.utc_offset[0] == '-': |
||
| 120 | timezone_offset = -timezone_offset |
||
| 121 | |||
| 122 | base_start_datetime_utc = None |
||
| 123 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
||
| 124 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
||
| 125 | try: |
||
| 126 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
||
| 127 | except ValueError: |
||
| 128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 129 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
||
| 130 | base_start_datetime_utc = \ |
||
| 131 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
| 132 | # nomalize the start datetime |
||
| 133 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
||
| 134 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
||
| 135 | else: |
||
| 136 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
||
| 137 | |||
| 138 | base_end_datetime_utc = None |
||
| 139 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
||
| 140 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
||
| 141 | try: |
||
| 142 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
||
| 143 | except ValueError: |
||
| 144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 145 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
||
| 146 | base_end_datetime_utc = \ |
||
| 147 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
| 148 | |||
| 149 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
||
| 150 | base_start_datetime_utc >= base_end_datetime_utc: |
||
| 151 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 152 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
||
| 153 | |||
| 154 | if reporting_period_start_datetime_local is None: |
||
| 155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 156 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
| 157 | else: |
||
| 158 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
||
| 159 | try: |
||
| 160 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
||
| 161 | '%Y-%m-%dT%H:%M:%S') |
||
| 162 | except ValueError: |
||
| 163 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 164 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
| 165 | reporting_start_datetime_utc = \ |
||
| 166 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
| 167 | # nomalize the start datetime |
||
| 168 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
||
| 169 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
||
| 170 | else: |
||
| 171 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
||
| 172 | |||
| 173 | if reporting_period_end_datetime_local is None: |
||
| 174 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 175 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
| 176 | else: |
||
| 177 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
||
| 178 | try: |
||
| 179 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
||
| 180 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 181 | timedelta(minutes=timezone_offset) |
||
| 182 | except ValueError: |
||
| 183 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 184 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
| 185 | |||
| 186 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
| 187 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 188 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
||
| 189 | |||
| 190 | # if turn quick mode on, do not return parameters data and excel file |
||
| 191 | is_quick_mode = False |
||
| 192 | if quick_mode is not None and \ |
||
| 193 | len(str.strip(quick_mode)) > 0 and \ |
||
| 194 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
||
| 195 | is_quick_mode = True |
||
| 196 | |||
| 197 | trans = utilities.get_translation(language) |
||
| 198 | trans.install() |
||
| 199 | _ = trans.gettext |
||
| 200 | |||
| 201 | ################################################################################################################ |
||
| 202 | # Step 2: query the equipment |
||
| 203 | ################################################################################################################ |
||
| 204 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
| 205 | cursor_system = cnx_system.cursor() |
||
| 206 | |||
| 207 | cnx_billing = mysql.connector.connect(**config.myems_billing_db) |
||
| 208 | cursor_billing = cnx_billing.cursor() |
||
| 209 | |||
| 210 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
||
| 211 | cursor_historical = cnx_historical.cursor() |
||
| 212 | |||
| 213 | if equipment_id is not None: |
||
| 214 | cursor_system.execute(" SELECT id, name, cost_center_id " |
||
| 215 | " FROM tbl_equipments " |
||
| 216 | " WHERE id = %s ", (equipment_id,)) |
||
| 217 | row_equipment = cursor_system.fetchone() |
||
| 218 | elif equipment_uuid is not None: |
||
| 219 | cursor_system.execute(" SELECT id, name, cost_center_id " |
||
| 220 | " FROM tbl_equipments " |
||
| 221 | " WHERE uuid = %s ", (equipment_uuid,)) |
||
| 222 | row_equipment = cursor_system.fetchone() |
||
| 223 | |||
| 224 | if row_equipment is None: |
||
|
|
|||
| 225 | if cursor_system: |
||
| 226 | cursor_system.close() |
||
| 227 | if cnx_system: |
||
| 228 | cnx_system.close() |
||
| 229 | |||
| 230 | if cursor_billing: |
||
| 231 | cursor_billing.close() |
||
| 232 | if cnx_billing: |
||
| 233 | cnx_billing.close() |
||
| 234 | |||
| 235 | if cursor_historical: |
||
| 236 | cursor_historical.close() |
||
| 237 | if cnx_historical: |
||
| 238 | cnx_historical.close() |
||
| 239 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.EQUIPMENT_NOT_FOUND') |
||
| 240 | |||
| 241 | equipment = dict() |
||
| 242 | equipment['id'] = row_equipment[0] |
||
| 243 | equipment['name'] = row_equipment[1] |
||
| 244 | equipment['cost_center_id'] = row_equipment[2] |
||
| 245 | |||
| 246 | ################################################################################################################ |
||
| 247 | # Step 3: query energy categories |
||
| 248 | ################################################################################################################ |
||
| 249 | energy_category_set = set() |
||
| 250 | # query energy categories in base period |
||
| 251 | cursor_billing.execute(" SELECT DISTINCT(energy_category_id) " |
||
| 252 | " FROM tbl_equipment_output_category_hourly " |
||
| 253 | " WHERE equipment_id = %s " |
||
| 254 | " AND start_datetime_utc >= %s " |
||
| 255 | " AND start_datetime_utc < %s ", |
||
| 256 | (equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
| 257 | rows_energy_categories = cursor_billing.fetchall() |
||
| 258 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 259 | for row_energy_category in rows_energy_categories: |
||
| 260 | energy_category_set.add(row_energy_category[0]) |
||
| 261 | |||
| 262 | # query energy categories in reporting period |
||
| 263 | cursor_billing.execute(" SELECT DISTINCT(energy_category_id) " |
||
| 264 | " FROM tbl_equipment_output_category_hourly " |
||
| 265 | " WHERE equipment_id = %s " |
||
| 266 | " AND start_datetime_utc >= %s " |
||
| 267 | " AND start_datetime_utc < %s ", |
||
| 268 | (equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
| 269 | rows_energy_categories = cursor_billing.fetchall() |
||
| 270 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 271 | for row_energy_category in rows_energy_categories: |
||
| 272 | energy_category_set.add(row_energy_category[0]) |
||
| 273 | |||
| 274 | # query all energy categories in base period and reporting period |
||
| 275 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
||
| 276 | " FROM tbl_energy_categories " |
||
| 277 | " ORDER BY id ", ) |
||
| 278 | rows_energy_categories = cursor_system.fetchall() |
||
| 279 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
||
| 280 | if cursor_system: |
||
| 281 | cursor_system.close() |
||
| 282 | if cnx_system: |
||
| 283 | cnx_system.close() |
||
| 284 | |||
| 285 | if cursor_billing: |
||
| 286 | cursor_billing.close() |
||
| 287 | if cnx_billing: |
||
| 288 | cnx_billing.close() |
||
| 289 | |||
| 290 | if cursor_historical: |
||
| 291 | cursor_historical.close() |
||
| 292 | if cnx_historical: |
||
| 293 | cnx_historical.close() |
||
| 294 | raise falcon.HTTPError(status=falcon.HTTP_404, |
||
| 295 | title='API.NOT_FOUND', |
||
| 296 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 297 | energy_category_dict = dict() |
||
| 298 | for row_energy_category in rows_energy_categories: |
||
| 299 | if row_energy_category[0] in energy_category_set: |
||
| 300 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
||
| 301 | "unit_of_measure": row_energy_category[2], |
||
| 302 | "kgce": row_energy_category[3], |
||
| 303 | "kgco2e": row_energy_category[4]} |
||
| 304 | |||
| 305 | ################################################################################################################ |
||
| 306 | # Step 4: query associated points |
||
| 307 | ################################################################################################################ |
||
| 308 | point_list = list() |
||
| 309 | cursor_system.execute(" SELECT p.id, ep.name, p.units, p.object_type " |
||
| 310 | " FROM tbl_equipments e, tbl_equipments_parameters ep, tbl_points p " |
||
| 311 | " WHERE e.id = %s AND e.id = ep.equipment_id AND ep.parameter_type = 'point' " |
||
| 312 | " AND ep.point_id = p.id " |
||
| 313 | " ORDER BY p.id ", (equipment['id'],)) |
||
| 314 | rows_points = cursor_system.fetchall() |
||
| 315 | if rows_points is not None and len(rows_points) > 0: |
||
| 316 | for row in rows_points: |
||
| 317 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
||
| 318 | |||
| 319 | ################################################################################################################ |
||
| 320 | # Step 5: query base period energy income |
||
| 321 | ################################################################################################################ |
||
| 322 | base = dict() |
||
| 323 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 324 | for energy_category_id in energy_category_set: |
||
| 325 | base[energy_category_id] = dict() |
||
| 326 | base[energy_category_id]['timestamps'] = list() |
||
| 327 | base[energy_category_id]['values'] = list() |
||
| 328 | base[energy_category_id]['subtotal'] = Decimal(0.0) |
||
| 329 | |||
| 330 | cursor_billing.execute(" SELECT start_datetime_utc, actual_value " |
||
| 331 | " FROM tbl_equipment_output_category_hourly " |
||
| 332 | " WHERE equipment_id = %s " |
||
| 333 | " AND energy_category_id = %s " |
||
| 334 | " AND start_datetime_utc >= %s " |
||
| 335 | " AND start_datetime_utc < %s " |
||
| 336 | " ORDER BY start_datetime_utc ", |
||
| 337 | (equipment['id'], |
||
| 338 | energy_category_id, |
||
| 339 | base_start_datetime_utc, |
||
| 340 | base_end_datetime_utc)) |
||
| 341 | rows_equipment_hourly = cursor_billing.fetchall() |
||
| 342 | |||
| 343 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
||
| 344 | base_start_datetime_utc, |
||
| 345 | base_end_datetime_utc, |
||
| 346 | period_type) |
||
| 347 | for row_equipment_periodically in rows_equipment_periodically: |
||
| 348 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 349 | timedelta(minutes=timezone_offset) |
||
| 350 | if period_type == 'hourly': |
||
| 351 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
| 352 | elif period_type == 'daily': |
||
| 353 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
| 354 | elif period_type == 'weekly': |
||
| 355 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
| 356 | elif period_type == 'monthly': |
||
| 357 | current_datetime = current_datetime_local.isoformat()[0:7] |
||
| 358 | elif period_type == 'yearly': |
||
| 359 | current_datetime = current_datetime_local.isoformat()[0:4] |
||
| 360 | |||
| 361 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
||
| 362 | else row_equipment_periodically[1] |
||
| 363 | base[energy_category_id]['timestamps'].append(current_datetime) |
||
| 364 | base[energy_category_id]['values'].append(actual_value) |
||
| 365 | base[energy_category_id]['subtotal'] += actual_value |
||
| 366 | |||
| 367 | ################################################################################################################ |
||
| 368 | # Step 8: query reporting period energy income |
||
| 369 | ################################################################################################################ |
||
| 370 | reporting = dict() |
||
| 371 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 372 | for energy_category_id in energy_category_set: |
||
| 373 | reporting[energy_category_id] = dict() |
||
| 374 | reporting[energy_category_id]['timestamps'] = list() |
||
| 375 | reporting[energy_category_id]['values'] = list() |
||
| 376 | reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
||
| 377 | |||
| 378 | cursor_billing.execute(" SELECT start_datetime_utc, actual_value " |
||
| 379 | " FROM tbl_equipment_output_category_hourly " |
||
| 380 | " WHERE equipment_id = %s " |
||
| 381 | " AND energy_category_id = %s " |
||
| 382 | " AND start_datetime_utc >= %s " |
||
| 383 | " AND start_datetime_utc < %s " |
||
| 384 | " ORDER BY start_datetime_utc ", |
||
| 385 | (equipment['id'], |
||
| 386 | energy_category_id, |
||
| 387 | reporting_start_datetime_utc, |
||
| 388 | reporting_end_datetime_utc)) |
||
| 389 | rows_equipment_hourly = cursor_billing.fetchall() |
||
| 390 | |||
| 391 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
||
| 392 | reporting_start_datetime_utc, |
||
| 393 | reporting_end_datetime_utc, |
||
| 394 | period_type) |
||
| 395 | for row_equipment_periodically in rows_equipment_periodically: |
||
| 396 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 397 | timedelta(minutes=timezone_offset) |
||
| 398 | if period_type == 'hourly': |
||
| 399 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
| 400 | elif period_type == 'daily': |
||
| 401 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
| 402 | elif period_type == 'weekly': |
||
| 403 | current_datetime = current_datetime_local.isoformat()[0:10] |
||
| 404 | elif period_type == 'monthly': |
||
| 405 | current_datetime = current_datetime_local.isoformat()[0:7] |
||
| 406 | elif period_type == 'yearly': |
||
| 407 | current_datetime = current_datetime_local.isoformat()[0:4] |
||
| 408 | |||
| 409 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
||
| 410 | else row_equipment_periodically[1] |
||
| 411 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
||
| 412 | reporting[energy_category_id]['values'].append(actual_value) |
||
| 413 | reporting[energy_category_id]['subtotal'] += actual_value |
||
| 414 | |||
| 415 | ################################################################################################################ |
||
| 416 | # Step 9: query tariff data |
||
| 417 | ################################################################################################################ |
||
| 418 | parameters_data = dict() |
||
| 419 | parameters_data['names'] = list() |
||
| 420 | parameters_data['timestamps'] = list() |
||
| 421 | parameters_data['values'] = list() |
||
| 422 | if not is_quick_mode: |
||
| 423 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0: |
||
| 424 | for energy_category_id in energy_category_set: |
||
| 425 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(equipment['cost_center_id'], |
||
| 426 | energy_category_id, |
||
| 427 | reporting_start_datetime_utc, |
||
| 428 | reporting_end_datetime_utc) |
||
| 429 | tariff_timestamp_list = list() |
||
| 430 | tariff_value_list = list() |
||
| 431 | for k, v in energy_category_tariff_dict.items(): |
||
| 432 | # convert k from utc to local |
||
| 433 | k = k + timedelta(minutes=timezone_offset) |
||
| 434 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
||
| 435 | tariff_value_list.append(v) |
||
| 436 | |||
| 437 | parameters_data['names'].append(_('Tariff') |
||
| 438 | + '-' + energy_category_dict[energy_category_id]['name']) |
||
| 439 | parameters_data['timestamps'].append(tariff_timestamp_list) |
||
| 440 | parameters_data['values'].append(tariff_value_list) |
||
| 441 | |||
| 442 | ################################################################################################################ |
||
| 443 | # Step 10: query associated points data |
||
| 444 | ################################################################################################################ |
||
| 445 | if not is_quick_mode: |
||
| 446 | for point in point_list: |
||
| 447 | point_values = [] |
||
| 448 | point_timestamps = [] |
||
| 449 | if point['object_type'] == 'ENERGY_VALUE': |
||
| 450 | query = (" SELECT utc_date_time, actual_value " |
||
| 451 | " FROM tbl_energy_value " |
||
| 452 | " WHERE point_id = %s " |
||
| 453 | " AND utc_date_time BETWEEN %s AND %s " |
||
| 454 | " ORDER BY utc_date_time ") |
||
| 455 | cursor_historical.execute(query, (point['id'], |
||
| 456 | reporting_start_datetime_utc, |
||
| 457 | reporting_end_datetime_utc)) |
||
| 458 | rows = cursor_historical.fetchall() |
||
| 459 | |||
| 460 | if rows is not None and len(rows) > 0: |
||
| 461 | for row in rows: |
||
| 462 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 463 | timedelta(minutes=timezone_offset) |
||
| 464 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
| 465 | point_timestamps.append(current_datetime) |
||
| 466 | point_values.append(row[1]) |
||
| 467 | elif point['object_type'] == 'ANALOG_VALUE': |
||
| 468 | query = (" SELECT utc_date_time, actual_value " |
||
| 469 | " FROM tbl_analog_value " |
||
| 470 | " WHERE point_id = %s " |
||
| 471 | " AND utc_date_time BETWEEN %s AND %s " |
||
| 472 | " ORDER BY utc_date_time ") |
||
| 473 | cursor_historical.execute(query, (point['id'], |
||
| 474 | reporting_start_datetime_utc, |
||
| 475 | reporting_end_datetime_utc)) |
||
| 476 | rows = cursor_historical.fetchall() |
||
| 477 | |||
| 478 | if rows is not None and len(rows) > 0: |
||
| 479 | for row in rows: |
||
| 480 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 481 | timedelta(minutes=timezone_offset) |
||
| 482 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
| 483 | point_timestamps.append(current_datetime) |
||
| 484 | point_values.append(row[1]) |
||
| 485 | elif point['object_type'] == 'DIGITAL_VALUE': |
||
| 486 | query = (" SELECT utc_date_time, actual_value " |
||
| 487 | " FROM tbl_digital_value " |
||
| 488 | " WHERE point_id = %s " |
||
| 489 | " AND utc_date_time BETWEEN %s AND %s " |
||
| 490 | " ORDER BY utc_date_time ") |
||
| 491 | cursor_historical.execute(query, (point['id'], |
||
| 492 | reporting_start_datetime_utc, |
||
| 493 | reporting_end_datetime_utc)) |
||
| 494 | rows = cursor_historical.fetchall() |
||
| 495 | |||
| 496 | if rows is not None and len(rows) > 0: |
||
| 497 | for row in rows: |
||
| 498 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 499 | timedelta(minutes=timezone_offset) |
||
| 500 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
| 501 | point_timestamps.append(current_datetime) |
||
| 502 | point_values.append(row[1]) |
||
| 503 | |||
| 504 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
||
| 505 | parameters_data['timestamps'].append(point_timestamps) |
||
| 506 | parameters_data['values'].append(point_values) |
||
| 507 | |||
| 508 | ################################################################################################################ |
||
| 509 | # Step 12: construct the report |
||
| 510 | ################################################################################################################ |
||
| 511 | if cursor_system: |
||
| 512 | cursor_system.close() |
||
| 513 | if cnx_system: |
||
| 514 | cnx_system.close() |
||
| 515 | |||
| 516 | if cursor_billing: |
||
| 517 | cursor_billing.close() |
||
| 518 | if cnx_billing: |
||
| 519 | cnx_billing.close() |
||
| 520 | |||
| 521 | if cursor_historical: |
||
| 522 | cursor_historical.close() |
||
| 523 | if cnx_historical: |
||
| 524 | cnx_historical.close() |
||
| 525 | |||
| 526 | result = dict() |
||
| 527 | |||
| 528 | result['equipment'] = dict() |
||
| 529 | result['equipment']['name'] = equipment['name'] |
||
| 530 | |||
| 531 | result['base_period'] = dict() |
||
| 532 | result['base_period']['names'] = list() |
||
| 533 | result['base_period']['units'] = list() |
||
| 534 | result['base_period']['timestamps'] = list() |
||
| 535 | result['base_period']['values'] = list() |
||
| 536 | result['base_period']['subtotals'] = list() |
||
| 537 | result['base_period']['total'] = Decimal(0.0) |
||
| 538 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 539 | for energy_category_id in energy_category_set: |
||
| 540 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
| 541 | result['base_period']['units'].append(config.currency_unit) |
||
| 542 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
||
| 543 | result['base_period']['values'].append(base[energy_category_id]['values']) |
||
| 544 | result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
||
| 545 | result['base_period']['total'] += base[energy_category_id]['subtotal'] |
||
| 546 | |||
| 547 | result['reporting_period'] = dict() |
||
| 548 | result['reporting_period']['names'] = list() |
||
| 549 | result['reporting_period']['energy_category_ids'] = list() |
||
| 550 | result['reporting_period']['units'] = list() |
||
| 551 | result['reporting_period']['timestamps'] = list() |
||
| 552 | result['reporting_period']['values'] = list() |
||
| 553 | result['reporting_period']['rates'] = list() |
||
| 554 | result['reporting_period']['subtotals'] = list() |
||
| 555 | result['reporting_period']['subtotals_per_unit_area'] = list() |
||
| 556 | result['reporting_period']['increment_rates'] = list() |
||
| 557 | result['reporting_period']['total'] = Decimal(0.0) |
||
| 558 | result['reporting_period']['total_per_unit_area'] = Decimal(0.0) |
||
| 559 | result['reporting_period']['total_increment_rate'] = Decimal(0.0) |
||
| 560 | result['reporting_period']['total_unit'] = config.currency_unit |
||
| 561 | |||
| 562 | View Code Duplication | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 563 | for energy_category_id in energy_category_set: |
||
| 564 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
| 565 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
||
| 566 | result['reporting_period']['units'].append(config.currency_unit) |
||
| 567 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
||
| 568 | result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
||
| 569 | result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
||
| 570 | result['reporting_period']['increment_rates'].append( |
||
| 571 | (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
||
| 572 | base[energy_category_id]['subtotal'] |
||
| 573 | if base[energy_category_id]['subtotal'] > 0.0 else None) |
||
| 574 | result['reporting_period']['total'] += reporting[energy_category_id]['subtotal'] |
||
| 575 | |||
| 576 | rate = list() |
||
| 577 | for index, value in enumerate(reporting[energy_category_id]['values']): |
||
| 578 | if index < len(base[energy_category_id]['values']) \ |
||
| 579 | and base[energy_category_id]['values'][index] != 0 and value != 0: |
||
| 580 | rate.append((value - base[energy_category_id]['values'][index]) |
||
| 581 | / base[energy_category_id]['values'][index]) |
||
| 582 | else: |
||
| 583 | rate.append(None) |
||
| 584 | result['reporting_period']['rates'].append(rate) |
||
| 585 | |||
| 586 | result['reporting_period']['total_increment_rate'] = \ |
||
| 587 | (result['reporting_period']['total'] - result['base_period']['total']) / \ |
||
| 588 | result['base_period']['total'] \ |
||
| 589 | if result['base_period']['total'] > Decimal(0.0) else None |
||
| 590 | |||
| 591 | result['parameters'] = { |
||
| 592 | "names": parameters_data['names'], |
||
| 593 | "timestamps": parameters_data['timestamps'], |
||
| 594 | "values": parameters_data['values'] |
||
| 595 | } |
||
| 596 | |||
| 597 | # export result to Excel file and then encode the file to base64 string |
||
| 598 | result['excel_bytes_base64'] = None |
||
| 599 | if not is_quick_mode: |
||
| 600 | result['excel_bytes_base64'] = excelexporters.equipmentincome.export(result, |
||
| 601 | equipment['name'], |
||
| 602 | base_period_start_datetime_local, |
||
| 603 | base_period_end_datetime_local, |
||
| 604 | reporting_period_start_datetime_local, |
||
| 605 | reporting_period_end_datetime_local, |
||
| 606 | period_type, |
||
| 607 | language) |
||
| 608 | resp.text = json.dumps(result) |
||
| 609 |