瀏覽代碼

Fix json endpoints

Guillermo Espinoza 7 年之前
父節點
當前提交
2ec5be5af2

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+tools/vendor
+statsd/endpoint/json/node_modules/
+statsd/endpoint/mongodb/node_modules/
+statsd/endpoint/mysql/node_modules/

+ 2 - 1
statsd/endpoint/json/Dockerfile

@@ -13,5 +13,6 @@ EXPOSE 8000
 
 RUN npm install -g nodemon
 
-CMD npm install && nodemon index.js
+RUN npm install
 
+CMD nodemon index.js

+ 2 - 1
statsd/endpoint/mongodb/Dockerfile

@@ -13,5 +13,6 @@ EXPOSE 8000
 
 RUN npm install -g nodemon
 
-CMD npm install && nodemon index.js
+RUN npm install
 
+CMD nodemon index.js

+ 35 - 15
statsd/endpoint/mongodb/index.js

@@ -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");

+ 11 - 2
statsd/endpoint/mongodb/package-lock.json

@@ -53,6 +53,11 @@
       "resolved": "https://registry.npmjs.org/arrify/-/arrify-1.0.1.tgz",
       "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0="
     },
+    "async": {
+      "version": "2.5.0",
+      "resolved": "https://registry.npmjs.org/async/-/async-2.5.0.tgz",
+      "integrity": "sha512-e+lJAJeNWuPCNyxZKOBdaJGyLGHugXVQtrAwtuAe2vhxTYxFTKE73p8JuTmdH0qdQZtDvI4dhJwjZc5zsfIsYw=="
+    },
     "async-each": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.1.tgz",
@@ -1307,8 +1312,7 @@
     "nan": {
       "version": "2.6.2",
       "resolved": "https://registry.npmjs.org/nan/-/nan-2.6.2.tgz",
-      "integrity": "sha1-5P805slf37WuzAjeZZb0NgWn20U=",
-      "optional": true
+      "integrity": "sha1-5P805slf37WuzAjeZZb0NgWn20U="
     },
     "negotiator": {
       "version": "0.6.1",
@@ -1620,6 +1624,11 @@
       "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.0.3.tgz",
       "integrity": "sha1-ZlZ+NwQ+608E2RvWWMDL77VbjgQ="
     },
+    "sleep": {
+      "version": "5.1.1",
+      "resolved": "https://registry.npmjs.org/sleep/-/sleep-5.1.1.tgz",
+      "integrity": "sha1-h4+h1E0I7rDyb7IBjvhinrGjq5Q="
+    },
     "slide": {
       "version": "1.1.6",
       "resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz",

+ 2 - 2
statsd/endpoint/mongodb/package.json

@@ -4,14 +4,14 @@
   "description": "",
   "main": "index.js",
   "dependencies": {
+    "async": "^2.5.0",
     "body-parser": "^1.15.1",
     "express": "^4.15.3",
     "lodash": "^4.13.1",
     "mongodb": "^2.2.29",
     "morgan": "^1.8.1",
     "mysql": "^2.13.0",
-    "nodemon": "^1.11.0",
-    "async": "2.5.0"
+    "nodemon": "^1.11.0"
   },
   "devDependencies": {},
   "scripts": {

+ 2 - 1
statsd/endpoint/mysql/Dockerfile

@@ -13,5 +13,6 @@ EXPOSE 8000
 
 RUN npm install -g nodemon
 
-CMD npm install && nodemon index.js
+RUN npm install
 
+CMD nodemon index.js

+ 14 - 16
statsd/endpoint/mysql/index.js

@@ -29,9 +29,10 @@ app.all('/', function(req, res) {
   res.end();
 });
 
-function search(table, res)
+function search(table, mysql_search_result, value, res)
 {
-    connection.query('SELECT DISTINCT(`name`) FROM `' + table + '` WHERE `name` NOT IN ("' + mysql_search_result.join('", "') + '") ORDER BY `name`', function (err, rows, fields) {
+//    connection.query('SELECT DISTINCT(`name`) FROM `' + table + '` WHERE `name` NOT IN ("' + mysql_search_result.join('", "') + '") ORDER BY `name`', function (err, rows, fields) {
+    connection.query('SELECT DISTINCT(`name`) FROM `' + table + '` WHERE `name` LIKE ("%' + value + '%") ORDER BY `name`', function (err, rows, fields) {
         if (err) {
             console.log(err);
             throw err;
@@ -41,25 +42,22 @@ function search(table, res)
                 mysql_search_result.push(rows[i].name);
             }
         }
+        if (res) {
+            setCORSHeaders(res);
+            res.json(mysql_search_result);
+            res.end();
+        }
     });
 }
 
-var mysql_search_result = [];
-
-search('gauges_statistics');
-search('counters_statistics');
-search('timers_statistics');
-search('sets_statistics');
-
 app.all('/search', function (req, res) {
-    setCORSHeaders(res);
-    res.json(mysql_search_result);
-    res.end();
+    var mysql_search_result = [];
+    var target = req.body.target; // metric
     
-    search('gauges_statistics');
-    search('counters_statistics');
-    search('timers_statistics');
-    search('sets_statistics');
+    search('gauges_statistics', mysql_search_result, target);
+    search('counters_statistics', mysql_search_result, target);
+    search('timers_statistics', mysql_search_result, target);
+    search('sets_statistics', mysql_search_result, target, res);
 });
 
 function query(table, req, mysql_query_result, res)