'use strict'; var express = require('express'); var morgan = require('morgan'); var bodyParser = require('body-parser'); var app = express(); var curlify = require('request-as-curl'); var rp = require('request-promise-native'); var timeout = process.env.TIMEOUT; var hosts = []; 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); console.log(curlify(req, req.body)); res.send('https://grafana.com/plugins/grafana-simple-json-datasource\n'); res.end(); }); var search_result = []; /* app.all('/search', function(req, res) { console.log(curlify(req, req.body)); search_result = []; var results = hosts.map( function(host, index) { var options = { method: 'POST', uri: host + '/search', body: req.body, timeout: timeout, json: true }; return rp(options) .then(function(parsedBody) { if (search_result.length == 0) { search_result = parsedBody; } else { var values = parsedBody; for (var i = 0; i < values.length; i++) { if (search_result.indexOf(values[i]) === -1) { search_result.push(values[i]); } } search_result.sort(); } return search_result; }) .catch(function(err) { return err; }); } ); Promise.all(results).then(function(value) { setCORSHeaders(res); res.json(search_result); res.end(); }, reason => { console.log(reason) res.status(500).send(reason); }); }); */ app.all('/query', function(req, res) { console.log(curlify(req, req.body)); var data = req.body; var from = 'now'; var to = 'now+6'; var interval = '30s'; var intervalMS = 30000; var maxDataPoints = 50; var datapoints = []; var resp = []; if (data != null && data.range != null && data.range.from != null) { from = data.range.from; } if (data != null && data.range != null && data.range.to != null) { to = data.range.to; } if (data != null && data.interval != null) { interval = data.interval; } if (data != null && data.intervalMS != null) { intervalMS = data.intervalMS; } if (data != null && data.maxDataPoints != null) { maxDataPoints = data.maxDataPoints; } from = getTime(from); to = getTime(to); interval = getTimeInterval(interval); for (var i = 0; i < data.targets.length; i++) { var target = data.targets[i].target; target = target.replace(/\s/g, ''); if (target.indexOf('sin') !== -1) { datapoints = sin(from, to, interval, maxDataPoints, target); } else if (target.indexOf('const') !== -1) { datapoints = cons(from, to, interval, maxDataPoints, target); target = 'const'; } else if (target.indexOf('random') !== -1) { datapoints = random(from, to, interval, maxDataPoints, target); } else if (target.indexOf('saws') !== -1) { datapoints = saws(from, to, interval, maxDataPoints, target); } var obj = { "target": target, "datapoints": datapoints }; resp.push(obj); } setCORSHeaders(res); res.json(resp); res.end(); }); /** * Funcion que dibuja una sierra */ function saws(from, to, interval, maxDataPoints, target) { var datapoints = []; var min = 0; var max = 1; var variable = 0.25; var value = min; var values = getValuesOperations(target, 'saws'); if (values == null) { if (interval == 20000) { interval = interval * 20; } } else { var numbers = getNumbers(values); if (numbers.length > 0) { min = parseFloat(numbers[0]); value = min; } if (numbers.length > 1) { max = parseFloat(numbers[1]); } if (numbers.length >= 2) { variable = parseFloat(numbers[2]); } } for(var i = 0; i < maxDataPoints; i++) { datapoints.push([value, from]); if (value >= max) { value = min; datapoints.push([value, from]); i++; } value += variable; from = from + interval; } return datapoints; } /** * Funcion que genera puntos random */ function random(from, to, interval, maxDataPoints, target) { var datapoints = []; var min = 0; var max = 1; var values = getValuesOperations(target, 'random'); if (values == null) { if (interval == 20000) { interval = interval * 20; } } else { var numbers = getNumbers(values); if (numbers.length > 0) { min = parseFloat(numbers[0]); } if (numbers.length >= 1) { max = parseFloat(numbers[1]); } } for(var i = 0; i < maxDataPoints; i++) { datapoints.push([Math.floor(Math.random()*(max-min+1))+min, from]); from = from + interval; } return datapoints; } /** * Funcion que genera una constante */ function cons(from, to, interval, maxDataPoints, target) { var datapoints = []; var values = getValuesOperations(target, 'const'); for(var i = 0; i < maxDataPoints; i++) { if (values == null) { datapoints.push([1, from]); } else { datapoints.push([values, from]); } from = from + interval; } return datapoints; } /** * Funcion que dibuja un seno */ function sin(from, to, interval, maxDataPoints, target) { var datapoints = []; var values = getValuesOperations(target, 'sin'); var variable = values == null ? '' : getVariable(values); for(var i = 0; i < maxDataPoints; i++) { if (values == null) { datapoints.push([Math.sin(from), from]); } else { datapoints.push([Math.sin(eval(values.replace(variable, from))), from]); } from = from + interval; } return datapoints; } /** * Obtiene el conjunto de parametros que se recibieron. */ function getValuesOperations(target, func) { var rx = new RegExp(func + "\\((.*)\\)"); var arr = rx.exec(target); if (arr != null && arr.length > 0) { return arr[1]; } else { return null; } } /** * Obtiene el nombre de la variable que se paso como parametro. */ function getVariable(parameter) { var rx = /[a-zA-Z]*/g; var resp = rx.exec(parameter); return resp; } /** * Obtiene los valores de los parametros que se reciben. */ function getNumbers(parameters) { var arr = parameters.split(','); if (arr != null && arr.length > 0) { return arr; } else { return null; } } /** * Obtiene los milisegundos a partir del valor pasado como parametro. * Se puede sumar o restar valorres en horas */ function getTime(value) { if (value.indexOf('now') !== -1) { var time = new Date().getTime(); if (value.indexOf('+') !== -1) { var tmp = value.split('+'); if (tmp.length() > 0) { value = time + (tmp[1] * 3600000); } else { value = time; } } else if (to.indexOf('-') !== -1) { var tmp = value.split('-'); if (tmp.length() > 0) { value = time - (tmp[1] * 3600000); } else { value = time; } } else { value = time; } } else { // parseo el valor recibido value = new Date(value).getTime(); } return value; } /** * Se obtiene un intervalo de tiempo en milisegungos a partir de segundos o minutos. */ function getTimeInterval(value) { if (value.indexOf('s') !== -1) { value = parseInt(value.replace('s','')) * 1000; } else if (value.indexOf('m') !== -1) { value = parseInt(value.replace('m','')) * 1000 * 60; } return value; } app.listen(8000); console.log("Server is listening to port 8000");