From 71753c29a5c3f604817ba513c74d6dc348407084 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 12 Mar 2017 16:59:36 +0100 Subject: [PATCH] Production graph display on website resolves #35 --- client.js | 19 ++++++------ static/nodes.ejs | 1 - static/slaveDisplay.js | 70 ++++++++++++++++++++++-------------------- static/style.css | 16 +++++++--- 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/client.js b/client.js index 6f159a65..371604b2 100644 --- a/client.js +++ b/client.js @@ -432,7 +432,7 @@ function instanceManagement() { } } - // flow statistics ------------------------------------------------------------ + // flow/production statistics ------------------------------------------------------------ oldFlowStats = false setInterval(function(){ fs.readFile(instancedirectory + "/script-output/flows.txt", {encoding: "utf8"}, function(err, data) { @@ -445,33 +445,32 @@ function instanceManagement() { try{ flowStats[flowStats.length] = JSON.parse(data[i]); } catch (e) { - console.log(" invalid json: " + i); + // console.log(" invalid json: " + i); + // some lines of JSON are invalid but don't worry, we just filter em out } } // fluids let flowStat1 = flowStats[flowStats.length-1].flows.player.input_counts // items let flowStat2 = flowStats[flowStats.length-2].flows.player.input_counts - // console.log(flowStat2) // merge fluid and item flows let totalFlows = {}; for(var key in flowStat1) totalFlows[key] = flowStat1[key]; for(var key in flowStat2) totalFlows[key] = flowStat2[key]; - // console.log(JSON.stringify(totalFlows)); throw "nah"; - if(oldFlowStats && totalFlows) { + if(oldFlowStats && totalFlows && oldTimestamp) { let payload = libomega.clone(totalFlows); // change from total reported to per time unit for(let key in oldFlowStats) { - //console.log(typeof oldFlowStats[key] + " | " + oldFlowStats[key]) - payload[key] = payload[key] - oldFlowStats[key] - //console.log(payload[key]) + // get production per minute + payload[key] = Math.floor((payload[key] - oldFlowStats[key])/(timestamp - oldTimestamp)*60000); } - console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]); + // console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]); needle.post(config.masterIP + ":" + config.masterPort + '/logStats', {timestamp: timestamp, slaveID: instanceconfig.unique,data: payload}, function (err, response, body) { // we did it, keep going }) } - oldFlowStats = totalFlows + oldTimestamp = timestamp; + oldFlowStats = totalFlows; fs.writeFileSync(instancedirectory + "/script-output/flows.txt", ""); } }) diff --git a/static/nodes.ejs b/static/nodes.ejs index f2346194..b519c797 100644 --- a/static/nodes.ejs +++ b/static/nodes.ejs @@ -12,7 +12,6 @@
-
\ No newline at end of file diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js index 4149374a..8caa53a9 100644 --- a/static/slaveDisplay.js +++ b/static/slaveDisplay.js @@ -1,7 +1,7 @@ // get all slaves recently connected to master // ask master for slaves -setInterval(function() { +setTimeout(function() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { @@ -15,22 +15,50 @@ setInterval(function() { // maybe include an option to use y-ymd-ymd-y for Trangar compatibility as well var seenDate = date.yyyymmdd(slaveData[key].time) if(g.trangarTime == true){ - var seenDate = (seenDate+"")[0]+"-"+(seenDate+"")[1]+(seenDate+"")[4]+(seenDate+"")[6]+"-"+(seenDate+"")[2]+(seenDate+"")[5]+(seenDate+"")[7]+"-"+(seenDate+"")[3] + var seenDate = (seenDate+"")[0]+"-"+(seenDate+"")[1]+(seenDate+"")[4]+(seenDate+"")[6]+"-"+(seenDate+"")[2]+(seenDate+"")[5]+(seenDate+"")[7]+"-"+(seenDate+"")[3]; } - HTML += "

ID: " + slaveData[key].unique + "

Last seen: "+seenDate+"

Port: "+slaveData[key].serverPort+"

Host: "+slaveData[key].mac+"

" + HTML += "
"; + HTML += '
'; + HTML += "

ID: " + slaveData[key].unique + "

Last seen: "+seenDate+"

Port: "+slaveData[key].serverPort+"

Host: "+slaveData[key].mac+"

"; + + HTML += "
"; } } document.querySelector("#slaves > #display").innerHTML = HTML + let graphs = document.querySelectorAll(".productionGraph"); + for(let i = 0; i < graphs.length; i++) { + // execute functions to make graphs + // ID of slave, ID of canvasjs div without # + makeGraph(graphs[i].id, graphs[i].id) + } } } xmlhttp.open("GET", "slaves", true); xmlhttp.send(); -}, 500) +}, 0) -function makeGraph(slaveID, selector, callback) { - post("http://localhost:8080/getStats", {slaveID: slaveID}, function(data){ - console.log("makeGraphed!") - callback(data); + +// ID of slave, ID of canvasjs div without # +function makeGraph(slaveID, selector) { + post("getStats", {slaveID: slaveID}, function(data){ + console.log("Building chart " + slaveID + " with this data:") + console.log(data) + if(data.length > 0) { + // find keys + let itemNames = []; + for(let key in data[data.length - 1].data) { + itemNames[itemNames.length] = key + } + let chartData = []; + for(let o = 0; o < itemNames.length; o++) { + if(itemNames[o] != "water"){ + chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); + } + } + console.log(chartData) + drawChart(selector, chartData) + } + // callback(data); }) } @@ -50,27 +78,6 @@ function post(url, data, callback) { } xhr.send(JSON.stringify(data)); } -// execute functions to make a graph, this is the entrypoint (during testing at least) -// ID of slave, blank string, function with JSON data as fist argument -makeGraph("-496927236", "", function(data){ - console.log("Building chart with this data:") - console.log(data) - // find keys - let itemNames = []; - for(let key in data[data.length - 1].data) { - itemNames[itemNames.length] = key - } - let chartData = []; - for(let o = 0; o < itemNames.length; o++) { - if(itemNames[o] != "water"){ - chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); - } - } - - - console.log(chartData) - drawChart("chartContainer", chartData) -}) function generateLineChartArray(data, nameKey) { let chartData = []; @@ -106,10 +113,7 @@ function drawChart(selector, chartData) { axisY:{ includeZero: true }, - data: chartData/*[{ - type: "line", - dataPoints: chartData - }]*/ + data: chartData }); // console.log(chart) chart.render(); diff --git a/static/style.css b/static/style.css index 86e80a73..6d3e0f02 100644 --- a/static/style.css +++ b/static/style.css @@ -36,12 +36,12 @@ /* Slave stuff */ .slaveBox { float:left; - width:200px; - height:110px; + width:100%; + /*height:110px;*/ background-color:#EEE; - border-radius: 10px; + /*border-radius: 10px; margin-left:5px; - margin-right:5px; + margin-right:5px;*/ } .slaveBox > p { margin:5px; @@ -49,6 +49,10 @@ .slaveBox > h2 { margin:5px; } +.productionGraph { + float:right; +} + .button-black { width:100%; height:40px; @@ -66,4 +70,8 @@ position: relative; top: 50%; transform: translateY(-50%); +} + +.canvasjs-chart-credit { + display:none; } \ No newline at end of file