diff --git a/README.md b/README.md index ecf9fb69..1824232e 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,13 @@ There are separate Django management commands for each task: $ ./manage.py pruneemails ```` +* Remove old records of sent notifications. For each check, remove + notifications that are older than the oldest stored ping for same check. + + ```` + $ ./manage.py prunenotifications + ```` + * Remove user accounts that match either of these conditions: * Account was created more than a month ago, and user has never logged in. These can happen when user enters invalid email address when signing up. diff --git a/hc/api/management/commands/prunenotifications.py b/hc/api/management/commands/prunenotifications.py new file mode 100644 index 00000000..82cc86dc --- /dev/null +++ b/hc/api/management/commands/prunenotifications.py @@ -0,0 +1,22 @@ +from django.core.management.base import BaseCommand +from django.db.models import Min + +from hc.api.models import Notification, Check + + +class Command(BaseCommand): + help = 'Prune stored notifications' + + def handle(self, *args, **options): + total = 0 + + q = Check.objects.filter(n_pings__gt=0) + q = q.annotate(min_ping_date=Min("ping__created")) + for check in q: + qq = Notification.objects.filter(owner_id=check.id, + created__lt=check.min_ping_date) + + num_deleted, _ = qq.delete() + total += num_deleted + + return "Done! Pruned %d notifications." % total