datasource.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. 'moment',
  6. './queryCtrl',
  7. ],
  8. function (angular, _, kbn) {
  9. 'use strict';
  10. var module = angular.module('grafana.services');
  11. module.factory('OpenTSDBDatasource', function($q, backendSrv, templateSrv) {
  12. function OpenTSDBDatasource(datasource) {
  13. this.type = 'opentsdb';
  14. this.editorSrc = 'app/features/opentsdb/partials/query.editor.html';
  15. this.url = datasource.url;
  16. this.name = datasource.name;
  17. this.supportMetrics = true;
  18. }
  19. // Called once per panel (graph)
  20. OpenTSDBDatasource.prototype.query = function(options) {
  21. var start = convertToTSDBTime(options.range.from);
  22. var end = convertToTSDBTime(options.range.to);
  23. var qs = [];
  24. _.each(options.targets, function(target) {
  25. if (!target.metric) { return; }
  26. qs.push(convertTargetToQuery(target, options));
  27. });
  28. var queries = _.compact(qs);
  29. // No valid targets, return the empty result to save a round trip.
  30. if (_.isEmpty(queries)) {
  31. var d = $q.defer();
  32. d.resolve({ data: [] });
  33. return d.promise;
  34. }
  35. var groupByTags = {};
  36. _.each(queries, function(query) {
  37. _.each(query.tags, function(val, key) {
  38. groupByTags[key] = true;
  39. });
  40. });
  41. return this.performTimeSeriesQuery(queries, start, end).then(function(response) {
  42. var metricToTargetMapping = mapMetricsToTargets(response.data, options);
  43. var result = _.map(response.data, function(metricData, index) {
  44. index = metricToTargetMapping[index];
  45. return transformMetricData(metricData, groupByTags, options.targets[index], options);
  46. });
  47. return { data: result };
  48. });
  49. };
  50. OpenTSDBDatasource.prototype.performTimeSeriesQuery = function(queries, start, end) {
  51. var reqBody = {
  52. start: start,
  53. queries: queries
  54. };
  55. // Relative queries (e.g. last hour) don't include an end time
  56. if (end) {
  57. reqBody.end = end;
  58. }
  59. var options = {
  60. method: 'POST',
  61. url: this.url + '/api/query',
  62. data: reqBody
  63. };
  64. return backendSrv.datasourceRequest(options);
  65. };
  66. OpenTSDBDatasource.prototype._performSuggestQuery = function(query) {
  67. return this._get('/api/suggest', {type: 'metrics', q: query}).then(function(result) {
  68. return result.data;
  69. });
  70. };
  71. OpenTSDBDatasource.prototype._performMetricKeyValueLookup = function(metric, key) {
  72. if(!metric || !key) {
  73. return $q.when([]);
  74. }
  75. var m = metric + "{" + key + "=*}";
  76. return this._get('/api/search/lookup', {m: m}).then(function(result) {
  77. result = result.data.results;
  78. var tagvs = [];
  79. _.each(result, function(r) {
  80. tagvs.push(r.tags[key]);
  81. });
  82. return tagvs;
  83. });
  84. };
  85. OpenTSDBDatasource.prototype._performMetricKeyLookup = function(metric) {
  86. if(!metric) { return $q.when([]); }
  87. return this._get('/api/search/lookup', {m: metric}).then(function(result) {
  88. result = result.data.results;
  89. var tagks = [];
  90. _.each(result, function(r) {
  91. _.each(r.tags, function(tagv, tagk) {
  92. if(tagks.indexOf(tagk) === -1) {
  93. tagks.push(tagk);
  94. }
  95. });
  96. });
  97. return tagks;
  98. });
  99. };
  100. OpenTSDBDatasource.prototype._get = function(relativeUrl, params) {
  101. return backendSrv.datasourceRequest({
  102. method: 'GET',
  103. url: this.url + relativeUrl,
  104. params: params,
  105. });
  106. };
  107. OpenTSDBDatasource.prototype.metricFindQuery = function(query) {
  108. if (!query) { return $q.when([]); }
  109. var interpolated;
  110. try {
  111. interpolated = templateSrv.replace(query);
  112. }
  113. catch (err) {
  114. return $q.reject(err);
  115. }
  116. var responseTransform = function(result) {
  117. return _.map(result, function(value) {
  118. return {text: value};
  119. });
  120. };
  121. var metrics_regex = /metrics\((.*)\)/;
  122. var tag_names_regex = /tag_names\((.*)\)/;
  123. var tag_values_regex = /tag_values\((.*),\s?(.*)\)/;
  124. var metrics_query = interpolated.match(metrics_regex);
  125. if (metrics_query) {
  126. return this._performSuggestQuery(metrics_query[1]).then(responseTransform);
  127. }
  128. var tag_names_query = interpolated.match(tag_names_regex);
  129. if (tag_names_query) {
  130. return this._performMetricKeyLookup(tag_names_query[1]).then(responseTransform);
  131. }
  132. var tag_values_query = interpolated.match(tag_values_regex);
  133. if (tag_values_query) {
  134. return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform);
  135. }
  136. return $q.when([]);
  137. };
  138. OpenTSDBDatasource.prototype.testDatasource = function() {
  139. return this.performSuggestQuery('cpu', 'metrics').then(function () {
  140. return { status: "success", message: "Data source is working", title: "Success" };
  141. });
  142. };
  143. function transformMetricData(md, groupByTags, target, options) {
  144. var metricLabel = createMetricLabel(md, target, groupByTags, options);
  145. var dps = [];
  146. // TSDB returns datapoints has a hash of ts => value.
  147. // Can't use _.pairs(invert()) because it stringifies keys/values
  148. _.each(md.dps, function (v, k) {
  149. dps.push([v, k * 1000]);
  150. });
  151. return { target: metricLabel, datapoints: dps };
  152. }
  153. function createMetricLabel(md, target, groupByTags, options) {
  154. if (target.alias) {
  155. var scopedVars = _.clone(options.scopedVars || {});
  156. _.each(md.tags, function(value, key) {
  157. scopedVars['tag_' + key] = {value: value};
  158. });
  159. return templateSrv.replace(target.alias, scopedVars);
  160. }
  161. var label = md.metric;
  162. var tagData = [];
  163. if (!_.isEmpty(md.tags)) {
  164. _.each(_.pairs(md.tags), function(tag) {
  165. if (_.has(groupByTags, tag[0])) {
  166. tagData.push(tag[0] + "=" + tag[1]);
  167. }
  168. });
  169. }
  170. if (!_.isEmpty(tagData)) {
  171. label += "{" + tagData.join(", ") + "}";
  172. }
  173. return label;
  174. }
  175. function convertTargetToQuery(target, options) {
  176. if (!target.metric || target.hide) {
  177. return null;
  178. }
  179. var query = {
  180. metric: templateSrv.replace(target.metric, options.scopedVars),
  181. aggregator: "avg"
  182. };
  183. if (target.aggregator) {
  184. query.aggregator = templateSrv.replace(target.aggregator);
  185. }
  186. if (target.shouldComputeRate) {
  187. query.rate = true;
  188. query.rateOptions = {
  189. counter: !!target.isCounter
  190. };
  191. if (target.counterMax && target.counterMax.length) {
  192. query.rateOptions.counterMax = parseInt(target.counterMax);
  193. }
  194. if (target.counterResetValue && target.counterResetValue.length) {
  195. query.rateOptions.resetValue = parseInt(target.counterResetValue);
  196. }
  197. }
  198. if (!target.disableDownsampling) {
  199. var interval = templateSrv.replace(target.downsampleInterval || options.interval);
  200. if (interval.match(/\.[0-9]+s/)) {
  201. interval = parseFloat(interval)*1000 + "ms";
  202. }
  203. query.downsample = interval + "-" + target.downsampleAggregator;
  204. }
  205. query.tags = angular.copy(target.tags);
  206. if(query.tags){
  207. for(var key in query.tags){
  208. query.tags[key] = templateSrv.replace(query.tags[key], options.scopedVars);
  209. }
  210. }
  211. return query;
  212. }
  213. function mapMetricsToTargets(metrics, options) {
  214. var interpolatedTagValue;
  215. return _.map(metrics, function(metricData) {
  216. return _.findIndex(options.targets, function(target) {
  217. return target.metric === metricData.metric &&
  218. _.all(target.tags, function(tagV, tagK) {
  219. interpolatedTagValue = templateSrv.replace(tagV, options.scopedVars);
  220. return metricData.tags[tagK] === interpolatedTagValue || interpolatedTagValue === "*";
  221. });
  222. });
  223. });
  224. }
  225. function convertToTSDBTime(date) {
  226. if (date === 'now') {
  227. return null;
  228. }
  229. date = kbn.parseDate(date);
  230. return date.getTime();
  231. }
  232. return OpenTSDBDatasource;
  233. });
  234. });