diff --git a/hc/api/management/commands/fillnpings.py b/hc/api/management/commands/fillnpings.py new file mode 100644 index 00000000..194aa573 --- /dev/null +++ b/hc/api/management/commands/fillnpings.py @@ -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.") diff --git a/hc/api/migrations/0020_check_n_pings.py b/hc/api/migrations/0020_check_n_pings.py new file mode 100644 index 00000000..b6cdf116 --- /dev/null +++ b/hc/api/migrations/0020_check_n_pings.py @@ -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), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index aa216d7e..c510e601 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -48,6 +48,7 @@ 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") diff --git a/hc/api/views.py b/hc/api/views.py index 3c00b6b2..bf729902 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -1,6 +1,7 @@ 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 @@ -16,6 +17,7 @@ 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" @@ -52,6 +54,7 @@ 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"