0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-03-15 20:54:53 +00:00

Fix time interval formatting in check details - downtime summary

In hc.lib.date.format_approx_duration, we were calling
timedeltaobj.total_seconds() and basing all calculations off that.
This method returns float, so the final result was "2.0 hours" or
"3.0 days" and similar. We now convert it to int, to get "2 hours",
"3 days" etc.
This commit is contained in:
Pēteris Caune 2023-09-26 10:30:34 +03:00
parent 507fd840d8
commit e48d361331
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
3 changed files with 28 additions and 1 deletions

View file

@ -20,6 +20,7 @@ version is 3.10.
- Fix "senddeletionnotices" to recognize "Supporter" subscriptions
- Fix "createsuperuser" to reject already registered email addresses (#880)
- Fix hc.accounts.views.check_token to handle non-UUID usernames (#882)
- Fix time interval formatting in Check Details page, downtime summary table
## v2.10 - 2023-07-02

View file

@ -147,6 +147,32 @@ class DetailsTestCase(BaseTestCase):
# 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.now")
def test_it_downtime_summary_handles_plural(self, mock_now: Mock) -> None:
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 2 hours later
f2 = Flip(owner=self.check)
f2.created = datetime(2020, 1, 15, 14, 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, "1 downtime, 2 hours total", html=True)
@patch("hc.lib.date.now")
def test_downtime_summary_handles_positive_utc_offset(self, mock_now: Mock) -> None:
mock_now.return_value = datetime(2020, 2, 1, tzinfo=timezone.utc)

View file

@ -62,7 +62,7 @@ def format_hms(duration: timedelta) -> str:
def format_approx_duration(duration: timedelta) -> str:
v = duration.total_seconds()
v = int(duration.total_seconds())
for unit in (DAY, HOUR, MINUTE, SECOND):
if v >= unit.nsecs:
vv = v // unit.nsecs