index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. var target = req.body.target;
  35. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  36. if (err) {
  37. console.log(err);
  38. }
  39. if (options.debug) {
  40. console.log("Connected successfully to server");
  41. }
  42. db.listCollections().toArray(function(err, collInfos) {
  43. var mongo_search_result = [];
  44. _.each(collInfos, function(collInfo) {
  45. if (collInfo.name.indexOf(target) && mongo_search_result.indexOf(collInfo.name) === -1) {
  46. mongo_search_result.push(collInfo.name);
  47. }
  48. });
  49. setCORSHeaders(res);
  50. res.json(mongo_search_result);
  51. res.end();
  52. });
  53. });
  54. });
  55. // vars global
  56. var global_result;
  57. var global_res;
  58. var global_names;
  59. var checkInterval;
  60. app.all('/query', function (req, res) {
  61. var mongo_query_result = [];
  62. var from = new Date(req.body.range.from);
  63. var to = new Date(req.body.range.to);
  64. var from_str = Math.floor(from.getTime() / 1000);
  65. var to_str = Math.floor(to.getTime() / 1000);
  66. var names = _.map(req.body.targets, function (t) {
  67. return t.target;
  68. });
  69. global_names = names;
  70. global_res = res;
  71. var interval = req.body.intervalMs / 1000;
  72. var maxDataPoints = req.body.maxDataPoints;
  73. mongo.connect("mongodb://" + options.host + "/" + options.name, function (err, db) {
  74. if (err) {
  75. console.log(err);
  76. }
  77. // https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
  78. _.each(names, function(name, index) {
  79. 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) {
  80. if (err) {
  81. console.log(err);
  82. }
  83. var result = {};
  84. result[name] = new Array();
  85. _.each(docs, function (doc) {
  86. var value = 0;
  87. if (doc.type == 'counters') {
  88. value = doc.count;
  89. } else if (doc.type == 'timers') {
  90. value = doc.durations;
  91. } else if (doc.type == 'gauges') {
  92. value = doc.gauge;
  93. } else if (doc.type == 'sets') {
  94. value = doc.set;
  95. }
  96. (result[name]).push([value, 1000 * doc.time]);
  97. });
  98. var data = {
  99. target: name,
  100. datapoints: result[name]
  101. };
  102. mongo_query_result.push(data);
  103. global_result = mongo_query_result;
  104. });
  105. if (index === names.length -1) {
  106. checkInterval = setInterval(checkResult, 1000);
  107. }
  108. });
  109. });
  110. });
  111. function checkResult()
  112. {
  113. if (global_result.length !== global_names.length) {
  114. return false;
  115. }
  116. setCORSHeaders(global_res);
  117. global_res.json(global_result);
  118. global_res.end();
  119. clearInterval(checkInterval);
  120. return true;
  121. }
  122. app.listen(8000);
  123. console.log("Server is listening to port 8000");