From 9ae4235c9b89936a1e12239ea02de01cabb89145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 4 Jun 2018 22:31:12 +0300 Subject: [PATCH] "My Checks" page: show the number of failed checks in the page title. --- hc/front/templatetags/hc_extras.py | 8 ++++++++ hc/front/views.py | 18 +++++++++++++----- static/js/checks.js | 5 +++++ templates/front/my_checks.html | 3 +-- .../front/snippets/python_requests_fail.html | 2 +- .../front/snippets/python_requests_fail.txt | 2 +- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index e1da352d..3c2c0fbc 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -73,3 +73,11 @@ def sortchecks(checks, key): @register.filter def trunc(s): return s[:150] + + +@register.filter +def num_down_title(num_down): + if num_down: + return "%d down – %s" % (num_down, settings.SITE_NAME) + else: + return settings.SITE_NAME diff --git a/hc/front/views.py b/hc/front/views.py index 89313c06..3fc212f2 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -24,7 +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.front.templatetags.hc_extras import num_down_title, sortchecks from hc.lib import jsonschema from pytz import all_timezones from pytz.exceptions import UnknownTimeZoneError @@ -35,9 +35,10 @@ VALID_SORT_VALUES = ("name", "-name", "last_ping", "-last_ping", "created") def _tags_statuses(checks): - tags, down, grace = {}, {}, {} + tags, down, grace, num_down = {}, {}, {}, 0 for check in checks: if check.get_status() == "down": + num_down += 1 for tag in check.tags_list(): down[tag] = "down" elif check.in_grace_period(): @@ -49,7 +50,7 @@ def _tags_statuses(checks): tags.update(grace) tags.update(down) - return tags + return tags, num_down @login_required @@ -61,12 +62,14 @@ def my_checks(request): checks = list(Check.objects.filter(user=request.team.user)) sortchecks(checks, request.profile.sort) - pairs = list(_tags_statuses(checks).items()) + tags_statuses, num_down = _tags_statuses(checks) + pairs = list(tags_statuses.items()) pairs.sort(key=lambda pair: pair[0].lower()) ctx = { "page": "checks", "checks": checks, + "num_down": num_down, "now": timezone.now(), "tags": pairs, "ping_endpoint": settings.PING_ENDPOINT, @@ -93,7 +96,12 @@ def status(request): "last_ping": render_to_string("front/last_ping_cell.html", ctx) }) - return JsonResponse({"details": details, "tags": _tags_statuses(checks)}) + tags_statuses, num_down = _tags_statuses(checks) + return JsonResponse({ + "details": details, + "tags": tags_statuses, + "title": num_down_title(num_down) + }) def _welcome_check(request): diff --git a/static/js/checks.js b/static/js/checks.js index 01c7dd50..2974ef68 100644 --- a/static/js/checks.js +++ b/static/js/checks.js @@ -297,6 +297,11 @@ $(function () { this.setAttribute("class", "btn btn-xs " + status); } }); + + if (document.title != data.title) { + document.title = data.title; + } + } }); } diff --git a/templates/front/my_checks.html b/templates/front/my_checks.html index 02e8d11d..30b6daea 100644 --- a/templates/front/my_checks.html +++ b/templates/front/my_checks.html @@ -1,8 +1,7 @@ {% extends "base.html" %} {% load compress staticfiles hc_extras %} -{% block title %}My Checks - {% site_name %}{% endblock %} - +{% block title %}{{ num_down|num_down_title }}{% endblock %} {% block content %}
diff --git a/templates/front/snippets/python_requests_fail.html b/templates/front/snippets/python_requests_fail.html index 748bcc00..37d6ea25 100644 --- a/templates/front/snippets/python_requests_fail.html +++ b/templates/front/snippets/python_requests_fail.html @@ -2,7 +2,7 @@ URL = "{{ ping_url }}" def do_work(): - # Do actual work here. + # Do your number crunching, backup dumping, newsletter sending work here. # Return a truthy value on success. # Return a falsy value or throw an exception on failure. return True diff --git a/templates/front/snippets/python_requests_fail.txt b/templates/front/snippets/python_requests_fail.txt index e85d5e4f..1fbfa16c 100644 --- a/templates/front/snippets/python_requests_fail.txt +++ b/templates/front/snippets/python_requests_fail.txt @@ -2,7 +2,7 @@ import requests URL = "PING_URL" def do_work(): - # Do actual work here. + # Do your number crunching, backup dumping, newsletter sending work here. # Return a truthy value on success. # Return a falsy value or throw an exception on failure. return True