From 63c53f4755febd7fd7a81641ab91743dad9fdfc1 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sat, 3 Sep 2016 15:46:08 +0100 Subject: [PATCH] Add status to node page --- lib/nunjuck-filters.js | 46 ++++++++++++++++++++++++------------------ views/node.html | 5 +++++ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/nunjuck-filters.js b/lib/nunjuck-filters.js index 36f7486..578a5a8 100644 --- a/lib/nunjuck-filters.js +++ b/lib/nunjuck-filters.js @@ -2,6 +2,27 @@ const prettyBytes = require('pretty-bytes'); const moment = require('moment'); const querystring = require('querystring'); +function humanTimeAgo(utcDate) { + const diff = moment.utc().diff(moment.utc(utcDate)); + const uptime = {}; + + uptime.s = Math.round(diff / 1000); + uptime.m = Math.floor(uptime.s / 60); + uptime.h = Math.floor(uptime.m / 60); + uptime.d = Math.floor(uptime.h / 24); + + uptime.s %= 60; + uptime.m %= 60; + uptime.h %= 24; + + let readableUptime = ''; + readableUptime += uptime.d ? ` ${uptime.d}d` : ''; + readableUptime += uptime.h ? ` ${uptime.h}h` : ''; + readableUptime += !uptime.d || !uptime.h && uptime.m ? ` ${uptime.m}m` : ''; + + return readableUptime.trim(); +} + const filters = { bandwidth: node => `${prettyBytes(node.advertised_bandwidth)}/s`, uptime: node => { @@ -12,25 +33,7 @@ const filters = { } // Check uptime - const lastRestarted = moment.utc(node.last_restarted); - const diff = moment.utc().diff(lastRestarted); - const uptime = {}; - - uptime.s = Math.round(diff / 1000); - uptime.m = Math.floor(uptime.s / 60); - uptime.h = Math.floor(uptime.m / 60); - uptime.d = Math.floor(uptime.h / 24); - - uptime.s %= 60; - uptime.m %= 60; - uptime.h %= 24; - - let readableUptime = ''; - readableUptime += uptime.d ? ` ${uptime.d}d` : ''; - readableUptime += uptime.h ? ` ${uptime.h}h` : ''; - readableUptime += !uptime.d || !uptime.h && uptime.m ? ` ${uptime.m}m` : ''; - - return readableUptime.trim(); + return humanTimeAgo(node.last_restarted); }, pagination: (req, direction) => { @@ -52,7 +55,10 @@ const filters = { }, name: node => node.nickname || node.fingerprint && node.fingerprint.slice(0, 8) - || node.hashed_fingerprint && node.hashed_fingerprint.slice(0, 8) + || node.hashed_fingerprint && node.hashed_fingerprint.slice(0, 8), + status: node => node.running ? + `Up for ${humanTimeAgo(node.last_restarted)}` : + `Down for ${humanTimeAgo(node.last_seen)}` }; module.exports = app => Object.keys(filters).forEach(filter => { diff --git a/views/node.html b/views/node.html index 751cd49..1325b52 100644 --- a/views/node.html +++ b/views/node.html @@ -6,6 +6,11 @@

{{ 'Invalid node' if error.statusCode == 400 else error.statusMessage }}

{% else %}

{{ node.type | title }}: {{ node | name }}

+ +
+

Status

+ {{ node | status }} +
{% endif %} {% endblock %}