Commit 907134b7a29177cb45aa461c549c004b1ae875af

Parents: aee39b4e9a45e1ba507c0017de50bb9dbbae7af8

From: Andy Green <andy@warmcat.com>
Date: Sun Jun 24 15:05:20 2018 +0700

js: add dynamic age update
This patch updates the emitted "ages" dynamically on the client side.

After updating on completion of the document load, it sets a timer
to update according to the smallest age it found.  If there are any
ages listed in minutes, then it will update again in 10s.  When the
most recent age is in hours, it updates every 5m.  If days, then
every 30m and so on.

This keeps the cost of the dynamic updates at worst once per 10s.
The updates are done entirely on the client side without contact
with the server.

To make this work reliably, since parsing datetimes is unreliable in
browser js, the unix time is added as an attribute to all age spans.

To make that reliable cross-platform, the unix time is treated as a
uint64_t when it is formatted for printing.

The rules for display conversion of the age is aligned with the
existing server-side rules in ui-shared.h.

If the client or server-side time are not synchronized by ntpd etc,
ages shown on the client will not relate to the original ages computed
at the server.  The client updates the ages immediately when the
DOM has finished loading, so in the case the times at the server and
client are not aligned, this patch changes what the user sees on the
page to reflect patch age compared to client time.

If the server and client clocks are aligned, this patch makes no
difference to what is seen on the page.

Signed-off-by: Andy Green <andy@warmcat.com>
Signed-off-by: Christian Hesse <mail@eworm.de>

Stats

cgit.h +1/-0
cgit.js +61/-0
ui-shared.c +1/-1

Changeset

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
diff --git a/cgit.h b/cgit.h
index a0cf5e904b697c5168d28e846ba153a41973d873..ddd2ccba0b8849f9d90d8d0c430930ee96dab77c 100644
--- a/cgit.h
+++ b/cgit.h
@@ -25,6 +25,7 @@ #include <xdiff/xdiff.h>
 #include <utf8.h>
 #include <notes.h>
 #include <graph.h>
+#include <inttypes.h>
 
 /* Add isgraph(x) to Git's sane ctype support (see git-compat-util.h) */
 #undef isgraph
diff --git a/cgit.js b/cgit.js
index b35e0bca0fb76ed1f688e51191b0166a4df64947..df3ad4ec97531bbb32a12c93025b69953867af79 100644
--- a/cgit.js
+++ b/cgit.js
@@ -5,3 +5,64 @@  *
  * Licensed under GNU General Public License v2
  *   (see COPYING for full license text)
  */
+
+(function () {
+
+/* This follows the logic and suffixes used in ui-shared.c */
+
+var age_classes = [ "age-mins", "age-hours", "age-days",    "age-weeks",    "age-months",    "age-years" ];
+var age_suffix =  [ "min.",     "hours",     "days",        "weeks",        "months",        "years",         "years" ];
+var age_next =    [ 60,         3600,        24 * 3600,     7 * 24 * 3600,  30 * 24 * 3600,  365 * 24 * 3600, 365 * 24 * 3600 ];
+var age_limit =   [ 7200,       24 * 7200,   7 * 24 * 7200, 30 * 24 * 7200, 365 * 25 * 7200, 365 * 25 * 7200 ];
+var update_next = [ 10,         5 * 60,      1800,          24 * 3600,      24 * 3600,       24 * 3600,       24 * 3600 ];
+
+function render_age(e, age) {
+	var t, n;
+
+	for (n = 0; n < age_classes.length; n++)
+		if (age < age_limit[n])
+			break;
+
+	t = Math.round(age / age_next[n]) + " " + age_suffix[n];
+
+	if (e.textContent != t) {
+		e.textContent = t;
+		if (n == age_classes.length)
+			n--;
+		if (e.className != age_classes[n])
+			e.className = age_classes[n];
+	}
+}
+
+function aging() {
+	var n, next = 24 * 3600,
+	    now_ut = Math.round((new Date().getTime() / 1000));
+
+	for (n = 0; n < age_classes.length; n++) {
+		var m, elems = document.getElementsByClassName(age_classes[n]);
+
+		if (elems.length && update_next[n] < next)
+			next = update_next[n];
+
+		for (m = 0; m < elems.length; m++) {
+			var age = now_ut - elems[m].getAttribute("data-ut");
+
+			render_age(elems[m], age);
+		}
+	}
+
+	/*
+	 * We only need to come back when the age might have changed.
+	 * Eg, if everything is counted in hours already, once per
+	 * 5 minutes is accurate enough.
+	 */
+
+	window.setTimeout(aging, next * 1000);
+}
+
+document.addEventListener("DOMContentLoaded", function() {
+	/* we can do the aging on DOM content load since no layout dependency */
+	aging();
+}, false);
+
+})();
diff --git a/ui-shared.c b/ui-shared.c
index c50b3e68568087221bb75ef25268af4d203b9338..11aed19a80e882c6999eecb32250cd206fee5210 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -673,7 +673,7 @@
 static void print_rel_date(time_t t, int tz, double value,
 	const char *class, const char *suffix)
 {
-	htmlf("<span class='%s' title='", class);
+	htmlf("<span class='%s' data-ut='%" PRIu64 "' title='", class, (uint64_t)t);
 	html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
 	htmlf("'>%.0f %s</span>", value, suffix);
 }