Skip to content

Commit

Permalink
Source data retention #72
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Jan 13, 2018
1 parent c4058dc commit f28b782
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dsmr_datalogger/migrations/0009_retention_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
name='RetentionSettings',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('data_retention_in_hours', models.IntegerField(blank=True, choices=[(None, 'None (keep all readings)'), (168, 'Discard most readings after one week'), (672, 'Discard most readings after one month'), (4032, 'Discard most readings after six months'), (8064, 'Discard most readings after one year')], default=None, help_text='The lifetime of readings, before discarding them.', null=True, verbose_name='Data retention')),
('data_retention_in_hours', models.IntegerField(blank=True, choices=[(None, 'None (keep all readings)'), (168, 'Discard most readings after one week'), (672, 'Discard most readings after one month'), (4032, 'Discard most readings after six months'), (8064, 'Discard most readings after one year')], default=None, help_text='The lifetime of readings, before discarding them. Please note that retention is applied during night time currently, between midnight and six in the morning.', null=True, verbose_name='Data retention')),
],
options={
'verbose_name': 'Retention configuration',
Expand Down
4 changes: 3 additions & 1 deletion dsmr_datalogger/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class RetentionSettings(SingletonModel):
default=RETENTION_NONE,
choices=RETENTION_CHOICES,
verbose_name=_('Data retention'),
help_text=_('The lifetime of readings, before discarding them.')
help_text=_(
'The lifetime of readings, before discarding them. Please note that retention is applied during night time '
'currently, between midnight and six in the morning.')
)

def __str__(self):
Expand Down
6 changes: 6 additions & 0 deletions dsmr_datalogger/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ def apply_data_retention():
# No retention enabled at all (default behaviour).
return

current_hour = timezone.now().hour

# Only cleanup during nights. Allow from midnight to six a.m.
if current_hour > 6:
return

# Each run should be capped, for obvious performance reasons.
MAX_HOURS_CLEANUP = 24

Expand Down
19 changes: 19 additions & 0 deletions dsmr_datalogger/tests/retention/test_retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ class TestRetention(TestCase):
'dsmr_datalogger/gas-consumption.json',
]

@mock.patch('django.utils.timezone.now')
def test_retention_timestamp_restrictions(self, now_mock):
now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 12, 25, hour=12))

RetentionSettings.get_solo()
RetentionSettings.objects.update(data_retention_in_hours=RetentionSettings.RETENTION_WEEK)

# Retention should do nothing, since it's noon.
self.assertEqual(DsmrReading.objects.count(), 52)
dsmr_datalogger.services.apply_data_retention()
self.assertEqual(DsmrReading.objects.count(), 52)

now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 12, 25, hour=5))

# Retention should kick in now.
self.assertEqual(DsmrReading.objects.count(), 52)
dsmr_datalogger.services.apply_data_retention()
self.assertEqual(DsmrReading.objects.count(), 2)

@mock.patch('django.utils.timezone.now')
def test_apply_data_retention(self, now_mock):
now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 12, 25))
Expand Down
2 changes: 1 addition & 1 deletion dsmrreader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
from django.utils.version import get_version


VERSION = (1, 12, 0, 'beta', 8)
VERSION = (1, 12, 0, 'beta', 9)

__version__ = get_version(VERSION)
Binary file modified dsmrreader/locales/nl/LC_MESSAGES/django.mo
Binary file not shown.
7 changes: 5 additions & 2 deletions dsmrreader/locales/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ msgstr "Verwijder de meeste metingen na een jaar"
msgid "Data retention"
msgstr "Dataretentie"

msgid "The lifetime of readings, before discarding them."
msgstr "De levensduur van metingen, voordat ze verwijderd worden."
msgid "The lifetime of readings, before discarding them. Please note that retention is applied during night time currently, between midnight and six in the morning."
msgstr "De levenduur van metingen, voordat ze opgeschoond worden. N.B.: Momenteel wordt retentie alleen doorgevoerd tijdens de nachturen, tussen middernacht en zes uur 's ochtends."

msgid "Retention configuration"
msgstr "Retentieconfiguratie"
Expand Down Expand Up @@ -1459,6 +1459,9 @@ msgstr "Nederlands"
msgid "English"
msgstr "Engels"

#~ msgid "The lifetime of readings, before discarding them."
#~ msgstr "De levensduur van metingen, voordat ze verwijderd worden."

#~ msgid "By default the application stores all readings taken. As there might be DSMR-readings about every second, this results in several millions of readings each year. This may cause degraded performance. For that reason you might want to apply retention to this data. Please note that enabling this feature will NOT discard ALL readings, as it will PRESERVE the first and last reading of each hour."
#~ msgstr "Standaard bewaart de applicatie alle metingen. Echter, gezien metingen elke seconde binnen kunnen komen, kan dat leiden tot miljoenen metingen per jaar. Dit kan op den duur verslechterde performance veroorzaken. Daarom is het mogelijk om retentie toe te passen op de gegevens. N.B.: Deze feature gooit NIET alle metingen weg en bewaart van elk uur de eerste en laatste."

Expand Down

0 comments on commit f28b782

Please sign in to comment.