Skip to content

Commit

Permalink
Na import historische gegevens de dagtotalen berekenen #1302
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Mar 7, 2021
1 parent da2b908 commit e34632a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ v4.13.0 - 2021-03-09

- ``Added``: MQTT: Tussenstand huidige maand/jaar [`#1291 <https://github.com/dsmrreader/dsmr-reader/issues/1291>`_]
- ``Added``: Meterstanden opnemen in dagstatistieken [`#1301 <https://github.com/dsmrreader/dsmr-reader/issues/1301>`_]
- ``Added``: Na import historische gegevens de dagtotalen berekenen [`#1302 <https://github.com/dsmrreader/dsmr-reader/issues/1302>`_]

- ``Changed``: Partial backups no longer run daily, but weekly instead [`#1301 <https://github.com/dsmrreader/dsmr-reader/issues/1301>`_]
- ``Changed``: 6e getal achter de komma nodig bij energiecontracten [`#1304 <https://github.com/dsmrreader/dsmr-reader/issues/1304>`_]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django.core.management.base import BaseCommand
from django.utils.translation import gettext as _

import dsmr_stats.services


class Command(BaseCommand):
help = _('Reconstructs missing day statistics by using available hour statistics')
help = 'Reconstructs missing day statistics (e.g.: after importing legacy data)'

def handle(self, **options):
dsmr_stats.services.reconstruct_missing_day_statistics()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.core.management.base import BaseCommand

import dsmr_stats.services


class Command(BaseCommand):
help = 'Reconstructs missing day statistics by using available hour statistics'

def handle(self, **options):
dsmr_stats.services.reconstruct_missing_day_statistics_by_hours()

print()
print('To recalculate prices as well, execute: ./manage.py dsmr_stats_recalculate_prices')
24 changes: 24 additions & 0 deletions dsmr_stats/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,30 @@ def recalculate_prices():


def reconstruct_missing_day_statistics():
""" Reconstructs missing day statistics. """
dates_to_generate = ElectricityConsumption.objects.exclude(
# Skip today.
read_at__date=timezone.localtime(timezone.now()).date()
).annotate(
truncated_date=TruncDate('read_at')
).exclude(
# Skip existing days.
truncated_date__in=DayStatistics.objects.all().values_list('day', flat=True)
).distinct().order_by('truncated_date').values_list(
'truncated_date', flat=True
)

# Budget distinct and sorting.
dates_to_generate = sorted(list(set(dates_to_generate)))

print('Found {} day(s) to reconstruct'.format(len(dates_to_generate)))

for current_day in dates_to_generate:
print(' - Reconstructing:', current_day)
create_statistics(target_day=current_day)


def reconstruct_missing_day_statistics_by_hours():
""" Reconstructs missing day statistics by using available hour statistics. """
dates_to_generate = HourStatistics.objects.all().annotate(
truncated_date=TruncDate('hour_start')
Expand Down
16 changes: 14 additions & 2 deletions dsmr_stats/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,18 @@ def test_recalculate_prices_return(self):
self.assertEqual(day_totals.total_cost, 45)

def test_reconstruct_missing_day_statistics(self):
DayStatistics.objects.all().delete()
HourStatistics.objects.all().delete()
self.assertEqual(DayStatistics.objects.all().count(), 0)
self.assertEqual(HourStatistics.objects.all().count(), 0)

dsmr_stats.services.reconstruct_missing_day_statistics()

# It should just trigger.
self.assertEqual(DayStatistics.objects.all().count(), 2)
self.assertEqual(HourStatistics.objects.all().count(), 5)

def test_reconstruct_missing_day_statistics_by_hours(self):
# Existing day should be ignored.
DayStatistics.objects.create(
day=timezone.make_aware(timezone.datetime(2020, 1, 1)).date(),
Expand All @@ -907,7 +919,7 @@ def test_reconstruct_missing_day_statistics(self):

self.assertEqual(DayStatistics.objects.all().count(), 1)

dsmr_stats.services.reconstruct_missing_day_statistics()
dsmr_stats.services.reconstruct_missing_day_statistics_by_hours()
self.assertEqual(DayStatistics.objects.all().count(), 1)

# Now add hours for missing day.
Expand All @@ -928,7 +940,7 @@ def test_reconstruct_missing_day_statistics(self):
gas=5,
)

dsmr_stats.services.reconstruct_missing_day_statistics()
dsmr_stats.services.reconstruct_missing_day_statistics_by_hours()
self.assertEqual(DayStatistics.objects.all().count(), 2)

latest = DayStatistics.objects.all().order_by('-pk')[0]
Expand Down
Binary file modified dsmrreader/locales/nl/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 3 additions & 3 deletions dsmrreader/locales/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2291,9 +2291,6 @@ msgstr "Verandert alle bestaande statistieken. NIET GEBRUIKEN IN PRODUCTIE! Word
msgid "Recalculates day statistics prices"
msgstr "Herberekent prijzen in dagtotalen"

msgid "Reconstructs missing day statistics by using available hour statistics"
msgstr "Reconstrueert missende dagstatistieken door beschikbare uurstatistieken te gebruiken"

msgid "Description"
msgstr "Omschrijving"

Expand Down Expand Up @@ -2621,6 +2618,9 @@ msgstr "Nederlands"
msgid "English"
msgstr "Engels"

#~ msgid "Reconstructs missing day statistics by using available hour statistics"
#~ msgstr "Reconstrueert missende dagstatistieken door beschikbare uurstatistieken te gebruiken"

#~ msgid "Based on the latest electricity consumption processed."
#~ msgstr "Gebaseerd op het meest recent verwerkte elektriciteitsverbruik."

Expand Down

0 comments on commit e34632a

Please sign in to comment.