Browse Source

dont need to keep track of n-pings

pull/25/head
Di Wu 9 years ago
parent
commit
679ab9cd19
6 changed files with 33 additions and 31 deletions
  1. +1
    -1
      hc/api/admin.py
  2. +0
    -13
      hc/api/management/commands/fillnpings.py
  3. +10
    -7
      hc/api/management/commands/prunepings.py
  4. +19
    -0
      hc/api/migrations/0021_remove_check_n_pings.py
  5. +3
    -7
      hc/api/models.py
  6. +0
    -3
      hc/api/views.py

+ 1
- 1
hc/api/admin.py View File

@ -29,7 +29,7 @@ class ChecksAdmin(admin.ModelAdmin):
search_fields = ["name", "user__email"]
list_display = ("id", "name_tags", "created", "code", "status", "email",
"last_ping", "n_pings")
"last_ping")
list_select_related = ("user", )
list_filter = ("status", OwnershipListFilter, "last_ping")
actions = ["send_alert"]


+ 0
- 13
hc/api/management/commands/fillnpings.py View File

@ -1,13 +0,0 @@
from django.core.management.base import BaseCommand
from hc.api.models import Check, Ping
class Command(BaseCommand):
help = 'Fill check.n_pings field'
def handle(self, *args, **options):
for check in Check.objects.all():
check.n_pings = Ping.objects.filter(owner=check).count()
check.save(update_fields=("n_pings", ))
return "Done!"

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

@ -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("---")


+ 19
- 0
hc/api/migrations/0021_remove_check_n_pings.py View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2015-12-31 03:55
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0020_check_n_pings'),
]
operations = [
migrations.RemoveField(
model_name='check',
name='n_pings',
),
]

+ 3
- 7
hc/api/models.py View File

@ -48,7 +48,6 @@ class Check(models.Model):
created = models.DateTimeField(auto_now_add=True)
timeout = models.DurationField(default=DEFAULT_TIMEOUT)
grace = models.DurationField(default=DEFAULT_GRACE)
n_pings = models.IntegerField(default=0)
last_ping = models.DateTimeField(null=True, blank=True)
alert_after = models.DateTimeField(null=True, blank=True, editable=False)
status = models.CharField(max_length=6, choices=STATUSES, default="new")
@ -104,7 +103,7 @@ class Check(models.Model):
"""
pings = Ping.objects.filter(owner=self).order_by("-created")
pings = Ping.objects.filter(owner=self).order_by("-id")
cutoff = pings[keep_limit:keep_limit+1]
# If cutoff is empty slice then the check has less than `keep_limit`
@ -112,13 +111,10 @@ class Check(models.Model):
if len(cutoff) == 0:
return 0
cutoff_date = cutoff[0].created
q = Ping.objects.filter(owner=self, created__lte=cutoff_date)
cutoff_id = cutoff[0].id
q = Ping.objects.filter(owner=self, id__lte=cutoff_id)
n_pruned, _ = q.delete()
self.n_pings = keep_limit
self.save(update_fields=("n_pings", ))
return n_pruned


+ 0
- 3
hc/api/views.py View File

@ -1,7 +1,6 @@
import json
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.db.models import F
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
@ -17,7 +16,6 @@ def ping(request, code):
except Check.DoesNotExist:
return HttpResponseBadRequest()
check.n_pings = F("n_pings") + 1
check.last_ping = timezone.now()
if check.status == "new":
check.status = "up"
@ -54,7 +52,6 @@ def handle_email(request):
except Check.DoesNotExist:
continue
check.n_pings = F("n_pings") + 1
check.last_ping = timezone.now()
if check.status == "new":
check.status = "up"


Loading…
Cancel
Save