Browse Source

Rename "Check.get_alert_after" to a now more fitting "Check.going_down_after"

pull/211/head
Pēteris Caune 6 years ago
parent
commit
5f9ebb178c
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
7 changed files with 53 additions and 45 deletions
  1. +4
    -5
      hc/api/management/commands/sendalerts.py
  2. +11
    -6
      hc/api/models.py
  3. +7
    -7
      hc/api/tests/test_check_going_down_after.py
  4. +2
    -1
      hc/api/tests/test_ping.py
  5. +23
    -21
      hc/api/tests/test_sendalerts.py
  6. +4
    -3
      hc/front/tests/test_update_timeout.py
  7. +2
    -2
      hc/front/views.py

+ 4
- 5
hc/api/management/commands/sendalerts.py View File

@ -96,23 +96,22 @@ class Command(BaseCommand):
if not check.is_down(): if not check.is_down():
# It is not down yet. Update alert_after # It is not down yet. Update alert_after
q.update(alert_after=check.get_alert_after())
q.update(alert_after=check.going_down_after())
return True return True
# Atomically update status # Atomically update status
num_updated = q.update(status="down")
flip_time = check.going_down_after()
num_updated = q.update(alert_after=None, status="down")
if num_updated != 1: if num_updated != 1:
# Nothing got updated: another worker process got there first. # Nothing got updated: another worker process got there first.
return True return True
flip = Flip(owner=check) flip = Flip(owner=check)
flip.created = check.get_alert_after()
flip.created = flip_time
flip.old_status = old_status flip.old_status = old_status
flip.new_status = "down" flip.new_status = "down"
flip.save() flip.save()
check.status = "down"
check.save()
return True return True
def handle(self, use_threads=True, loop=True, *args, **options): def handle(self, use_threads=True, loop=True, *args, **options):


+ 11
- 6
hc/api/models.py View File

@ -142,8 +142,13 @@ class Check(models.Model):
if result != NEVER: if result != NEVER:
return result return result
def get_alert_after(self):
""" Return the datetime when check potentially goes down. """
def going_down_after(self):
""" Return the datetime when the check goes down.
If the check is new or paused, and not currently running, return None.
If the check is already down, also return None.
"""
grace_start = self.get_grace_start() grace_start = self.get_grace_start()
if grace_start is not None: if grace_start is not None:
@ -155,11 +160,11 @@ class Check(models.Model):
if self.status == "down": if self.status == "down":
return True return True
alert_after = self.get_alert_after()
if alert_after is None:
down_after = self.going_down_after()
if down_after is None:
return False return False
return timezone.now() >= alert_after
return timezone.now() >= down_after
def get_status(self, now=None): def get_status(self, now=None):
""" Return current status for display. """ """ Return current status for display. """
@ -246,7 +251,7 @@ class Check(models.Model):
self.status = new_status self.status = new_status
self.alert_after = self.get_alert_after()
self.alert_after = self.going_down_after()
self.n_pings = models.F("n_pings") + 1 self.n_pings = models.F("n_pings") + 1
self.has_confirmation_link = "confirm" in str(body).lower() self.has_confirmation_link = "confirm" in str(body).lower()
self.save() self.save()


hc/api/tests/test_check_alert_after.py → hc/api/tests/test_check_going_down_after.py View File


+ 2
- 1
hc/api/tests/test_ping.py View File

@ -17,7 +17,8 @@ class PingTestCase(TestCase):
self.check.refresh_from_db() self.check.refresh_from_db()
self.assertEqual(self.check.status, "up") self.assertEqual(self.check.status, "up")
self.assertEqual(self.check.alert_after, self.check.get_alert_after())
expected_aa = self.check.last_ping + td(days=1, hours=1)
self.assertEqual(self.check.alert_after, expected_aa)
ping = Ping.objects.latest("id") ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "http") self.assertEqual(ping.scheme, "http")


+ 23
- 21
hc/api/tests/test_sendalerts.py View File

@ -1,4 +1,4 @@
from datetime import timedelta
from datetime import timedelta as td
from io import StringIO from io import StringIO
from mock import Mock, patch from mock import Mock, patch
@ -14,18 +14,20 @@ class SendAlertsTestCase(BaseTestCase):
def test_it_handles_grace_period(self): def test_it_handles_grace_period(self):
check = Check(user=self.alice, status="up") check = Check(user=self.alice, status="up")
# 1 day 30 minutes after ping the check is in grace period: # 1 day 30 minutes after ping the check is in grace period:
check.last_ping = now() - timedelta(days=1, minutes=30)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=1, minutes=30)
check.alert_after = check.last_ping + td(days=1, hours=1)
check.save() check.save()
Command().handle_going_down() Command().handle_going_down()
check.refresh_from_db()
self.assertEqual(check.status, "up")
self.assertEqual(Flip.objects.count(), 0) self.assertEqual(Flip.objects.count(), 0)
def test_it_creates_a_flip_when_check_goes_down(self): def test_it_creates_a_flip_when_check_goes_down(self):
check = Check(user=self.alice, status="up") check = Check(user=self.alice, status="up")
check.last_ping = now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=2)
check.alert_after = check.last_ping + td(days=1, hours=1)
check.save() check.save()
result = Command().handle_going_down() result = Command().handle_going_down()
@ -36,17 +38,19 @@ class SendAlertsTestCase(BaseTestCase):
# It should create a flip object # It should create a flip object
flip = Flip.objects.get() flip = Flip.objects.get()
self.assertEqual(flip.owner_id, check.id) self.assertEqual(flip.owner_id, check.id)
self.assertEqual(flip.created, check.alert_after)
self.assertEqual(flip.new_status, "down") self.assertEqual(flip.new_status, "down")
# It should change stored status to "down"
# It should change stored status to "down", and clear out alert_after
check.refresh_from_db() check.refresh_from_db()
self.assertEqual(check.status, "down") self.assertEqual(check.status, "down")
self.assertEqual(check.alert_after, None)
@patch("hc.api.management.commands.sendalerts.notify_on_thread") @patch("hc.api.management.commands.sendalerts.notify_on_thread")
def test_it_processes_flip(self, mock_notify): def test_it_processes_flip(self, mock_notify):
check = Check(user=self.alice, status="up") check = Check(user=self.alice, status="up")
check.last_ping = now() check.last_ping = now()
check.alert_after = check.get_alert_after()
check.alert_after = check.last_ping + td(days=1, hours=1)
check.save() check.save()
flip = Flip(owner=check, created=check.last_ping) flip = Flip(owner=check, created=check.last_ping)
@ -69,7 +73,7 @@ class SendAlertsTestCase(BaseTestCase):
@patch("hc.api.management.commands.sendalerts.notify_on_thread") @patch("hc.api.management.commands.sendalerts.notify_on_thread")
def test_it_updates_alert_after(self, mock_notify): def test_it_updates_alert_after(self, mock_notify):
check = Check(user=self.alice, status="up") check = Check(user=self.alice, status="up")
check.last_ping = now() - timedelta(hours=1)
check.last_ping = now() - td(hours=1)
check.alert_after = check.last_ping check.alert_after = check.last_ping
check.save() check.save()
@ -79,8 +83,9 @@ class SendAlertsTestCase(BaseTestCase):
self.assertTrue(result) self.assertTrue(result)
# alert_after should have been increased # alert_after should have been increased
expected_aa = check.last_ping + td(days=1, hours=1)
check.refresh_from_db() check.refresh_from_db()
self.assertTrue(check.alert_after > check.last_ping)
self.assertEqual(check.alert_after, expected_aa)
# a flip should have not been created # a flip should have not been created
self.assertEqual(Flip.objects.count(), 0) self.assertEqual(Flip.objects.count(), 0)
@ -88,8 +93,8 @@ class SendAlertsTestCase(BaseTestCase):
@patch("hc.api.management.commands.sendalerts.notify") @patch("hc.api.management.commands.sendalerts.notify")
def test_it_works_synchronously(self, mock_notify): def test_it_works_synchronously(self, mock_notify):
check = Check(user=self.alice, status="up") check = Check(user=self.alice, status="up")
check.last_ping = now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=2)
check.alert_after = check.last_ping + td(days=1, hours=1)
check.save() check.save()
call_command("sendalerts", loop=False, use_threads=False, call_command("sendalerts", loop=False, use_threads=False,
@ -99,12 +104,11 @@ class SendAlertsTestCase(BaseTestCase):
self.assertTrue(mock_notify.called) self.assertTrue(mock_notify.called)
def test_it_updates_owners_next_nag_date(self): def test_it_updates_owners_next_nag_date(self):
self.profile.nag_period = timedelta(hours=1)
self.profile.nag_period = td(hours=1)
self.profile.save() self.profile.save()
check = Check(user=self.alice, status="down") check = Check(user=self.alice, status="down")
check.last_ping = now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=2)
check.save() check.save()
flip = Flip(owner=check, created=check.last_ping) flip = Flip(owner=check, created=check.last_ping)
@ -118,12 +122,11 @@ class SendAlertsTestCase(BaseTestCase):
self.assertIsNotNone(self.profile.next_nag_date) self.assertIsNotNone(self.profile.next_nag_date)
def test_it_updates_members_next_nag_date(self): def test_it_updates_members_next_nag_date(self):
self.bobs_profile.nag_period = timedelta(hours=1)
self.bobs_profile.nag_period = td(hours=1)
self.bobs_profile.save() self.bobs_profile.save()
check = Check(user=self.alice, status="down") check = Check(user=self.alice, status="down")
check.last_ping = now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=2)
check.save() check.save()
flip = Flip(owner=check, created=check.last_ping) flip = Flip(owner=check, created=check.last_ping)
@ -137,14 +140,13 @@ class SendAlertsTestCase(BaseTestCase):
self.assertIsNotNone(self.bobs_profile.next_nag_date) self.assertIsNotNone(self.bobs_profile.next_nag_date)
def test_it_does_not_touch_already_set_next_nag_dates(self): def test_it_does_not_touch_already_set_next_nag_dates(self):
original_nag_date = now() - timedelta(minutes=30)
self.profile.nag_period = timedelta(hours=1)
original_nag_date = now() - td(minutes=30)
self.profile.nag_period = td(hours=1)
self.profile.next_nag_date = original_nag_date self.profile.next_nag_date = original_nag_date
self.profile.save() self.profile.save()
check = Check(user=self.alice, status="down") check = Check(user=self.alice, status="down")
check.last_ping = now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.last_ping = now() - td(days=2)
check.save() check.save()
flip = Flip(owner=check, created=check.last_ping) flip = Flip(owner=check, created=check.last_ping)


+ 4
- 3
hc/front/tests/test_update_timeout.py View File

@ -1,7 +1,7 @@
from datetime import timedelta as td from datetime import timedelta as td
from django.utils import timezone from django.utils import timezone
from hc.api.models import Flip, Check
from hc.api.models import Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
@ -9,7 +9,7 @@ class UpdateTimeoutTestCase(BaseTestCase):
def setUp(self): def setUp(self):
super(UpdateTimeoutTestCase, self).setUp() super(UpdateTimeoutTestCase, self).setUp()
self.check = Check(user=self.alice)
self.check = Check(user=self.alice, status="up")
self.check.last_ping = timezone.now() self.check.last_ping = timezone.now()
self.check.save() self.check.save()
@ -28,7 +28,8 @@ class UpdateTimeoutTestCase(BaseTestCase):
self.assertEqual(self.check.grace.total_seconds(), 60) self.assertEqual(self.check.grace.total_seconds(), 60)
# alert_after should be updated too # alert_after should be updated too
self.assertEqual(self.check.alert_after, self.check.get_alert_after())
expected_aa = self.check.last_ping + td(seconds=3600 + 60)
self.assertEqual(self.check.alert_after, expected_aa)
def test_it_does_not_update_status(self): def test_it_does_not_update_status(self):
self.check.last_ping = timezone.now() - td(days=2) self.check.last_ping = timezone.now() - td(days=2)


+ 2
- 2
hc/front/views.py View File

@ -18,7 +18,7 @@ from django.utils.crypto import get_random_string
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from hc.api.models import (DEFAULT_GRACE, DEFAULT_TIMEOUT, Channel, Check, from hc.api.models import (DEFAULT_GRACE, DEFAULT_TIMEOUT, Channel, Check,
Flip, Ping, Notification)
Ping, Notification)
from hc.api.transports import Telegram from hc.api.transports import Telegram
from hc.front.forms import (AddWebhookForm, NameTagsForm, from hc.front.forms import (AddWebhookForm, NameTagsForm,
TimeoutForm, AddUrlForm, AddEmailForm, TimeoutForm, AddUrlForm, AddEmailForm,
@ -297,7 +297,7 @@ def update_timeout(request, code):
check.tz = form.cleaned_data["tz"] check.tz = form.cleaned_data["tz"]
check.grace = td(minutes=form.cleaned_data["grace"]) check.grace = td(minutes=form.cleaned_data["grace"])
check.alert_after = check.get_alert_after()
check.alert_after = check.going_down_after()
check.save() check.save()
if "/details/" in request.META.get("HTTP_REFERER", ""): if "/details/" in request.META.get("HTTP_REFERER", ""):


Loading…
Cancel
Save