| @@ 388-494 (lines=107) @@ | ||
| 385 | # Step 8: query reporting period energy saving |
|
| 386 | ################################################################################################################ |
|
| 387 | reporting = dict() |
|
| 388 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 389 | for energy_category_id in energy_category_set: |
|
| 390 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 391 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 392 | ||
| 393 | reporting[energy_category_id] = dict() |
|
| 394 | reporting[energy_category_id]['timestamps'] = list() |
|
| 395 | reporting[energy_category_id]['values_baseline'] = list() |
|
| 396 | reporting[energy_category_id]['values_actual'] = list() |
|
| 397 | reporting[energy_category_id]['values_saving'] = list() |
|
| 398 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 399 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 400 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 401 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 402 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 403 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 404 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 405 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 406 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 407 | # query reporting period's energy baseline |
|
| 408 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 409 | " FROM tbl_space_input_category_hourly " |
|
| 410 | " WHERE space_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 | (space['id'], |
|
| 416 | energy_category_id, |
|
| 417 | reporting_start_datetime_utc, |
|
| 418 | reporting_end_datetime_utc)) |
|
| 419 | rows_space_hourly = cursor_energy_baseline.fetchall() |
|
| 420 | ||
| 421 | rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
|
| 422 | reporting_start_datetime_utc, |
|
| 423 | reporting_end_datetime_utc, |
|
| 424 | period_type) |
|
| 425 | for row_space_periodically in rows_space_periodically: |
|
| 426 | current_datetime_local = row_space_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 | baseline_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
|
| 438 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 439 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 440 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 441 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 442 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 443 | ||
| 444 | # query reporting period's energy actual |
|
| 445 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 446 | " FROM tbl_space_input_category_hourly " |
|
| 447 | " WHERE space_id = %s " |
|
| 448 | " AND energy_category_id = %s " |
|
| 449 | " AND start_datetime_utc >= %s " |
|
| 450 | " AND start_datetime_utc < %s " |
|
| 451 | " ORDER BY start_datetime_utc ", |
|
| 452 | (space['id'], |
|
| 453 | energy_category_id, |
|
| 454 | reporting_start_datetime_utc, |
|
| 455 | reporting_end_datetime_utc)) |
|
| 456 | rows_space_hourly = cursor_energy.fetchall() |
|
| 457 | ||
| 458 | rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
|
| 459 | reporting_start_datetime_utc, |
|
| 460 | reporting_end_datetime_utc, |
|
| 461 | period_type) |
|
| 462 | for row_space_periodically in rows_space_periodically: |
|
| 463 | current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 464 | timedelta(minutes=timezone_offset) |
|
| 465 | if period_type == 'hourly': |
|
| 466 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 467 | elif period_type == 'daily': |
|
| 468 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 469 | elif period_type == 'monthly': |
|
| 470 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 471 | elif period_type == 'yearly': |
|
| 472 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 473 | ||
| 474 | actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
|
| 475 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 476 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 477 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 478 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 479 | ||
| 480 | # calculate reporting period's energy savings |
|
| 481 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
|
| 482 | reporting[energy_category_id]['values_saving'].append( |
|
| 483 | reporting[energy_category_id]['values_baseline'][i] - |
|
| 484 | reporting[energy_category_id]['values_actual'][i]) |
|
| 485 | ||
| 486 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 487 | reporting[energy_category_id]['subtotal_baseline'] - \ |
|
| 488 | reporting[energy_category_id]['subtotal_actual'] |
|
| 489 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 490 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 491 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 492 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 493 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 494 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 495 | ################################################################################################################ |
|
| 496 | # Step 9: query tariff data |
|
| 497 | ################################################################################################################ |
|
| @@ 277-383 (lines=107) @@ | ||
| 274 | # Step 7: query base period energy saving |
|
| 275 | ################################################################################################################ |
|
| 276 | base = dict() |
|
| 277 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 278 | for energy_category_id in energy_category_set: |
|
| 279 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 280 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 281 | ||
| 282 | base[energy_category_id] = dict() |
|
| 283 | base[energy_category_id]['timestamps'] = list() |
|
| 284 | base[energy_category_id]['values_baseline'] = list() |
|
| 285 | base[energy_category_id]['values_actual'] = list() |
|
| 286 | base[energy_category_id]['values_saving'] = list() |
|
| 287 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 288 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 289 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 290 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 291 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 292 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 293 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 294 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 295 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 296 | # query base period's energy baseline |
|
| 297 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 298 | " FROM tbl_space_input_category_hourly " |
|
| 299 | " WHERE space_id = %s " |
|
| 300 | " AND energy_category_id = %s " |
|
| 301 | " AND start_datetime_utc >= %s " |
|
| 302 | " AND start_datetime_utc < %s " |
|
| 303 | " ORDER BY start_datetime_utc ", |
|
| 304 | (space['id'], |
|
| 305 | energy_category_id, |
|
| 306 | base_start_datetime_utc, |
|
| 307 | base_end_datetime_utc)) |
|
| 308 | rows_space_hourly = cursor_energy_baseline.fetchall() |
|
| 309 | ||
| 310 | rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
|
| 311 | base_start_datetime_utc, |
|
| 312 | base_end_datetime_utc, |
|
| 313 | period_type) |
|
| 314 | for row_space_periodically in rows_space_periodically: |
|
| 315 | current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 316 | timedelta(minutes=timezone_offset) |
|
| 317 | if period_type == 'hourly': |
|
| 318 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 319 | elif period_type == 'daily': |
|
| 320 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 321 | elif period_type == 'monthly': |
|
| 322 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 323 | elif period_type == 'yearly': |
|
| 324 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 325 | ||
| 326 | baseline_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
|
| 327 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 328 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 329 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 330 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 331 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 332 | ||
| 333 | # query base period's energy actual |
|
| 334 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 335 | " FROM tbl_space_input_category_hourly " |
|
| 336 | " WHERE space_id = %s " |
|
| 337 | " AND energy_category_id = %s " |
|
| 338 | " AND start_datetime_utc >= %s " |
|
| 339 | " AND start_datetime_utc < %s " |
|
| 340 | " ORDER BY start_datetime_utc ", |
|
| 341 | (space['id'], |
|
| 342 | energy_category_id, |
|
| 343 | base_start_datetime_utc, |
|
| 344 | base_end_datetime_utc)) |
|
| 345 | rows_space_hourly = cursor_energy.fetchall() |
|
| 346 | ||
| 347 | rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly, |
|
| 348 | base_start_datetime_utc, |
|
| 349 | base_end_datetime_utc, |
|
| 350 | period_type) |
|
| 351 | for row_space_periodically in rows_space_periodically: |
|
| 352 | current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 353 | timedelta(minutes=timezone_offset) |
|
| 354 | if period_type == 'hourly': |
|
| 355 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 356 | elif period_type == 'daily': |
|
| 357 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 358 | elif period_type == 'monthly': |
|
| 359 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 360 | elif period_type == 'yearly': |
|
| 361 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 362 | ||
| 363 | actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1] |
|
| 364 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 365 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 368 | ||
| 369 | # calculate base period's energy savings |
|
| 370 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
| 371 | base[energy_category_id]['values_saving'].append( |
|
| 372 | base[energy_category_id]['values_baseline'][i] - |
|
| 373 | base[energy_category_id]['values_actual'][i]) |
|
| 374 | ||
| 375 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 376 | base[energy_category_id]['subtotal_baseline'] - \ |
|
| 377 | base[energy_category_id]['subtotal_actual'] |
|
| 378 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 379 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 380 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 381 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 382 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 383 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 384 | ################################################################################################################ |
|
| 385 | # Step 8: query reporting period energy saving |
|
| 386 | ################################################################################################################ |
|
| @@ 262-368 (lines=107) @@ | ||
| 259 | # Step 6: query base period energy saving |
|
| 260 | ################################################################################################################ |
|
| 261 | base = dict() |
|
| 262 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 263 | for energy_category_id in energy_category_set: |
|
| 264 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 265 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 266 | ||
| 267 | base[energy_category_id] = dict() |
|
| 268 | base[energy_category_id]['timestamps'] = list() |
|
| 269 | base[energy_category_id]['values_baseline'] = list() |
|
| 270 | base[energy_category_id]['values_actual'] = list() |
|
| 271 | base[energy_category_id]['values_saving'] = list() |
|
| 272 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 273 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 274 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 275 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 276 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 277 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 278 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 279 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 280 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 281 | # query base period's energy baseline |
|
| 282 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 283 | " FROM tbl_tenant_input_category_hourly " |
|
| 284 | " WHERE tenant_id = %s " |
|
| 285 | " AND energy_category_id = %s " |
|
| 286 | " AND start_datetime_utc >= %s " |
|
| 287 | " AND start_datetime_utc < %s " |
|
| 288 | " ORDER BY start_datetime_utc ", |
|
| 289 | (tenant['id'], |
|
| 290 | energy_category_id, |
|
| 291 | base_start_datetime_utc, |
|
| 292 | base_end_datetime_utc)) |
|
| 293 | rows_tenant_hourly = cursor_energy_baseline.fetchall() |
|
| 294 | ||
| 295 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 296 | base_start_datetime_utc, |
|
| 297 | base_end_datetime_utc, |
|
| 298 | period_type) |
|
| 299 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 300 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 301 | timedelta(minutes=timezone_offset) |
|
| 302 | if period_type == 'hourly': |
|
| 303 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 304 | elif period_type == 'daily': |
|
| 305 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 306 | elif period_type == 'monthly': |
|
| 307 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 308 | elif period_type == 'yearly': |
|
| 309 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 310 | ||
| 311 | baseline_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 312 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 313 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 314 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 315 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 316 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 317 | ||
| 318 | # query base period's energy actual |
|
| 319 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 320 | " FROM tbl_tenant_input_category_hourly " |
|
| 321 | " WHERE tenant_id = %s " |
|
| 322 | " AND energy_category_id = %s " |
|
| 323 | " AND start_datetime_utc >= %s " |
|
| 324 | " AND start_datetime_utc < %s " |
|
| 325 | " ORDER BY start_datetime_utc ", |
|
| 326 | (tenant['id'], |
|
| 327 | energy_category_id, |
|
| 328 | base_start_datetime_utc, |
|
| 329 | base_end_datetime_utc)) |
|
| 330 | rows_tenant_hourly = cursor_energy.fetchall() |
|
| 331 | ||
| 332 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 333 | base_start_datetime_utc, |
|
| 334 | base_end_datetime_utc, |
|
| 335 | period_type) |
|
| 336 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 337 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 338 | timedelta(minutes=timezone_offset) |
|
| 339 | if period_type == 'hourly': |
|
| 340 | current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') |
|
| 341 | elif period_type == 'daily': |
|
| 342 | current_datetime = current_datetime_local.strftime('%Y-%m-%d') |
|
| 343 | elif period_type == 'monthly': |
|
| 344 | current_datetime = current_datetime_local.strftime('%Y-%m') |
|
| 345 | elif period_type == 'yearly': |
|
| 346 | current_datetime = current_datetime_local.strftime('%Y') |
|
| 347 | ||
| 348 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 349 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 350 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 351 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 352 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 353 | ||
| 354 | # calculate base period's energy savings |
|
| 355 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
| 356 | base[energy_category_id]['values_saving'].append( |
|
| 357 | base[energy_category_id]['values_baseline'][i] - |
|
| 358 | base[energy_category_id]['values_actual'][i]) |
|
| 359 | ||
| 360 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 361 | base[energy_category_id]['subtotal_baseline'] - \ |
|
| 362 | base[energy_category_id]['subtotal_actual'] |
|
| 363 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 366 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 369 | ################################################################################################################ |
|
| 370 | # Step 7: query reporting period energy saving |
|
| 371 | ################################################################################################################ |
|