index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. var express = require('express');
  3. var morgan = require('morgan');
  4. var bodyParser = require('body-parser');
  5. var _ = require('lodash');
  6. var app = express();
  7. var mongo = require('mongodb'),
  8. MongoClient = mongo.MongoClient,
  9. async = require('async'),
  10. util = require('util'),
  11. dbs = {},
  12. options = {
  13. debug: false,
  14. prefix: true,
  15. size: 100,
  16. max: 2610,
  17. name: 'statsd',
  18. host: 'mongodb',
  19. port: 27017
  20. };
  21. app.use(morgan('combined'));
  22. app.use(bodyParser.json());
  23. function setCORSHeaders(res) {
  24. res.setHeader("Access-Control-Allow-Origin", "*");
  25. res.setHeader("Access-Control-Allow-Methods", "POST");
  26. res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
  27. }
  28. app.all('/', function(req, res) {
  29. setCORSHeaders(res);
  30. res.send('https://grafana.com/plugins/grafana-simple-json-datasource\n');
  31. res.end();
  32. });
  33. app.all('/search', function (req, res) {
  34. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  35. if (err) {
  36. console.log(err);
  37. }
  38. if (options.debug) {
  39. console.log("Connected successfully to server");
  40. }
  41. db.listCollections().toArray(function(err, collInfos) {
  42. var mongo_search_result = [];
  43. _.each(collInfos, function(collInfo) {
  44. if (mongo_search_result.indexOf(collInfo.name) === -1) {
  45. mongo_search_result.push(collInfo.name);
  46. }
  47. });
  48. setCORSHeaders(res);
  49. res.json(mongo_search_result);
  50. res.end();
  51. });
  52. });
  53. });
  54. app.all('/query', function (req, res) {
  55. var mongo_query_result = [];
  56. var from = new Date(req.body.range.from);
  57. var to = new Date(req.body.range.to);
  58. var from_str = Math.floor(from.getTime() / 1000);
  59. var to_str = Math.floor(to.getTime() / 1000);
  60. var names = _.map(req.body.targets, function (t) {
  61. return t.target;
  62. });
  63. var interval = req.body.intervalMs / 1000;
  64. var maxDataPoints = req.body.maxDataPoints;
  65. // cada 1s se chequea si hay resultados y se retorna
  66. setInterval(function () {
  67. if (mongo_query_result.length == names.length) {
  68. if (!res._headerSent) {
  69. setCORSHeaders(res);
  70. res.json(mongo_query_result);
  71. res.end();
  72. }
  73. }
  74. }, 1000);
  75. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  76. if (err) {
  77. console.log(err);
  78. }
  79. // https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
  80. _.each(names, function(name) {
  81. 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) {
  82. if (err) {
  83. console.log(err);
  84. }
  85. var result = {};
  86. result[name] = new Array();
  87. for (var x = 0; x < docs.length; x++) {
  88. var doc = docs[x];
  89. var value = 0;
  90. if (doc.type == 'counters') {
  91. value = doc.count;
  92. } else if (doc.type == 'timers') {
  93. value = doc.durations;
  94. } else if (doc.type == 'gauges') {
  95. value = doc.gauge;
  96. } else if (doc.type == 'sets') {
  97. value = doc.set;
  98. }
  99. (result[name]).push([value, 1000 * doc.time]);
  100. }
  101. var data = {
  102. target: name,
  103. datapoints: result[name]
  104. };
  105. mongo_query_result.push(data);
  106. });
  107. });
  108. });
  109. });
  110. app.listen(8000);
  111. console.log("Server is listening to port 8000");