Browse Source

Fix `prunepings` and `prunepingsslow`, fixes #264

pull/272/head
Pēteris Caune 5 years ago
parent
commit
e0f161157d
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 53 additions and 4 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +2
    -2
      hc/api/management/commands/prunepings.py
  3. +2
    -2
      hc/api/management/commands/prunepingsslow.py
  4. +24
    -0
      hc/api/tests/test_prunepings.py
  5. +24
    -0
      hc/api/tests/test_prunepingsslow.py

+ 1
- 0
CHANGELOG.md View File

@ -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 badges for tags containing special characters (#240, #237)
- Fix the "Integrations" page for when the user has no active project - Fix the "Integrations" page for when the user has no active project
- Prevent email clients from opening the one-time login links (#255) - 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 ## 1.7.0 - 2019-05-02


+ 2
- 2
hc/api/management/commands/prunepings.py View File

@ -14,8 +14,8 @@ class Command(BaseCommand):
Profile.objects.get_or_create(user_id=user.id) Profile.objects.get_or_create(user_id=user.id)
q = Ping.objects 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) q = q.filter(n__gt=0)
n_pruned, _ = q.delete() n_pruned, _ = q.delete()


+ 2
- 2
hc/api/management/commands/prunepingsslow.py View File

@ -21,11 +21,11 @@ class Command(BaseCommand):
Profile.objects.get_or_create(user_id=user.id) Profile.objects.get_or_create(user_id=user.id)
checks = Check.objects 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: for check in checks:
q = Ping.objects.filter(owner_id=check.id) 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) q = q.filter(n__gt=0)
n_pruned, _ = q.delete() n_pruned, _ = q.delete()


+ 24
- 0
hc/api/tests/test_prunepings.py View 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
- 0
hc/api/tests/test_prunepingsslow.py View 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…
Cancel
Save