Production graph display on website

resolves #35
This commit is contained in:
Danielv123 committed 2017-03-12 17:01:05 +01:00
1 parent 14b9ca0068
commit 71753c29a5
4 files changed
+58 -48

No files matched your search

+9 -10
View File
@@ -432,7 +432,7 @@ function instanceManagement() {
} }
} }
// flow statistics ------------------------------------------------------------ // flow/production statistics ------------------------------------------------------------
oldFlowStats = false oldFlowStats = false
setInterval(function(){ setInterval(function(){
fs.readFile(instancedirectory + "/script-output/flows.txt", {encoding: "utf8"}, function(err, data) { fs.readFile(instancedirectory + "/script-output/flows.txt", {encoding: "utf8"}, function(err, data) {
@@ -445,33 +445,32 @@ function instanceManagement() {
try{ try{
flowStats[flowStats.length] = JSON.parse(data[i]); flowStats[flowStats.length] = JSON.parse(data[i]);
} catch (e) { } 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 // fluids
let flowStat1 = flowStats[flowStats.length-1].flows.player.input_counts let flowStat1 = flowStats[flowStats.length-1].flows.player.input_counts
// items // items
let flowStat2 = flowStats[flowStats.length-2].flows.player.input_counts let flowStat2 = flowStats[flowStats.length-2].flows.player.input_counts
// console.log(flowStat2)
// merge fluid and item flows // merge fluid and item flows
let totalFlows = {}; let totalFlows = {};
for(var key in flowStat1) totalFlows[key] = flowStat1[key]; for(var key in flowStat1) totalFlows[key] = flowStat1[key];
for(var key in flowStat2) totalFlows[key] = flowStat2[key]; for(var key in flowStat2) totalFlows[key] = flowStat2[key];
// console.log(JSON.stringify(totalFlows)); throw "nah"; if(oldFlowStats && totalFlows && oldTimestamp) {
if(oldFlowStats && totalFlows) {
let payload = libomega.clone(totalFlows); let payload = libomega.clone(totalFlows);
// change from total reported to per time unit // change from total reported to per time unit
for(let key in oldFlowStats) { for(let key in oldFlowStats) {
//console.log(typeof oldFlowStats[key] + " | " + oldFlowStats[key]) // get production per minute
payload[key] = payload[key] - oldFlowStats[key] payload[key] = Math.floor((payload[key] - oldFlowStats[key])/(timestamp - oldTimestamp)*60000);
//console.log(payload[key])
} }
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) { needle.post(config.masterIP + ":" + config.masterPort + '/logStats', {timestamp: timestamp, slaveID: instanceconfig.unique,data: payload}, function (err, response, body) {
// we did it, keep going // we did it, keep going
}) })
} }
oldFlowStats = totalFlows oldTimestamp = timestamp;
oldFlowStats = totalFlows;
fs.writeFileSync(instancedirectory + "/script-output/flows.txt", ""); fs.writeFileSync(instancedirectory + "/script-output/flows.txt", "");
} }
}) })
-1
View File
@@ -12,7 +12,6 @@
<div id="display"> <div id="display">
</div> </div>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div> </div>
</div> </div>
</body> </body>
+37 -33
View File
@@ -1,7 +1,7 @@
// get all slaves recently connected to master // get all slaves recently connected to master
// ask master for slaves // ask master for slaves
setInterval(function() { setTimeout(function() {
var xmlhttp = new XMLHttpRequest(); var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() { xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 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 // maybe include an option to use y-ymd-ymd-y for Trangar compatibility as well
var seenDate = date.yyyymmdd(slaveData[key].time) var seenDate = date.yyyymmdd(slaveData[key].time)
if(g.trangarTime == true){ 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 += "<div class='slaveBox'><h2>ID: " + slaveData[key].unique + "</h2><p>Last seen: "+seenDate+"</p><p>Port: "+slaveData[key].serverPort+"</p><p>Host: "+slaveData[key].mac+"</p></div>" HTML += "<div class='slaveBox'>";
HTML += '<div id="' + slaveData[key].unique + '" class="productionGraph" style="height: 250px; width: calc(100% - 200px);"></div>';
HTML += "<h2>ID: " + slaveData[key].unique + "</h2><p>Last seen: "+seenDate+"</p><p>Port: "+slaveData[key].serverPort+"</p><p>Host: "+slaveData[key].mac+"</p>";
HTML += "</div>";
} }
} }
document.querySelector("#slaves > #display").innerHTML = 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.open("GET", "slaves", true);
xmlhttp.send(); xmlhttp.send();
}, 500) }, 0)
function makeGraph(slaveID, selector, callback) {
post("http://localhost:8080/getStats", {slaveID: slaveID}, function(data){ // ID of slave, ID of canvasjs div without #
console.log("makeGraphed!") function makeGraph(slaveID, selector) {
callback(data); 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)); 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) { function generateLineChartArray(data, nameKey) {
let chartData = []; let chartData = [];
@@ -106,10 +113,7 @@ function drawChart(selector, chartData) {
axisY:{ axisY:{
includeZero: true includeZero: true
}, },
data: chartData/*[{ data: chartData
type: "line",
dataPoints: chartData
}]*/
}); });
// console.log(chart) // console.log(chart)
chart.render(); chart.render();
+12 -4
View File
@@ -36,12 +36,12 @@
/* Slave stuff */ /* Slave stuff */
.slaveBox { .slaveBox {
float:left; float:left;
width:200px; width:100%;
height:110px; /*height:110px;*/
background-color:#EEE; background-color:#EEE;
border-radius: 10px; /*border-radius: 10px;
margin-left:5px; margin-left:5px;
margin-right:5px; margin-right:5px;*/
} }
.slaveBox > p { .slaveBox > p {
margin:5px; margin:5px;
@@ -49,6 +49,10 @@
.slaveBox > h2 { .slaveBox > h2 {
margin:5px; margin:5px;
} }
.productionGraph {
float:right;
}
.button-black { .button-black {
width:100%; width:100%;
height:40px; height:40px;
@@ -67,3 +71,7 @@
top: 50%; top: 50%;
transform: translateY(-50%); transform: translateY(-50%);
} }
.canvasjs-chart-credit {
display:none;
}