From fd367b42da68d4a03803d6d1cf9d7266645dc157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 25 May 2018 20:20:58 +0300 Subject: [PATCH] Always show failed checks first. Fixes #173 --- hc/front/templatetags/hc_extras.py | 10 ++++++++++ hc/front/views.py | 2 ++ static/js/checks.js | 8 ++++++++ templates/front/my_checks_desktop.html | 26 +++++++++++++++++++------- templates/front/my_checks_mobile.html | 6 ++++-- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index b312d803..e1da352d 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -48,8 +48,14 @@ def last_ping_key(check): return check.last_ping.isoformat() if check.last_ping else "9999" +def not_down_key(check): + return check.get_status() != "down" + + @register.filter def sortchecks(checks, key): + """Sort the list of checks in-place by given key, then by status=down. """ + if key == "created": checks.sort(key=lambda check: check.created) elif key.endswith("name"): @@ -57,6 +63,10 @@ def sortchecks(checks, key): elif key.endswith("last_ping"): checks.sort(key=last_ping_key, reverse=key.startswith("-")) + # Move failed checks to the beginning. Sorts in python are stable + # so this does not mess up the previous sort. + checks.sort(key=not_down_key) + return checks diff --git a/hc/front/views.py b/hc/front/views.py index 8c5976ae..27204d11 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -24,6 +24,7 @@ from hc.front.forms import (AddWebhookForm, NameTagsForm, TimeoutForm, AddUrlForm, AddEmailForm, AddOpsGenieForm, CronForm, AddSmsForm) from hc.front.schemas import telegram_callback +from hc.front.templatetags.hc_extras import sortchecks from hc.lib import jsonschema from pytz import all_timezones from pytz.exceptions import UnknownTimeZoneError @@ -58,6 +59,7 @@ def my_checks(request): request.profile.save() checks = list(Check.objects.filter(user=request.team.user)) + sortchecks(checks, request.profile.sort) pairs = list(_tags_statuses(checks).items()) pairs.sort(key=lambda pair: pair[0].lower()) diff --git a/static/js/checks.js b/static/js/checks.js index f790b6e9..01c7dd50 100644 --- a/static/js/checks.js +++ b/static/js/checks.js @@ -192,6 +192,7 @@ $(function () { return false; }); + // Filtering by tags $("#my-checks-tags button").click(function() { // .active has not been updated yet by bootstrap code, // so cannot use it @@ -238,12 +239,19 @@ $(function () { }); $('[data-toggle="tooltip"]').tooltip({ + html: true, title: function() { var cssClasses = this.getAttribute("class"); if (cssClasses.indexOf("icon-new") > -1) return "New. Has never received a ping."; if (cssClasses.indexOf("icon-paused") > -1) return "Monitoring paused. Ping to resume."; + + if (cssClasses.indexOf("sort-name") > -1) + return "Sort by name
(but failed always first)"; + + if (cssClasses.indexOf("sort-last-ping") > -1) + return "Sort by last ping
(but failed always first)"; } }); diff --git a/templates/front/my_checks_desktop.html b/templates/front/my_checks_desktop.html index dc7f0c77..b4a6d0fc 100644 --- a/templates/front/my_checks_desktop.html +++ b/templates/front/my_checks_desktop.html @@ -4,11 +4,17 @@ {% if sort == "name" %} - Name + + Name + {% elif sort == "-name" %} - Name + + Name + {% else %} - Name + + Name + {% endif %} Ping URL @@ -18,16 +24,22 @@ {% if sort == "last_ping" %} - Last Ping + + Last Ping + {% elif sort == "-last_ping" %} - Last Ping + + Last Ping + {% else %} - Last Ping + + Last Ping + {% endif %} - {% for check in checks|sortchecks:sort %} + {% for check in checks %} {% if check.in_grace_period %} diff --git a/templates/front/my_checks_mobile.html b/templates/front/my_checks_mobile.html index 49417dfa..41e3816c 100644 --- a/templates/front/my_checks_mobile.html +++ b/templates/front/my_checks_mobile.html @@ -1,7 +1,7 @@ {% load hc_extras humanize %}