graphiteDatasource.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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($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. this.supportAnnotations = true;
  21. this.annotationEditorSrc = 'app/partials/graphite/annotation_editor.html';
  22. }
  23. GraphiteDatasource.prototype.query = function(filterSrv, options) {
  24. try {
  25. var graphOptions = {
  26. from: this.translateTime(options.range.from, 'round-down'),
  27. until: this.translateTime(options.range.to, 'round-up'),
  28. targets: options.targets,
  29. format: options.format,
  30. maxDataPoints: options.maxDataPoints,
  31. };
  32. var params = this.buildGraphiteParams(filterSrv, graphOptions);
  33. if (options.format === 'png') {
  34. return $q.when(this.url + '/render' + '?' + params.join('&'));
  35. }
  36. var httpOptions = { method: this.render_method, url: '/render' };
  37. if (httpOptions.method === 'GET') {
  38. httpOptions.url = httpOptions.url + '?' + params.join('&');
  39. }
  40. else {
  41. httpOptions.data = params.join('&');
  42. httpOptions.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
  43. }
  44. return this.doGraphiteRequest(httpOptions);
  45. }
  46. catch(err) {
  47. return $q.reject(err);
  48. }
  49. };
  50. GraphiteDatasource.prototype.annotationQuery = function(annotation, filterSrv, rangeUnparsed) {
  51. // Graphite metric as annotation
  52. if (annotation.target) {
  53. var graphiteQuery = {
  54. range: rangeUnparsed,
  55. targets: [{ target: annotation.target }],
  56. format: 'json',
  57. maxDataPoints: 100
  58. };
  59. return this.query(filterSrv, graphiteQuery)
  60. .then(function(result) {
  61. var list = [];
  62. for (var i = 0; i < result.data.length; i++) {
  63. var target = result.data[i];
  64. for (var y = 0; y < target.datapoints.length; y++) {
  65. var datapoint = target.datapoints[y];
  66. if (!datapoint[0]) { continue; }
  67. list.push({
  68. annotation: annotation,
  69. time: datapoint[1] * 1000,
  70. title: target.target
  71. });
  72. }
  73. }
  74. return list;
  75. });
  76. }
  77. // Graphite event as annotation
  78. else if (annotation.tags) {
  79. return this.events({ range: rangeUnparsed, tags: annotation.tags })
  80. .then(function(results) {
  81. var list = [];
  82. for (var i = 0; i < results.data; i++) {
  83. list.push({
  84. annotation: annotation,
  85. time: event.when * 1000,
  86. title: event.what,
  87. tags: event.tags,
  88. text: event.data
  89. });
  90. }
  91. return list;
  92. });
  93. }
  94. };
  95. GraphiteDatasource.prototype.events = function(options) {
  96. try {
  97. var tags = '';
  98. if (options.tags) {
  99. tags = '&tags=' + options.tags;
  100. }
  101. return this.doGraphiteRequest({
  102. method: 'GET',
  103. url: '/events/get_data?from=' + this.translateTime(options.range.from) + '&until=' + this.translateTime(options.range.to) + tags,
  104. });
  105. }
  106. catch(err) {
  107. return $q.reject(err);
  108. }
  109. };
  110. GraphiteDatasource.prototype.translateTime = function(date, rounding) {
  111. if (_.isString(date)) {
  112. if (date === 'now') {
  113. return 'now';
  114. }
  115. else if (date.indexOf('now') >= 0) {
  116. date = date.substring(3);
  117. date = date.replace('m', 'min');
  118. date = date.replace('M', 'mon');
  119. return date;
  120. }
  121. date = kbn.parseDate(date);
  122. }
  123. date = moment.utc(date);
  124. if (rounding === 'round-up') {
  125. if (date.get('s')) {
  126. date.add('m', 1);
  127. }
  128. }
  129. else if (rounding === 'round-down') {
  130. // graphite' s from filter is exclusive
  131. // here we step back one minute in order
  132. // to guarantee that we get all the data that
  133. // exists for the specified range
  134. if (date.get('s')) {
  135. date.subtract('m', 1);
  136. }
  137. }
  138. return date.unix();
  139. };
  140. GraphiteDatasource.prototype.metricFindQuery = function(filterSrv, query) {
  141. var interpolated;
  142. try {
  143. interpolated = encodeURIComponent(filterSrv.applyTemplateToTarget(query));
  144. }
  145. catch(err) {
  146. return $q.reject(err);
  147. }
  148. return this.doGraphiteRequest({method: 'GET', url: '/metrics/find/?query=' + interpolated })
  149. .then(function(results) {
  150. return _.map(results.data, function(metric) {
  151. return {
  152. text: metric.text,
  153. expandable: metric.expandable ? true : false
  154. };
  155. });
  156. });
  157. };
  158. GraphiteDatasource.prototype.listDashboards = function(query) {
  159. return this.doGraphiteRequest({ method: 'GET', url: '/dashboard/find/', params: {query: query || ''} })
  160. .then(function(results) {
  161. return results.data.dashboards;
  162. });
  163. };
  164. GraphiteDatasource.prototype.loadDashboard = function(dashName) {
  165. return this.doGraphiteRequest({method: 'GET', url: '/dashboard/load/' + encodeURIComponent(dashName) });
  166. };
  167. GraphiteDatasource.prototype.doGraphiteRequest = function(options) {
  168. if (this.basicAuth) {
  169. options.withCredentials = true;
  170. options.headers = options.headers || {};
  171. options.headers.Authorization = 'Basic ' + this.basicAuth;
  172. }
  173. options.url = this.url + options.url;
  174. return $http(options);
  175. };
  176. GraphiteDatasource.prototype.buildGraphiteParams = function(filterSrv, options) {
  177. var clean_options = [];
  178. var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints'];
  179. if (options.format !== 'png') {
  180. options['format'] = 'json';
  181. }
  182. _.each(options, function (value, key) {
  183. if ($.inArray(key, graphite_options) === -1) {
  184. return;
  185. }
  186. if (key === "targets") {
  187. _.each(value, function (value) {
  188. if (value.target && !value.hide) {
  189. var targetValue = filterSrv.applyTemplateToTarget(value.target);
  190. clean_options.push("target=" + encodeURIComponent(targetValue));
  191. }
  192. }, this);
  193. }
  194. else if (value !== null) {
  195. clean_options.push(key + "=" + encodeURIComponent(value));
  196. }
  197. }, this);
  198. return clean_options;
  199. };
  200. return GraphiteDatasource;
  201. });
  202. });