From 56168b17d0478ed80e5f9ef5a8a10b9330f3e7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 21 Dec 2016 12:50:06 +0200 Subject: [PATCH] Usability tweaks in api_check_changelist admin view. --- hc/api/admin.py | 17 +++++++++++++--- hc/front/templatetags/hc_extras.py | 31 +++--------------------------- hc/lib/date.py | 29 ++++++++++++++++++++++++++++ static/css/admin/checks.css | 1 + 4 files changed, 47 insertions(+), 31 deletions(-) create mode 100644 hc/lib/date.py diff --git a/hc/api/admin.py b/hc/api/admin.py index cd0ad9f6..122e9cd2 100644 --- a/hc/api/admin.py +++ b/hc/api/admin.py @@ -2,6 +2,7 @@ from django.contrib import admin from django.core.paginator import Paginator from django.db import connection from hc.api.models import Channel, Check, Notification, Ping +from hc.lib.date import format_duration class OwnershipListFilter(admin.SimpleListFilter): @@ -28,10 +29,10 @@ class ChecksAdmin(admin.ModelAdmin): } search_fields = ["name", "user__email", "code"] - list_display = ("id", "name_tags", "created", "code", "status", "email", - "last_ping", "n_pings") + list_display = ("id", "name_tags", "created", "code", "timeout_schedule", + "status", "email", "last_ping", "n_pings") list_select_related = ("user", ) - list_filter = ("status", OwnershipListFilter, "last_ping") + list_filter = ("status", OwnershipListFilter, "kind", "last_ping") actions = ["send_alert"] def email(self, obj): @@ -43,6 +44,16 @@ class ChecksAdmin(admin.ModelAdmin): return "%s [%s]" % (obj.name, obj.tags) + def timeout_schedule(self, obj): + if obj.kind == "simple": + return format_duration(obj.timeout) + elif obj.kind == "cron": + return obj.schedule + else: + return "Unknown" + + timeout_schedule.short_description = "Schedule" + def send_alert(self, request, qs): for check in qs: check.send_alert() diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index cc1c073e..3014db9a 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -1,39 +1,14 @@ from django import template from django.conf import settings -register = template.Library() - - -class Unit(object): - def __init__(self, name, nsecs): - self.name = name - self.plural = name + "s" - self.nsecs = nsecs - +from hc.lib.date import format_duration -MINUTE = Unit("minute", 60) -HOUR = Unit("hour", MINUTE.nsecs * 60) -DAY = Unit("day", HOUR.nsecs * 24) -WEEK = Unit("week", DAY.nsecs * 7) +register = template.Library() @register.filter def hc_duration(td): - remaining_seconds = int(td.total_seconds()) - result = [] - - for unit in (WEEK, DAY, HOUR, MINUTE): - if unit == WEEK and remaining_seconds % unit.nsecs != 0: - # Say "8 days" instead of "1 week 1 day" - continue - - v, remaining_seconds = divmod(remaining_seconds, unit.nsecs) - if v == 1: - result.append("1 %s" % unit.name) - elif v > 1: - result.append("%d %s" % (v, unit.plural)) - - return " ".join(result) + return format_duration(td) @register.simple_tag diff --git a/hc/lib/date.py b/hc/lib/date.py new file mode 100644 index 00000000..8bcaf623 --- /dev/null +++ b/hc/lib/date.py @@ -0,0 +1,29 @@ +class Unit(object): + def __init__(self, name, nsecs): + self.name = name + self.plural = name + "s" + self.nsecs = nsecs + + +MINUTE = Unit("minute", 60) +HOUR = Unit("hour", MINUTE.nsecs * 60) +DAY = Unit("day", HOUR.nsecs * 24) +WEEK = Unit("week", DAY.nsecs * 7) + + +def format_duration(td): + remaining_seconds = int(td.total_seconds()) + result = [] + + for unit in (WEEK, DAY, HOUR, MINUTE): + if unit == WEEK and remaining_seconds % unit.nsecs != 0: + # Say "8 days" instead of "1 week 1 day" + continue + + v, remaining_seconds = divmod(remaining_seconds, unit.nsecs) + if v == 1: + result.append("1 %s" % unit.name) + elif v > 1: + result.append("%d %s" % (v, unit.plural)) + + return " ".join(result) diff --git a/static/css/admin/checks.css b/static/css/admin/checks.css index e91d22ff..5fed941b 100644 --- a/static/css/admin/checks.css +++ b/static/css/admin/checks.css @@ -1,3 +1,4 @@ .field-code { font-family: monospace; + font-size: 80%; } \ No newline at end of file