Luciano Andrade 8 роки тому
батько
коміт
f80bf443ca

+ 1 - 1
bin/make-release

@@ -3,5 +3,5 @@ for repo in "base" "ftth" "mapas"
 cd $repo
 git checkout $relesase
 git fetch
-git merge origin/master
+git merge origin/$relesase
 cd ..

+ 1 - 1
docker-compose.yml

@@ -113,7 +113,7 @@ services:
     links:
       - mysql:mysql
     build: 
-      context: ./stats/endpoint
+      context: ./stats/endpoint/mysql
 
   statsd:
     image: fd3-statsd-mysql-backend

+ 15 - 0
stats/endpoint/mysql/Dockerfile

@@ -0,0 +1,15 @@
+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
+
+EXPOSE 8000
+
+RUN npm install -g nodemon
+
+CMD npm install && nodemon index.js
+

+ 169 - 0
stats/endpoint/mysql/index.js

@@ -0,0 +1,169 @@
+var express = require('express');
+var morgan = require('morgan');
+var bodyParser = require('body-parser');
+var _ = require('lodash');
+var app = express();
+
+
+var mysql      = require('mysql');
+var connection =  mysql.createConnection({
+  host     : 'mysql',
+  user     : 'statsd',
+  password : '2uhyenoi2y0',
+  database : 'statsd_db'
+});
+
+app.use(morgan('combined'))
+
+app.use(bodyParser.json());
+
+var timeserie = [
+	{'target': 's1', 'datapoints':  [ [0,0], [1,0], [2,0], [3,0], [4,0] , [5,0], [6,0] , [7,0], [8,0], [9,0], [10,0], [11,0], [12,0], [13,0], [14,0], [15,0]]},
+	{'target': 's2', 'datapoints':  [ [15,0], [14,0], [13,0], [12,0], [11,0] , [10,0], [9,0] , [8,0], [7,0], [6,0], [5,0], [4,0], [3,0], [2,0], [1,0], [0,0]]},
+	{'target': 's3', 'datapoints':  [ [0,0], [1,0], [2,0], [3,0], [4,0] , [5,0], [6,0] , [7,0], [8,0], [9,0], [10,0], [11,0], [12,0], [13,0], [14,0], [15,0]]},
+	{'target': 's4', 'datapoints':  [ [15,0], [14,0], [13,0], [12,0], [11,0] , [10,0], [9,0] , [8,0], [7,0], [6,0], [5,0], [4,0], [3,0], [2,0], [1,0], [0,0]]},
+	{'target': 's5', 'datapoints':  [ [0,0], [1,0], [2,0], [3,0], [4,0] , [5,0], [6,0] , [7,0], [8,0], [9,0], [10,0], [11,0], [12,0], [13,0], [14,0], [15,0]]},
+	{'target': 's6', 'datapoints':  [ [15,0], [14,0], [13,0], [12,0], [11,0] , [10,0], [9,0] , [8,0], [7,0], [6,0], [5,0], [4,0], [3,0], [2,0], [1,0], [0,0]]}
+	]; //require('./series');
+
+var now = Date.now();
+
+for (var i = timeserie.length -1; i >= 0; i--) {
+  var series = timeserie[i];
+  var decreaser = 0;
+  for (var y = series.datapoints.length -1; y >= 0; y--) {
+    series.datapoints[y][1] = Math.round((now - decreaser) /1000) * 1000;
+    decreaser += 50000;
+  }
+}
+
+var annotation = {
+  name : "annotation name",
+  enabled: true,
+  datasource: "generic datasource",
+  showLine: true,
+}
+
+var annotations = [
+  { annotation: annotation, "title": "Donlad trump is kinda funny", "time": 1450754160000, text: "teeext", tags: "taaags" },
+  { annotation: annotation, "title": "Wow he really won", "time": 1450754160000, text: "teeext", tags: "taaags" },
+  { annotation: annotation, "title": "When is the next ", "time": 1450754160000, text: "teeext", tags: "taaags" }
+];
+
+var now = Date.now();
+var decreaser = 0;
+for (var i = 0;i < annotations.length; i++) {
+  var anon = annotations[i];
+  anon.time = (now - decreaser);
+  decreaser += 1000000
+}
+
+var table =
+  {
+    columns: [{text: 'Time', type: 'time'}, {text: 'Country', type: 'string'}, {text: 'Number', type: 'number'}],
+    values: [
+      [ 1234567, 'SE', 123 ],
+      [ 1234567, 'DE', 231 ],
+      [ 1234567, 'US', 321 ],
+    ]
+  };
+  
+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");  
+}
+
+
+var now = Date.now();
+var decreaser = 0;
+for (var i = 0;i < table.values.length; i++) {
+  var anon = table.values[i];
+
+  anon[0] = (now - decreaser);
+  decreaser += 1000000
+}
+
+app.all('/', function(req, res) {
+  setCORSHeaders(res);
+  res.send('https://grafana.com/plugins/grafana-simple-json-datasource\n');
+  res.end();
+});
+
+app.all('/search', function(req, res){
+  setCORSHeaders(res);
+  var result = [];
+
+  connection.query('SELECT `name` FROM `gauges_statistics` GROUP BY `name` ORDER BY `name`', function(err, rows, fields) {
+  if (err) throw err;
+	  _.each(rows, function(ts) {
+		  result.push(ts.name);
+	  });
+	  res.json(result);
+	  res.end();
+  });
+
+});
+
+/**
+ * Este metodo no esta implementado.
+ */
+//app.all('/annotations', function(req, res) {
+//  setCORSHeaders(res);
+//  console.log(req.url);
+//  console.log(req.body);
+//
+//  res.json(annotations);
+//  res.end();
+//})
+
+/**
+ * @todo :  intervalMs: 500,
+ *          targets: [ { 
+ * 		type: 'timeserie' 
+ * 	    }]
+ * 	    maxDataPoints: 1920,
+ * 
+ */
+app.all('/query', function(req, res){
+  setCORSHeaders(res);
+  console.log(req.url);
+  console.log(req.body);
+
+  var tsResult = [];
+
+  var from = new Date(req.body.range.from);
+  var to = new Date(req.body.range.to);
+  var from_str = Math.floor(from.getTime()/1000);
+  var to_str = Math.floor(to.getTime()/1000);
+  var names = _.map(req.body.targets, function(t){
+	return t.target
+	console.log(t);
+  });
+  var sql = 'SELECT * FROM `gauges_statistics` WHERE (timestamp BETWEEN ' + from_str +' AND ' + to_str + ') AND (`name` IN ("' + names.join('", "')  + '")) ORDER BY timestamp ASC';
+	  console.log(sql);
+	  connection.query(sql, function(err, rows, fields) {
+		var result = {};
+		_.each(rows, function (d){
+			if(!(d.name in result)){
+				result[d.name] = new Array();
+                        }
+			(result[d.name]).push([d.value, 1000 * d.timestamp]);
+                });
+
+		_.each(_.keys(result), function (d){
+			var data = {target: d};
+			data['datapoints'] = result[d];
+			tsResult.push(data);
+		});
+
+		res.json(tsResult);
+		res.end();
+
+  });
+
+});
+
+app.listen(8000);
+
+console.log("Server is listening to port 8000");

+ 20 - 0
stats/endpoint/mysql/package.json

@@ -0,0 +1,20 @@
+{
+  "name": "statsd_mysql_grafana_datasource",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "dependencies": {
+    "body-parser": "^1.15.1",
+    "express": "^4.13.4",
+    "lodash": "^4.13.1",
+    "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"
+}