From b520d9611ba60a78b9bb49aeb55e5cd91b3fbe83 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 09:06:04 +0200 Subject: [PATCH] Red/green/yellow dot to tell slave status on /nodeDetails/ --- static/nodeDetails.js | 47 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/static/nodeDetails.js b/static/nodeDetails.js index 805dc8ac..ec4c47b4 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -12,19 +12,24 @@ function post(url, data, callback) { } xhr.send(JSON.stringify(data)); } +// callback(err, json) function getJSON(url, callback) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', url, true); - xhr.responseType = 'json'; - xhr.onload = function() { - var status = xhr.status; - if (status == 200) { - callback(null, xhr.response); - } else { - callback(status); - } - }; - xhr.send(); + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'json'; + xhr.onload = function() { + var status = xhr.status; + if (status == 200) { + callback(null, xhr.response); + } else { + callback(status); + } + }; + // triggers if connection is refused + xhr.onerror = function(e){ + callback(e); + }; + xhr.send(); }; // function to sort arrays of objects after a keys value function sortByKey(array, key) { @@ -43,6 +48,24 @@ function getParameterByName(name, url) { return decodeURIComponent(results[2].replace(/\+/g, " ")); } + + + +/// code below here +// update the online indicator dot +setInterval(function(){ + getJSON("/api/slaves", function(err, slaveData){ + if(err){ + document.querySelector("#onlineIndicator").style.backgroundColor = "yellow"; + // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other + } else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){ + document.querySelector("#onlineIndicator").style.backgroundColor = "green"; + } else { + document.querySelector("#onlineIndicator").style.backgroundColor = "red"; + } + }); +}, 1000); + function populateSlaveInfo(){ let slaveID = getParameterByName("slaveID"); if(!slaveID){