| @@ 10-721 (lines=712) @@ | ||
| 7 | from decimal import * |
|
| 8 | ||
| 9 | ||
| 10 | 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 combined equipment |
|
| 23 | # Step 3: query energy categories |
|
| 24 | # Step 4: query associated points |
|
| 25 | # Step 5: query base period energy input |
|
| 26 | # Step 6: query base period energy output |
|
| 27 | # Step 7: query reporting period energy input |
|
| 28 | # Step 8: query reporting period energy output |
|
| 29 | # Step 9: query tariff data |
|
| 30 | # Step 10: query associated sensors and points data |
|
| 31 | # Step 11: construct the report |
|
| 32 | #################################################################################################################### |
|
| 33 | @staticmethod |
|
| 34 | def on_get(req, resp): |
|
| 35 | print(req.params) |
|
| 36 | combined_equipment_id = req.params.get('combinedequipmentid') |
|
| 37 | period_type = req.params.get('periodtype') |
|
| 38 | base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 39 | base_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 40 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 41 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 42 | ||
| 43 | ################################################################################################################ |
|
| 44 | # Step 1: valid parameters |
|
| 45 | ################################################################################################################ |
|
| 46 | if combined_equipment_id is None: |
|
| 47 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 48 | title='API.BAD_REQUEST', |
|
| 49 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 50 | else: |
|
| 51 | combined_equipment_id = str.strip(combined_equipment_id) |
|
| 52 | if not combined_equipment_id.isdigit() or int(combined_equipment_id) <= 0: |
|
| 53 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 54 | title='API.BAD_REQUEST', |
|
| 55 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 56 | ||
| 57 | if period_type is None: |
|
| 58 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
|
| 59 | else: |
|
| 60 | period_type = str.strip(period_type) |
|
| 61 | if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
|
| 62 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
|
| 63 | ||
| 64 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 65 | if config.utc_offset[0] == '-': |
|
| 66 | timezone_offset = -timezone_offset |
|
| 67 | ||
| 68 | base_start_datetime_utc = None |
|
| 69 | if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
|
| 70 | base_start_datetime_local = str.strip(base_start_datetime_local) |
|
| 71 | try: |
|
| 72 | base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
|
| 73 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 74 | timedelta(minutes=timezone_offset) |
|
| 75 | except ValueError: |
|
| 76 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 77 | description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") |
|
| 78 | ||
| 79 | base_end_datetime_utc = None |
|
| 80 | if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
|
| 81 | base_end_datetime_local = str.strip(base_end_datetime_local) |
|
| 82 | try: |
|
| 83 | base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
|
| 84 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 85 | timedelta(minutes=timezone_offset) |
|
| 86 | except ValueError: |
|
| 87 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 88 | description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") |
|
| 89 | ||
| 90 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 91 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 92 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') |
|
| 94 | ||
| 95 | if reporting_start_datetime_local is None: |
|
| 96 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 97 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
|
| 98 | else: |
|
| 99 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
|
| 100 | try: |
|
| 101 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
|
| 102 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 103 | timedelta(minutes=timezone_offset) |
|
| 104 | except ValueError: |
|
| 105 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 106 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
|
| 107 | ||
| 108 | if reporting_end_datetime_local is None: |
|
| 109 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 110 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
|
| 111 | else: |
|
| 112 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
|
| 113 | try: |
|
| 114 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
|
| 115 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 116 | timedelta(minutes=timezone_offset) |
|
| 117 | except ValueError: |
|
| 118 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 119 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
|
| 120 | ||
| 121 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 122 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 123 | description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
|
| 124 | ||
| 125 | ################################################################################################################ |
|
| 126 | # Step 2: query the combined equipment |
|
| 127 | ################################################################################################################ |
|
| 128 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 129 | cursor_system = cnx_system.cursor() |
|
| 130 | ||
| 131 | cnx_energy = mysql.connector.connect(**config.myems_billing_db) |
|
| 132 | cursor_energy = cnx_energy.cursor() |
|
| 133 | ||
| 134 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 135 | cursor_historical = cnx_historical.cursor() |
|
| 136 | ||
| 137 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
| 138 | " FROM tbl_combined_equipments " |
|
| 139 | " WHERE id = %s ", (combined_equipment_id,)) |
|
| 140 | row_combined_equipment = cursor_system.fetchone() |
|
| 141 | if row_combined_equipment is None: |
|
| 142 | if cursor_system: |
|
| 143 | cursor_system.close() |
|
| 144 | if cnx_system: |
|
| 145 | cnx_system.disconnect() |
|
| 146 | ||
| 147 | if cursor_energy: |
|
| 148 | cursor_energy.close() |
|
| 149 | if cnx_energy: |
|
| 150 | cnx_energy.disconnect() |
|
| 151 | ||
| 152 | if cnx_historical: |
|
| 153 | cnx_historical.close() |
|
| 154 | if cursor_historical: |
|
| 155 | cursor_historical.disconnect() |
|
| 156 | raise falcon.HTTPError(falcon.HTTP_404, |
|
| 157 | title='API.NOT_FOUND', |
|
| 158 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 159 | ||
| 160 | combined_equipment = dict() |
|
| 161 | combined_equipment['id'] = row_combined_equipment[0] |
|
| 162 | combined_equipment['name'] = row_combined_equipment[1] |
|
| 163 | combined_equipment['cost_center_id'] = row_combined_equipment[2] |
|
| 164 | ||
| 165 | ################################################################################################################ |
|
| 166 | # Step 3: query input energy categories and output energy categories |
|
| 167 | ################################################################################################################ |
|
| 168 | energy_category_set_input = set() |
|
| 169 | energy_category_set_output = set() |
|
| 170 | # query input energy categories in base period |
|
| 171 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 172 | " FROM tbl_combined_equipment_input_category_hourly " |
|
| 173 | " WHERE combined_equipment_id = %s " |
|
| 174 | " AND start_datetime_utc >= %s " |
|
| 175 | " AND start_datetime_utc < %s ", |
|
| 176 | (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 177 | rows_energy_categories = cursor_energy.fetchall() |
|
| 178 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 179 | for row_energy_category in rows_energy_categories: |
|
| 180 | energy_category_set_input.add(row_energy_category[0]) |
|
| 181 | ||
| 182 | # query input energy categories in reporting period |
|
| 183 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 184 | " FROM tbl_combined_equipment_input_category_hourly " |
|
| 185 | " WHERE combined_equipment_id = %s " |
|
| 186 | " AND start_datetime_utc >= %s " |
|
| 187 | " AND start_datetime_utc < %s ", |
|
| 188 | (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 189 | rows_energy_categories = cursor_energy.fetchall() |
|
| 190 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 191 | for row_energy_category in rows_energy_categories: |
|
| 192 | energy_category_set_input.add(row_energy_category[0]) |
|
| 193 | ||
| 194 | # query output energy categories in base period |
|
| 195 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 196 | " FROM tbl_combined_equipment_output_category_hourly " |
|
| 197 | " WHERE combined_equipment_id = %s " |
|
| 198 | " AND start_datetime_utc >= %s " |
|
| 199 | " AND start_datetime_utc < %s ", |
|
| 200 | (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 201 | rows_energy_categories = cursor_energy.fetchall() |
|
| 202 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 203 | for row_energy_category in rows_energy_categories: |
|
| 204 | energy_category_set_output.add(row_energy_category[0]) |
|
| 205 | ||
| 206 | # query output energy categories in reporting period |
|
| 207 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 208 | " FROM tbl_combined_equipment_output_category_hourly " |
|
| 209 | " WHERE combined_equipment_id = %s " |
|
| 210 | " AND start_datetime_utc >= %s " |
|
| 211 | " AND start_datetime_utc < %s ", |
|
| 212 | (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 213 | rows_energy_categories = cursor_energy.fetchall() |
|
| 214 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 215 | for row_energy_category in rows_energy_categories: |
|
| 216 | energy_category_set_output.add(row_energy_category[0]) |
|
| 217 | ||
| 218 | # query properties of all energy categories above |
|
| 219 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 220 | " FROM tbl_energy_categories " |
|
| 221 | " ORDER BY id ", ) |
|
| 222 | rows_energy_categories = cursor_system.fetchall() |
|
| 223 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 224 | if cursor_system: |
|
| 225 | cursor_system.close() |
|
| 226 | if cnx_system: |
|
| 227 | cnx_system.disconnect() |
|
| 228 | ||
| 229 | if cursor_energy: |
|
| 230 | cursor_energy.close() |
|
| 231 | if cnx_energy: |
|
| 232 | cnx_energy.disconnect() |
|
| 233 | ||
| 234 | if cnx_historical: |
|
| 235 | cnx_historical.close() |
|
| 236 | if cursor_historical: |
|
| 237 | cursor_historical.disconnect() |
|
| 238 | raise falcon.HTTPError(falcon.HTTP_404, |
|
| 239 | title='API.NOT_FOUND', |
|
| 240 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 241 | energy_category_dict = dict() |
|
| 242 | for row_energy_category in rows_energy_categories: |
|
| 243 | if row_energy_category[0] in energy_category_set_input or \ |
|
| 244 | row_energy_category[0] in energy_category_set_output: |
|
| 245 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 246 | "unit_of_measure": row_energy_category[2], |
|
| 247 | "kgce": row_energy_category[3], |
|
| 248 | "kgco2e": row_energy_category[4]} |
|
| 249 | ||
| 250 | ################################################################################################################ |
|
| 251 | # Step 4: query associated points |
|
| 252 | ################################################################################################################ |
|
| 253 | point_list = list() |
|
| 254 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 255 | " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " |
|
| 256 | " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " |
|
| 257 | " AND ep.point_id = p.id " |
|
| 258 | " ORDER BY p.id ", (combined_equipment['id'],)) |
|
| 259 | rows_points = cursor_system.fetchall() |
|
| 260 | if rows_points is not None and len(rows_points) > 0: |
|
| 261 | for row in rows_points: |
|
| 262 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 263 | ||
| 264 | ################################################################################################################ |
|
| 265 | # Step 5: query base period energy input |
|
| 266 | ################################################################################################################ |
|
| 267 | base_input = dict() |
|
| 268 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 269 | for energy_category_id in energy_category_set_input: |
|
| 270 | base_input[energy_category_id] = dict() |
|
| 271 | base_input[energy_category_id]['timestamps'] = list() |
|
| 272 | base_input[energy_category_id]['values'] = list() |
|
| 273 | base_input[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 274 | ||
| 275 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 276 | " FROM tbl_combined_equipment_input_category_hourly " |
|
| 277 | " WHERE combined_equipment_id = %s " |
|
| 278 | " AND energy_category_id = %s " |
|
| 279 | " AND start_datetime_utc >= %s " |
|
| 280 | " AND start_datetime_utc < %s " |
|
| 281 | " ORDER BY start_datetime_utc ", |
|
| 282 | (combined_equipment['id'], |
|
| 283 | energy_category_id, |
|
| 284 | base_start_datetime_utc, |
|
| 285 | base_end_datetime_utc)) |
|
| 286 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
| 287 | ||
| 288 | rows_combined_equipment_periodically = \ |
|
| 289 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
| 290 | base_start_datetime_utc, |
|
| 291 | base_end_datetime_utc, |
|
| 292 | period_type) |
|
| 293 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
| 294 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 295 | timedelta(minutes=timezone_offset) |
|
| 296 | if period_type == 'hourly': |
|
| 297 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 298 | elif period_type == 'daily': |
|
| 299 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 300 | elif period_type == 'monthly': |
|
| 301 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 302 | elif period_type == 'yearly': |
|
| 303 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 304 | ||
| 305 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
| 306 | else row_combined_equipment_periodically[1] |
|
| 307 | base_input[energy_category_id]['timestamps'].append(current_datetime) |
|
| 308 | base_input[energy_category_id]['values'].append(actual_value) |
|
| 309 | base_input[energy_category_id]['subtotal'] += actual_value |
|
| 310 | ||
| 311 | ################################################################################################################ |
|
| 312 | # Step 6: query base period energy output |
|
| 313 | ################################################################################################################ |
|
| 314 | base_output = dict() |
|
| 315 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 316 | for energy_category_id in energy_category_set_output: |
|
| 317 | base_output[energy_category_id] = dict() |
|
| 318 | base_output[energy_category_id]['timestamps'] = list() |
|
| 319 | base_output[energy_category_id]['values'] = list() |
|
| 320 | base_output[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 321 | ||
| 322 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 323 | " FROM tbl_combined_equipment_output_category_hourly " |
|
| 324 | " WHERE combined_equipment_id = %s " |
|
| 325 | " AND energy_category_id = %s " |
|
| 326 | " AND start_datetime_utc >= %s " |
|
| 327 | " AND start_datetime_utc < %s " |
|
| 328 | " ORDER BY start_datetime_utc ", |
|
| 329 | (combined_equipment['id'], |
|
| 330 | energy_category_id, |
|
| 331 | base_start_datetime_utc, |
|
| 332 | base_end_datetime_utc)) |
|
| 333 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
| 334 | ||
| 335 | rows_combined_equipment_periodically = \ |
|
| 336 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
| 337 | base_start_datetime_utc, |
|
| 338 | base_end_datetime_utc, |
|
| 339 | period_type) |
|
| 340 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
| 341 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 342 | timedelta(minutes=timezone_offset) |
|
| 343 | if period_type == 'hourly': |
|
| 344 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 345 | elif period_type == 'daily': |
|
| 346 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 347 | elif period_type == 'monthly': |
|
| 348 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 349 | elif period_type == 'yearly': |
|
| 350 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 351 | ||
| 352 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
| 353 | else row_combined_equipment_periodically[1] |
|
| 354 | base_output[energy_category_id]['timestamps'].append(current_datetime) |
|
| 355 | base_output[energy_category_id]['values'].append(actual_value) |
|
| 356 | base_output[energy_category_id]['subtotal'] += actual_value |
|
| 357 | ################################################################################################################ |
|
| 358 | # Step 7: query reporting period energy input |
|
| 359 | ################################################################################################################ |
|
| 360 | reporting_input = dict() |
|
| 361 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 362 | for energy_category_id in energy_category_set_input: |
|
| 363 | ||
| 364 | reporting_input[energy_category_id] = dict() |
|
| 365 | reporting_input[energy_category_id]['timestamps'] = list() |
|
| 366 | reporting_input[energy_category_id]['values'] = list() |
|
| 367 | reporting_input[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 368 | ||
| 369 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 370 | " FROM tbl_combined_equipment_input_category_hourly " |
|
| 371 | " WHERE combined_equipment_id = %s " |
|
| 372 | " AND energy_category_id = %s " |
|
| 373 | " AND start_datetime_utc >= %s " |
|
| 374 | " AND start_datetime_utc < %s " |
|
| 375 | " ORDER BY start_datetime_utc ", |
|
| 376 | (combined_equipment['id'], |
|
| 377 | energy_category_id, |
|
| 378 | reporting_start_datetime_utc, |
|
| 379 | reporting_end_datetime_utc)) |
|
| 380 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
| 381 | ||
| 382 | rows_combined_equipment_periodically = \ |
|
| 383 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
| 384 | reporting_start_datetime_utc, |
|
| 385 | reporting_end_datetime_utc, |
|
| 386 | period_type) |
|
| 387 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
| 388 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 389 | timedelta(minutes=timezone_offset) |
|
| 390 | if period_type == 'hourly': |
|
| 391 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 392 | elif period_type == 'daily': |
|
| 393 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 394 | elif period_type == 'monthly': |
|
| 395 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 396 | elif period_type == 'yearly': |
|
| 397 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 398 | ||
| 399 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
| 400 | else row_combined_equipment_periodically[1] |
|
| 401 | reporting_input[energy_category_id]['timestamps'].append(current_datetime) |
|
| 402 | reporting_input[energy_category_id]['values'].append(actual_value) |
|
| 403 | reporting_input[energy_category_id]['subtotal'] += actual_value |
|
| 404 | ||
| 405 | ################################################################################################################ |
|
| 406 | # Step 8: query reporting period energy output |
|
| 407 | ################################################################################################################ |
|
| 408 | reporting_output = dict() |
|
| 409 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 410 | for energy_category_id in energy_category_set_output: |
|
| 411 | ||
| 412 | reporting_output[energy_category_id] = dict() |
|
| 413 | reporting_output[energy_category_id]['timestamps'] = list() |
|
| 414 | reporting_output[energy_category_id]['values'] = list() |
|
| 415 | reporting_output[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 416 | ||
| 417 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 418 | " FROM tbl_combined_equipment_output_category_hourly " |
|
| 419 | " WHERE combined_equipment_id = %s " |
|
| 420 | " AND energy_category_id = %s " |
|
| 421 | " AND start_datetime_utc >= %s " |
|
| 422 | " AND start_datetime_utc < %s " |
|
| 423 | " ORDER BY start_datetime_utc ", |
|
| 424 | (combined_equipment['id'], |
|
| 425 | energy_category_id, |
|
| 426 | reporting_start_datetime_utc, |
|
| 427 | reporting_end_datetime_utc)) |
|
| 428 | rows_combined_equipment_hourly = cursor_energy.fetchall() |
|
| 429 | ||
| 430 | rows_combined_equipment_periodically = \ |
|
| 431 | utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, |
|
| 432 | reporting_start_datetime_utc, |
|
| 433 | reporting_end_datetime_utc, |
|
| 434 | period_type) |
|
| 435 | for row_combined_equipment_periodically in rows_combined_equipment_periodically: |
|
| 436 | current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 437 | timedelta(minutes=timezone_offset) |
|
| 438 | if period_type == 'hourly': |
|
| 439 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 440 | elif period_type == 'daily': |
|
| 441 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 442 | elif period_type == 'monthly': |
|
| 443 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 444 | elif period_type == 'yearly': |
|
| 445 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 446 | ||
| 447 | actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ |
|
| 448 | else row_combined_equipment_periodically[1] |
|
| 449 | reporting_output[energy_category_id]['timestamps'].append(current_datetime) |
|
| 450 | reporting_output[energy_category_id]['values'].append(actual_value) |
|
| 451 | reporting_output[energy_category_id]['subtotal'] += actual_value |
|
| 452 | ||
| 453 | ################################################################################################################ |
|
| 454 | # Step 9: query tariff data |
|
| 455 | ################################################################################################################ |
|
| 456 | parameters_data = dict() |
|
| 457 | parameters_data['names'] = list() |
|
| 458 | parameters_data['timestamps'] = list() |
|
| 459 | parameters_data['values'] = list() |
|
| 460 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 461 | for energy_category_id in energy_category_set_input: |
|
| 462 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'], |
|
| 463 | energy_category_id, |
|
| 464 | reporting_start_datetime_utc, |
|
| 465 | reporting_end_datetime_utc) |
|
| 466 | tariff_timestamp_list = list() |
|
| 467 | tariff_value_list = list() |
|
| 468 | for k, v in energy_category_tariff_dict.items(): |
|
| 469 | # convert k from utc to local |
|
| 470 | k = k + timedelta(minutes=timezone_offset) |
|
| 471 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
| 472 | tariff_value_list.append(v) |
|
| 473 | ||
| 474 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
|
| 475 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 476 | parameters_data['values'].append(tariff_value_list) |
|
| 477 | ||
| 478 | ################################################################################################################ |
|
| 479 | # Step 10: query associated sensors and points data |
|
| 480 | ################################################################################################################ |
|
| 481 | for point in point_list: |
|
| 482 | point_values = [] |
|
| 483 | point_timestamps = [] |
|
| 484 | if point['object_type'] == 'ANALOG_VALUE': |
|
| 485 | query = (" SELECT utc_date_time, actual_value " |
|
| 486 | " FROM tbl_analog_value " |
|
| 487 | " WHERE point_id = %s " |
|
| 488 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 489 | " ORDER BY utc_date_time ") |
|
| 490 | cursor_historical.execute(query, (point['id'], |
|
| 491 | reporting_start_datetime_utc, |
|
| 492 | reporting_end_datetime_utc)) |
|
| 493 | rows = cursor_historical.fetchall() |
|
| 494 | ||
| 495 | if rows is not None and len(rows) > 0: |
|
| 496 | for row in rows: |
|
| 497 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 498 | timedelta(minutes=timezone_offset) |
|
| 499 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 500 | point_timestamps.append(current_datetime) |
|
| 501 | point_values.append(row[1]) |
|
| 502 | ||
| 503 | elif point['object_type'] == 'ENERGY_VALUE': |
|
| 504 | query = (" SELECT utc_date_time, actual_value " |
|
| 505 | " FROM tbl_energy_value " |
|
| 506 | " WHERE point_id = %s " |
|
| 507 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 508 | " ORDER BY utc_date_time ") |
|
| 509 | cursor_historical.execute(query, (point['id'], |
|
| 510 | reporting_start_datetime_utc, |
|
| 511 | reporting_end_datetime_utc)) |
|
| 512 | rows = cursor_historical.fetchall() |
|
| 513 | ||
| 514 | if rows is not None and len(rows) > 0: |
|
| 515 | for row in rows: |
|
| 516 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 517 | timedelta(minutes=timezone_offset) |
|
| 518 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 519 | point_timestamps.append(current_datetime) |
|
| 520 | point_values.append(row[1]) |
|
| 521 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 522 | query = (" SELECT utc_date_time, actual_value " |
|
| 523 | " FROM tbl_digital_value " |
|
| 524 | " WHERE point_id = %s " |
|
| 525 | " AND utc_date_time BETWEEN %s AND %s ") |
|
| 526 | cursor_historical.execute(query, (point['id'], |
|
| 527 | reporting_start_datetime_utc, |
|
| 528 | reporting_end_datetime_utc)) |
|
| 529 | rows = cursor_historical.fetchall() |
|
| 530 | ||
| 531 | if rows is not None and len(rows) > 0: |
|
| 532 | for row in rows: |
|
| 533 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 534 | timedelta(minutes=timezone_offset) |
|
| 535 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 536 | point_timestamps.append(current_datetime) |
|
| 537 | point_values.append(row[1]) |
|
| 538 | ||
| 539 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 540 | parameters_data['timestamps'].append(point_timestamps) |
|
| 541 | parameters_data['values'].append(point_values) |
|
| 542 | ||
| 543 | ################################################################################################################ |
|
| 544 | # Step 11: construct the report |
|
| 545 | ################################################################################################################ |
|
| 546 | if cursor_system: |
|
| 547 | cursor_system.close() |
|
| 548 | if cnx_system: |
|
| 549 | cnx_system.disconnect() |
|
| 550 | ||
| 551 | if cursor_energy: |
|
| 552 | cursor_energy.close() |
|
| 553 | if cnx_energy: |
|
| 554 | cnx_energy.disconnect() |
|
| 555 | ||
| 556 | result = dict() |
|
| 557 | ||
| 558 | result['combined_equipment'] = dict() |
|
| 559 | result['combined_equipment']['name'] = combined_equipment['name'] |
|
| 560 | ||
| 561 | result['base_period_input'] = dict() |
|
| 562 | result['base_period_input']['names'] = list() |
|
| 563 | result['base_period_input']['units'] = list() |
|
| 564 | result['base_period_input']['timestamps'] = list() |
|
| 565 | result['base_period_input']['values'] = list() |
|
| 566 | result['base_period_input']['subtotals'] = list() |
|
| 567 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 568 | for energy_category_id in energy_category_set_input: |
|
| 569 | result['base_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 570 | result['base_period_input']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 571 | result['base_period_input']['timestamps'].append(base_input[energy_category_id]['timestamps']) |
|
| 572 | result['base_period_input']['values'].append(base_input[energy_category_id]['values']) |
|
| 573 | result['base_period_input']['subtotals'].append(base_input[energy_category_id]['subtotal']) |
|
| 574 | ||
| 575 | result['base_period_output'] = dict() |
|
| 576 | result['base_period_output']['names'] = list() |
|
| 577 | result['base_period_output']['units'] = list() |
|
| 578 | result['base_period_output']['timestamps'] = list() |
|
| 579 | result['base_period_output']['values'] = list() |
|
| 580 | result['base_period_output']['subtotals'] = list() |
|
| 581 | ||
| 582 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 583 | for energy_category_id in energy_category_set_output: |
|
| 584 | result['base_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 585 | result['base_period_output']['units'].append( |
|
| 586 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 587 | result['base_period_output']['timestamps'].append(base_output[energy_category_id]['timestamps']) |
|
| 588 | result['base_period_output']['values'].append(base_output[energy_category_id]['values']) |
|
| 589 | result['base_period_output']['subtotals'].append(base_output[energy_category_id]['subtotal']) |
|
| 590 | ||
| 591 | result['base_period_efficiency'] = dict() |
|
| 592 | result['base_period_efficiency']['names'] = list() |
|
| 593 | result['base_period_efficiency']['units'] = list() |
|
| 594 | result['base_period_efficiency']['timestamps'] = list() |
|
| 595 | result['base_period_efficiency']['values'] = list() |
|
| 596 | result['base_period_efficiency']['cumulations'] = list() |
|
| 597 | ||
| 598 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 599 | for energy_category_id_output in energy_category_set_output: |
|
| 600 | for energy_category_id_input in energy_category_set_input: |
|
| 601 | result['base_period_efficiency']['names'].append( |
|
| 602 | energy_category_dict[energy_category_id_output]['name'] + '/' + |
|
| 603 | energy_category_dict[energy_category_id_input]['name']) |
|
| 604 | result['base_period_efficiency']['units'].append( |
|
| 605 | energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
|
| 606 | energy_category_dict[energy_category_id_input]['unit_of_measure']) |
|
| 607 | result['base_period_efficiency']['timestamps'].append( |
|
| 608 | base_output[energy_category_id_output]['timestamps']) |
|
| 609 | efficiency_values = list() |
|
| 610 | for i in range(len(base_output[energy_category_id_output]['timestamps'])): |
|
| 611 | efficiency_values.append((base_output[energy_category_id_output]['values'][i] / |
|
| 612 | base_input[energy_category_id_input]['values'][i]) |
|
| 613 | if base_input[energy_category_id_input]['values'][i] > Decimal(0.0) |
|
| 614 | else None) |
|
| 615 | result['base_period_efficiency']['values'].append(efficiency_values) |
|
| 616 | ||
| 617 | base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
|
| 618 | base_input[energy_category_id_input]['subtotal']) if \ |
|
| 619 | base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 620 | result['base_period_efficiency']['cumulations'].append(base_cumulation) |
|
| 621 | ||
| 622 | result['reporting_period_input'] = dict() |
|
| 623 | result['reporting_period_input']['names'] = list() |
|
| 624 | result['reporting_period_input']['energy_category_ids'] = list() |
|
| 625 | result['reporting_period_input']['units'] = list() |
|
| 626 | result['reporting_period_input']['timestamps'] = list() |
|
| 627 | result['reporting_period_input']['values'] = list() |
|
| 628 | result['reporting_period_input']['subtotals'] = list() |
|
| 629 | result['reporting_period_input']['increment_rates'] = list() |
|
| 630 | ||
| 631 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 632 | for energy_category_id in energy_category_set_input: |
|
| 633 | result['reporting_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 634 | result['reporting_period_input']['energy_category_ids'].append(energy_category_id) |
|
| 635 | result['reporting_period_input']['units'].append( |
|
| 636 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 637 | result['reporting_period_input']['timestamps'].append( |
|
| 638 | reporting_input[energy_category_id]['timestamps']) |
|
| 639 | result['reporting_period_input']['values'].append( |
|
| 640 | reporting_input[energy_category_id]['values']) |
|
| 641 | result['reporting_period_input']['subtotals'].append( |
|
| 642 | reporting_input[energy_category_id]['subtotal']) |
|
| 643 | result['reporting_period_input']['increment_rates'].append( |
|
| 644 | (reporting_input[energy_category_id]['subtotal'] - |
|
| 645 | base_input[energy_category_id]['subtotal']) / |
|
| 646 | base_input[energy_category_id]['subtotal'] |
|
| 647 | if base_input[energy_category_id]['subtotal'] > 0.0 else None) |
|
| 648 | ||
| 649 | result['reporting_period_output'] = dict() |
|
| 650 | result['reporting_period_output']['names'] = list() |
|
| 651 | result['reporting_period_output']['energy_category_ids'] = list() |
|
| 652 | result['reporting_period_output']['units'] = list() |
|
| 653 | result['reporting_period_output']['timestamps'] = list() |
|
| 654 | result['reporting_period_output']['values'] = list() |
|
| 655 | result['reporting_period_output']['subtotals'] = list() |
|
| 656 | result['reporting_period_output']['increment_rates'] = list() |
|
| 657 | ||
| 658 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 659 | for energy_category_id in energy_category_set_output: |
|
| 660 | result['reporting_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 661 | result['reporting_period_output']['energy_category_ids'].append(energy_category_id) |
|
| 662 | result['reporting_period_output']['units'].append( |
|
| 663 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 664 | result['reporting_period_output']['timestamps'].append( |
|
| 665 | reporting_output[energy_category_id]['timestamps']) |
|
| 666 | result['reporting_period_output']['values'].append(reporting_output[energy_category_id]['values']) |
|
| 667 | result['reporting_period_output']['subtotals'].append(reporting_output[energy_category_id]['subtotal']) |
|
| 668 | result['reporting_period_output']['increment_rates'].append( |
|
| 669 | (reporting_output[energy_category_id]['subtotal'] - |
|
| 670 | base_output[energy_category_id]['subtotal']) / |
|
| 671 | base_output[energy_category_id]['subtotal'] |
|
| 672 | if base_output[energy_category_id]['subtotal'] > 0.0 else None) |
|
| 673 | ||
| 674 | result['reporting_period_efficiency'] = dict() |
|
| 675 | result['reporting_period_efficiency']['names'] = list() |
|
| 676 | result['reporting_period_efficiency']['units'] = list() |
|
| 677 | result['reporting_period_efficiency']['timestamps'] = list() |
|
| 678 | result['reporting_period_efficiency']['values'] = list() |
|
| 679 | result['reporting_period_efficiency']['cumulations'] = list() |
|
| 680 | result['reporting_period_efficiency']['increment_rates'] = list() |
|
| 681 | ||
| 682 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 683 | for energy_category_id_output in energy_category_set_output: |
|
| 684 | for energy_category_id_input in energy_category_set_input: |
|
| 685 | result['reporting_period_efficiency']['names'].append( |
|
| 686 | energy_category_dict[energy_category_id_output]['name'] + '/' + |
|
| 687 | energy_category_dict[energy_category_id_input]['name']) |
|
| 688 | result['reporting_period_efficiency']['units'].append( |
|
| 689 | energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
|
| 690 | energy_category_dict[energy_category_id_input]['unit_of_measure']) |
|
| 691 | result['reporting_period_efficiency']['timestamps'].append( |
|
| 692 | reporting_output[energy_category_id_output]['timestamps']) |
|
| 693 | efficiency_values = list() |
|
| 694 | for i in range(len(reporting_output[energy_category_id_output]['timestamps'])): |
|
| 695 | efficiency_values.append((reporting_output[energy_category_id_output]['values'][i] / |
|
| 696 | reporting_input[energy_category_id_input]['values'][i]) |
|
| 697 | if reporting_input[energy_category_id_input]['values'][i] > |
|
| 698 | Decimal(0.0) else None) |
|
| 699 | result['reporting_period_efficiency']['values'].append(efficiency_values) |
|
| 700 | ||
| 701 | base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
|
| 702 | base_input[energy_category_id_input]['subtotal']) if \ |
|
| 703 | base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 704 | ||
| 705 | reporting_cumulation = (reporting_output[energy_category_id_output]['subtotal'] / |
|
| 706 | reporting_input[energy_category_id_input]['subtotal']) if \ |
|
| 707 | reporting_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 708 | ||
| 709 | result['reporting_period_efficiency']['cumulations'].append(reporting_cumulation) |
|
| 710 | result['reporting_period_efficiency']['increment_rates'].append( |
|
| 711 | ((reporting_cumulation - base_cumulation) / base_cumulation if (base_cumulation > Decimal(0.0)) |
|
| 712 | else None) |
|
| 713 | ) |
|
| 714 | ||
| 715 | result['parameters'] = { |
|
| 716 | "names": parameters_data['names'], |
|
| 717 | "timestamps": parameters_data['timestamps'], |
|
| 718 | "values": parameters_data['values'] |
|
| 719 | } |
|
| 720 | ||
| 721 | resp.body = json.dumps(result) |
|
| 722 | ||
| @@ 10-711 (lines=702) @@ | ||
| 7 | from decimal import * |
|
| 8 | ||
| 9 | ||
| 10 | 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 equipment |
|
| 23 | # Step 3: query energy categories |
|
| 24 | # Step 4: query associated points |
|
| 25 | # Step 5: query base period energy input |
|
| 26 | # Step 6: query base period energy output |
|
| 27 | # Step 7: query reporting period energy input |
|
| 28 | # Step 8: query reporting period energy output |
|
| 29 | # Step 9: query tariff data |
|
| 30 | # Step 10: query associated sensors and points data |
|
| 31 | # Step 11: construct the report |
|
| 32 | #################################################################################################################### |
|
| 33 | @staticmethod |
|
| 34 | def on_get(req, resp): |
|
| 35 | print(req.params) |
|
| 36 | equipment_id = req.params.get('equipmentid') |
|
| 37 | period_type = req.params.get('periodtype') |
|
| 38 | base_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 39 | base_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 40 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 41 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 42 | ||
| 43 | ################################################################################################################ |
|
| 44 | # Step 1: valid parameters |
|
| 45 | ################################################################################################################ |
|
| 46 | if equipment_id is None: |
|
| 47 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
|
| 48 | else: |
|
| 49 | equipment_id = str.strip(equipment_id) |
|
| 50 | if not equipment_id.isdigit() or int(equipment_id) <= 0: |
|
| 51 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_EQUIPMENT_ID') |
|
| 52 | ||
| 53 | if period_type is None: |
|
| 54 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
|
| 55 | else: |
|
| 56 | period_type = str.strip(period_type) |
|
| 57 | if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: |
|
| 58 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') |
|
| 59 | ||
| 60 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 61 | if config.utc_offset[0] == '-': |
|
| 62 | timezone_offset = -timezone_offset |
|
| 63 | ||
| 64 | base_start_datetime_utc = None |
|
| 65 | if base_start_datetime_local is not None and len(str.strip(base_start_datetime_local)) > 0: |
|
| 66 | base_start_datetime_local = str.strip(base_start_datetime_local) |
|
| 67 | try: |
|
| 68 | base_start_datetime_utc = datetime.strptime(base_start_datetime_local, |
|
| 69 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 70 | timedelta(minutes=timezone_offset) |
|
| 71 | except ValueError: |
|
| 72 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 73 | description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") |
|
| 74 | ||
| 75 | base_end_datetime_utc = None |
|
| 76 | if base_end_datetime_local is not None and len(str.strip(base_end_datetime_local)) > 0: |
|
| 77 | base_end_datetime_local = str.strip(base_end_datetime_local) |
|
| 78 | try: |
|
| 79 | base_end_datetime_utc = datetime.strptime(base_end_datetime_local, |
|
| 80 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 81 | timedelta(minutes=timezone_offset) |
|
| 82 | except ValueError: |
|
| 83 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 84 | description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") |
|
| 85 | ||
| 86 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 87 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 88 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 89 | description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') |
|
| 90 | ||
| 91 | if reporting_start_datetime_local is None: |
|
| 92 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 93 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
|
| 94 | else: |
|
| 95 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
|
| 96 | try: |
|
| 97 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
|
| 98 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 99 | timedelta(minutes=timezone_offset) |
|
| 100 | except ValueError: |
|
| 101 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 102 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
|
| 103 | ||
| 104 | if reporting_end_datetime_local is None: |
|
| 105 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 106 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
|
| 107 | else: |
|
| 108 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
|
| 109 | try: |
|
| 110 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
|
| 111 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 112 | timedelta(minutes=timezone_offset) |
|
| 113 | except ValueError: |
|
| 114 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 115 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
|
| 116 | ||
| 117 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 118 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 119 | description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
|
| 120 | ||
| 121 | ################################################################################################################ |
|
| 122 | # Step 2: query the equipment |
|
| 123 | ################################################################################################################ |
|
| 124 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 125 | cursor_system = cnx_system.cursor() |
|
| 126 | ||
| 127 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 128 | cursor_energy = cnx_energy.cursor() |
|
| 129 | ||
| 130 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 131 | cursor_historical = cnx_historical.cursor() |
|
| 132 | ||
| 133 | cursor_system.execute(" SELECT id, name, cost_center_id " |
|
| 134 | " FROM tbl_equipments " |
|
| 135 | " WHERE id = %s ", (equipment_id,)) |
|
| 136 | row_equipment = cursor_system.fetchone() |
|
| 137 | if row_equipment is None: |
|
| 138 | if cursor_system: |
|
| 139 | cursor_system.close() |
|
| 140 | if cnx_system: |
|
| 141 | cnx_system.disconnect() |
|
| 142 | ||
| 143 | if cursor_energy: |
|
| 144 | cursor_energy.close() |
|
| 145 | if cnx_energy: |
|
| 146 | cnx_energy.disconnect() |
|
| 147 | ||
| 148 | if cnx_historical: |
|
| 149 | cnx_historical.close() |
|
| 150 | if cursor_historical: |
|
| 151 | cursor_historical.disconnect() |
|
| 152 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.EQUIPMENT_NOT_FOUND') |
|
| 153 | ||
| 154 | equipment = dict() |
|
| 155 | equipment['id'] = row_equipment[0] |
|
| 156 | equipment['name'] = row_equipment[1] |
|
| 157 | equipment['cost_center_id'] = row_equipment[2] |
|
| 158 | ||
| 159 | ################################################################################################################ |
|
| 160 | # Step 3: query input energy categories and output energy categories |
|
| 161 | ################################################################################################################ |
|
| 162 | energy_category_set_input = set() |
|
| 163 | energy_category_set_output = set() |
|
| 164 | # query input energy categories in base period |
|
| 165 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 166 | " FROM tbl_equipment_input_category_hourly " |
|
| 167 | " WHERE equipment_id = %s " |
|
| 168 | " AND start_datetime_utc >= %s " |
|
| 169 | " AND start_datetime_utc < %s ", |
|
| 170 | (equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 171 | rows_energy_categories = cursor_energy.fetchall() |
|
| 172 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 173 | for row_energy_category in rows_energy_categories: |
|
| 174 | energy_category_set_input.add(row_energy_category[0]) |
|
| 175 | ||
| 176 | # query input energy categories in reporting period |
|
| 177 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 178 | " FROM tbl_equipment_input_category_hourly " |
|
| 179 | " WHERE equipment_id = %s " |
|
| 180 | " AND start_datetime_utc >= %s " |
|
| 181 | " AND start_datetime_utc < %s ", |
|
| 182 | (equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 183 | rows_energy_categories = cursor_energy.fetchall() |
|
| 184 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 185 | for row_energy_category in rows_energy_categories: |
|
| 186 | energy_category_set_input.add(row_energy_category[0]) |
|
| 187 | ||
| 188 | # query output energy categories in base period |
|
| 189 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 190 | " FROM tbl_equipment_output_category_hourly " |
|
| 191 | " WHERE equipment_id = %s " |
|
| 192 | " AND start_datetime_utc >= %s " |
|
| 193 | " AND start_datetime_utc < %s ", |
|
| 194 | (equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 195 | rows_energy_categories = cursor_energy.fetchall() |
|
| 196 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 197 | for row_energy_category in rows_energy_categories: |
|
| 198 | energy_category_set_output.add(row_energy_category[0]) |
|
| 199 | ||
| 200 | # query output energy categories in reporting period |
|
| 201 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 202 | " FROM tbl_equipment_output_category_hourly " |
|
| 203 | " WHERE equipment_id = %s " |
|
| 204 | " AND start_datetime_utc >= %s " |
|
| 205 | " AND start_datetime_utc < %s ", |
|
| 206 | (equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 207 | rows_energy_categories = cursor_energy.fetchall() |
|
| 208 | if rows_energy_categories is not None or len(rows_energy_categories) > 0: |
|
| 209 | for row_energy_category in rows_energy_categories: |
|
| 210 | energy_category_set_output.add(row_energy_category[0]) |
|
| 211 | ||
| 212 | # query properties of all energy categories above |
|
| 213 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 214 | " FROM tbl_energy_categories " |
|
| 215 | " ORDER BY id ", ) |
|
| 216 | rows_energy_categories = cursor_system.fetchall() |
|
| 217 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 218 | if cursor_system: |
|
| 219 | cursor_system.close() |
|
| 220 | if cnx_system: |
|
| 221 | cnx_system.disconnect() |
|
| 222 | ||
| 223 | if cursor_energy: |
|
| 224 | cursor_energy.close() |
|
| 225 | if cnx_energy: |
|
| 226 | cnx_energy.disconnect() |
|
| 227 | ||
| 228 | if cnx_historical: |
|
| 229 | cnx_historical.close() |
|
| 230 | if cursor_historical: |
|
| 231 | cursor_historical.disconnect() |
|
| 232 | raise falcon.HTTPError(falcon.HTTP_404, |
|
| 233 | title='API.NOT_FOUND', |
|
| 234 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 235 | energy_category_dict = dict() |
|
| 236 | for row_energy_category in rows_energy_categories: |
|
| 237 | if row_energy_category[0] in energy_category_set_input or \ |
|
| 238 | row_energy_category[0] in energy_category_set_output: |
|
| 239 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 240 | "unit_of_measure": row_energy_category[2], |
|
| 241 | "kgce": row_energy_category[3], |
|
| 242 | "kgco2e": row_energy_category[4]} |
|
| 243 | ||
| 244 | ################################################################################################################ |
|
| 245 | # Step 4: query associated points |
|
| 246 | ################################################################################################################ |
|
| 247 | point_list = list() |
|
| 248 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 249 | " FROM tbl_equipments e, tbl_equipments_parameters ep, tbl_points p " |
|
| 250 | " WHERE e.id = %s AND e.id = ep.equipment_id AND ep.parameter_type = 'point' " |
|
| 251 | " AND ep.point_id = p.id " |
|
| 252 | " ORDER BY p.id ", (equipment['id'],)) |
|
| 253 | rows_points = cursor_system.fetchall() |
|
| 254 | if rows_points is not None and len(rows_points) > 0: |
|
| 255 | for row in rows_points: |
|
| 256 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 257 | ||
| 258 | ################################################################################################################ |
|
| 259 | # Step 5: query base period energy input |
|
| 260 | ################################################################################################################ |
|
| 261 | base_input = dict() |
|
| 262 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 263 | for energy_category_id in energy_category_set_input: |
|
| 264 | base_input[energy_category_id] = dict() |
|
| 265 | base_input[energy_category_id]['timestamps'] = list() |
|
| 266 | base_input[energy_category_id]['values'] = list() |
|
| 267 | base_input[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 268 | ||
| 269 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 270 | " FROM tbl_equipment_input_category_hourly " |
|
| 271 | " WHERE equipment_id = %s " |
|
| 272 | " AND energy_category_id = %s " |
|
| 273 | " AND start_datetime_utc >= %s " |
|
| 274 | " AND start_datetime_utc < %s " |
|
| 275 | " ORDER BY start_datetime_utc ", |
|
| 276 | (equipment['id'], |
|
| 277 | energy_category_id, |
|
| 278 | base_start_datetime_utc, |
|
| 279 | base_end_datetime_utc)) |
|
| 280 | rows_equipment_hourly = cursor_energy.fetchall() |
|
| 281 | ||
| 282 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
|
| 283 | base_start_datetime_utc, |
|
| 284 | base_end_datetime_utc, |
|
| 285 | period_type) |
|
| 286 | for row_equipment_periodically in rows_equipment_periodically: |
|
| 287 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 288 | timedelta(minutes=timezone_offset) |
|
| 289 | if period_type == 'hourly': |
|
| 290 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 291 | elif period_type == 'daily': |
|
| 292 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 293 | elif period_type == 'monthly': |
|
| 294 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 295 | elif period_type == 'yearly': |
|
| 296 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 297 | ||
| 298 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
|
| 299 | else row_equipment_periodically[1] |
|
| 300 | base_input[energy_category_id]['timestamps'].append(current_datetime) |
|
| 301 | base_input[energy_category_id]['values'].append(actual_value) |
|
| 302 | base_input[energy_category_id]['subtotal'] += actual_value |
|
| 303 | ||
| 304 | ################################################################################################################ |
|
| 305 | # Step 6: query base period energy output |
|
| 306 | ################################################################################################################ |
|
| 307 | base_output = dict() |
|
| 308 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 309 | for energy_category_id in energy_category_set_output: |
|
| 310 | base_output[energy_category_id] = dict() |
|
| 311 | base_output[energy_category_id]['timestamps'] = list() |
|
| 312 | base_output[energy_category_id]['values'] = list() |
|
| 313 | base_output[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 314 | ||
| 315 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 316 | " FROM tbl_equipment_output_category_hourly " |
|
| 317 | " WHERE equipment_id = %s " |
|
| 318 | " AND energy_category_id = %s " |
|
| 319 | " AND start_datetime_utc >= %s " |
|
| 320 | " AND start_datetime_utc < %s " |
|
| 321 | " ORDER BY start_datetime_utc ", |
|
| 322 | (equipment['id'], |
|
| 323 | energy_category_id, |
|
| 324 | base_start_datetime_utc, |
|
| 325 | base_end_datetime_utc)) |
|
| 326 | rows_equipment_hourly = cursor_energy.fetchall() |
|
| 327 | ||
| 328 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
|
| 329 | base_start_datetime_utc, |
|
| 330 | base_end_datetime_utc, |
|
| 331 | period_type) |
|
| 332 | for row_equipment_periodically in rows_equipment_periodically: |
|
| 333 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 334 | timedelta(minutes=timezone_offset) |
|
| 335 | if period_type == 'hourly': |
|
| 336 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 337 | elif period_type == 'daily': |
|
| 338 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 339 | elif period_type == 'monthly': |
|
| 340 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 341 | elif period_type == 'yearly': |
|
| 342 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 343 | ||
| 344 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
|
| 345 | else row_equipment_periodically[1] |
|
| 346 | base_output[energy_category_id]['timestamps'].append(current_datetime) |
|
| 347 | base_output[energy_category_id]['values'].append(actual_value) |
|
| 348 | base_output[energy_category_id]['subtotal'] += actual_value |
|
| 349 | ################################################################################################################ |
|
| 350 | # Step 7: query reporting period energy input |
|
| 351 | ################################################################################################################ |
|
| 352 | reporting_input = dict() |
|
| 353 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 354 | for energy_category_id in energy_category_set_input: |
|
| 355 | ||
| 356 | reporting_input[energy_category_id] = dict() |
|
| 357 | reporting_input[energy_category_id]['timestamps'] = list() |
|
| 358 | reporting_input[energy_category_id]['values'] = list() |
|
| 359 | reporting_input[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 360 | ||
| 361 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 362 | " FROM tbl_equipment_input_category_hourly " |
|
| 363 | " WHERE equipment_id = %s " |
|
| 364 | " AND energy_category_id = %s " |
|
| 365 | " AND start_datetime_utc >= %s " |
|
| 366 | " AND start_datetime_utc < %s " |
|
| 367 | " ORDER BY start_datetime_utc ", |
|
| 368 | (equipment['id'], |
|
| 369 | energy_category_id, |
|
| 370 | reporting_start_datetime_utc, |
|
| 371 | reporting_end_datetime_utc)) |
|
| 372 | rows_equipment_hourly = cursor_energy.fetchall() |
|
| 373 | ||
| 374 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
|
| 375 | reporting_start_datetime_utc, |
|
| 376 | reporting_end_datetime_utc, |
|
| 377 | period_type) |
|
| 378 | for row_equipment_periodically in rows_equipment_periodically: |
|
| 379 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 380 | timedelta(minutes=timezone_offset) |
|
| 381 | if period_type == 'hourly': |
|
| 382 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 383 | elif period_type == 'daily': |
|
| 384 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 385 | elif period_type == 'monthly': |
|
| 386 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 387 | elif period_type == 'yearly': |
|
| 388 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 389 | ||
| 390 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
|
| 391 | else row_equipment_periodically[1] |
|
| 392 | reporting_input[energy_category_id]['timestamps'].append(current_datetime) |
|
| 393 | reporting_input[energy_category_id]['values'].append(actual_value) |
|
| 394 | reporting_input[energy_category_id]['subtotal'] += actual_value |
|
| 395 | ||
| 396 | ################################################################################################################ |
|
| 397 | # Step 8: query reporting period energy output |
|
| 398 | ################################################################################################################ |
|
| 399 | reporting_output = dict() |
|
| 400 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 401 | for energy_category_id in energy_category_set_output: |
|
| 402 | ||
| 403 | reporting_output[energy_category_id] = dict() |
|
| 404 | reporting_output[energy_category_id]['timestamps'] = list() |
|
| 405 | reporting_output[energy_category_id]['values'] = list() |
|
| 406 | reporting_output[energy_category_id]['subtotal'] = Decimal(0.0) |
|
| 407 | ||
| 408 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 409 | " FROM tbl_equipment_output_category_hourly " |
|
| 410 | " WHERE equipment_id = %s " |
|
| 411 | " AND energy_category_id = %s " |
|
| 412 | " AND start_datetime_utc >= %s " |
|
| 413 | " AND start_datetime_utc < %s " |
|
| 414 | " ORDER BY start_datetime_utc ", |
|
| 415 | (equipment['id'], |
|
| 416 | energy_category_id, |
|
| 417 | reporting_start_datetime_utc, |
|
| 418 | reporting_end_datetime_utc)) |
|
| 419 | rows_equipment_hourly = cursor_energy.fetchall() |
|
| 420 | ||
| 421 | rows_equipment_periodically = utilities.aggregate_hourly_data_by_period(rows_equipment_hourly, |
|
| 422 | reporting_start_datetime_utc, |
|
| 423 | reporting_end_datetime_utc, |
|
| 424 | period_type) |
|
| 425 | for row_equipment_periodically in rows_equipment_periodically: |
|
| 426 | current_datetime_local = row_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 427 | timedelta(minutes=timezone_offset) |
|
| 428 | if period_type == 'hourly': |
|
| 429 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 430 | elif period_type == 'daily': |
|
| 431 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 432 | elif period_type == 'monthly': |
|
| 433 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 434 | elif period_type == 'yearly': |
|
| 435 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 436 | ||
| 437 | actual_value = Decimal(0.0) if row_equipment_periodically[1] is None \ |
|
| 438 | else row_equipment_periodically[1] |
|
| 439 | reporting_output[energy_category_id]['timestamps'].append(current_datetime) |
|
| 440 | reporting_output[energy_category_id]['values'].append(actual_value) |
|
| 441 | reporting_output[energy_category_id]['subtotal'] += actual_value |
|
| 442 | ||
| 443 | ################################################################################################################ |
|
| 444 | # Step 9: query tariff data |
|
| 445 | ################################################################################################################ |
|
| 446 | parameters_data = dict() |
|
| 447 | parameters_data['names'] = list() |
|
| 448 | parameters_data['timestamps'] = list() |
|
| 449 | parameters_data['values'] = list() |
|
| 450 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 451 | for energy_category_id in energy_category_set_input: |
|
| 452 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(equipment['cost_center_id'], |
|
| 453 | energy_category_id, |
|
| 454 | reporting_start_datetime_utc, |
|
| 455 | reporting_end_datetime_utc) |
|
| 456 | tariff_timestamp_list = list() |
|
| 457 | tariff_value_list = list() |
|
| 458 | for k, v in energy_category_tariff_dict.items(): |
|
| 459 | # convert k from utc to local |
|
| 460 | k = k + timedelta(minutes=timezone_offset) |
|
| 461 | tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) |
|
| 462 | tariff_value_list.append(v) |
|
| 463 | ||
| 464 | parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) |
|
| 465 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 466 | parameters_data['values'].append(tariff_value_list) |
|
| 467 | ||
| 468 | ################################################################################################################ |
|
| 469 | # Step 10: query associated sensors and points data |
|
| 470 | ################################################################################################################ |
|
| 471 | for point in point_list: |
|
| 472 | point_values = [] |
|
| 473 | point_timestamps = [] |
|
| 474 | if point['object_type'] == 'ANALOG_VALUE': |
|
| 475 | query = (" SELECT utc_date_time, actual_value " |
|
| 476 | " FROM tbl_analog_value " |
|
| 477 | " WHERE point_id = %s " |
|
| 478 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 479 | " ORDER BY utc_date_time ") |
|
| 480 | cursor_historical.execute(query, (point['id'], |
|
| 481 | reporting_start_datetime_utc, |
|
| 482 | reporting_end_datetime_utc)) |
|
| 483 | rows = cursor_historical.fetchall() |
|
| 484 | ||
| 485 | if rows is not None and len(rows) > 0: |
|
| 486 | for row in rows: |
|
| 487 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 488 | timedelta(minutes=timezone_offset) |
|
| 489 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 490 | point_timestamps.append(current_datetime) |
|
| 491 | point_values.append(row[1]) |
|
| 492 | ||
| 493 | elif point['object_type'] == 'ENERGY_VALUE': |
|
| 494 | query = (" SELECT utc_date_time, actual_value " |
|
| 495 | " FROM tbl_energy_value " |
|
| 496 | " WHERE point_id = %s " |
|
| 497 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 498 | " ORDER BY utc_date_time ") |
|
| 499 | cursor_historical.execute(query, (point['id'], |
|
| 500 | reporting_start_datetime_utc, |
|
| 501 | reporting_end_datetime_utc)) |
|
| 502 | rows = cursor_historical.fetchall() |
|
| 503 | ||
| 504 | if rows is not None and len(rows) > 0: |
|
| 505 | for row in rows: |
|
| 506 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 507 | timedelta(minutes=timezone_offset) |
|
| 508 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 509 | point_timestamps.append(current_datetime) |
|
| 510 | point_values.append(row[1]) |
|
| 511 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 512 | query = (" SELECT utc_date_time, actual_value " |
|
| 513 | " FROM tbl_digital_value " |
|
| 514 | " WHERE point_id = %s " |
|
| 515 | " AND utc_date_time BETWEEN %s AND %s ") |
|
| 516 | cursor_historical.execute(query, (point['id'], |
|
| 517 | reporting_start_datetime_utc, |
|
| 518 | reporting_end_datetime_utc)) |
|
| 519 | rows = cursor_historical.fetchall() |
|
| 520 | ||
| 521 | if rows is not None and len(rows) > 0: |
|
| 522 | for row in rows: |
|
| 523 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 524 | timedelta(minutes=timezone_offset) |
|
| 525 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 526 | point_timestamps.append(current_datetime) |
|
| 527 | point_values.append(row[1]) |
|
| 528 | ||
| 529 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 530 | parameters_data['timestamps'].append(point_timestamps) |
|
| 531 | parameters_data['values'].append(point_values) |
|
| 532 | ||
| 533 | ################################################################################################################ |
|
| 534 | # Step 11: construct the report |
|
| 535 | ################################################################################################################ |
|
| 536 | if cursor_system: |
|
| 537 | cursor_system.close() |
|
| 538 | if cnx_system: |
|
| 539 | cnx_system.disconnect() |
|
| 540 | ||
| 541 | if cursor_energy: |
|
| 542 | cursor_energy.close() |
|
| 543 | if cnx_energy: |
|
| 544 | cnx_energy.disconnect() |
|
| 545 | ||
| 546 | result = dict() |
|
| 547 | ||
| 548 | result['equipment'] = dict() |
|
| 549 | result['equipment']['name'] = equipment['name'] |
|
| 550 | ||
| 551 | result['base_period_input'] = dict() |
|
| 552 | result['base_period_input']['names'] = list() |
|
| 553 | result['base_period_input']['units'] = list() |
|
| 554 | result['base_period_input']['timestamps'] = list() |
|
| 555 | result['base_period_input']['values'] = list() |
|
| 556 | result['base_period_input']['subtotals'] = list() |
|
| 557 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 558 | for energy_category_id in energy_category_set_input: |
|
| 559 | result['base_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 560 | result['base_period_input']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 561 | result['base_period_input']['timestamps'].append(base_input[energy_category_id]['timestamps']) |
|
| 562 | result['base_period_input']['values'].append(base_input[energy_category_id]['values']) |
|
| 563 | result['base_period_input']['subtotals'].append(base_input[energy_category_id]['subtotal']) |
|
| 564 | ||
| 565 | result['base_period_output'] = dict() |
|
| 566 | result['base_period_output']['names'] = list() |
|
| 567 | result['base_period_output']['units'] = list() |
|
| 568 | result['base_period_output']['timestamps'] = list() |
|
| 569 | result['base_period_output']['values'] = list() |
|
| 570 | result['base_period_output']['subtotals'] = list() |
|
| 571 | ||
| 572 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 573 | for energy_category_id in energy_category_set_output: |
|
| 574 | result['base_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 575 | result['base_period_output']['units'].append( |
|
| 576 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 577 | result['base_period_output']['timestamps'].append(base_output[energy_category_id]['timestamps']) |
|
| 578 | result['base_period_output']['values'].append(base_output[energy_category_id]['values']) |
|
| 579 | result['base_period_output']['subtotals'].append(base_output[energy_category_id]['subtotal']) |
|
| 580 | ||
| 581 | result['base_period_efficiency'] = dict() |
|
| 582 | result['base_period_efficiency']['names'] = list() |
|
| 583 | result['base_period_efficiency']['units'] = list() |
|
| 584 | result['base_period_efficiency']['timestamps'] = list() |
|
| 585 | result['base_period_efficiency']['values'] = list() |
|
| 586 | result['base_period_efficiency']['cumulations'] = list() |
|
| 587 | ||
| 588 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 589 | for energy_category_id_output in energy_category_set_output: |
|
| 590 | for energy_category_id_input in energy_category_set_input: |
|
| 591 | result['base_period_efficiency']['names'].append( |
|
| 592 | energy_category_dict[energy_category_id_output]['name'] + '/' + |
|
| 593 | energy_category_dict[energy_category_id_input]['name']) |
|
| 594 | result['base_period_efficiency']['units'].append( |
|
| 595 | energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
|
| 596 | energy_category_dict[energy_category_id_input]['unit_of_measure']) |
|
| 597 | result['base_period_efficiency']['timestamps'].append( |
|
| 598 | base_output[energy_category_id_output]['timestamps']) |
|
| 599 | efficiency_values = list() |
|
| 600 | for i in range(len(base_output[energy_category_id_output]['timestamps'])): |
|
| 601 | efficiency_values.append((base_output[energy_category_id_output]['values'][i] / |
|
| 602 | base_input[energy_category_id_input]['values'][i]) |
|
| 603 | if base_input[energy_category_id_input]['values'][i] > Decimal(0.0) |
|
| 604 | else None) |
|
| 605 | result['base_period_efficiency']['values'].append(efficiency_values) |
|
| 606 | ||
| 607 | base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
|
| 608 | base_input[energy_category_id_input]['subtotal']) if \ |
|
| 609 | base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 610 | result['base_period_efficiency']['cumulations'].append(base_cumulation) |
|
| 611 | ||
| 612 | result['reporting_period_input'] = dict() |
|
| 613 | result['reporting_period_input']['names'] = list() |
|
| 614 | result['reporting_period_input']['energy_category_ids'] = list() |
|
| 615 | result['reporting_period_input']['units'] = list() |
|
| 616 | result['reporting_period_input']['timestamps'] = list() |
|
| 617 | result['reporting_period_input']['values'] = list() |
|
| 618 | result['reporting_period_input']['subtotals'] = list() |
|
| 619 | result['reporting_period_input']['increment_rates'] = list() |
|
| 620 | ||
| 621 | if energy_category_set_input is not None and len(energy_category_set_input) > 0: |
|
| 622 | for energy_category_id in energy_category_set_input: |
|
| 623 | result['reporting_period_input']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 624 | result['reporting_period_input']['energy_category_ids'].append(energy_category_id) |
|
| 625 | result['reporting_period_input']['units'].append( |
|
| 626 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 627 | result['reporting_period_input']['timestamps'].append( |
|
| 628 | reporting_input[energy_category_id]['timestamps']) |
|
| 629 | result['reporting_period_input']['values'].append( |
|
| 630 | reporting_input[energy_category_id]['values']) |
|
| 631 | result['reporting_period_input']['subtotals'].append( |
|
| 632 | reporting_input[energy_category_id]['subtotal']) |
|
| 633 | result['reporting_period_input']['increment_rates'].append( |
|
| 634 | (reporting_input[energy_category_id]['subtotal'] - |
|
| 635 | base_input[energy_category_id]['subtotal']) / |
|
| 636 | base_input[energy_category_id]['subtotal'] |
|
| 637 | if base_input[energy_category_id]['subtotal'] > 0.0 else None) |
|
| 638 | ||
| 639 | result['reporting_period_output'] = dict() |
|
| 640 | result['reporting_period_output']['names'] = list() |
|
| 641 | result['reporting_period_output']['energy_category_ids'] = list() |
|
| 642 | result['reporting_period_output']['units'] = list() |
|
| 643 | result['reporting_period_output']['timestamps'] = list() |
|
| 644 | result['reporting_period_output']['values'] = list() |
|
| 645 | result['reporting_period_output']['subtotals'] = list() |
|
| 646 | result['reporting_period_output']['increment_rates'] = list() |
|
| 647 | ||
| 648 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 649 | for energy_category_id in energy_category_set_output: |
|
| 650 | result['reporting_period_output']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 651 | result['reporting_period_output']['energy_category_ids'].append(energy_category_id) |
|
| 652 | result['reporting_period_output']['units'].append( |
|
| 653 | energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 654 | result['reporting_period_output']['timestamps'].append( |
|
| 655 | reporting_output[energy_category_id]['timestamps']) |
|
| 656 | result['reporting_period_output']['values'].append(reporting_output[energy_category_id]['values']) |
|
| 657 | result['reporting_period_output']['subtotals'].append(reporting_output[energy_category_id]['subtotal']) |
|
| 658 | result['reporting_period_output']['increment_rates'].append( |
|
| 659 | (reporting_output[energy_category_id]['subtotal'] - |
|
| 660 | base_output[energy_category_id]['subtotal']) / |
|
| 661 | base_output[energy_category_id]['subtotal'] |
|
| 662 | if base_output[energy_category_id]['subtotal'] > 0.0 else None) |
|
| 663 | ||
| 664 | result['reporting_period_efficiency'] = dict() |
|
| 665 | result['reporting_period_efficiency']['names'] = list() |
|
| 666 | result['reporting_period_efficiency']['units'] = list() |
|
| 667 | result['reporting_period_efficiency']['timestamps'] = list() |
|
| 668 | result['reporting_period_efficiency']['values'] = list() |
|
| 669 | result['reporting_period_efficiency']['cumulations'] = list() |
|
| 670 | result['reporting_period_efficiency']['increment_rates'] = list() |
|
| 671 | ||
| 672 | if energy_category_set_output is not None and len(energy_category_set_output) > 0: |
|
| 673 | for energy_category_id_output in energy_category_set_output: |
|
| 674 | for energy_category_id_input in energy_category_set_input: |
|
| 675 | result['reporting_period_efficiency']['names'].append( |
|
| 676 | energy_category_dict[energy_category_id_output]['name'] + '/' + |
|
| 677 | energy_category_dict[energy_category_id_input]['name']) |
|
| 678 | result['reporting_period_efficiency']['units'].append( |
|
| 679 | energy_category_dict[energy_category_id_output]['unit_of_measure'] + '/' + |
|
| 680 | energy_category_dict[energy_category_id_input]['unit_of_measure']) |
|
| 681 | result['reporting_period_efficiency']['timestamps'].append( |
|
| 682 | reporting_output[energy_category_id_output]['timestamps']) |
|
| 683 | efficiency_values = list() |
|
| 684 | for i in range(len(reporting_output[energy_category_id_output]['timestamps'])): |
|
| 685 | efficiency_values.append((reporting_output[energy_category_id_output]['values'][i] / |
|
| 686 | reporting_input[energy_category_id_input]['values'][i]) |
|
| 687 | if reporting_input[energy_category_id_input]['values'][i] > |
|
| 688 | Decimal(0.0) else None) |
|
| 689 | result['reporting_period_efficiency']['values'].append(efficiency_values) |
|
| 690 | ||
| 691 | base_cumulation = (base_output[energy_category_id_output]['subtotal'] / |
|
| 692 | base_input[energy_category_id_input]['subtotal']) if \ |
|
| 693 | base_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 694 | ||
| 695 | reporting_cumulation = (reporting_output[energy_category_id_output]['subtotal'] / |
|
| 696 | reporting_input[energy_category_id_input]['subtotal']) if \ |
|
| 697 | reporting_input[energy_category_id_input]['subtotal'] > Decimal(0.0) else None |
|
| 698 | ||
| 699 | result['reporting_period_efficiency']['cumulations'].append(reporting_cumulation) |
|
| 700 | result['reporting_period_efficiency']['increment_rates'].append( |
|
| 701 | ((reporting_cumulation - base_cumulation) / base_cumulation if (base_cumulation > Decimal(0.0)) |
|
| 702 | else None) |
|
| 703 | ) |
|
| 704 | ||
| 705 | result['parameters'] = { |
|
| 706 | "names": parameters_data['names'], |
|
| 707 | "timestamps": parameters_data['timestamps'], |
|
| 708 | "values": parameters_data['values'] |
|
| 709 | } |
|
| 710 | ||
| 711 | resp.body = json.dumps(result) |
|
| 712 | ||