graphiteDatasource.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'jquery',
  5. 'config',
  6. 'kbn',
  7. 'moment'
  8. ],
  9. function (angular, _, $, config, kbn, moment) {
  10. 'use strict';
  11. var module = angular.module('kibana.services');
  12. module.factory('GraphiteDatasource', function(dashboard, $q, $http) {
  13. function GraphiteDatasource(datasource) {
  14. this.type = 'graphite';
  15. this.basicAuth = datasource.basicAuth;
  16. this.url = datasource.url;
  17. this.editorSrc = 'app/partials/graphite/editor.html';
  18. this.name = datasource.name;
  19. this.render_method = datasource.render_method || 'POST';
  20. }
  21. GraphiteDatasource.prototype.query = function(filterSrv, options) {
  22. try {
  23. var graphOptions = {
  24. from: this.translateTime(options.range.from, 'round-down'),
  25. until: this.translateTime(options.range.to, 'round-up'),
  26. targets: options.targets,
  27. format: options.format,
  28. maxDataPoints: options.maxDataPoints,
  29. };
  30. var params = this.buildGraphiteParams(filterSrv, graphOptions);
  31. if (options.format === 'png') {
  32. return $q.when(this.url + '/render' + '?' + params.join('&'));
  33. }
  34. var httpOptions = { method: this.render_method, url: '/render' };
  35. if (httpOptions.method === 'GET') {
  36. httpOptions.url = httpOptions.url + '?' + params.join('&');
  37. }
  38. else {
  39. httpOptions.data = params.join('&');
  40. httpOptions.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
  41. }
  42. return this.doGraphiteRequest(httpOptions);
  43. }
  44. catch(err) {
  45. return $q.reject(err);
  46. }
  47. };
  48. GraphiteDatasource.prototype.events = function(options) {
  49. try {
  50. var tags = '';
  51. if (options.tags) {
  52. tags = '&tags=' + options.tags;
  53. }
  54. return this.doGraphiteRequest({
  55. method: 'GET',
  56. url: '/events/get_data?from=' + this.translateTime(options.range.from) + '&until=' + this.translateTime(options.range.to) + tags,
  57. });
  58. }
  59. catch(err) {
  60. return $q.reject(err);
  61. }
  62. };
  63. GraphiteDatasource.prototype.translateTime = function(date, rounding) {
  64. if (_.isString(date)) {
  65. if (date === 'now') {
  66. return 'now';
  67. }
  68. else if (date.indexOf('now') >= 0) {
  69. date = date.substring(3);
  70. date = date.replace('m', 'min');
  71. date = date.replace('M', 'mon');
  72. return date;
  73. }
  74. date = kbn.parseDate(date);
  75. }
  76. date = moment.utc(date);
  77. if (rounding === 'round-up') {
  78. if (date.get('s')) {
  79. date.add('m', 1);
  80. }
  81. }
  82. else if (rounding === 'round-down') {
  83. // graphite' s from filter is exclusive
  84. // here we step back one minute in order
  85. // to guarantee that we get all the data that
  86. // exists for the specified range
  87. if (date.get('s')) {
  88. date.subtract('m', 1);
  89. }
  90. }
  91. if (dashboard.current.timezone === 'browser') {
  92. date = date.local();
  93. }
  94. if (config.timezoneOffset) {
  95. date = date.zone(config.timezoneOffset);
  96. }
  97. return date.format('HH:mm_YYYYMMDD');
  98. };
  99. GraphiteDatasource.prototype.metricFindQuery = function(filterSrv, query) {
  100. var interpolated;
  101. try {
  102. interpolated = encodeURIComponent(filterSrv.applyTemplateToTarget(query));
  103. }
  104. catch(err) {
  105. return $q.reject(err);
  106. }
  107. return this.doGraphiteRequest({method: 'GET', url: '/metrics/find/?query=' + interpolated })
  108. .then(function(results) {
  109. return _.map(results.data, function(metric) {
  110. return {
  111. text: metric.text,
  112. expandable: metric.expandable ? true : false
  113. };
  114. });
  115. });
  116. };
  117. GraphiteDatasource.prototype.listDashboards = function(query) {
  118. return this.doGraphiteRequest({ method: 'GET', url: '/dashboard/find/', params: {query: query || ''} })
  119. .then(function(results) {
  120. return results.data.dashboards;
  121. });
  122. };
  123. GraphiteDatasource.prototype.loadDashboard = function(dashName) {
  124. return this.doGraphiteRequest({method: 'GET', url: '/dashboard/load/' + encodeURIComponent(dashName) });
  125. };
  126. GraphiteDatasource.prototype.doGraphiteRequest = function(options) {
  127. if (this.basicAuth) {
  128. options.withCredentials = true;
  129. options.headers = options.headers || {};
  130. options.headers.Authorization = 'Basic ' + this.basicAuth;
  131. }
  132. options.url = this.url + options.url;
  133. return $http(options);
  134. };
  135. GraphiteDatasource.prototype.buildGraphiteParams = function(filterSrv, options) {
  136. var clean_options = [];
  137. var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints'];
  138. if (options.format !== 'png') {
  139. options['format'] = 'json';
  140. }
  141. _.each(options, function (value, key) {
  142. if ($.inArray(key, graphite_options) === -1) {
  143. return;
  144. }
  145. if (key === "targets") {
  146. _.each(value, function (value) {
  147. if (value.target && !value.hide) {
  148. var targetValue = filterSrv.applyTemplateToTarget(value.target);
  149. clean_options.push("target=" + encodeURIComponent(targetValue));
  150. }
  151. }, this);
  152. }
  153. else if (value !== null) {
  154. clean_options.push(key + "=" + encodeURIComponent(value));
  155. }
  156. }, this);
  157. return clean_options;
  158. };
  159. return GraphiteDatasource;
  160. });
  161. });