0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-07 22:25:35 +00:00

Increase the precision in hc.lib.date.format_approx_duration

Format durations in one of the following forms:

* "{x} days {y} h"
* "{y} h {z} min"
* "{z} min {w} sec"
This commit is contained in:
Pēteris Caune 2023-10-02 12:50:59 +03:00
parent d40ce1eaac
commit 5d99c544ee
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
4 changed files with 43 additions and 12 deletions

View file

@ -16,6 +16,7 @@ version is 3.10.
- Improve ntfy notifications (include tags, period, last ping type etc.)
- Add an "Account closed." confirmation message after closing an account
- Add monthly uptime percentage display in Check Details page (#773)
- Increase the precision of calculated downtime duration in check's details and reports
### Bug Fixes
- Fix "senddeletionnotices" to recognize "Supporter" subscriptions

View file

@ -145,7 +145,7 @@ class DetailsTestCase(BaseTestCase):
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")
self.assertContains(r, "1 downtime, 1 h 0 min total")
self.assertContains(r, "99.8% uptime")
@patch("hc.lib.date.now")
@ -172,7 +172,7 @@ class DetailsTestCase(BaseTestCase):
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url)
self.assertContains(r, "1 downtime, 2 hours total")
self.assertContains(r, "1 downtime, 2 h 0 min total")
self.assertContains(r, "99.7% uptime")
@patch("hc.lib.date.now")

View file

@ -62,16 +62,22 @@ def format_hms(duration: timedelta) -> str:
def format_approx_duration(duration: timedelta) -> str:
v = int(duration.total_seconds())
for unit in (DAY, HOUR, MINUTE, SECOND):
if v >= unit.nsecs:
vv = v // unit.nsecs
if vv == 1:
return f"1 {unit.name}"
else:
return f"{vv} {unit.plural}"
total_seconds = int(duration.total_seconds())
return ""
mins, secs = divmod(total_seconds, 60)
hours, mins = divmod(mins, 60)
days, hours = divmod(hours, 24)
if days == 1:
return f"1 day {hours} h"
if days:
return f"{days} days {hours} h"
if hours:
return f"{hours} h {mins} min"
return f"{mins} min {secs} sec"
def month_boundaries(months: int, tzstr: str) -> list[datetime]:

View file

@ -6,7 +6,13 @@ from datetime import timezone
from unittest import TestCase
from unittest.mock import Mock, patch
from hc.lib.date import format_hms, month_boundaries, seconds_in_month, week_boundaries
from hc.lib.date import (
format_approx_duration,
format_hms,
month_boundaries,
seconds_in_month,
week_boundaries,
)
CURRENT_TIME = datetime(2020, 1, 15, tzinfo=timezone.utc)
MOCK_NOW = Mock(return_value=CURRENT_TIME)
@ -38,6 +44,24 @@ class DateFormattingTestCase(TestCase):
self.assertEqual(s, "1 h 0 min 0 sec")
class ApproxFormattingTestCase(TestCase):
def test_days_work(self) -> None:
s = format_approx_duration(td(days=3, hours=6, minutes=12, seconds=24))
self.assertEqual(s, "3 days 6 h")
def test_one_day_works(self) -> None:
s = format_approx_duration(td(days=1, hours=6, minutes=12, seconds=24))
self.assertEqual(s, "1 day 6 h")
def test_hours_work(self) -> None:
s = format_approx_duration(td(hours=6, minutes=12, seconds=24))
self.assertEqual(s, "6 h 12 min")
def test_minutes_work(self) -> None:
s = format_approx_duration(td(minutes=12, seconds=24))
self.assertEqual(s, "12 min 24 sec")
@patch("hc.lib.date.now", MOCK_NOW)
class MonthBoundaryTestCase(TestCase):
def test_utc_works(self) -> None: