myems /
myems-api
| 1 | import falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | from datetime import datetime, timedelta, timezone |
||
| 6 | from core import utilities |
||
| 7 | from decimal import Decimal |
||
| 8 | |||
| 9 | |||
| 10 | View Code Duplication | class Reporting: |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 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 combined equipment |
||
| 23 | # Step 3: query energy categories |
||
| 24 | # Step 4: query associated points |
||
| 25 | # Step 5: query base period energy output |
||
| 26 | # Step 6: query reporting period energy output |
||
| 27 | # Step 7: query tariff data |
||
| 28 | # Step 8: query associated points data |
||
| 29 | # Step 9: construct the report |
||
| 30 | #################################################################################################################### |
||
| 31 | @staticmethod |
||
| 32 | def on_get(req, resp): |
||
| 33 | print(req.params) |
||
| 34 | combined_equipment_id = req.params.get('combinedequipmentid') |
||
| 35 | period_type = req.params.get('periodtype') |
||
| 36 | base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
||
| 37 | base_end_datetime_local = req.params.get('baseperiodenddatetime') |
||
| 38 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
| 39 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
| 40 | |||
| 41 | ################################################################################################################ |
||
| 42 | # Step 1: valid parameters |
||
| 43 | ################################################################################################################ |
||
| 44 | if combined_equipment_id is None: |
||
| 45 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 46 | title='API.BAD_REQUEST', |
||
| 47 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 48 | else: |
||
| 49 | combined_equipment_id = str.strip(combined_equipment_id) |
||
| 50 | if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0: |
||
| 51 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 52 | title='API.BAD_REQUEST', |
||
| 53 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 54 | |||
| 55 | if period_type is None: |
||
| 56 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
| 57 | else: |
||
| 58 | period_type = str.strip(period_type) |
||
| 59 | if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
||
| 60 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
||
| 61 | |||
| 62 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 63 | if config.utc_offset[0] == '-': |
||
| 64 | timezone_offset = -timezone_offset |
||
| 65 | |||
| 66 | base_start_datetime_utc = None |
||
| 67 | if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
||
| 68 | base_start_datetime_local = str.strip(base_start_datetime_local) |
||
| 69 | try: |
||
| 70 | base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
||
| 71 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 72 | timedelta(minutes=timezone_offset) |
||
| 73 | except ValueError: |
||
| 74 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 75 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
||
| 76 | |||
| 77 | base_end_datetime_utc = None |
||
| 78 | if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
||
| 79 | base_end_datetime_local = str.strip(base_end_datetime_local) |
||
| 80 | try: |
||
| 81 | base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
||
| 82 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 83 | timedelta(minutes=timezone_offset) |
||
| 84 | except ValueError: |
||
| 85 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 86 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
||
| 87 | |||
| 88 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
||
| 89 | base_start_datetime_utc >= base_end_datetime_utc: |
||
| 90 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 91 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
||
| 92 | |||
| 93 | if reporting_start_datetime_local is None: |
||
| 94 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 95 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
| 96 | else: |
||
| 97 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
||
| 98 | try: |
||
| 99 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
||
| 100 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 101 | timedelta(minutes=timezone_offset) |
||
| 102 | except ValueError: |
||
| 103 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 104 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
| 105 | |||
| 106 | if reporting_end_datetime_local is None: |
||
| 107 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 108 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
| 109 | else: |
||
| 110 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
||
| 111 | try: |
||
| 112 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
||
| 113 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 114 | timedelta(minutes=timezone_offset) |
||
| 115 | except ValueError: |
||
| 116 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 117 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
| 118 | |||
| 119 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
| 120 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 121 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
||
| 122 | |||
| 123 | ################################################################################################################ |
||
| 124 | # Step 2: query the combined equipment |
||
| 125 | ################################################################################################################ |
||
| 126 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
| 127 | cursor_system = cnx_system.cursor() |
||
| 128 | |||
| 129 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
||
| 130 | cursor_energy = cnx_energy.cursor() |
||
| 131 | |||
| 132 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
||
| 133 | cursor_historical = cnx_historical.cursor() |
||
| 134 | |||
| 135 | cursor_system.execute(" SELECT id, name, cost_center_id " |
||
| 136 | " FROM tbl_combined_equipments " |
||
| 137 | " WHERE id = %s ", (combined_equipment_id,)) |
||
| 138 | row_combined_equipment = cursor_system.fetchone() |
||
| 139 | if row_combined_equipment 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 cnx_historical: |
||
| 151 | cnx_historical.close() |
||
| 152 | if cursor_historical: |
||
| 153 | cursor_historical.disconnect() |
||
| 154 | raise falcon.HTTPError(falcon.HTTP_404, |
||
| 155 | title='API.NOT_FOUND', |
||
| 156 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 157 | |||
| 158 | combined_equipment = dict() |
||
| 159 | combined_equipment['id'] = row_combined_equipment[0] |
||
| 160 | combined_equipment['name'] = row_combined_equipment[1] |
||
| 161 | combined_equipment['cost_center_id'] = row_combined_equipment[2] |
||
| 162 | |||
| 163 | ################################################################################################################ |
||
| 164 | # Step 3: query energy categories |
||
| 165 | ################################################################################################################ |
||
| 166 | energy_category_set = set() |
||
| 167 | # query energy categories in base period |
||
| 168 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
||
| 169 | " FROM tbl_combined_equipment_output_category_hourly " |
||
| 170 | " WHERE combined_equipment_id = %s " |
||
| 171 | " AND start_datetime_utc >= %s " |
||
| 172 | " AND start_datetime_utc < %s ", |
||
| 173 | (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
||
| 174 | rows_energy_categories = cursor_energy.fetchall() |
||
| 175 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
||
| 176 | for row_energy_category in rows_energy_categories: |
||
| 177 | energy_category_set.add(row_energy_category[0]) |
||
| 178 | |||
| 179 | # query energy categories in reporting period |
||
| 180 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
||
| 181 | " FROM tbl_combined_equipment_output_category_hourly " |
||
| 182 | " WHERE combined_equipment_id = %s " |
||
| 183 | " AND start_datetime_utc >= %s " |
||
| 184 | " AND start_datetime_utc < %s ", |
||
| 185 | (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
| 186 | rows_energy_categories = cursor_energy.fetchall() |
||
| 187 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
||
| 188 | for row_energy_category in rows_energy_categories: |
||
| 189 | energy_category_set.add(row_energy_category[0]) |
||
| 190 | |||
| 191 | # query all energy categories in base period and reporting period |
||
| 192 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
||
| 193 | " FROM tbl_energy_categories " |
||
| 194 | " ORDER BY id ", ) |
||
| 195 | rows_energy_categories = cursor_system.fetchall() |
||
| 196 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
||
| 197 | if cursor_system: |
||
| 198 | cursor_system.close() |
||
| 199 | if cnx_system: |
||
| 200 | cnx_system.disconnect() |
||
| 201 | |||
| 202 | if cursor_energy: |
||
| 203 | cursor_energy.close() |
||
| 204 | if cnx_energy: |
||
| 205 | cnx_energy.disconnect() |
||
| 206 | |||
| 207 | if cnx_historical: |
||
| 208 | cnx_historical.close() |
||
| 209 | if cursor_historical: |
||
| 210 | cursor_historical.disconnect() |
||
| 211 | raise falcon.HTTPError(falcon.HTTP_404, |
||
| 212 | title='API.NOT_FOUND', |
||
| 213 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 214 | energy_category_dict = dict() |
||
| 215 | for row_energy_category in rows_energy_categories: |
||
| 216 | if row_energy_category[0] in energy_category_set: |
||
| 217 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
||
| 218 | "unit_of_measure": row_energy_category[2], |
||
| 219 | "kgce": row_energy_category[3], |
||
| 220 | "kgco2e": row_energy_category[4]} |
||
| 221 | |||
| 222 | ################################################################################################################ |
||
| 223 | # Step 4: query associated points |
||
| 224 | ################################################################################################################ |
||
| 225 | point_list = list() |
||
| 226 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
||
| 227 | " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " |
||
| 228 | " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " |
||
| 229 | " AND ep.point_id = p.id " |
||
| 230 | " ORDER BY p.id ", (combined_equipment['id'],)) |
||
| 231 | rows_points = cursor_system.fetchall() |
||
| 232 | if rows_points is not None and len(rows_points) > 0: |
||
| 233 | for row in rows_points: |
||
| 234 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
||
| 235 | |||
| 236 | ################################################################################################################ |
||
| 237 | # Step 5: query base period energy output |
||
| 238 | ################################################################################################################ |
||
| 239 | base = dict() |
||
| 240 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 241 | for energy_category_id in energy_category_set: |
||
| 242 | base[energy_category_id] = dict() |
||
| 243 | base[energy_category_id]['timestamps'] = list() |
||
| 244 | base[energy_category_id]['values'] = list() |
||
| 245 | base[energy_category_id]['subtotal'] = Decimal(0.0) |
||
| 246 | |||
| 247 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
||
| 248 | " FROM tbl_combined_equipment_output_category_hourly " |
||
| 249 | " WHERE combined_equipment_id = %s " |
||
| 250 | " AND energy_category_id = %s " |
||
| 251 | " AND start_datetime_utc >= %s " |
||
| 252 | " AND start_datetime_utc < %s " |
||
| 253 | " ORDER BY start_datetime_utc ", |
||
| 254 | (combined_equipment['id'], |
||
| 255 | energy_category_id, |
||
| 256 | base_start_datetime_utc, |
||
| 257 | base_end_datetime_utc)) |
||
| 258 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
||
| 259 | |||
| 260 | rows_combined_equipment_periodically = \ |
||
| 261 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
||
| 262 | base_start_datetime_utc, |
||
| 263 | base_end_datetime_utc, |
||
| 264 | period_type) |
||
| 265 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
||
| 266 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 267 | timedelta(minutes=timezone_offset) |
||
| 268 | if period_type == 'hourly': |
||
| 269 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 270 | elif period_type == 'daily': |
||
| 271 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
| 272 | elif period_type == 'monthly': |
||
| 273 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
| 274 | elif period_type == 'yearly': |
||
| 275 | current_datetime = current_datetime_local.strftime('%Y') |
||
| 276 | |||
| 277 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
||
| 278 | else row_combined_equipment_periodically[1] |
||
| 279 | base[energy_category_id]['timestamps'].append(current_datetime) |
||
|
0 ignored issues
–
show
|
|||
| 280 | base[energy_category_id]['values'].append(actual_value) |
||
| 281 | base[energy_category_id]['subtotal'] += actual_value |
||
| 282 | |||
| 283 | ################################################################################################################ |
||
| 284 | # Step 8: query reporting period energy output |
||
| 285 | ################################################################################################################ |
||
| 286 | reporting = dict() |
||
| 287 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 288 | for energy_category_id in energy_category_set: |
||
| 289 | reporting[energy_category_id] = dict() |
||
| 290 | reporting[energy_category_id]['timestamps'] = list() |
||
| 291 | reporting[energy_category_id]['values'] = list() |
||
| 292 | reporting[energy_category_id]['subtotal'] = Decimal(0.0) |
||
| 293 | |||
| 294 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
||
| 295 | " FROM tbl_combined_equipment_output_category_hourly " |
||
| 296 | " WHERE combined_equipment_id = %s " |
||
| 297 | " AND energy_category_id = %s " |
||
| 298 | " AND start_datetime_utc >= %s " |
||
| 299 | " AND start_datetime_utc < %s " |
||
| 300 | " ORDER BY start_datetime_utc ", |
||
| 301 | (combined_equipment['id'], |
||
| 302 | energy_category_id, |
||
| 303 | reporting_start_datetime_utc, |
||
| 304 | reporting_end_datetime_utc)) |
||
| 305 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
||
| 306 | |||
| 307 | rows_combined_equipment_periodically = \ |
||
| 308 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
||
| 309 | reporting_start_datetime_utc, |
||
| 310 | reporting_end_datetime_utc, |
||
| 311 | period_type) |
||
| 312 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
||
| 313 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
||
| 314 | timedelta(minutes=timezone_offset) |
||
| 315 | if period_type == 'hourly': |
||
| 316 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 317 | elif period_type == 'daily': |
||
| 318 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
||
| 319 | elif period_type == 'monthly': |
||
| 320 | current_datetime = current_datetime_local.strftime('%Y-%m') |
||
| 321 | elif period_type == 'yearly': |
||
| 322 | current_datetime = current_datetime_local.strftime('%Y') |
||
| 323 | |||
| 324 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
||
| 325 | else row_combined_equipment_periodically[1] |
||
| 326 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
||
| 327 | reporting[energy_category_id]['values'].append(actual_value) |
||
| 328 | reporting[energy_category_id]['subtotal'] += actual_value |
||
| 329 | |||
| 330 | ################################################################################################################ |
||
| 331 | # Step 9: query tariff data |
||
| 332 | ################################################################################################################ |
||
| 333 | parameters_data = dict() |
||
| 334 | parameters_data['names'] = list() |
||
| 335 | parameters_data['timestamps'] = list() |
||
| 336 | parameters_data['values'] = list() |
||
| 337 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 338 | for energy_category_id in energy_category_set: |
||
| 339 | energy_category_tariff_dict = \ |
||
| 340 | utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'], |
||
| 341 | energy_category_id, |
||
| 342 | reporting_start_datetime_utc, |
||
| 343 | reporting_end_datetime_utc) |
||
| 344 | tariff_timestamp_list = list() |
||
| 345 | tariff_value_list = list() |
||
| 346 | for k, v in energy_category_tariff_dict.items(): |
||
| 347 | # convert k from utc to local |
||
| 348 | k = k + timedelta(minutes=timezone_offset) |
||
| 349 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
||
| 350 | tariff_value_list.append(v) |
||
| 351 | |||
| 352 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
||
| 353 | parameters_data['timestamps'].append(tariff_timestamp_list) |
||
| 354 | parameters_data['values'].append(tariff_value_list) |
||
| 355 | |||
| 356 | ################################################################################################################ |
||
| 357 | # Step 10: query associated points data |
||
| 358 | ################################################################################################################ |
||
| 359 | for point in point_list: |
||
| 360 | point_values = [] |
||
| 361 | point_timestamps = [] |
||
| 362 | if point['object_type'] == 'ANALOG_VALUE': |
||
| 363 | query = (" SELECT utc_date_time, actual_value " |
||
| 364 | " FROM tbl_analog_value " |
||
| 365 | " WHERE point_id = %s " |
||
| 366 | " AND utc_date_time BETWEEN %s AND %s " |
||
| 367 | " ORDER BY utc_date_time ") |
||
| 368 | cursor_historical.execute(query, (point['id'], |
||
| 369 | reporting_start_datetime_utc, |
||
| 370 | reporting_end_datetime_utc)) |
||
| 371 | rows = cursor_historical.fetchall() |
||
| 372 | |||
| 373 | if rows is not None and len(rows) > 0: |
||
| 374 | for row in rows: |
||
| 375 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 376 | timedelta(minutes=timezone_offset) |
||
| 377 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 378 | point_timestamps.append(current_datetime) |
||
| 379 | point_values.append(row[1]) |
||
| 380 | |||
| 381 | elif point['object_type'] == 'ENERGY_VALUE': |
||
| 382 | query = (" SELECT utc_date_time, actual_value " |
||
| 383 | " FROM tbl_energy_value " |
||
| 384 | " WHERE point_id = %s " |
||
| 385 | " AND utc_date_time BETWEEN %s AND %s " |
||
| 386 | " ORDER BY utc_date_time ") |
||
| 387 | cursor_historical.execute(query, (point['id'], |
||
| 388 | reporting_start_datetime_utc, |
||
| 389 | reporting_end_datetime_utc)) |
||
| 390 | rows = cursor_historical.fetchall() |
||
| 391 | |||
| 392 | if rows is not None and len(rows) > 0: |
||
| 393 | for row in rows: |
||
| 394 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 395 | timedelta(minutes=timezone_offset) |
||
| 396 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 397 | point_timestamps.append(current_datetime) |
||
| 398 | point_values.append(row[1]) |
||
| 399 | elif point['object_type'] == 'DIGITAL_VALUE': |
||
| 400 | query = (" SELECT utc_date_time, actual_value " |
||
| 401 | " FROM tbl_digital_value " |
||
| 402 | " WHERE point_id = %s " |
||
| 403 | " AND utc_date_time BETWEEN %s AND %s ") |
||
| 404 | cursor_historical.execute(query, (point['id'], |
||
| 405 | reporting_start_datetime_utc, |
||
| 406 | reporting_end_datetime_utc)) |
||
| 407 | rows = cursor_historical.fetchall() |
||
| 408 | |||
| 409 | if rows is not None and len(rows) > 0: |
||
| 410 | for row in rows: |
||
| 411 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
| 412 | timedelta(minutes=timezone_offset) |
||
| 413 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
||
| 414 | point_timestamps.append(current_datetime) |
||
| 415 | point_values.append(row[1]) |
||
| 416 | |||
| 417 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
||
| 418 | parameters_data['timestamps'].append(point_timestamps) |
||
| 419 | parameters_data['values'].append(point_values) |
||
| 420 | |||
| 421 | ################################################################################################################ |
||
| 422 | # Step 12: construct the report |
||
| 423 | ################################################################################################################ |
||
| 424 | if cursor_system: |
||
| 425 | cursor_system.close() |
||
| 426 | if cnx_system: |
||
| 427 | cnx_system.disconnect() |
||
| 428 | |||
| 429 | if cursor_energy: |
||
| 430 | cursor_energy.close() |
||
| 431 | if cnx_energy: |
||
| 432 | cnx_energy.disconnect() |
||
| 433 | |||
| 434 | result = dict() |
||
| 435 | |||
| 436 | result['combined_equipment'] = dict() |
||
| 437 | result['combined_equipment']['name'] = combined_equipment['name'] |
||
| 438 | |||
| 439 | result['base_period'] = dict() |
||
| 440 | result['base_period']['names'] = list() |
||
| 441 | result['base_period']['units'] = list() |
||
| 442 | result['base_period']['timestamps'] = list() |
||
| 443 | result['base_period']['values'] = list() |
||
| 444 | result['base_period']['subtotals'] = list() |
||
| 445 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 446 | for energy_category_id in energy_category_set: |
||
| 447 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
| 448 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
||
| 449 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
||
| 450 | result['base_period']['values'].append(base[energy_category_id]['values']) |
||
| 451 | result['base_period']['subtotals'].append(base[energy_category_id]['subtotal']) |
||
| 452 | |||
| 453 | result['reporting_period'] = dict() |
||
| 454 | result['reporting_period']['names'] = list() |
||
| 455 | result['reporting_period']['energy_category_ids'] = list() |
||
| 456 | result['reporting_period']['units'] = list() |
||
| 457 | result['reporting_period']['timestamps'] = list() |
||
| 458 | result['reporting_period']['values'] = list() |
||
| 459 | result['reporting_period']['subtotals'] = list() |
||
| 460 | result['reporting_period']['increment_rates'] = list() |
||
| 461 | |||
| 462 | if energy_category_set is not None and len(energy_category_set) > 0: |
||
| 463 | for energy_category_id in energy_category_set: |
||
| 464 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
||
| 465 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
||
| 466 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
||
| 467 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
||
| 468 | result['reporting_period']['values'].append(reporting[energy_category_id]['values']) |
||
| 469 | result['reporting_period']['subtotals'].append(reporting[energy_category_id]['subtotal']) |
||
| 470 | result['reporting_period']['increment_rates'].append( |
||
| 471 | (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / |
||
| 472 | base[energy_category_id]['subtotal'] |
||
| 473 | if base[energy_category_id]['subtotal'] > 0.0 else None) |
||
| 474 | |||
| 475 | result['parameters'] = { |
||
| 476 | "names": parameters_data['names'], |
||
| 477 | "timestamps": parameters_data['timestamps'], |
||
| 478 | "values": parameters_data['values'] |
||
| 479 | } |
||
| 480 | |||
| 481 | resp.body = json.dumps(result) |
||
| 482 |