|
@@ -63,12 +63,6 @@ app.all('/search', function (req, res) {
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
-// vars global
|
|
|
|
-var global_result;
|
|
|
|
-var global_res;
|
|
|
|
-var global_names;
|
|
|
|
-var checkInterval;
|
|
|
|
-
|
|
|
|
app.all('/query', function (req, res) {
|
|
app.all('/query', function (req, res) {
|
|
var mongo_query_result = [];
|
|
var mongo_query_result = [];
|
|
var from = new Date(req.body.range.from);
|
|
var from = new Date(req.body.range.from);
|
|
@@ -83,19 +77,32 @@ app.all('/query', function (req, res) {
|
|
var interval = req.body.intervalMs / 1000;
|
|
var interval = req.body.intervalMs / 1000;
|
|
var maxDataPoints = req.body.maxDataPoints;
|
|
var maxDataPoints = req.body.maxDataPoints;
|
|
|
|
|
|
|
|
+ // cada 1s se chequea si hay resultados y se retorna
|
|
|
|
+ setInterval(function () {
|
|
|
|
+ if (mongo_query_result.length == names.length) {
|
|
|
|
+ if (!res._headerSent) {
|
|
|
|
+ setCORSHeaders(res);
|
|
|
|
+ res.json(mongo_query_result);
|
|
|
|
+ res.end();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }, 1000);
|
|
|
|
+
|
|
mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
|
|
mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
|
|
if (err) {
|
|
if (err) {
|
|
console.log(err);
|
|
console.log(err);
|
|
}
|
|
}
|
|
// https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
|
|
// https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
|
|
- _.each(names, function(name, index) {
|
|
|
|
|
|
+ for (var i = 0; i < names.length; i++) {
|
|
|
|
+ var name = names[i];
|
|
db.collection(""+name).find({ time: { $gte: from_str, $lte: to_str } }, { type: 1, time: 1, count:1, durations: 1, gauge: 1, set: 1, $slice: maxDataPoints }).sort({ time: 1 }).toArray(function (err, docs) {
|
|
db.collection(""+name).find({ time: { $gte: from_str, $lte: to_str } }, { type: 1, time: 1, count:1, durations: 1, gauge: 1, set: 1, $slice: maxDataPoints }).sort({ time: 1 }).toArray(function (err, docs) {
|
|
if (err) {
|
|
if (err) {
|
|
console.log(err);
|
|
console.log(err);
|
|
}
|
|
}
|
|
var result = {};
|
|
var result = {};
|
|
result[name] = new Array();
|
|
result[name] = new Array();
|
|
- _.each(docs, function (doc) {
|
|
|
|
|
|
+ for (var x = 0; x < docs.length; x++) {
|
|
|
|
+ var doc = docs[x];
|
|
var value = 0;
|
|
var value = 0;
|
|
if (doc.type == 'counters') {
|
|
if (doc.type == 'counters') {
|
|
value = doc.count;
|
|
value = doc.count;
|
|
@@ -107,41 +114,22 @@ app.all('/query', function (req, res) {
|
|
value = doc.set;
|
|
value = doc.set;
|
|
}
|
|
}
|
|
(result[name]).push([value, 1000 * doc.time]);
|
|
(result[name]).push([value, 1000 * doc.time]);
|
|
- });
|
|
|
|
- var data = {
|
|
|
|
- target: name,
|
|
|
|
- datapoints: result[name]
|
|
|
|
- };
|
|
|
|
- mongo_query_result.push(data);
|
|
|
|
- global_result = mongo_query_result;
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var keys = _.keys(result);
|
|
|
|
+ for (var x = 0; x < keys.length; x ++) {
|
|
|
|
+ var key = keys[x];
|
|
|
|
+ var data = {
|
|
|
|
+ target: key,
|
|
|
|
+ datapoints: result[key]
|
|
|
|
+ };
|
|
|
|
+ mongo_query_result.push(data);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
- if (index === names.length -1) {
|
|
|
|
- checkInterval = setInterval(checkResult, 1000);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
-function checkResult()
|
|
|
|
-{
|
|
|
|
- if (global_result.length !== global_names.length) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- clearInterval(checkInterval);
|
|
|
|
-
|
|
|
|
- if (global_res) {
|
|
|
|
- setCORSHeaders(global_res);
|
|
|
|
- global_res.json(global_result);
|
|
|
|
- global_res.end();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- global_res = null;
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
app.listen(8000);
|
|
app.listen(8000);
|
|
|
|
|
|
console.log("Server is listening to port 8000");
|
|
console.log("Server is listening to port 8000");
|