From 8ded8def068cd6146b322d49b007c63e61610f8f Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 24 Oct 2019 21:45:48 +0200 Subject: [PATCH] implement command to clean old values from the database --- sensors/collector/collector.py | 9 ++++++- .../collector/management/commands/clean.py | 8 ++++++ sensors/collector/tests/test_collector.py | 27 +++++++++++++++++++ sensors/run.sh | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 sensors/collector/management/commands/clean.py diff --git a/sensors/collector/collector.py b/sensors/collector/collector.py index cb99add..b43ae0a 100644 --- a/sensors/collector/collector.py +++ b/sensors/collector/collector.py @@ -1,5 +1,5 @@ import os -from datetime import datetime +from datetime import datetime, timedelta if os.uname()[4].startswith("arm"): from sense_hat import SenseHat @@ -31,3 +31,10 @@ def values_to_db(): Temperature.objects.create(value=get_temperature(), time=time) Humidity.objects.create(value=get_humidity(), time=time) Pressure.objects.create(value=get_pressure(), time=time) + + +def clean_db(): + time = datetime.now() - timedelta(days=30) + Temperature.objects.filter(time__lt=time).delete() + Humidity.objects.filter(time__lt=time).delete() + Pressure.objects.filter(time__lt=time).delete() diff --git a/sensors/collector/management/commands/clean.py b/sensors/collector/management/commands/clean.py new file mode 100644 index 0000000..7b82d51 --- /dev/null +++ b/sensors/collector/management/commands/clean.py @@ -0,0 +1,8 @@ +from django.core.management.base import BaseCommand, CommandError +from collector.collector import clean_db + +class Command(BaseCommand): + help = 'Clean out values older than 30 days.' + + def handle(self, *args, **options): + clean_db() diff --git a/sensors/collector/tests/test_collector.py b/sensors/collector/tests/test_collector.py index e3a9864..de2a0ff 100644 --- a/sensors/collector/tests/test_collector.py +++ b/sensors/collector/tests/test_collector.py @@ -1,4 +1,6 @@ +from datetime import datetime, timedelta import pytest +from mixer.backend.django import mixer from collector import collector from collector.models import Temperature, Humidity, Pressure @@ -52,3 +54,28 @@ def test_values_to_db(monkeypatch): assert (temp.value == 25.5 and humidity.value == 45 and pressure.value == 1013) + + +def test_clean_db_remove_values(): + old_time = datetime.now() - timedelta(days=30) + mixer.blend('collector.Temperature', time=old_time) + mixer.blend('collector.Pressure', time=old_time) + mixer.blend('collector.Humidity', time=old_time) + collector.clean_db() + temp = Temperature.objects.all() + pressure = Pressure.objects.all() + humidity = Humidity.objects.all() + assert not temp.exists() and not pressure.exists() and not humidity.exists() + + +def test_clean_db_dont_remove_values(): + old_time = datetime.now() - timedelta(days=29) + mixer.blend('collector.Temperature', time=old_time) + mixer.blend('collector.Pressure', time=old_time) + mixer.blend('collector.Humidity', time=old_time) + collector.clean_db() + temp = Temperature.objects.all() + pressure = Pressure.objects.all() + humidity = Humidity.objects.all() + assert temp.exists() and pressure.exists() and humidity.exists() + diff --git a/sensors/run.sh b/sensors/run.sh index 6f43245..dc5d70f 100755 --- a/sensors/run.sh +++ b/sensors/run.sh @@ -4,5 +4,6 @@ export DJANGO_SETTINGS_MODULE=sensors.settings.production while : do ./manage.py collect + ./manage.py clean sleep 600 done