added new period type 'weekly' to meterenergy and aggregate_hourly_data_by_period in API
Merge branch 'PR76' into developpull/75/MERGE
commit
f3cda07639
|
@ -1,3 +1,4 @@
|
||||||
|
import calendar
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
import collections
|
import collections
|
||||||
|
@ -11,7 +12,7 @@ import statistics
|
||||||
# rows_hourly: list of (start_datetime_utc, actual_value), should belong to one energy_category_id
|
# rows_hourly: list of (start_datetime_utc, actual_value), should belong to one energy_category_id
|
||||||
# start_datetime_utc: start datetime in utc
|
# start_datetime_utc: start datetime in utc
|
||||||
# end_datetime_utc: end 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
|
# 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):
|
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 \
|
if start_datetime_utc is None or \
|
||||||
end_datetime_utc is None or \
|
end_datetime_utc is None or \
|
||||||
start_datetime_utc >= end_datetime_utc 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()
|
return list()
|
||||||
|
|
||||||
start_datetime_utc = start_datetime_utc.replace(tzinfo=None)
|
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
|
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":
|
elif period_type == "monthly":
|
||||||
result_rows_monthly = list()
|
result_rows_monthly = list()
|
||||||
# todo: add config.working_day_start_time_local
|
# 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()
|
result_rows_yearly = list()
|
||||||
# todo: add config.working_day_start_time_local
|
# todo: add config.working_day_start_time_local
|
||||||
# todo: add config.minutes_to_count
|
# 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]))
|
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(
|
current_datetime_utc = start_datetime_local.replace(month=1, day=1, hour=0) - timedelta(
|
||||||
hours=int(config.utc_offset[1:3]))
|
hours=int(config.utc_offset[1:3]))
|
||||||
|
|
|
@ -53,7 +53,7 @@ class Reporting:
|
||||||
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
|
||||||
else:
|
else:
|
||||||
period_type = str.strip(period_type)
|
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')
|
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])
|
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')
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
|
||||||
elif period_type == 'daily':
|
elif period_type == 'daily':
|
||||||
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
|
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':
|
elif period_type == 'monthly':
|
||||||
current_datetime = current_datetime_local.strftime('%Y-%m')
|
current_datetime = current_datetime_local.strftime('%Y-%m')
|
||||||
elif period_type == 'yearly':
|
elif period_type == 'yearly':
|
||||||
|
@ -250,6 +252,8 @@ class Reporting:
|
||||||
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
|
current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
|
||||||
elif period_type == 'daily':
|
elif period_type == 'daily':
|
||||||
current_datetime = current_datetime_local.strftime('%Y-%m-%d')
|
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':
|
elif period_type == 'monthly':
|
||||||
current_datetime = current_datetime_local.strftime('%Y-%m')
|
current_datetime = current_datetime_local.strftime('%Y-%m')
|
||||||
elif period_type == 'yearly':
|
elif period_type == 'yearly':
|
||||||
|
|
Loading…
Reference in New Issue