From 1110c6908c8832c0b3e92afeded7b54ab58e5ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 26 Feb 2018 12:10:56 +0200 Subject: [PATCH] Testcase for /checks/status/. More efficient DOM updates. --- hc/front/tests/test_status.py | 23 +++++++++++++++ static/css/my_checks_mobile.css | 20 +++++++++++++ static/js/checks.js | 41 ++++++++++++++++++-------- templates/front/my_checks_desktop.html | 10 +++---- templates/front/my_checks_mobile.html | 22 ++++---------- 5 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 hc/front/tests/test_status.py diff --git a/hc/front/tests/test_status.py b/hc/front/tests/test_status.py new file mode 100644 index 00000000..5dd3e4ec --- /dev/null +++ b/hc/front/tests/test_status.py @@ -0,0 +1,23 @@ +from hc.api.models import Check +from hc.test import BaseTestCase + + +class MyChecksTestCase(BaseTestCase): + + def setUp(self): + super(MyChecksTestCase, self).setUp() + self.check = Check(user=self.alice, name="Alice Was Here") + self.check.tags = "foo" + self.check.save() + + def test_it_works(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/status/") + doc = r.json() + + self.assertEqual(doc["tags"]["foo"], "up") + + detail = doc["details"][0] + self.assertEqual(detail["code"], str(self.check.code)) + self.assertEqual(detail["status"], "new") + self.assertIn("Never", detail["last_ping"]) diff --git a/static/css/my_checks_mobile.css b/static/css/my_checks_mobile.css index 8ae13ccb..95a82d79 100644 --- a/static/css/my_checks_mobile.css +++ b/static/css/my_checks_mobile.css @@ -36,4 +36,24 @@ #checks-list th { width: 100px; +} + +#checks-list .label { + text-transform: uppercase; +} + +.label-new, .label-paused { + background-color: #777777; +} + +.label-up { + background-color: #22bc66; +} + +.label-grace { + background-color: #f0ad4e; +} + +.label-down { + background-color: #d9534f; } \ No newline at end of file diff --git a/static/js/checks.js b/static/js/checks.js index 1817242f..b1c0fc02 100644 --- a/static/js/checks.js +++ b/static/js/checks.js @@ -258,21 +258,36 @@ $(function () { }); // Auto-refresh + var lastStatus = {}; + var lastPing = {}; function refresh() { - $.getJSON("/checks/status/", function(data) { - for(var i=0, el; el=data.details[i]; i++) { - var elId = "#check-desktop-" + el.code; - $(elId + " .indicator-cell span").attr("class", "status icon-" + el.status); - $(elId + " .last-ping-cell").html(el.last_ping); - $(elId + " .li-pause-check").toggleClass("disabled", el.status == "paused"); - } - - $("#my-checks-tags button").each(function(a) { - var status = data.tags[this.innerText]; - if (status) { - this.setAttribute("class", "btn btn-xs " + status); + $.ajax({ + url: "/checks/status/", + dataType: "json", + timeout: 2000, + success: function(data) { + for(var i=0, el; el=data.details[i]; i++) { + if (lastStatus[el.code] != el.status) { + lastStatus[el.code] = el.status; + $("#si-" + el.code).attr("class", "status icon-" + el.status); + $("#sl-" + el.code).attr("class", "label label-" + el.status).text(el.status); + $("#pause-li-" + el.code).toggleClass("disabled", el.status == "paused"); + } + + if (lastPing[el.code] != el.last_ping) { + lastPing[el.code] = el.last_ping; + $("#lpd-" + el.code).html(el.last_ping); + $("#lpm-" + el.code).html(el.last_ping); + } } - }); + + $("#my-checks-tags button").each(function(a) { + var status = data.tags[this.innerText]; + if (status) { + this.setAttribute("class", "btn btn-xs " + status); + } + }); + } }); } diff --git a/templates/front/my_checks_desktop.html b/templates/front/my_checks_desktop.html index ce279073..dad611ec 100644 --- a/templates/front/my_checks_desktop.html +++ b/templates/front/my_checks_desktop.html @@ -28,12 +28,12 @@ {% for check in checks|sortchecks:sort %} - + {% if check.in_grace_period %} - + {% else %} - + {% endif %} @@ -77,7 +77,7 @@ - + {% include "front/last_ping_cell.html" with check=check %} @@ -86,7 +86,7 @@