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
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", "");
}
})
-1
View File
@@ -12,7 +12,6 @@
<div id="display">
</div>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</div>
</body>
+37 -33
View File
@@ -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 += "<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
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();
+12 -4
View File
@@ -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;
}