123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 'use strict';
- var express = require('express');
- var morgan = require('morgan');
- var bodyParser = require('body-parser');
- var _ = require('lodash');
- var app = express();
- var mongo = require('mongodb'),
- MongoClient = mongo.MongoClient,
- async = require('async'),
- util = require('util'),
- dbs = {},
- options = {
- debug: false,
- prefix: true,
- size: 100,
- max: 2610,
- name: 'statsd',
- host: 'mongodb',
- port: 27017
- };
- 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();
- });
- app.all('/search', function (req, res) {
- mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
- if (err) {
- console.log(err);
- }
- if (options.debug) {
- console.log("Connected successfully to server");
- }
- db.listCollections().toArray(function(err, collInfos) {
- var mongo_search_result = [];
- _.each(collInfos, function(collInfo) {
- if (mongo_search_result.indexOf(collInfo.name) === -1) {
- mongo_search_result.push(collInfo.name);
- }
- });
-
- setCORSHeaders(res);
- res.json(mongo_search_result);
- res.end();
- });
- });
- });
- app.all('/query', function (req, res) {
- var mongo_query_result = [];
- 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;
- });
- 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) {
- 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();
- for (var x = 0; x < docs.length; x++) {
- var doc = docs[x];
- var value = 0;
- if (doc.type == 'counters') {
- value = doc.count;
- } else if (doc.type == 'timers') {
- value = doc.durations;
- } else if (doc.type == 'gauges') {
- value = doc.gauge;
- } else if (doc.type == 'sets') {
- value = doc.set;
- }
- (result[name]).push([value, 1000 * doc.time]);
- }
- var data = {
- target: name,
- datapoints: result[name]
- };
- mongo_query_result.push(data);
- });
- });
-
- });
- });
- app.listen(8000);
- console.log("Server is listening to port 8000");
|