graphiteDatasource.js 7.7 KB

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