Skip to content

Commit

Permalink
PVOutput exports schedulen naar ingestelde upload interval #441
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Feb 22, 2018
1 parent 1c278b3 commit 7d22bb7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ v1.14.0 - 2018-xx-xx

**Tickets resolved in this release:**

- [`#441 <https://github.com/dennissiemensma/dsmr-reader/issues/441>`_] PVOutput exports schedulen naar ingestelde upload interval - by pyrocumulus
- [`#436 <https://github.com/dennissiemensma/dsmr-reader/issues/436>`_] Provide built-in authentication method for public webinterface


Expand Down
23 changes: 10 additions & 13 deletions dsmr_pvoutput/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.utils import timezone
import datetime
import requests

from dsmr_pvoutput.models.settings import PVOutputAddStatusSettings, PVOutputAPISettings
Expand All @@ -21,27 +20,25 @@ def should_export():

def schedule_next_export():
""" Schedules the next export, according to user preference. """
status_settings = PVOutputAddStatusSettings.get_solo()

next_export = timezone.now() + timezone.timedelta(minutes=status_settings.upload_interval)
next_export = round_to_nearest_upload_interval(date=next_export, round_to_minutes=status_settings.upload_interval)

next_export = get_next_export()
print(' - PVOutput | Delaying the next export until: {}'.format(next_export))

status_settings = PVOutputAddStatusSettings.get_solo()
status_settings.next_export = next_export
status_settings.save()


def round_to_nearest_upload_interval(date=None, round_to_minutes=5):
if date is None:
date = timezone.now()
def get_next_export():
""" Rounds the timestamp to the nearest upload interval, preventing the uploads to shift forward. """
status_settings = PVOutputAddStatusSettings.get_solo()

round_to_seconds = round_to_minutes*60
next_export = timezone.now() + timezone.timedelta(minutes=status_settings.upload_interval)

seconds = (date.replace(tzinfo=None) - date.min).seconds
rounding = (seconds+round_to_seconds/2) // round_to_seconds * round_to_seconds
# Make sure it shifts back to the closest interval point possible.
minute_marker = next_export.minute
minute_marker = minute_marker - (minute_marker % status_settings.upload_interval)

return date + datetime.timedelta(0, rounding-seconds, -date.microsecond)
return next_export.replace(minute=minute_marker, second=0)


def export():
Expand Down
19 changes: 19 additions & 0 deletions dsmr_pvoutput/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ def test_should_export_no_need(self, now_mock):
PVOutputAddStatusSettings.objects.update(next_export=timezone.now() + timezone.timedelta(hours=1))
self.assertFalse(dsmr_pvoutput.services.should_export())

@mock.patch('django.utils.timezone.now')
def test_get_next_export(self, now_mock):
PVOutputAddStatusSettings.get_solo()

PVOutputAddStatusSettings.objects.update(upload_interval=PVOutputAddStatusSettings.INTERVAL_5_MINUTES)
now_mock.return_value = timezone.make_aware(timezone.datetime(2018, 2, 1, minute=13))
result = dsmr_pvoutput.services.get_next_export()
self.assertEqual(result.minute, 15)

PVOutputAddStatusSettings.objects.update(upload_interval=PVOutputAddStatusSettings.INTERVAL_15_MINUTES)
now_mock.return_value = timezone.make_aware(timezone.datetime(2018, 2, 1, minute=25))
result = dsmr_pvoutput.services.get_next_export()
self.assertEqual(result.minute, 30)

# Pass hour mark.
now_mock.return_value = timezone.make_aware(timezone.datetime(2018, 2, 1, minute=59))
result = dsmr_pvoutput.services.get_next_export()
self.assertEqual(result.minute, 0)

@mock.patch('django.utils.timezone.now')
def test_should_export_okay(self, now_mock):
now_mock.return_value = timezone.make_aware(timezone.datetime(2017, 10, 1, hour=15))
Expand Down

0 comments on commit 7d22bb7

Please sign in to comment.