implement command to clean old values from the database
This commit is contained in:
parent
c980c2ac76
commit
8ded8def06
|
@ -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()
|
||||
|
|
|
@ -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()
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -4,5 +4,6 @@ export DJANGO_SETTINGS_MODULE=sensors.settings.production
|
|||
while :
|
||||
do
|
||||
./manage.py collect
|
||||
./manage.py clean
|
||||
sleep 600
|
||||
done
|
||||
|
|
Loading…
Reference in New Issue