From 869b5f9cede3448e0d8e13fe865f74bcec9aa1bb Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 28 Oct 2017 22:13:40 +0200 Subject: [PATCH] sentItemStats for master is now working as well --- master.js | 33 ++++++++++++++++++--------------- static/main.js | 12 +++++++----- static/storage.js | 4 ++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/master.js b/master.js index 4673d01e..1e642f13 100644 --- a/master.js +++ b/master.js @@ -294,7 +294,7 @@ app.post("/api/remove", function(req, res) { //console.log("removed: " + object.name + " " + object.count + " . " + item + " and sent to " + object.instanceID + " | " + object.instanceName); db.items.removeItem(object); // res.send("successier"); - let sentItemStatistics = sentItemStatisticsBySlaveID[x.instanceID]; + let sentItemStatistics = sentItemStatisticsBySlaveID[object.instanceID]; if(sentItemStatistics === undefined){ sentItemStatistics = new averagedTimeSeries({ maxEntries: config.itemStats.maxEntries, @@ -306,6 +306,8 @@ app.post("/api/remove", function(req, res) { key:object.name, value:object.count, }); + //console.log(sentItemStatistics.data) + sentItemStatisticsBySlaveID[object.instanceID] = sentItemStatistics; res.send(object); } else { // if we didn't have enough, attempt giving out a smaller amount next time @@ -378,9 +380,9 @@ app.post("/api/logStats", function(req,res) { // {slaveID: string, fromTime: Date, toTime, Date} app.post("/api/getStats", function(req,res) { if(typeof req.body == "string"){ - req.body = JSON.stringify(req.body); + req.body = JSON.parse(req.body); } - console.log(req.body); + // console.log(req.body); if(typeof req.body == "object" && req.body.slaveID && req.body.statistic === undefined) { // if not specified, get stats for last 24 hours if(!req.body.fromTime) { @@ -401,7 +403,7 @@ app.post("/api/getStats", function(req,res) { }); } else if(typeof req.body == "object" && req.body.slaveID && typeof req.body.slaveID == "string" && req.body.itemName){ if(req.body.statistic == "place"){ - console.log("sending place data..."); + console.log(`sending place data for slaveID ${req.body.slaveID} ${req.body.itemName}`); // Gather data //console.log(recievedItemStatisticsBySlaveID) let itemStats = recievedItemStatisticsBySlaveID[req.body.slaveID]; @@ -417,21 +419,22 @@ app.post("/api/getStats", function(req,res) { data: data, }); } else if(req.body.statistic == "remove"){ - console.log("sending remove data..."); + console.log(`sending remove data for slaveID ${req.body.slaveID} ${req.body.itemName}`); // Gather data - //console.log(sentItemStatisticsBySlaveID) + console.log(sentItemStatisticsBySlaveID) let itemStats = sentItemStatisticsBySlaveID[req.body.slaveID]; - if(itemStats === undefined){ + console.log(itemStats) + if(typeof itemStats == "object"){ + let data = itemStats.get(config.itemStats.maxEntries, req.body.itemName); + //console.log(itemStats.get(config.itemStats.maxEntries, req.body.itemName)); + res.send({ + maxEntries:config.itemStats.maxEntries, + entriesPerSecond: config.itemStats.entriesPerSecond, + data: data, + }); + } else { res.send({statusForDebugging:"no data available"}); - return false; } - let data = itemStats.get(config.itemStats.maxEntries, req.body.itemName); - - res.send({ - maxEntries:config.itemStats.maxEntries, - entriesPerSecond: config.itemStats.entriesPerSecond, - data: data, - }); } } }); diff --git a/static/main.js b/static/main.js index 186639be..633bbb47 100644 --- a/static/main.js +++ b/static/main.js @@ -26,8 +26,9 @@ debugTools = { addItem: function(name, count){ console.log(`Attempting to add ${count} ${name}`) postJSON("api/place", { - instanceName: "debug", - unique: "debug", + instanceName: "unknown", + instanceID: "unknown", + unique: "unknown", name: name, count: count, }, function(data){ @@ -35,10 +36,11 @@ debugTools = { }); }, removeItem: function(name, count){ - console.log(name) + console.log(`Attempting to remove ${count} ${name}`) postJSON("api/remove", { - instanceName: "debug", - unique: "debug", + instanceName: "unknown", + instanceID: "unknown", + unique: "unknown", name: name, count: count, }, function(data){ diff --git a/static/storage.js b/static/storage.js index f454a324..ca23d745 100644 --- a/static/storage.js +++ b/static/storage.js @@ -117,7 +117,7 @@ function makeItemGraphs(itemName){ slaveIDs.forEach(function(slaveID){ ["place", "remove"].forEach(statistic => { console.log("Making chart for "+slaveID+"'s "+statistic+" statistic") - HTML += '
'; + HTML += '
'; setTimeout(function(){ let xmlhttp = new XMLHttpRequest(); // new HttpRequest instance @@ -128,7 +128,7 @@ function makeItemGraphs(itemName){ if(xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200) { // console.log(xmlhttp.responseText); if(isJSON(xmlhttp.responseText) && JSON.parse(xmlhttp.responseText).statusForDebugging === undefined){ - drawChart("contentGraph"+slaveID, [JSON.parse(xmlhttp.response).data], slaveID+" "+itemName); + drawChart(statistic+"contentGraph"+slaveID, [JSON.parse(xmlhttp.response).data], slaveID+" "+itemName); // console.log(JSON.parse(xmlhttp.response).data); } }