Add command to purge old logs
authorMagnus Hagander <magnus@hagander.net>
Thu, 8 Feb 2018 19:36:07 +0000 (20:36 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 8 Feb 2018 19:36:07 +0000 (20:36 +0100)
hamnadmin/hamnadmin/register/management/commands/delete_old_logs.py [new file with mode: 0644]

diff --git a/hamnadmin/hamnadmin/register/management/commands/delete_old_logs.py b/hamnadmin/hamnadmin/register/management/commands/delete_old_logs.py
new file mode 100644 (file)
index 0000000..e4f1d19
--- /dev/null
@@ -0,0 +1,17 @@
+from django.core.management.base import BaseCommand
+from django.db import transaction, connection
+
+from datetime import timedelta
+
+# How long should we keep logs?
+LOG_KEEP_DAYS=300
+
+class Command(BaseCommand):
+       help = "Delete old logs"
+
+       def handle(self, *args, **options):
+               with transaction.atomic():
+                       curs = connection.cursor()
+                       curs.execute("DELETE FROM aggregatorlog WHERE success AND ts < NOW()-%(age)s", {
+                               'age': timedelta(LOG_KEEP_DAYS),
+                       })