Explorar o código

Json Endpoint

Guillermo Espinoza %!s(int64=8) %!d(string=hai) anos
pai
achega
85a170efcb

+ 11 - 0
docker-compose.yml

@@ -149,6 +149,7 @@ services:
     image: grafana/grafana
     links:
      - jsonendpoint:endpoint
+     - mysql_jsonendpoint:endpoint
      - mongodb_jsonendpoint:endpoint
      - mysql:mysql
     ports:
@@ -168,6 +169,16 @@ services:
 
 
   jsonendpoint:
+    restart: always
+    image: fd3_simple_json_endpoint
+    ports:
+      - 9003:8000
+    build: 
+      context: ./statsd/endpoint/json
+    volumes:
+      - ./statsd/endpoint/json:/opt/datasource
+      
+  mysql_jsonendpoint:
     restart: always
     image: fd3_simple_json_endpoint_mysql
     environment:

+ 17 - 9
statsd/backend/mongodb/mongodb.js

@@ -45,10 +45,11 @@ var aggregate = {
 	gauges: function(time, key, val) {
 		return {
 			db: dbPrefix(key),
-			col: colPrefix('gauges', key),
+			col: key,
 			data: {
 				time: time,
-				gauge: val
+				gauge: val,
+                                type: 'gauges'
 			},
 		};
 	},
@@ -63,7 +64,11 @@ var aggregate = {
 		return {
 			db: dbPrefix(key),
 			col: colPrefix('timers', key),
-			data: val
+			data: {
+                            time: time,
+                            durations: val,
+                            type: 'timers'
+                        }
 		};
 	},
 	/**
@@ -75,10 +80,11 @@ var aggregate = {
 	timers: function(time, key, val) {
 		return {
 			db: dbPrefix(key),
-			col: colPrefix('timers', key),
+			col: key,
 			data: {
 				time: time,
-				durations: val
+				durations: val,
+                                type: 'timers'
 			},
 		};
 	},
@@ -91,10 +97,11 @@ var aggregate = {
 	counters: function(time, key, val) {
 		return {
 			db: dbPrefix(key),
-			col: colPrefix('counters', key),
+			col: key,
 			data: {
 				time: time,
-				count: val
+				count: val,
+                                type: 'counters'
 			},
 		};
 	},
@@ -107,10 +114,11 @@ var aggregate = {
 	sets: function(time, key, val) {
 		return {
 			db: dbPrefix(key),
-			col: colPrefix('sets', key),
+			col: key,
 			data: {
 				time: time,
-				set: val
+				set: val,
+                                type: 'sets'
 			},
 		};
 	}

+ 17 - 0
statsd/endpoint/json/Dockerfile

@@ -0,0 +1,17 @@
+FROM debian:8
+RUN apt-get update && apt-get install -yq git wget tmux vim nodejs npm
+
+RUN npm install -g n
+
+RUN n stable
+
+WORKDIR /opt/datasource
+COPY index.js /opt/datasource/index.js
+COPY package.json /opt/datasource/package.json
+
+EXPOSE 8000
+
+RUN npm install -g nodemon
+
+CMD npm install && nodejs index.js
+

+ 94 - 0
statsd/endpoint/json/index.js

@@ -0,0 +1,94 @@
+'use strict';
+
+var express = require('express');
+var morgan = require('morgan');
+var bodyParser = require('body-parser');
+var _ = require('lodash');
+var app = express();
+
+var request = require('request');
+
+app.use(morgan('combined'));
+app.use(bodyParser.json());
+
+function setCORSHeaders(res) {
+  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) {
+  setCORSHeaders(res);
+  res.send('https://grafana.com/plugins/grafana-simple-json-datasource\n');
+  res.end();
+});
+
+function search(url, body, res)
+{
+    request.post(
+        url,
+        body,
+        function (error, response, body) {
+            if (!error && response.statusCode == 200) {
+                if (result.length == 0) {
+                    result = JSON.parse(body);
+                } else {
+                    result.concat(JSON.parse(body));
+                }
+                
+                if (res) {
+                    setCORSHeaders(res);
+                    res.json(result);
+                    res.end();
+                }
+            } else {
+                console.log(error);
+            }
+        }
+    );
+}
+
+function query(url, req, res)
+{
+    request.post({
+        url: url,
+        headers: req.headers,
+        body: JSON.stringify(req.body)
+    },
+        function (error, response, body) {
+            if (!error && response.statusCode == 200) {
+                if (queryResult.length == 0) {
+                    queryResult = JSON.parse(body);
+                } else {
+                    queryResult.concat(JSON.parse(body));
+                }
+                
+                if (res) {
+                    setCORSHeaders(res);
+                    res.json(queryResult);
+                    res.end();
+                }
+            } else {
+                console.log(error);
+            }
+        }
+    );
+}
+
+var result = [];
+app.all('/search', function (req, res) {
+    result = [];
+    search('http://mysql_jsonendpoint:8000/search', req.body);
+    search('http://mongodb_jsonendpoint:8000/search', req.body, res);
+});
+
+var queryResult = [];
+app.all('/query', function (req, res) {
+    queryResult = [];
+    query('http://mysql_jsonendpoint:8000/query', req);
+    query('http://mongodb_jsonendpoint:8000/query', req, res);
+});
+
+app.listen(8000);
+
+console.log("Server is listening to port 8000");

+ 21 - 0
statsd/endpoint/json/package.json

@@ -0,0 +1,21 @@
+{
+  "name": "statsd_json_grafana_datasource",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "dependencies": {
+    "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"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC"
+}

+ 6 - 6
statsd/endpoint/mongodb/index.js

@@ -48,7 +48,7 @@ app.all('/search', function (req, res) {
         }
         db.listCollections().toArray(function(err, collInfos) {
             result = [];
-            for (i = 0; i <= collInfos.length -1; i++) {
+            for (var i = 0; i <= collInfos.length -1; i++) {
                 if (collInfos[i]) {
                     result.push(collInfos[i].name);
                 }
@@ -77,7 +77,7 @@ app.all('/query', function (req, res) {
             console.log(err);
         }
 //        https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
-        db.collection(name).find({ time: { $gte: from_str, $lte: to_str } }, { 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) {
                 console.log(err);
             }
@@ -85,13 +85,13 @@ app.all('/query', function (req, res) {
             result[name] = new Array();
             _.each(docs, function (d) {
                 var value = 0;
-                if (name.indexOf('counters')>= 0) {
+                if (d.type == 'counters') {
                     value = d.count;
-                } else if (name.indexOf('timers')>= 0) {
+                } else if (d.type == 'timers') {
                     value = d.durations;
-                } else if (name.indexOf('gauges')>= 0) {
+                } else if (d.type == 'gauges') {
                     value = d.gauge;
-                } else if (name.indexOf('sets')>= 0) {
+                } else if (d.type == 'sets') {
                     value = d.set;
                 }
                 (result[name]).push([value, 1000 * d.time]);