datasource.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import * as dateMath from 'app/core/utils/datemath';
  4. import {isVersionGtOrEq} from 'app/core/utils/version';
  5. /** @ngInject */
  6. export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) {
  7. this.basicAuth = instanceSettings.basicAuth;
  8. this.url = instanceSettings.url;
  9. this.name = instanceSettings.name;
  10. this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || '0.9';
  11. this.supportsTags = supportsTags(this.graphiteVersion);
  12. this.cacheTimeout = instanceSettings.cacheTimeout;
  13. this.withCredentials = instanceSettings.withCredentials;
  14. this.render_method = instanceSettings.render_method || 'POST';
  15. this.getQueryOptionsInfo = function() {
  16. return {
  17. "maxDataPoints": true,
  18. "cacheTimeout": true,
  19. "links": [
  20. {
  21. text: "Help",
  22. url: "http://docs.grafana.org/features/datasources/graphite/#using-graphite-in-grafana"
  23. }
  24. ]
  25. };
  26. };
  27. this.query = function(options) {
  28. var graphOptions = {
  29. from: this.translateTime(options.rangeRaw.from, false),
  30. until: this.translateTime(options.rangeRaw.to, true),
  31. targets: options.targets,
  32. format: options.format,
  33. cacheTimeout: options.cacheTimeout || this.cacheTimeout,
  34. maxDataPoints: options.maxDataPoints,
  35. };
  36. var params = this.buildGraphiteParams(graphOptions, options.scopedVars);
  37. if (params.length === 0) {
  38. return $q.when({data: []});
  39. }
  40. var httpOptions: any = {
  41. method: 'POST',
  42. url: '/render',
  43. data: params.join('&'),
  44. headers: {
  45. 'Content-Type': 'application/x-www-form-urlencoded'
  46. },
  47. };
  48. if (options.panelId) {
  49. httpOptions.requestId = this.name + '.panelId.' + options.panelId;
  50. }
  51. return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs);
  52. };
  53. this.convertDataPointsToMs = function(result) {
  54. if (!result || !result.data) { return []; }
  55. for (var i = 0; i < result.data.length; i++) {
  56. var series = result.data[i];
  57. for (var y = 0; y < series.datapoints.length; y++) {
  58. series.datapoints[y][1] *= 1000;
  59. }
  60. }
  61. return result;
  62. };
  63. this.parseTags = function(tagString) {
  64. let tags = [];
  65. tags = tagString.split(',');
  66. if (tags.length === 1) {
  67. tags = tagString.split(' ');
  68. if (tags[0] === '') {
  69. tags = [];
  70. }
  71. }
  72. return tags;
  73. };
  74. this.annotationQuery = function(options) {
  75. // Graphite metric as annotation
  76. if (options.annotation.target) {
  77. var target = templateSrv.replace(options.annotation.target, {}, 'glob');
  78. var graphiteQuery = {
  79. rangeRaw: options.rangeRaw,
  80. targets: [{ target: target }],
  81. format: 'json',
  82. maxDataPoints: 100
  83. };
  84. return this.query(graphiteQuery).then(function(result) {
  85. var list = [];
  86. for (var i = 0; i < result.data.length; i++) {
  87. var target = result.data[i];
  88. for (var y = 0; y < target.datapoints.length; y++) {
  89. var datapoint = target.datapoints[y];
  90. if (!datapoint[0]) { continue; }
  91. list.push({
  92. annotation: options.annotation,
  93. time: datapoint[1],
  94. title: target.target
  95. });
  96. }
  97. }
  98. return list;
  99. });
  100. } else {
  101. // Graphite event as annotation
  102. var tags = templateSrv.replace(options.annotation.tags);
  103. return this.events({range: options.rangeRaw, tags: tags}).then(results => {
  104. var list = [];
  105. for (var i = 0; i < results.data.length; i++) {
  106. var e = results.data[i];
  107. var tags = e.tags;
  108. if (_.isString(e.tags)) {
  109. tags = this.parseTags(e.tags);
  110. }
  111. list.push({
  112. annotation: options.annotation,
  113. time: e.when * 1000,
  114. title: e.what,
  115. tags: tags,
  116. text: e.data
  117. });
  118. }
  119. return list;
  120. });
  121. }
  122. };
  123. this.events = function(options) {
  124. try {
  125. var tags = '';
  126. if (options.tags) {
  127. tags = '&tags=' + options.tags;
  128. }
  129. return this.doGraphiteRequest({
  130. method: 'GET',
  131. url: '/events/get_data?from=' + this.translateTime(options.range.from, false) +
  132. '&until=' + this.translateTime(options.range.to, true) + tags,
  133. });
  134. } catch (err) {
  135. return $q.reject(err);
  136. }
  137. };
  138. this.targetContainsTemplate = function(target) {
  139. return templateSrv.variableExists(target.target);
  140. };
  141. this.translateTime = function(date, roundUp) {
  142. if (_.isString(date)) {
  143. if (date === 'now') {
  144. return 'now';
  145. } else if (date.indexOf('now-') >= 0 && date.indexOf('/') === -1) {
  146. date = date.substring(3);
  147. date = date.replace('m', 'min');
  148. date = date.replace('M', 'mon');
  149. return date;
  150. }
  151. date = dateMath.parse(date, roundUp);
  152. }
  153. // graphite' s from filter is exclusive
  154. // here we step back one minute in order
  155. // to guarantee that we get all the data that
  156. // exists for the specified range
  157. if (roundUp) {
  158. if (date.get('s')) {
  159. date.add(1, 'm');
  160. }
  161. } else if (roundUp === false) {
  162. if (date.get('s')) {
  163. date.subtract(1, 'm');
  164. }
  165. }
  166. return date.unix();
  167. };
  168. this.metricFindQuery = function(query, optionalOptions) {
  169. let options = optionalOptions || {};
  170. let interpolatedQuery = templateSrv.replace(query);
  171. let httpOptions: any = {
  172. method: 'GET',
  173. url: '/metrics/find',
  174. params: {
  175. query: interpolatedQuery
  176. },
  177. // for cancellations
  178. requestId: options.requestId,
  179. };
  180. if (options && options.range) {
  181. httpOptions.params.from = this.translateTime(options.range.from, false);
  182. httpOptions.params.until = this.translateTime(options.range.to, true);
  183. }
  184. return this.doGraphiteRequest(httpOptions).then(results => {
  185. return _.map(results.data, metric => {
  186. return {
  187. text: metric.text,
  188. expandable: metric.expandable ? true : false
  189. };
  190. });
  191. });
  192. };
  193. this.getTags = function(optionalOptions) {
  194. let options = optionalOptions || {};
  195. let httpOptions: any = {
  196. method: 'GET',
  197. url: '/tags',
  198. // for cancellations
  199. requestId: options.requestId,
  200. };
  201. if (options && options.range) {
  202. httpOptions.params.from = this.translateTime(options.range.from, false);
  203. httpOptions.params.until = this.translateTime(options.range.to, true);
  204. }
  205. return this.doGraphiteRequest(httpOptions).then(results => {
  206. return _.map(results.data, tag => {
  207. return {
  208. text: tag.tag,
  209. id: tag.id
  210. };
  211. });
  212. });
  213. };
  214. this.getTagValues = function(tag, optionalOptions) {
  215. let options = optionalOptions || {};
  216. let httpOptions: any = {
  217. method: 'GET',
  218. url: '/tags/' + tag,
  219. // for cancellations
  220. requestId: options.requestId,
  221. };
  222. if (options && options.range) {
  223. httpOptions.params.from = this.translateTime(options.range.from, false);
  224. httpOptions.params.until = this.translateTime(options.range.to, true);
  225. }
  226. return this.doGraphiteRequest(httpOptions).then(results => {
  227. if (results.data && results.data.values) {
  228. return _.map(results.data.values, value => {
  229. return {
  230. text: value.value,
  231. id: value.id
  232. };
  233. });
  234. } else {
  235. return [];
  236. }
  237. });
  238. };
  239. this.testDatasource = function() {
  240. return this.metricFindQuery('*').then(function () {
  241. return { status: "success", message: "Data source is working"};
  242. });
  243. };
  244. this.doGraphiteRequest = function(options) {
  245. if (this.basicAuth || this.withCredentials) {
  246. options.withCredentials = true;
  247. }
  248. if (this.basicAuth) {
  249. options.headers = options.headers || {};
  250. options.headers.Authorization = this.basicAuth;
  251. }
  252. options.url = this.url + options.url;
  253. options.inspect = {type: 'graphite'};
  254. return backendSrv.datasourceRequest(options);
  255. };
  256. this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  257. this.buildGraphiteParams = function(options, scopedVars) {
  258. var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
  259. var clean_options = [], targets = {};
  260. var target, targetValue, i;
  261. var regex = /\#([A-Z])/g;
  262. var intervalFormatFixRegex = /'(\d+)m'/gi;
  263. var hasTargets = false;
  264. options['format'] = 'json';
  265. function fixIntervalFormat(match) {
  266. return match.replace('m', 'min').replace('M', 'mon');
  267. }
  268. for (i = 0; i < options.targets.length; i++) {
  269. target = options.targets[i];
  270. if (!target.target) {
  271. continue;
  272. }
  273. if (!target.refId) {
  274. target.refId = this._seriesRefLetters[i];
  275. }
  276. targetValue = templateSrv.replace(target.target, scopedVars);
  277. targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
  278. targets[target.refId] = targetValue;
  279. }
  280. function nestedSeriesRegexReplacer(match, g1) {
  281. return targets[g1] || match;
  282. }
  283. for (i = 0; i < options.targets.length; i++) {
  284. target = options.targets[i];
  285. if (!target.target) {
  286. continue;
  287. }
  288. targetValue = targets[target.refId];
  289. targetValue = targetValue.replace(regex, nestedSeriesRegexReplacer);
  290. targets[target.refId] = targetValue;
  291. if (!target.hide) {
  292. hasTargets = true;
  293. clean_options.push("target=" + encodeURIComponent(targetValue));
  294. }
  295. }
  296. _.each(options, function (value, key) {
  297. if (_.indexOf(graphite_options, key) === -1) { return; }
  298. if (value) {
  299. clean_options.push(key + "=" + encodeURIComponent(value));
  300. }
  301. });
  302. if (!hasTargets) {
  303. return [];
  304. }
  305. return clean_options;
  306. };
  307. }
  308. function supportsTags(version: string): boolean {
  309. return isVersionGtOrEq(version, '1.1');
  310. }