Kaynağa Gözat

Fix mongoDb endpoint

Guillermo Espinoza 7 yıl önce
ebeveyn
işleme
5cb22023b3
1 değiştirilmiş dosya ile 27 ekleme ve 39 silme
  1. 27 39
      statsd/endpoint/mongodb/index.js

+ 27 - 39
statsd/endpoint/mongodb/index.js

@@ -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) {
     var mongo_query_result = [];
     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 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) {
         if (err) {
             console.log(err);
         }
 //        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) {
                 if (err) {
                     console.log(err);
                 }
                 var result = {};
                 result[name] = new Array();
-                _.each(docs, function (doc) {
+                for (var x = 0; x < docs.length; x++) {
+                    var doc = docs[x];
                     var value = 0;
                     if (doc.type == 'counters') {
                         value = doc.count;
@@ -107,41 +114,22 @@ app.all('/query', function (req, res) {
                         value = doc.set;
                     }
                     (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);
 
 console.log("Server is listening to port 8000");