Browse Source

Add n_pings field to Check model. This holds a continually updated count of api_ping entries for each check.

pull/25/head
Pēteris Caune 9 years ago
parent
commit
15478d414c
4 changed files with 37 additions and 0 deletions
  1. +13
    -0
      hc/api/management/commands/fillnpings.py
  2. +20
    -0
      hc/api/migrations/0020_check_n_pings.py
  3. +1
    -0
      hc/api/models.py
  4. +3
    -0
      hc/api/views.py

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

@ -0,0 +1,13 @@
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()
print("Done.")

+ 20
- 0
hc/api/migrations/0020_check_n_pings.py View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2015-12-30 12:06
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0019_check_tags'),
]
operations = [
migrations.AddField(
model_name='check',
name='n_pings',
field=models.IntegerField(default=0),
),
]

+ 1
- 0
hc/api/models.py View File

@ -48,6 +48,7 @@ class Check(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
timeout = models.DurationField(default=DEFAULT_TIMEOUT) timeout = models.DurationField(default=DEFAULT_TIMEOUT)
grace = models.DurationField(default=DEFAULT_GRACE) grace = models.DurationField(default=DEFAULT_GRACE)
n_pings = models.IntegerField(default=0)
last_ping = models.DateTimeField(null=True, blank=True) last_ping = models.DateTimeField(null=True, blank=True)
alert_after = models.DateTimeField(null=True, blank=True, editable=False) alert_after = models.DateTimeField(null=True, blank=True, editable=False)
status = models.CharField(max_length=6, choices=STATUSES, default="new") status = models.CharField(max_length=6, choices=STATUSES, default="new")


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

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


Loading…
Cancel
Save