From f178981334c5144c9089af4a1c0df61edd7ab9df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Sun, 3 Jan 2016 02:43:56 +0200
Subject: [PATCH] Log: instead of timeline, show placeholders for "expected but
not received" pings.
---
hc/front/views.py | 42 +++++++++++++++++++------------
static/css/log.css | 53 +++++-----------------------------------
templates/front/log.html | 42 ++++++++++++++++---------------
3 files changed, 54 insertions(+), 83 deletions(-)
diff --git a/hc/front/views.py b/hc/front/views.py
index 49abadad..dcd007cf 100644
--- a/hc/front/views.py
+++ b/hc/front/views.py
@@ -1,5 +1,6 @@
from collections import Counter
from datetime import timedelta as td
+from itertools import tee
from django.conf import settings
from django.contrib.auth.decorators import login_required
@@ -15,6 +16,14 @@ from hc.api.models import Channel, Check, Ping
from hc.front.forms import AddChannelForm, NameTagsForm, TimeoutForm
+# from itertools recipes:
+def pairwise(iterable):
+ "s -> (s0,s1), (s1,s2), (s2, s3), ..."
+ a, b = tee(iterable)
+ next(b, None)
+ return zip(a, b)
+
+
@login_required
def my_checks(request):
checks = Check.objects.filter(user=request.user).order_by("created")
@@ -185,30 +194,31 @@ def log(request, code):
profile = Profile.objects.for_user(request.user)
limit = profile.ping_log_limit
- pings = Ping.objects.filter(owner=check).order_by("-created")[:limit]
+ pings = Ping.objects.filter(owner=check).order_by("created")[:limit]
+ pings = list(pings)
+
+ # Add a dummy ping object at the end. We iterate over *pairs* of pings
+ # and don't want to handle a special case of a check with a single ping.
+ pings.append(Ping(created=timezone.now()))
# Now go through pings, calculate time gaps, and decorate
# the pings list for convenient use in template
wrapped = []
- now = timezone.now()
- for i, ping in enumerate(pings):
- prev = now if i == 0 else pings[i - 1].created
-
- duration = prev - ping.created
- if duration > check.timeout:
- downtime = {"prev_date": prev, "date": ping.created}
- if i > 0:
- wrapped[-1]["status"] = "late"
- if duration > check.timeout + check.grace:
- downtime["down"] = True
- if i > 0:
- wrapped[-1]["status"] = "down"
+ early = False
+ for older, newer in pairwise(pings):
+ wrapped.append({"ping": older, "early": early})
- wrapped.append(downtime)
+ # Fill in "missed ping" placeholders:
+ expected_date = older.created + check.timeout
+ while expected_date + check.grace < newer.created:
+ wrapped.append({"placeholder_date": expected_date})
+ expected_date = expected_date + check.timeout
- wrapped.append({"ping": ping})
+ # Prepare early flag for next ping to come
+ early = older.created + check.timeout > newer.created + check.grace
+ wrapped.reverse()
ctx = {
"check": check,
"pings": wrapped
diff --git a/static/css/log.css b/static/css/log.css
index cd419af1..9327a0ca 100644
--- a/static/css/log.css
+++ b/static/css/log.css
@@ -5,7 +5,6 @@
#log td {
color: #444;
- padding: 16px 8px;
position: relative;
border: 0;
border-bottom: 1px solid #E5E5E5;
@@ -15,7 +14,7 @@
white-space: nowrap;
}
-#log .date {
+#log .date, #log .time {
display: inline-block;
width: 70px;
}
@@ -36,52 +35,12 @@
font-size: 11px;
}
-
-#log .bullet {
- position: absolute;
- display: block;
- top: 50%;
- margin-top: -6px;
- left: 0;
- width: 12px;
- height: 12px;
- border: 2px solid #FFF;
- border-radius: 6px;
- background: #376f37;
-}
-
-#log .tl {
- position: absolute;
- left: 4px;
- width: 4px;
- background: #5cb85c;
-}
-
-#log .top {
- top: 0;
- height: 50%;
-}
-
-#log .bottom {
- top: 50%;
- height: 50%;
+#log .ok {
+ color: #5cb85c;
}
-#log .full-down {
- top: 0;
- height: 100%;
- background: #d9534f;
-}
-
-#log .down { background: #d9534f; }
-#log .late { background: #f0ad4e; }
-
-#log .tl-cell {
- border: 0;
+#log tr.missing td {
+ color: #d9534f;
+ background: #fff3f2;
}
-#log .downtime-cell {
- border: 0;
- font-size: small;
- color: #d9534f;
-}
\ No newline at end of file
diff --git a/templates/front/log.html b/templates/front/log.html
index 52d3f64f..e5afd369 100644
--- a/templates/front/log.html
+++ b/templates/front/log.html
@@ -20,7 +20,6 @@
Note: Dates and times are displayed in UTC.
-
@@ -35,11 +34,9 @@
{% for record in pings %}
{% if record.ping %}
-
-
-
-
-
+ |
+
+
|
@@ -49,6 +46,9 @@
{{ record.ping.created|date:"H:i" }}
+ {% if record.early %}
+ early
+ {% endif %}
|
@@ -62,22 +62,24 @@
{{ record.ping.ua }}
|
- {% else %}
- {% if record.down %}
-
-
-
+ {% endif %}
+ {% if record.placeholder_date %}
+ |
+
+
|
-
- No ping received for {{ record.date|timesince:record.prev_date }}
-
+ |
+
+
+ {{ record.placeholder_date|date:"N j" }}
+
+
+ {{ record.placeholder_date|date:"H:i" }}
+
+
|
-
- {% endif %}
-
-
-
-
+ |
+ Ping expected but not received
|
{% endif %}