Browse Source

In the "List checks" API response, the "next_ping" date was incorrect for checks using cron syntax. Fixed.

pull/133/head
Pēteris Caune 7 years ago
parent
commit
a62350cdad
3 changed files with 20 additions and 3 deletions
  1. +0
    -1
      hc/api/decorators.py
  2. +7
    -2
      hc/api/models.py
  3. +13
    -0
      hc/api/tests/test_check_model.py

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

@ -1,6 +1,5 @@
import json import json
import re import re
import uuid
from functools import wraps from functools import wraps
from django.contrib.auth.models import User from django.contrib.auth.models import User


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

@ -47,6 +47,11 @@ PO_PRIORITIES = {
} }
def isostring(dt):
"""Convert the datetime to ISO 8601 format with no microseconds. """
return dt.replace(microsecond=0).isoformat()
class Check(models.Model): class Check(models.Model):
class Meta: class Meta:
@ -167,8 +172,8 @@ class Check(models.Model):
result["tz"] = self.tz result["tz"] = self.tz
if self.last_ping: if self.last_ping:
result["last_ping"] = self.last_ping.replace(microsecond=0).isoformat()
result["next_ping"] = (self.last_ping + self.timeout).replace(microsecond=0).isoformat()
result["last_ping"] = isostring(self.last_ping)
result["next_ping"] = isostring(self.get_grace_start())
else: else:
result["last_ping"] = None result["last_ping"] = None
result["next_ping"] = None result["next_ping"] = None


+ 13
- 0
hc/api/tests/test_check_model.py View File

@ -74,3 +74,16 @@ class CheckModelTestCase(TestCase):
# 11:30am # 11:30am
now = dt + timedelta(days=1, minutes=90) now = dt + timedelta(days=1, minutes=90)
self.assertEqual(check.get_status(now), "down") self.assertEqual(check.get_status(now), "down")
def test_next_ping_with_cron_syntax(self):
dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
# Expect ping every round hour
check = Check()
check.kind = "cron"
check.schedule = "0 * * * *"
check.status = "up"
check.last_ping = dt
d = check.to_dict()
self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")

Loading…
Cancel
Save