Usability tweaks in api_check_changelist admin view.

This commit is contained in:
Pēteris Caune 2016-12-21 12:50:06 +02:00
parent b22b0a44e2
commit 56168b17d0
4 changed files with 47 additions and 31 deletions

View File

@ -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()

View File

@ -1,39 +1,14 @@
from django import template
from django.conf import settings
from hc.lib.date import format_duration
register = template.Library()
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)
@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

29
hc/lib/date.py Normal file
View File

@ -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)

View File

@ -1,3 +1,4 @@
.field-code {
font-family: monospace;
font-size: 80%;
}