Browse Source

Usability tweaks in api_check_changelist admin view.

pull/109/head
Pēteris Caune 8 years ago
parent
commit
56168b17d0
4 changed files with 47 additions and 31 deletions
  1. +14
    -3
      hc/api/admin.py
  2. +3
    -28
      hc/front/templatetags/hc_extras.py
  3. +29
    -0
      hc/lib/date.py
  4. +1
    -0
      static/css/admin/checks.css

+ 14
- 3
hc/api/admin.py 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()


+ 3
- 28
hc/front/templatetags/hc_extras.py View File

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


+ 29
- 0
hc/lib/date.py 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)

+ 1
- 0
static/css/admin/checks.css View File

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

Loading…
Cancel
Save