forked from GithubBackups/healthchecks
Add n_pings field to Check model. This holds a continually updated count of api_ping entries for each check.
This commit is contained in:
parent
1c2ee95968
commit
15478d414c
13
hc/api/management/commands/fillnpings.py
Normal file
13
hc/api/management/commands/fillnpings.py
Normal 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
hc/api/migrations/0020_check_n_pings.py
Normal file
20
hc/api/migrations/0020_check_n_pings.py
Normal 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),
|
||||
),
|
||||
]
|
@ -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")
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user