forked from GithubBackups/healthchecks
Fix prunepings
and prunepingsslow
, fixes #264
This commit is contained in:
parent
4867fab291
commit
e0f161157d
@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
|
||||
- Fix badges for tags containing special characters (#240, #237)
|
||||
- Fix the "Integrations" page for when the user has no active project
|
||||
- Prevent email clients from opening the one-time login links (#255)
|
||||
- Fix `prunepings` and `prunepingsslow`, they got broken when adding Projects (#264)
|
||||
|
||||
|
||||
## 1.7.0 - 2019-05-02
|
||||
|
@ -14,8 +14,8 @@ class Command(BaseCommand):
|
||||
Profile.objects.get_or_create(user_id=user.id)
|
||||
|
||||
q = Ping.objects
|
||||
q = q.annotate(limit=F("owner__user__profile__ping_log_limit"))
|
||||
q = q.filter(n__lt=F("owner__n_pings") - F("limit"))
|
||||
q = q.annotate(limit=F("owner__project__owner__profile__ping_log_limit"))
|
||||
q = q.filter(n__lte=F("owner__n_pings") - F("limit"))
|
||||
q = q.filter(n__gt=0)
|
||||
n_pruned, _ = q.delete()
|
||||
|
||||
|
@ -21,11 +21,11 @@ class Command(BaseCommand):
|
||||
Profile.objects.get_or_create(user_id=user.id)
|
||||
|
||||
checks = Check.objects
|
||||
checks = checks.annotate(limit=F("user__profile__ping_log_limit"))
|
||||
checks = checks.annotate(limit=F("project__owner__profile__ping_log_limit"))
|
||||
|
||||
for check in checks:
|
||||
q = Ping.objects.filter(owner_id=check.id)
|
||||
q = q.filter(n__lt=check.n_pings - check.limit)
|
||||
q = q.filter(n__lte=check.n_pings - check.limit)
|
||||
q = q.filter(n__gt=0)
|
||||
n_pruned, _ = q.delete()
|
||||
|
||||
|
24
hc/api/tests/test_prunepings.py
Normal file
24
hc/api/tests/test_prunepings.py
Normal file
@ -0,0 +1,24 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.utils import timezone
|
||||
from hc.api.management.commands.prunepings import Command
|
||||
from hc.api.models import Check, Ping
|
||||
from hc.test import BaseTestCase
|
||||
|
||||
|
||||
class PrunePingsTestCase(BaseTestCase):
|
||||
year_ago = timezone.now() - timedelta(days=365)
|
||||
|
||||
def test_it_removes_old_pings(self):
|
||||
self.profile.ping_log_limit = 1
|
||||
self.profile.save()
|
||||
|
||||
c = Check(project=self.project, n_pings=2)
|
||||
c.save()
|
||||
|
||||
Ping.objects.create(owner=c, n=1)
|
||||
Ping.objects.create(owner=c, n=2)
|
||||
|
||||
Command().handle()
|
||||
|
||||
self.assertEqual(Ping.objects.count(), 1)
|
24
hc/api/tests/test_prunepingsslow.py
Normal file
24
hc/api/tests/test_prunepingsslow.py
Normal file
@ -0,0 +1,24 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.utils import timezone
|
||||
from hc.api.management.commands.prunepingsslow import Command
|
||||
from hc.api.models import Check, Ping
|
||||
from hc.test import BaseTestCase
|
||||
|
||||
|
||||
class PrunePingsSlowTestCase(BaseTestCase):
|
||||
year_ago = timezone.now() - timedelta(days=365)
|
||||
|
||||
def test_it_removes_old_pings(self):
|
||||
self.profile.ping_log_limit = 1
|
||||
self.profile.save()
|
||||
|
||||
c = Check(project=self.project, n_pings=2)
|
||||
c.save()
|
||||
|
||||
Ping.objects.create(owner=c, n=1)
|
||||
Ping.objects.create(owner=c, n=2)
|
||||
|
||||
Command().handle()
|
||||
|
||||
self.assertEqual(Ping.objects.count(), 1)
|
Loading…
x
Reference in New Issue
Block a user