index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (res) {
  25. res.setHeader("Access-Control-Allow-Origin", "*");
  26. res.setHeader("Access-Control-Allow-Methods", "POST");
  27. res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
  28. }
  29. }
  30. app.all('/', function(req, res) {
  31. setCORSHeaders(res);
  32. res.send('https://grafana.com/plugins/grafana-simple-json-datasource\n');
  33. res.end();
  34. });
  35. app.all('/search', function (req, res) {
  36. var target = req.body.target;
  37. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  38. if (err) {
  39. console.log(err);
  40. }
  41. if (options.debug) {
  42. console.log("Connected successfully to server");
  43. }
  44. db.listCollections().toArray(function(err, collInfos) {
  45. var mongo_search_result = [];
  46. _.each(collInfos, function(collInfo) {
  47. if (collInfo.name.indexOf(target) && mongo_search_result.indexOf(collInfo.name) === -1) {
  48. mongo_search_result.push(collInfo.name);
  49. }
  50. });
  51. setCORSHeaders(res);
  52. res.json(mongo_search_result);
  53. res.end();
  54. });
  55. });
  56. });
  57. app.all('/query', function (req, res) {
  58. var mongo_query_result = [];
  59. var from = new Date(req.body.range.from);
  60. var to = new Date(req.body.range.to);
  61. var from_str = Math.floor(from.getTime() / 1000);
  62. var to_str = Math.floor(to.getTime() / 1000);
  63. var names = _.map(req.body.targets, function (t) {
  64. return t.target;
  65. });
  66. global_names = names;
  67. global_res = res;
  68. var interval = req.body.intervalMs / 1000;
  69. var maxDataPoints = req.body.maxDataPoints;
  70. // cada 1s se chequea si hay resultados y se retorna
  71. setInterval(function () {
  72. if (mongo_query_result.length == names.length) {
  73. if (!res._headerSent) {
  74. setCORSHeaders(res);
  75. res.json(mongo_query_result);
  76. res.end();
  77. }
  78. }
  79. }, 1000);
  80. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  81. if (err) {
  82. console.log(err);
  83. }
  84. // https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
  85. for (var i = 0; i < names.length; i++) {
  86. var name = names[i];
  87. 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) {
  88. if (err) {
  89. console.log(err);
  90. }
  91. var result = {};
  92. result[name] = new Array();
  93. for (var x = 0; x < docs.length; x++) {
  94. var doc = docs[x];
  95. var value = 0;
  96. if (doc.type == 'counters') {
  97. value = doc.count;
  98. } else if (doc.type == 'timers') {
  99. value = doc.durations;
  100. } else if (doc.type == 'gauges') {
  101. value = doc.gauge;
  102. } else if (doc.type == 'sets') {
  103. value = doc.set;
  104. }
  105. (result[name]).push([value, 1000 * doc.time]);
  106. }
  107. var keys = _.keys(result);
  108. for (var x = 0; x < keys.length; x ++) {
  109. var key = keys[x];
  110. var data = {
  111. target: key,
  112. datapoints: result[key]
  113. };
  114. mongo_query_result.push(data);
  115. }
  116. });
  117. }
  118. });
  119. });
  120. app.listen(8000);
  121. console.log("Server is listening to port 8000");