From 77033760f9ebe15199569110986b4146117991e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 17 Jan 2020 14:30:32 +0200 Subject: [PATCH] Make sure Check.last_ping and Ping.created timestamps match exactly --- CHANGELOG.md | 1 + hc/api/migrations/0069_auto_20200117_1227.py | 19 +++++++++++++++++++ hc/api/models.py | 9 ++++++--- hc/api/tests/test_ping.py | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 hc/api/migrations/0069_auto_20200117_1227.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 49185ae2..38b941d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ### Bug Fixes - Increase the allowable length of Matrix room alias to 100 (#320) +- Make sure Check.last_ping and Ping.created timestamps match exactly ## v1.12.0 - 2020-01-02 diff --git a/hc/api/migrations/0069_auto_20200117_1227.py b/hc/api/migrations/0069_auto_20200117_1227.py new file mode 100644 index 00000000..b501a813 --- /dev/null +++ b/hc/api/migrations/0069_auto_20200117_1227.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.1 on 2020-01-17 12:27 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0068_auto_20200117_1023'), + ] + + operations = [ + migrations.AlterField( + model_name='ping', + name='created', + field=models.DateTimeField(default=django.utils.timezone.now), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index 4682690f..a5ee60ad 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -236,13 +236,15 @@ class Check(models.Model): return result def ping(self, remote_addr, scheme, method, ua, body, action): + now = timezone.now() + if action == "start": - self.last_start = timezone.now() + self.last_start = now # Don't update "last_ping" field. elif action == "ign": pass else: - self.last_ping = timezone.now() + self.last_ping = now if self.last_start: self.last_duration = self.last_ping - self.last_start self.last_start = None @@ -267,6 +269,7 @@ class Check(models.Model): ping = Ping(owner=self) ping.n = self.n_pings + ping.created = now if action in ("start", "fail", "ign"): ping.kind = action @@ -321,7 +324,7 @@ class Ping(models.Model): id = models.BigAutoField(primary_key=True) n = models.IntegerField(null=True) owner = models.ForeignKey(Check, models.CASCADE) - created = models.DateTimeField(auto_now_add=True) + created = models.DateTimeField(default=timezone.now) kind = models.CharField(max_length=6, blank=True, null=True) scheme = models.CharField(max_length=10, default="http") remote_addr = models.GenericIPAddressField(blank=True, null=True) diff --git a/hc/api/tests/test_ping.py b/hc/api/tests/test_ping.py index 08b30715..45349607 100644 --- a/hc/api/tests/test_ping.py +++ b/hc/api/tests/test_ping.py @@ -25,6 +25,7 @@ class PingTestCase(BaseTestCase): ping = Ping.objects.latest("id") self.assertEqual(ping.scheme, "http") self.assertEqual(ping.kind, None) + self.assertEqual(ping.created, self.check.last_ping) def test_it_changes_status_of_paused_check(self): self.check.status = "paused"