From 954ca4576bacdb00c4ae57fee84759c4df35be29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 1 Mar 2019 14:39:44 +0200 Subject: [PATCH] Improved logic for displaying job execution times in log. Fixes #219 --- CHANGELOG.md | 1 + hc/front/templatetags/hc_extras.py | 6 +++--- hc/front/views.py | 7 ++++++- hc/lib/date.py | 9 +++++++-- hc/lib/tests/test_date.py | 17 ++++++++++++----- templates/front/details_events.html | 2 +- templates/front/log.html | 2 +- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4f0e04e..d29085f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Improvements - Add the "desc" field (check's description) to API responses - Add maxlength attribute to HTML input=text elements +- Improved logic for displaying job execution times in log (#219) ### Bug Fixes - Fix refreshing of the checks page filtered by tags (#221) diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index c25182f5..78a1010c 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -5,7 +5,7 @@ from django.conf import settings from django.utils.html import escape from django.utils.safestring import mark_safe -from hc.lib.date import format_duration, format_mins_secs +from hc.lib.date import format_duration, format_hms register = template.Library() @@ -16,8 +16,8 @@ def hc_duration(td): @register.filter -def mins_secs(td): - return format_mins_secs(td) +def hms(td): + return format_hms(td) @register.simple_tag diff --git a/hc/front/views.py b/hc/front/views.py index 4737e6ce..8281d887 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -39,6 +39,7 @@ STATUS_TEXT_TMPL = get_template("front/log_status_text.html") LAST_PING_TMPL = get_template("front/last_ping_cell.html") EVENTS_TMPL = get_template("front/details_events.html") ONE_HOUR = td(hours=1) +TWELVE_HOURS = td(hours=12) def _tags_statuses(checks): @@ -405,6 +406,10 @@ def remove_check(request, code): def _get_events(check, limit): + # max time between start and ping where we will consider + # the both events related. + max_delta = min(ONE_HOUR + check.grace, TWELVE_HOURS) + pings = Ping.objects.filter(owner=check).order_by("-id")[:limit] pings = list(pings) @@ -412,7 +417,7 @@ def _get_events(check, limit): for ping in pings: if ping.kind == "start" and prev and prev.kind != "start": delta = prev.created - ping.created - if delta < ONE_HOUR: + if delta < max_delta: setattr(prev, "delta", delta) prev = ping diff --git a/hc/lib/date.py b/hc/lib/date.py index 528fbe7b..a358a167 100644 --- a/hc/lib/date.py +++ b/hc/lib/date.py @@ -29,12 +29,17 @@ def format_duration(td): return " ".join(result) -def format_mins_secs(td): +def format_hms(td): total_seconds = int(td.total_seconds()) result = [] mins, secs = divmod(total_seconds, 60) - if mins: + h, mins = divmod(mins, 60) + + if h: + result.append("%d h" % h) + + if h or mins: result.append("%d min" % mins) result.append("%s sec" % secs) diff --git a/hc/lib/tests/test_date.py b/hc/lib/tests/test_date.py index 8bc4827a..26dbe9dd 100644 --- a/hc/lib/tests/test_date.py +++ b/hc/lib/tests/test_date.py @@ -1,20 +1,27 @@ from datetime import timedelta as td from django.test import TestCase -from hc.lib.date import format_mins_secs +from hc.lib.date import format_hms class DateFormattingTestCase(TestCase): def test_mins_secs_work(self): - s = format_mins_secs(td(seconds=0)) + s = format_hms(td(seconds=0)) self.assertEqual(s, "0 sec") - s = format_mins_secs(td(seconds=1)) + s = format_hms(td(seconds=1)) self.assertEqual(s, "1 sec") - s = format_mins_secs(td(seconds=61)) + s = format_hms(td(seconds=61)) self.assertEqual(s, "1 min 1 sec") - s = format_mins_secs(td(seconds=62)) + s = format_hms(td(seconds=62)) self.assertEqual(s, "1 min 2 sec") + + def test_hours_work(self): + s = format_hms(td(seconds=62 + 60 * 60)) + self.assertEqual(s, "1 h 1 min 2 sec") + + s = format_hms(td(seconds=60 * 60)) + self.assertEqual(s, "1 h 0 min 0 sec") diff --git a/templates/front/details_events.html b/templates/front/details_events.html index 80da98a1..22f300b4 100644 --- a/templates/front/details_events.html +++ b/templates/front/details_events.html @@ -24,7 +24,7 @@ {% if event.delta %}
- {{ event.delta|mins_secs }} + {{ event.delta|hms }}
{% endif %} diff --git a/templates/front/log.html b/templates/front/log.html index c444569b..45807b7a 100644 --- a/templates/front/log.html +++ b/templates/front/log.html @@ -58,7 +58,7 @@ {% if event.delta %}
- {{ event.delta|mins_secs }} + {{ event.delta|hms }}
{% endif %}