Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add yesterday stats when day totals are not yet available
Browse files Browse the repository at this point in the history
RichieB2B committed Feb 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5da612b commit bcd1ae8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dsmr_stats/services.py
Original file line number Diff line number Diff line change
@@ -419,7 +419,6 @@ def year_statistics(target_date: datetime.date):
return range_statistics(start=start_of_year, end=end_of_year)


# @TODO: Consider reworking this to meter positions in favor of https://github.com/dsmrreader/dsmr-reader/issues/1811
def period_totals() -> Dict:
"""Retrieves year/month period totals and merges them with today's consumption."""
today = timezone.localtime(timezone.now())
@@ -429,6 +428,17 @@ def period_totals() -> Dict:
except LookupError:
today_stats = {}

# also add yesterday stats if today totals are not available yet
# see https://github.com/dsmrreader/dsmr-reader/issues/1811
if not day_statistics(target_date=today):
try:
yesterday = today - timezone.timedelta(days=1)
yesterday_stats = dsmr_consumption.services.day_consumption(day=yesterday)
except LookupError:
yesterday_stats = {}
else:
yesterday_stats = {}

month_stats = month_statistics(target_date=today)
year_stats = year_statistics(target_date=today)
excluded_keys = (
@@ -443,13 +453,15 @@ def period_totals() -> Dict:
continue

# Assumes same keys, zero value fallback.
month_stats[k] += yesterday_stats.get(k, 0)
month_stats[k] += today_stats.get(k, 0)

for k in year_stats.keys():
if k in excluded_keys or year_stats[k] is None:
continue

# Assumes same keys, zero value fallback.
year_stats[k] += yesterday_stats.get(k, 0)
year_stats[k] += today_stats.get(k, 0)

return dict(

0 comments on commit bcd1ae8

Please sign in to comment.