You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
669 B

  1. from django.core.management.base import BaseCommand
  2. from django.db.models import Min
  3. from hc.api.models import Notification, Check
  4. class Command(BaseCommand):
  5. help = 'Prune stored notifications'
  6. def handle(self, *args, **options):
  7. total = 0
  8. q = Check.objects.filter(n_pings__gt=100)
  9. q = q.annotate(min_ping_date=Min("ping__created"))
  10. for check in q:
  11. qq = Notification.objects.filter(owner_id=check.id,
  12. created__lt=check.min_ping_date)
  13. num_deleted, _ = qq.delete()
  14. total += num_deleted
  15. return "Done! Pruned %d notifications." % total