|
@@ -25,11 +25,9 @@ app.use(morgan('combined'));
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
function setCORSHeaders(res) {
|
|
|
- if (res) {
|
|
|
- res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
- res.setHeader("Access-Control-Allow-Methods", "POST");
|
|
|
- res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
|
|
|
- }
|
|
|
+ res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ res.setHeader("Access-Control-Allow-Methods", "POST");
|
|
|
+ res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
|
|
|
}
|
|
|
|
|
|
app.all('/', function(req, res) {
|
|
@@ -39,8 +37,6 @@ 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);
|
|
@@ -51,7 +47,7 @@ app.all('/search', function (req, res) {
|
|
|
db.listCollections().toArray(function(err, collInfos) {
|
|
|
var mongo_search_result = [];
|
|
|
_.each(collInfos, function(collInfo) {
|
|
|
- if (collInfo.name.indexOf(target) && mongo_search_result.indexOf(collInfo.name) === -1) {
|
|
|
+ if (mongo_search_result.indexOf(collInfo.name) === -1) {
|
|
|
mongo_search_result.push(collInfo.name);
|
|
|
}
|
|
|
});
|
|
@@ -72,8 +68,6 @@ 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;
|
|
|
|
|
@@ -93,8 +87,7 @@ app.all('/query', function (req, res) {
|
|
|
console.log(err);
|
|
|
}
|
|
|
// https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
|
|
|
- for (var i = 0; i < names.length; i++) {
|
|
|
- var name = names[i];
|
|
|
+ _.each(names, function(name) {
|
|
|
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) {
|
|
|
console.log(err);
|
|
@@ -115,21 +108,18 @@ app.all('/query', function (req, res) {
|
|
|
}
|
|
|
(result[name]).push([value, 1000 * doc.time]);
|
|
|
}
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
+
|
|
|
+ var data = {
|
|
|
+ target: name,
|
|
|
+ datapoints: result[name]
|
|
|
+ };
|
|
|
+ mongo_query_result.push(data);
|
|
|
});
|
|
|
- }
|
|
|
+ });
|
|
|
+
|
|
|
});
|
|
|
});
|
|
|
|
|
|
app.listen(8000);
|
|
|
|
|
|
-console.log("Server is listening to port 8000");
|
|
|
+console.log("Server is listening to port 8000");
|