|
@@ -37,6 +37,8 @@ app.all('/', function(req, res) {
|
|
|
});
|
|
|
|
|
|
app.all('/search', function (req, res) {
|
|
|
+ var target = req.body.target;
|
|
|
+
|
|
|
mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
|
|
|
if (err) {
|
|
|
console.log(err);
|
|
@@ -47,7 +49,7 @@ app.all('/search', function (req, res) {
|
|
|
db.listCollections().toArray(function(err, collInfos) {
|
|
|
var mongo_search_result = [];
|
|
|
_.each(collInfos, function(collInfo) {
|
|
|
- if (mongo_search_result.indexOf(collInfo.name) === -1) {
|
|
|
+ if (collInfo.name.indexOf(target) && mongo_search_result.indexOf(collInfo.name) === -1) {
|
|
|
mongo_search_result.push(collInfo.name);
|
|
|
}
|
|
|
});
|
|
@@ -59,6 +61,12 @@ 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) {
|
|
|
var mongo_query_result = [];
|
|
|
var from = new Date(req.body.range.from);
|
|
@@ -68,6 +76,8 @@ app.all('/query', function (req, res) {
|
|
|
var names = _.map(req.body.targets, function (t) {
|
|
|
return t.target;
|
|
|
});
|
|
|
+ global_names = names;
|
|
|
+ global_res = res;
|
|
|
var interval = req.body.intervalMs / 1000;
|
|
|
var maxDataPoints = req.body.maxDataPoints;
|
|
|
|
|
@@ -96,26 +106,36 @@ app.all('/query', function (req, res) {
|
|
|
}
|
|
|
(result[name]).push([value, 1000 * doc.time]);
|
|
|
});
|
|
|
-
|
|
|
- _.each(_.keys(result), function (key) {
|
|
|
- var data = {
|
|
|
- target: key,
|
|
|
- datapoints: result[key]
|
|
|
- };
|
|
|
- mongo_query_result.push(data);
|
|
|
- });
|
|
|
-
|
|
|
- if (index === names.length -1) {
|
|
|
- setCORSHeaders(res);
|
|
|
- res.json(mongo_query_result);
|
|
|
- res.end();
|
|
|
- }
|
|
|
+ var data = {
|
|
|
+ target: name,
|
|
|
+ datapoints: result[name]
|
|
|
+ };
|
|
|
+ mongo_query_result.push(data);
|
|
|
+ global_result = mongo_query_result;
|
|
|
});
|
|
|
+ if (index === names.length -1) {
|
|
|
+ checkInterval = setInterval(checkResult, 1000);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+function checkResult()
|
|
|
+{
|
|
|
+ if (global_result.length !== global_names.length) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ setCORSHeaders(global_res);
|
|
|
+ global_res.json(global_result);
|
|
|
+ global_res.end();
|
|
|
+
|
|
|
+ clearInterval(checkInterval);
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
app.listen(8000);
|
|
|
|
|
|
console.log("Server is listening to port 8000");
|