datasource.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, $http, 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. qs.push(convertTargetToQuery(target, options.interval));
  26. });
  27. var queries = _.compact(qs);
  28. // No valid targets, return the empty result to save a round trip.
  29. if (_.isEmpty(queries)) {
  30. var d = $q.defer();
  31. d.resolve({ data: [] });
  32. return d.promise;
  33. }
  34. var groupByTags = {};
  35. _.each(queries, function(query) {
  36. _.each(query.tags, function(val, key) {
  37. groupByTags[key] = true;
  38. });
  39. });
  40. return this.performTimeSeriesQuery(queries, start, end).then(function(response) {
  41. var metricToTargetMapping = mapMetricsToTargets(response.data, options.targets);
  42. var result = _.map(response.data, function(metricData, index) {
  43. index = metricToTargetMapping[index];
  44. return transformMetricData(metricData, groupByTags, options.targets[index]);
  45. });
  46. return { data: result };
  47. });
  48. };
  49. OpenTSDBDatasource.prototype.performTimeSeriesQuery = function(queries, start, end) {
  50. var reqBody = {
  51. start: start,
  52. queries: queries
  53. };
  54. // Relative queries (e.g. last hour) don't include an end time
  55. if (end) {
  56. reqBody.end = end;
  57. }
  58. var options = {
  59. method: 'POST',
  60. url: this.url + '/api/query',
  61. data: reqBody
  62. };
  63. return $http(options);
  64. };
  65. OpenTSDBDatasource.prototype.performSuggestQuery = function(query, type) {
  66. var options = {
  67. method: 'GET',
  68. url: this.url + '/api/suggest',
  69. params: {
  70. type: type,
  71. q: query
  72. }
  73. };
  74. return $http(options).then(function(result) {
  75. return result.data;
  76. });
  77. };
  78. OpenTSDBDatasource.prototype.performAggregatorsQuery = function() {
  79. var options = {
  80. method: 'GET',
  81. url: this.url + '/api/aggregators'
  82. };
  83. return $http(options).then(function(result) {
  84. if (result.data instanceof Array) {
  85. return result.data.sort();
  86. } else {
  87. return result.data;
  88. }
  89. });
  90. };
  91. function transformMetricData(md, groupByTags, options) {
  92. var metricLabel = createMetricLabel(md, options, groupByTags);
  93. var dps = [];
  94. // TSDB returns datapoints has a hash of ts => value.
  95. // Can't use _.pairs(invert()) because it stringifies keys/values
  96. _.each(md.dps, function (v, k) {
  97. dps.push([v, k * 1000]);
  98. });
  99. return { target: metricLabel, datapoints: dps };
  100. }
  101. function createMetricLabel(md, options, groupByTags) {
  102. if (!_.isUndefined(options) && options.alias) {
  103. var scopedVars = {};
  104. _.each(md.tags, function(value, key) {
  105. scopedVars['tag_' + key] = {value: value};
  106. });
  107. return templateSrv.replace(options.alias, scopedVars);
  108. }
  109. var label = md.metric;
  110. var tagData = [];
  111. if (!_.isEmpty(md.tags)) {
  112. _.each(_.pairs(md.tags), function(tag) {
  113. if (_.has(groupByTags, tag[0])) {
  114. tagData.push(tag[0] + "=" + tag[1]);
  115. }
  116. });
  117. }
  118. if (!_.isEmpty(tagData)) {
  119. label += "{" + tagData.join(", ") + "}";
  120. }
  121. return label;
  122. }
  123. function convertTargetToQuery(target, interval) {
  124. if (!target.metric || target.hide) {
  125. return null;
  126. }
  127. var query = {
  128. metric: templateSrv.replace(target.metric),
  129. aggregator: "avg"
  130. };
  131. if (target.aggregator) {
  132. query.aggregator = templateSrv.replace(target.aggregator);
  133. }
  134. if (target.shouldComputeRate) {
  135. query.rate = true;
  136. query.rateOptions = {
  137. counter: !!target.isCounter
  138. };
  139. if (target.counterMax && target.counterMax.length) {
  140. query.rateOptions.counterMax = parseInt(target.counterMax);
  141. }
  142. if (target.counterResetValue && target.counterResetValue.length) {
  143. query.rateOptions.resetValue = parseInt(target.counterResetValue);
  144. }
  145. }
  146. if (!target.disableDownsampling) {
  147. interval = templateSrv.replace(target.downsampleInterval || interval);
  148. if (interval.match(/\.[0-9]+s/)) {
  149. interval = parseFloat(interval)*1000 + "ms";
  150. }
  151. query.downsample = interval + "-" + target.downsampleAggregator;
  152. }
  153. query.tags = angular.copy(target.tags);
  154. if(query.tags){
  155. for(var key in query.tags){
  156. query.tags[key] = templateSrv.replace(query.tags[key]);
  157. }
  158. }
  159. return query;
  160. }
  161. function mapMetricsToTargets(metrics, targets) {
  162. return _.map(metrics, function(metricData) {
  163. return _.findIndex(targets, function(target) {
  164. return target.metric === metricData.metric &&
  165. _.all(target.tags, function(tagV, tagK) { return metricData.tags[tagK] !== void 0; });
  166. });
  167. });
  168. }
  169. function convertToTSDBTime(date) {
  170. if (date === 'now') {
  171. return null;
  172. }
  173. date = kbn.parseDate(date);
  174. return date.getTime();
  175. }
  176. return OpenTSDBDatasource;
  177. });
  178. });