diff --git a/CHANGELOG.md b/CHANGELOG.md index b3685037..4fae1271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. +## v1.20.0 - Unreleased + +## Bug Fixes +- Fix downtime summary to handle months when the check didn't exist yet (#472) + ## v1.19.0 - 2021-02-03 ## Improvements diff --git a/hc/api/models.py b/hc/api/models.py index 46d3af5d..4821eb3d 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -304,7 +304,7 @@ class Check(models.Model): ping.exitstatus = exitstatus ping.save() - def downtimes(self, months=3): + def downtimes(self, months=2): """ Calculate the number of downtimes and downtime minutes per month. Returns a list of (datetime, downtime_in_secs, number_of_outages) tuples. @@ -340,6 +340,12 @@ class Check(models.Model): if prev_status != "---": status = prev_status + # Set counters to None for months when the check didn't exist yet + for ym in totals: + if ym < monthkey(self.created): + totals[ym][1] = None + totals[ym][2] = None + return sorted(totals.values()) diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py index c1a35512..acbfb21b 100644 --- a/hc/api/tests/test_check_model.py +++ b/hc/api/tests/test_check_model.py @@ -1,10 +1,13 @@ from datetime import datetime, timedelta -from unittest.mock import patch +from unittest.mock import Mock, patch from django.utils import timezone from hc.api.models import Check, Flip from hc.test import BaseTestCase +CURRENT_TIME = datetime(2020, 1, 15, tzinfo=timezone.utc) +MOCK_NOW = Mock(return_value=CURRENT_TIME) + class CheckModelTestCase(BaseTestCase): def test_it_strips_tags(self): @@ -162,29 +165,45 @@ class CheckModelTestCase(BaseTestCase): d = check.to_dict() self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00") + @patch("hc.api.models.timezone.now", MOCK_NOW) def test_downtimes_handles_no_flips(self): - check = Check.objects.create(project=self.project) - r = check.downtimes(10) - self.assertEqual(len(r), 10) - for dt, downtime, outages in r: - self.assertEqual(downtime.total_seconds(), 0) - self.assertEqual(outages, 0) + check = Check(project=self.project) + check.created = datetime(2019, 1, 1, tzinfo=timezone.utc) + nov, dec, jan = check.downtimes(3) + + # Nov. 2019 + self.assertEqual(nov[0].strftime("%m-%Y"), "11-2019") + self.assertEqual(nov[1], timedelta()) + self.assertEqual(nov[2], 0) + + # Dec. 2019 + self.assertEqual(dec[0].strftime("%m-%Y"), "12-2019") + self.assertEqual(dec[1], timedelta()) + self.assertEqual(dec[2], 0) + + # Jan. 2020 + self.assertEqual(jan[0].strftime("%m-%Y"), "01-2020") + self.assertEqual(jan[1], timedelta()) + self.assertEqual(jan[2], 0) + + @patch("hc.api.models.timezone.now", MOCK_NOW) def test_downtimes_handles_currently_down_check(self): - check = Check.objects.create(project=self.project, status="down") + check = Check(project=self.project, status="down") + check.created = datetime(2019, 1, 1, tzinfo=timezone.utc) r = check.downtimes(10) self.assertEqual(len(r), 10) for dt, downtime, outages in r: self.assertEqual(outages, 1) - @patch("hc.api.models.timezone.now") - def test_downtimes_handles_flip_one_day_ago(self, mock_now): - mock_now.return_value = datetime(2019, 7, 19, tzinfo=timezone.utc) - + @patch("hc.api.models.timezone.now", MOCK_NOW) + def test_downtimes_handles_flip_one_day_ago(self): check = Check.objects.create(project=self.project, status="down") + check.created = datetime(2019, 1, 1, tzinfo=timezone.utc) + flip = Flip(owner=check) - flip.created = datetime(2019, 7, 18, tzinfo=timezone.utc) + flip.created = datetime(2020, 1, 14, tzinfo=timezone.utc) flip.old_status = "up" flip.new_status = "down" flip.save() @@ -192,20 +211,20 @@ class CheckModelTestCase(BaseTestCase): r = check.downtimes(10) self.assertEqual(len(r), 10) for dt, downtime, outages in r: - if dt.month == 7: + if dt.month == 1: self.assertEqual(downtime.total_seconds(), 86400) self.assertEqual(outages, 1) else: self.assertEqual(downtime.total_seconds(), 0) self.assertEqual(outages, 0) - @patch("hc.api.models.timezone.now") - def test_downtimes_handles_flip_two_months_ago(self, mock_now): - mock_now.return_value = datetime(2019, 7, 19, tzinfo=timezone.utc) - + @patch("hc.api.models.timezone.now", MOCK_NOW) + def test_downtimes_handles_flip_two_months_ago(self): check = Check.objects.create(project=self.project, status="down") + check.created = datetime(2019, 1, 1, tzinfo=timezone.utc) + flip = Flip(owner=check) - flip.created = datetime(2019, 5, 19, tzinfo=timezone.utc) + flip.created = datetime(2019, 11, 15, tzinfo=timezone.utc) flip.old_status = "up" flip.new_status = "down" flip.save() @@ -213,13 +232,32 @@ class CheckModelTestCase(BaseTestCase): r = check.downtimes(10) self.assertEqual(len(r), 10) for dt, downtime, outages in r: - if dt.month == 7: + if dt.month == 11: self.assertEqual(outages, 1) - elif dt.month == 6: - self.assertEqual(downtime.total_seconds(), 30 * 86400) + elif dt.month == 12: + self.assertEqual(downtime.total_seconds(), 31 * 86400) self.assertEqual(outages, 1) - elif dt.month == 5: + elif dt.month == 1: self.assertEqual(outages, 1) else: self.assertEqual(downtime.total_seconds(), 0) self.assertEqual(outages, 0) + + @patch("hc.api.models.timezone.now", MOCK_NOW) + def test_downtimes_handles_months_when_check_did_not_exist(self): + check = Check(project=self.project) + check.created = datetime(2020, 1, 1, tzinfo=timezone.utc) + + nov, dec, jan = check.downtimes(3) + + # Nov. 2019 + self.assertIsNone(nov[1]) + self.assertIsNone(nov[2]) + + # Dec. 2019 + self.assertIsNone(dec[1]) + self.assertIsNone(dec[2]) + + # Jan. 2020 + self.assertEqual(jan[1], timedelta()) + self.assertEqual(jan[2], 0) diff --git a/hc/front/tests/test_details.py b/hc/front/tests/test_details.py index 7202acd7..0ebc8b88 100644 --- a/hc/front/tests/test_details.py +++ b/hc/front/tests/test_details.py @@ -1,6 +1,8 @@ -from datetime import timedelta as td +from datetime import datetime, timedelta as td +from unittest.mock import patch -from hc.api.models import Check, Ping +from django.utils import timezone +from hc.api.models import Flip, Check, Ping from hc.test import BaseTestCase @@ -105,3 +107,49 @@ class DetailsTestCase(BaseTestCase): r = self.client.get(self.url) self.assertContains(r, f"* * * * * /your/command.sh") self.assertContains(r, 'FIXME: replace "* * * * *"') + + @patch("hc.lib.date.timezone.now") + def test_it_calculates_downtime_summary(self, mock_now): + mock_now.return_value = datetime(2020, 2, 1, tzinfo=timezone.utc) + + self.check.created = datetime(2019, 1, 1, 0, 0, 0, tzinfo=timezone.utc) + self.check.save() + + # going down on Jan 15, at 12:00 + f1 = Flip(owner=self.check) + f1.created = datetime(2020, 1, 15, 12, 0, 0, tzinfo=timezone.utc) + f1.old_status = "up" + f1.new_status = "down" + f1.save() + + # back up on Jan 15, at 13:00 + f2 = Flip(owner=self.check) + f2.created = datetime(2020, 1, 15, 13, 0, 0, tzinfo=timezone.utc) + f2.old_status = "down" + f2.new_status = "up" + f2.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "Feb. 2020") + self.assertContains(r, "Jan. 2020") + self.assertContains(r, "Dec. 2019") + + # The summary for Jan. 2020 should be "1 downtime, 1 hour total" + self.assertContains(r, "1 downtime, 1 hour total", html=True) + + @patch("hc.lib.date.timezone.now") + def test_it_handles_months_when_check_did_not_exist(self, mock_now): + mock_now.return_value = datetime(2020, 2, 1, tzinfo=timezone.utc) + + self.check.created = datetime(2020, 1, 10, 0, 0, 0, tzinfo=timezone.utc) + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "Feb. 2020") + self.assertContains(r, "Jan. 2020") + self.assertContains(r, "Dec. 2019") + + # The summary for Dec. 2019 should be "–" + self.assertContains(r, "–", html=True) diff --git a/hc/lib/date.py b/hc/lib/date.py index d95f6bc8..92620b26 100644 --- a/hc/lib/date.py +++ b/hc/lib/date.py @@ -71,7 +71,7 @@ def format_approx_duration(td): return "" -def month_boundaries(months=3): +def month_boundaries(months=2): result = [] now = timezone.now() diff --git a/templates/emails/summary-downtimes-html.html b/templates/emails/summary-downtimes-html.html index 1755dd74..ae79d10c 100644 --- a/templates/emails/summary-downtimes-html.html +++ b/templates/emails/summary-downtimes-html.html @@ -7,11 +7,9 @@ {{ group.grouper|mangle_link }} {% for dt in month_boundaries %} - {% if not forloop.last %} {{ dt|date:"N Y"}} - {% endif %} {% endfor %} {% for check in group.list|sortchecks:sort %} @@ -63,7 +61,6 @@ {% endif %} {% for boundary, seconds, count in check.downtimes %} - {% if not forloop.last %} {% if count %} {{ count }} downtime{{ count|pluralize }}, @@ -72,10 +69,13 @@ {% else %} - All good! + {% if count is None %} + {% comment %} The check didn't exist yet {% endcomment %} + {% else %} + All good! + {% endif %} {% endif %} - {% endif %} {% endfor %} {% endfor %} diff --git a/templates/front/details_downtimes.html b/templates/front/details_downtimes.html index bc614007..a408f296 100644 --- a/templates/front/details_downtimes.html +++ b/templates/front/details_downtimes.html @@ -1,12 +1,14 @@ {% load hc_extras %} - {% for boundary, seconds, count in downtimes reversed %} + {% for boundary, down_timedelta, count in downtimes reversed %}
{{ boundary|date:"N Y"}} {% if count %} {{ count }} downtime{{ count|pluralize }}, - {{ seconds|hc_approx_duration }} total + {{ down_timedelta|hc_approx_duration }} total + {% elif count is None %} + – {% else %} All good! {% endif %}