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.

36 lines
1.2 KiB

  1. from django.db.models import F
  2. from django.contrib.auth.models import User
  3. from django.core.management.base import BaseCommand
  4. from hc.accounts.models import Profile
  5. from hc.api.models import Check, Ping
  6. class Command(BaseCommand):
  7. help = """Prune pings based on limits in user profiles.
  8. This command prunes each check individually. So it does the work
  9. in small chunks instead of a few big SQL queries like the `prunepings`
  10. command. It is appropriate for initial pruning of the potentially
  11. huge api_ping table.
  12. """
  13. def handle(self, *args, **options):
  14. # Create any missing user profiles
  15. for user in User.objects.filter(profile=None):
  16. Profile.objects.get_or_create(user_id=user.id)
  17. checks = Check.objects
  18. checks = checks.annotate(limit=F("project__owner__profile__ping_log_limit"))
  19. for check in checks:
  20. q = Ping.objects.filter(owner_id=check.id)
  21. q = q.filter(n__lte=check.n_pings - check.limit)
  22. q = q.filter(n__gt=0)
  23. n_pruned, _ = q.delete()
  24. self.stdout.write(
  25. "Pruned %d pings for check %s (%s)" % (n_pruned, check.id, check.name)
  26. )
  27. return "Done!"