added new period type 'weekly' to meterenergy and aggregate_hourly_data_by_period in API

Merge branch 'PR76' into develop
pull/75/MERGE
13621160019@163.com 2021-10-28 21:53:41 +08:00
commit f3cda07639
2 changed files with 31 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import calendar
from datetime import datetime, timedelta
import mysql.connector
import collections
@ -11,7 +12,7 @@ import statistics
# rows_hourly: list of (start_datetime_utc, actual_value), should belong to one energy_category_id
# start_datetime_utc: start datetime in utc
# end_datetime_utc: end datetime in utc
# period_type: one of the following period types, 'hourly', 'daily', 'monthly' and 'yearly'
# period_type: one of the following period types, 'hourly', 'daily', 'weekly', 'monthly' and 'yearly'
# Note: this procedure doesn't work with multiple energy categories
########################################################################################################################
def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetime_utc, period_type):
@ -19,7 +20,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim
if start_datetime_utc is None or \
end_datetime_utc is None or \
start_datetime_utc >= end_datetime_utc or \
period_type not in ('hourly', 'daily', 'monthly', 'yearly'):
period_type not in ('hourly', 'daily', 'weekly', 'monthly', 'yearly'):
return list()
start_datetime_utc = start_datetime_utc.replace(tzinfo=None)
@ -58,6 +59,28 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim
return result_rows_daily
elif period_type == 'weekly':
result_rows_weekly = list()
# todo: add config.working_day_start_time_local
# todo: add config.minutes_to_count
# calculate the start datetime in utc of the monday in the first week in local
start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3]))
weekday = start_datetime_local.weekday()
current_datetime_utc = \
start_datetime_local.replace(hour=0) - timedelta(days=weekday, hours=int(config.utc_offset[1:3]))
while current_datetime_utc <= end_datetime_utc:
next_datetime_utc = current_datetime_utc + timedelta(days=7)
subtotal = Decimal(0.0)
for row in rows_hourly:
if current_datetime_utc <= row[0] < next_datetime_utc:
subtotal += row[1]
result_rows_weekly.append((current_datetime_utc, subtotal))
current_datetime_utc = next_datetime_utc
return result_rows_weekly
elif period_type == "monthly":
result_rows_monthly = list()
# todo: add config.working_day_start_time_local
@ -143,7 +166,7 @@ def aggregate_hourly_data_by_period(rows_hourly, start_datetime_utc, end_datetim
result_rows_yearly = list()
# todo: add config.working_day_start_time_local
# todo: add config.minutes_to_count
# calculate the start datetime in utc of the first day in the first month in local
# calculate the start datetime in utc of the first day in the first year in local
start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3]))
current_datetime_utc = start_datetime_local.replace(month=1, day=1, hour=0) - timedelta(
hours=int(config.utc_offset[1:3]))

View File

@ -53,7 +53,7 @@ class Reporting:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
else:
period_type = str.strip(period_type)
if period_type not in ['hourly', 'daily', 'monthly', 'yearly']:
if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
@ -208,6 +208,8 @@ class Reporting:
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
elif period_type == 'daily':
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
elif period_type == 'weekly':
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
elif period_type == 'monthly':
current_datetime = current_datetime_local.strftime('%Y-%m')
elif period_type == 'yearly':
@ -250,6 +252,8 @@ class Reporting:
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
elif period_type == 'daily':
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
elif period_type == 'weekly':
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
elif period_type == 'monthly':
current_datetime = current_datetime_local.strftime('%Y-%m')
elif period_type == 'yearly':