index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // vars global
  58. var global_result;
  59. var global_res;
  60. var global_names;
  61. var checkInterval;
  62. app.all('/query', function (req, res) {
  63. var mongo_query_result = [];
  64. var from = new Date(req.body.range.from);
  65. var to = new Date(req.body.range.to);
  66. var from_str = Math.floor(from.getTime() / 1000);
  67. var to_str = Math.floor(to.getTime() / 1000);
  68. var names = _.map(req.body.targets, function (t) {
  69. return t.target;
  70. });
  71. global_names = names;
  72. global_res = res;
  73. var interval = req.body.intervalMs / 1000;
  74. var maxDataPoints = req.body.maxDataPoints;
  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, index) {
  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. _.each(docs, function (doc) {
  88. var value = 0;
  89. if (doc.type == 'counters') {
  90. value = doc.count;
  91. } else if (doc.type == 'timers') {
  92. value = doc.durations;
  93. } else if (doc.type == 'gauges') {
  94. value = doc.gauge;
  95. } else if (doc.type == 'sets') {
  96. value = doc.set;
  97. }
  98. (result[name]).push([value, 1000 * doc.time]);
  99. });
  100. var data = {
  101. target: name,
  102. datapoints: result[name]
  103. };
  104. mongo_query_result.push(data);
  105. global_result = mongo_query_result;
  106. });
  107. if (index === names.length -1) {
  108. checkInterval = setInterval(checkResult, 1000);
  109. }
  110. });
  111. });
  112. });
  113. function checkResult()
  114. {
  115. if (global_result.length !== global_names.length) {
  116. return false;
  117. }
  118. clearInterval(checkInterval);
  119. if (global_res) {
  120. setCORSHeaders(global_res);
  121. global_res.json(global_result);
  122. global_res.end();
  123. }
  124. global_res = null;
  125. return true;
  126. }
  127. app.listen(8000);
  128. console.log("Server is listening to port 8000");