From 4787aa7f624f191511023311648cc8ddd90198f7 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Thu, 26 Oct 2017 15:58:28 +0200 Subject: [PATCH] [BUGFIX] Fix uptime in nodelist --- lib/infobox/node.js | 2 +- lib/nodelist.js | 36 +++++++++++++++--------------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/infobox/node.js b/lib/infobox/node.js index 9c8a17a..b54829e 100644 --- a/lib/infobox/node.js +++ b/lib/infobox/node.js @@ -52,7 +52,7 @@ define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper'], return undefined; } - return moment.duration(d.uptime, 'seconds').humanize(); + return moment.utc(d.uptime).local().fromNow(true); } function showFirstseen(d) { diff --git a/lib/nodelist.js b/lib/nodelist.js index 392324a..4064425 100644 --- a/lib/nodelist.js +++ b/lib/nodelist.js @@ -2,28 +2,18 @@ define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) { 'use strict'; V = V.default; - function getUptime(now, d) { - if (d.is_online && 'uptime' in d) { - return Math.round(d.uptime); - } else if (!d.is_online && 'lastseen' in d) { - return Math.round(-(now.unix() - d.lastseen.unix())); - } - return 0; - } - function showUptime(uptime) { - var s = ''; - uptime /= 3600; - - if (uptime !== undefined) { - if (Math.abs(uptime) >= 24) { - s = Math.round(uptime / 24) + 'd'; - } else { - s = Math.round(uptime) + 'h'; - } + // 1000ms are 1 second and 60 second are 1min: 60 * 1000 = 60000 + var s = uptime / 60000; + if (Math.abs(s) < 60) { + return Math.round(s) + ' m'; } - - return s; + s /= 60; + if (Math.abs(s) < 24) { + return Math.round(s) + ' h'; + } + s /= 24; + return Math.round(s) + ' d'; } var headings = [{ @@ -101,7 +91,11 @@ define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) { this.setData = function setData(d) { var data = d.nodes.all.map(function (e) { var n = Object.create(e); - n.uptime = getUptime(d.now, e); + if (e.is_online) { + n.uptime = d.now - new Date(e.uptime).getTime(); + } else { + n.uptime = e.lastseen - d.now; + } n.neighbours = e.neighbours; return n; });