From e0f161157dc9baadebfdbd1e982ca16d69f0f93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 24 Jun 2019 18:02:36 +0300 Subject: [PATCH] Fix `prunepings` and `prunepingsslow`, fixes #264 --- CHANGELOG.md | 1 + hc/api/management/commands/prunepings.py | 4 ++-- hc/api/management/commands/prunepingsslow.py | 4 ++-- hc/api/tests/test_prunepings.py | 24 ++++++++++++++++++++ hc/api/tests/test_prunepingsslow.py | 24 ++++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 hc/api/tests/test_prunepings.py create mode 100644 hc/api/tests/test_prunepingsslow.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 90449772..e706b1fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/hc/api/management/commands/prunepings.py b/hc/api/management/commands/prunepings.py index a0688cde..8b85dd99 100644 --- a/hc/api/management/commands/prunepings.py +++ b/hc/api/management/commands/prunepings.py @@ -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() diff --git a/hc/api/management/commands/prunepingsslow.py b/hc/api/management/commands/prunepingsslow.py index 20b10c1c..b73a28a5 100644 --- a/hc/api/management/commands/prunepingsslow.py +++ b/hc/api/management/commands/prunepingsslow.py @@ -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() diff --git a/hc/api/tests/test_prunepings.py b/hc/api/tests/test_prunepings.py new file mode 100644 index 00000000..9c28262e --- /dev/null +++ b/hc/api/tests/test_prunepings.py @@ -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) diff --git a/hc/api/tests/test_prunepingsslow.py b/hc/api/tests/test_prunepingsslow.py new file mode 100644 index 00000000..0db46549 --- /dev/null +++ b/hc/api/tests/test_prunepingsslow.py @@ -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)