mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Master has POST endpoint for flows data
#35 still not sure how to do the graphs on clientside though
This commit is contained in:
1 parent
dc103d1bed
commit
dcc17e2fa8
2 files changed
+43
-18
No files matched your search
@@ -202,18 +202,25 @@ app.get("/inventory", function(req, res) {
|
|||||||
// {timestamp: Date, slaveID: string, data: {"item":number}}
|
// {timestamp: Date, slaveID: string, data: {"item":number}}
|
||||||
app.post("/logStats", function(req,res) {
|
app.post("/logStats", function(req,res) {
|
||||||
if(typeof req.body == "object" && req.body.slaveID && req.body.timestamp && req.body.data) {
|
if(typeof req.body == "object" && req.body.slaveID && req.body.timestamp && req.body.data) {
|
||||||
db.flows.insert({
|
if(Number(req.body.timestamp) != NaN){
|
||||||
slaveID: req.body.slaveID,
|
req.body.timestamp = Number(req.body.timestamp);
|
||||||
timestamp: req.body.timestamp,
|
db.flows.insert({
|
||||||
data: req.body.data,
|
slaveID: req.body.slaveID,
|
||||||
});
|
timestamp: req.body.timestamp,
|
||||||
console.log("inserted: " + req.body.slaveID + " | " + req.body.timestamp)
|
data: req.body.data,
|
||||||
|
});
|
||||||
|
console.log("inserted: " + req.body.slaveID + " | " + req.body.timestamp)
|
||||||
|
} else {
|
||||||
|
console.log("error invalid timestamp " + req.body.timestamp);
|
||||||
|
res.send(failure)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.send("failure")
|
res.send("failure")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// {slaveID: string, fromTime: Date, toTime, Date}
|
// {slaveID: string, fromTime: Date, toTime, Date}
|
||||||
app.post("/getStats", function(req,res) {
|
app.post("/getStats", function(req,res) {
|
||||||
|
console.log(req.body)
|
||||||
if(typeof req.body == "object" && req.body.slaveID) {
|
if(typeof req.body == "object" && req.body.slaveID) {
|
||||||
// if not specified, get stats for last 24 hours
|
// if not specified, get stats for last 24 hours
|
||||||
if(!req.body.fromTime) {
|
if(!req.body.fromTime) {
|
||||||
@@ -222,19 +229,14 @@ app.post("/getStats", function(req,res) {
|
|||||||
if(!req.body.toTime) {
|
if(!req.body.toTime) {
|
||||||
req.body.toTime = Date.now();
|
req.body.toTime = Date.now();
|
||||||
}
|
}
|
||||||
|
console.log("Looking... " + JSON.stringify(req.body))
|
||||||
db.flows.find({
|
db.flows.find({
|
||||||
$and: [
|
slaveID: req.body.slaveID,
|
||||||
{
|
|
||||||
slaveID: req.body.slaveID,
|
|
||||||
}, {
|
|
||||||
timestamp: {
|
|
||||||
$gte: req.body.toTime,
|
|
||||||
$lt: req.body.fromTime,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}, function(err, docs) {
|
}, function(err, docs) {
|
||||||
|
let entries = docs.filter(function (el) {
|
||||||
|
return el.timestamp <= req.body.toTime && el.timestamp >= req.body.fromTime;
|
||||||
|
});
|
||||||
|
console.log(entries)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
+24
-1
@@ -1,3 +1,5 @@
|
|||||||
|
|
||||||
|
// get all slaves recently connected to master
|
||||||
// ask master for slaves
|
// ask master for slaves
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
var xmlhttp = new XMLHttpRequest();
|
var xmlhttp = new XMLHttpRequest();
|
||||||
@@ -25,4 +27,25 @@ setInterval(function() {
|
|||||||
xmlhttp.send();
|
xmlhttp.send();
|
||||||
}, 500)
|
}, 500)
|
||||||
|
|
||||||
// get all slaves recently connected to master
|
function makeGraph(slaveID, selector) {
|
||||||
|
post("http://localhost:8080/getStats", {slaveID: slaveID}, function(data){
|
||||||
|
console.log(data);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// production info
|
||||||
|
// string, object, function(object)
|
||||||
|
function post(url, data, callback) {
|
||||||
|
console.log("POST " + url + JSON.stringify(data))
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("POST", url, true);
|
||||||
|
xhr.setRequestHeader("Content-type", "application/json");
|
||||||
|
xhr.onreadystatechange = function () {
|
||||||
|
console.log(xhr.readyState + "WOW")
|
||||||
|
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||||
|
var json = JSON.parse(xhr.responseText);
|
||||||
|
callback(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xhr.send(JSON.stringify(data));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user