|
|
@ -1,6 +1,7 @@ |
|
|
|
from django.db.models import F |
|
|
|
from django.contrib.auth.models import User |
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
from django.db.models import F, Count |
|
|
|
|
|
|
|
from hc.accounts.models import Profile |
|
|
|
from hc.api.models import Check |
|
|
|
|
|
|
@ -9,18 +10,20 @@ class Command(BaseCommand): |
|
|
|
help = 'Prune pings based on limits in user profiles' |
|
|
|
|
|
|
|
def handle(self, *args, **options): |
|
|
|
|
|
|
|
# Create any missing user profiles |
|
|
|
for user in User.objects.filter(profile=None): |
|
|
|
for user in User.objects.filter(profile=None).iterator(): |
|
|
|
Profile.objects.for_user(user) |
|
|
|
|
|
|
|
# Select checks having n_ping greater than the limit in user profile |
|
|
|
# Select checks having more pings than the limit in user profile |
|
|
|
checks = Check.objects |
|
|
|
checks = checks.annotate(limit=F("user__profile__ping_log_limit")) |
|
|
|
checks = checks.filter(n_pings__gt=F("limit")) |
|
|
|
checks = checks.annotate( |
|
|
|
limit=F("user__profile__ping_log_limit"), |
|
|
|
count_pings=Count("ping")) |
|
|
|
checks = checks.filter(count_pings__gt=F("limit")) |
|
|
|
checks = checks.select_related("user").order_by("user", "id") |
|
|
|
|
|
|
|
total = 0 |
|
|
|
for check in checks: |
|
|
|
for check in checks.iterator(): |
|
|
|
n = check.prune_pings(check.limit) |
|
|
|
total += n |
|
|
|
self.stdout.write("---") |
|
|
|