diff --git a/hc/api/decorators.py b/hc/api/decorators.py index 366a2636..ec4c7d8a 100644 --- a/hc/api/decorators.py +++ b/hc/api/decorators.py @@ -1,6 +1,5 @@ import json import re -import uuid from functools import wraps from django.contrib.auth.models import User diff --git a/hc/api/models.py b/hc/api/models.py index 8eddb2ae..5ce384f3 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -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 Meta: @@ -167,8 +172,8 @@ class Check(models.Model): result["tz"] = self.tz 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: result["last_ping"] = None result["next_ping"] = None diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py index d34b8f55..b214b90f 100644 --- a/hc/api/tests/test_check_model.py +++ b/hc/api/tests/test_check_model.py @@ -74,3 +74,16 @@ class CheckModelTestCase(TestCase): # 11:30am now = dt + timedelta(days=1, minutes=90) 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")