graphiteSrv.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'jquery',
  5. 'config',
  6. 'kbn'
  7. ],
  8. function (angular, _, $, config, kbn) {
  9. 'use strict';
  10. var module = angular.module('kibana.services');
  11. module.service('graphiteSrv', function($http, $q, filterSrv) {
  12. this.query = function(options) {
  13. try {
  14. var graphOptions = {
  15. from: this.translateTime(options.range.from),
  16. until: this.translateTime(options.range.to),
  17. targets: options.targets,
  18. maxDataPoints: options.maxDataPoints
  19. };
  20. var params = buildGraphitePostParams(graphOptions);
  21. return doGraphiteRequest({
  22. method: 'POST',
  23. url: config.graphiteUrl + '/render/',
  24. data: params.join('&'),
  25. headers: {
  26. 'Content-Type': 'application/x-www-form-urlencoded',
  27. }
  28. });
  29. }
  30. catch(err) {
  31. return $q.reject(err);
  32. }
  33. };
  34. this.translateTime = function(date) {
  35. if (_.isString(date)) {
  36. if (date === 'now') {
  37. return 'now';
  38. }
  39. else if (date.indexOf('now') >= 0) {
  40. date = date.substring(3);
  41. date = date.replace('m', 'min');
  42. date = date.replace('M', 'mon');
  43. return date;
  44. }
  45. date = kbn.parseDate(date);
  46. }
  47. date = moment.utc(date).local();
  48. if (config.timezoneOffset) {
  49. date = date.zone(config.timezoneOffset)
  50. }
  51. return date.format('HH:mm_YYYYMMDD');
  52. };
  53. this.match = function(targets, graphiteTargetStr) {
  54. var found = targets[0];
  55. for (var i = 0; i < targets.length; i++) {
  56. if (targets[i].target === graphiteTargetStr) {
  57. found = targets[i];
  58. break;
  59. }
  60. if(targets[i].target.match("'" + graphiteTargetStr + "'")) {
  61. found = targets[i];
  62. }
  63. }
  64. return found;
  65. };
  66. this.metricFindQuery = function(query) {
  67. var interpolated;
  68. try {
  69. interpolated = filterSrv.applyFilterToTarget(query);
  70. }
  71. catch(err) {
  72. return $q.reject(err);
  73. }
  74. var url = config.graphiteUrl + '/metrics/find/?query=' + interpolated;
  75. return doGraphiteRequest({method: 'GET', url: url})
  76. .then(function(results) {
  77. return _.map(results.data, function(metric) {
  78. return {
  79. text: metric.text,
  80. expandable: metric.expandable ? true : false
  81. };
  82. });
  83. });
  84. };
  85. this.listDashboards = function(query) {
  86. var url = config.graphiteUrl + '/dashboard/find/';
  87. return doGraphiteRequest({ method: 'GET', url: url, params: {query: query || ''} })
  88. .then(function(results) {
  89. return results.data.dashboards;
  90. });
  91. };
  92. this.loadDashboard = function(dashName) {
  93. var url = config.graphiteUrl + '/dashboard/load/' + encodeURIComponent(dashName);
  94. return doGraphiteRequest({method: 'GET', url: url});
  95. };
  96. function doGraphiteRequest(options) {
  97. if (config.graphiteBasicAuth) {
  98. options.withCredentials = true;
  99. options.headers = options.headers || {};
  100. options.headers.Authorization = 'Basic ' + config.graphiteBasicAuth;
  101. }
  102. return $http(options);
  103. }
  104. function buildGraphitePostParams(options) {
  105. var clean_options = [];
  106. var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints'];
  107. options['format'] = 'json';
  108. $.each(options, function (key, value) {
  109. if ($.inArray(key, graphite_options) === -1) {
  110. return;
  111. }
  112. if (key === "targets") {
  113. $.each(value, function (index, value) {
  114. if (!value.hide) {
  115. var targetValue = filterSrv.applyFilterToTarget(value.target);
  116. clean_options.push("target=" + encodeURIComponent(targetValue));
  117. }
  118. });
  119. }
  120. else if (value !== null) {
  121. clean_options.push(key + "=" + encodeURIComponent(value));
  122. }
  123. });
  124. return clean_options;
  125. }
  126. });
  127. });