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.

23 lines
654 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(
  12. owner_id=check.id, created__lt=check.min_ping_date
  13. )
  14. num_deleted, _ = qq.delete()
  15. total += num_deleted
  16. return "Done! Pruned %d notifications." % total