From 5fce84cb5ca01f2783ce1d65c71531b033e55d49 Mon Sep 17 00:00:00 2001 From: Xaver Maierhofer Date: Sun, 12 Feb 2017 22:18:23 +0100 Subject: [PATCH] [TASK] Refactor showBar in node infobox --- lib/infobox/node.js | 54 ++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/lib/infobox/node.js b/lib/infobox/node.js index 2996ea4..8f6819a 100644 --- a/lib/infobox/node.js +++ b/lib/infobox/node.js @@ -18,17 +18,10 @@ define(['sorttable', 'virtual-dom', 'chroma-js', 'moment', 'helper'], function showStatus(d) { return function (el) { el.classList.add(d.flags.unseen ? 'unseen' : (d.flags.online ? 'online' : 'offline')); - if (d.flags.online) { - el.textContent = _.t('node.lastOnline', { - time: d.lastseen.fromNow(), - date: d.lastseen.format('DD.MM.YYYY, H:mm:ss') - }); - } else { - el.textContent = _.t('node.lastOffline', { - time: d.lastseen.fromNow(), - date: d.lastseen.format('DD.MM.YYYY, H:mm:ss') - }); - } + el.textContent = _.t((d.flags.online ? 'node.lastOnline' : 'node.lastOffline'), { + time: d.lastseen.fromNow(), + date: d.lastseen.format('DD.MM.YYYY, H:mm:ss') + }); }; } @@ -116,34 +109,19 @@ define(['sorttable', 'virtual-dom', 'chroma-js', 'moment', 'helper'], }; } - function showBar(v) { + function showBar(v, width, warning) { var span = document.createElement('span'); span.classList.add('bar'); var bar = document.createElement('span'); - bar.style.width = (v * 100) + '%'; - span.appendChild(bar); - - var label = document.createElement('label'); - label.textContent = (Math.round(v * 100)) + ' %'; - span.appendChild(label); - - return span; - } - - function showLoadBar(v, p) { - var span = document.createElement('span'); - span.classList.add('bar'); - - var bar = document.createElement('span'); - bar.style.width = ((v * 100) % 100) + '%'; - if (v >= p) { + bar.style.width = (width * 100) + '%'; + if (warning) { span.classList.add('warning'); } span.appendChild(bar); var label = document.createElement('label'); - label.textContent = (v); + label.textContent = v; span.appendChild(label); return span; @@ -155,7 +133,13 @@ define(['sorttable', 'virtual-dom', 'chroma-js', 'moment', 'helper'], } return function (el) { - el.appendChild(showLoadBar(d.statistics.loadavg.toFixed(2), d.nodeinfo.hardware.nproc)); + var value = d.statistics.loadavg.toFixed(2); + var width = d.statistics.loadavg % 1; + var warning = false; + if (d.statistics.loadavg >= d.nodeinfo.hardware.nproc) { + warning = true; + } + el.appendChild(showBar(value, width, warning)); }; } @@ -165,7 +149,13 @@ define(['sorttable', 'virtual-dom', 'chroma-js', 'moment', 'helper'], } return function (el) { - el.appendChild(showBar(d.statistics.memory_usage)); + var value = Math.round(d.statistics.memory_usage * 100) + ' %'; + var width = d.statistics.memory_usage; + var warning = false; + if (d.statistics.memory_usage >= 0.8) { + warning = true; + } + el.appendChild(showBar(value, width, warning)); }; }