2014-03-14 12:14:16 -07:00
|
|
|
define([
|
|
|
|
|
'angular',
|
2014-08-07 14:35:19 +02:00
|
|
|
'lodash',
|
2014-10-09 16:59:29 +02:00
|
|
|
'kbn',
|
2014-12-30 21:24:55 +01:00
|
|
|
'moment',
|
|
|
|
|
'./queryCtrl',
|
2014-03-14 12:14:16 -07:00
|
|
|
],
|
|
|
|
|
function (angular, _, kbn) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2014-07-28 18:11:52 +02:00
|
|
|
var module = angular.module('grafana.services');
|
2014-03-14 12:14:16 -07:00
|
|
|
|
2014-10-09 16:59:29 +02:00
|
|
|
module.factory('OpenTSDBDatasource', function($q, $http, templateSrv) {
|
2014-03-14 12:14:16 -07:00
|
|
|
|
|
|
|
|
function OpenTSDBDatasource(datasource) {
|
|
|
|
|
this.type = 'opentsdb';
|
2014-12-30 21:24:55 +01:00
|
|
|
this.editorSrc = 'app/features/opentsdb/partials/query.editor.html';
|
2014-03-14 12:14:16 -07:00
|
|
|
this.url = datasource.url;
|
|
|
|
|
this.name = datasource.name;
|
2014-07-31 12:27:49 +02:00
|
|
|
this.supportMetrics = true;
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called once per panel (graph)
|
2014-09-09 10:29:59 +02:00
|
|
|
OpenTSDBDatasource.prototype.query = function(options) {
|
2014-03-14 12:14:16 -07:00
|
|
|
var start = convertToTSDBTime(options.range.from);
|
|
|
|
|
var end = convertToTSDBTime(options.range.to);
|
2015-02-06 18:39:05 +02:00
|
|
|
var qs = [];
|
2015-02-23 12:48:07 +01:00
|
|
|
|
2015-02-06 18:15:27 +02:00
|
|
|
if (options.interval.match(/\.[0-9]+s/)) {
|
2015-02-06 18:39:05 +02:00
|
|
|
options.interval = parseFloat(options.interval)*1000 + "ms";
|
2015-02-06 18:15:27 +02:00
|
|
|
}
|
2015-02-06 21:07:31 +02:00
|
|
|
_.each(options.targets, function(target) {
|
2015-02-06 21:15:14 +02:00
|
|
|
qs.push(convertTargetToQuery(target, options.interval));
|
2015-02-06 21:07:31 +02:00
|
|
|
});
|
2015-02-23 12:48:07 +01:00
|
|
|
|
2015-02-06 18:15:27 +02:00
|
|
|
var queries = _.compact(qs);
|
2014-03-14 12:14:16 -07:00
|
|
|
|
|
|
|
|
// No valid targets, return the empty result to save a round trip.
|
|
|
|
|
if (_.isEmpty(queries)) {
|
|
|
|
|
var d = $q.defer();
|
|
|
|
|
d.resolve({ data: [] });
|
|
|
|
|
return d.promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var groupByTags = {};
|
|
|
|
|
_.each(queries, function(query) {
|
|
|
|
|
_.each(query.tags, function(val, key) {
|
2014-06-25 23:56:25 +02:00
|
|
|
groupByTags[key] = true;
|
2014-03-14 12:14:16 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.performTimeSeriesQuery(queries, start, end)
|
2014-07-23 14:19:21 -04:00
|
|
|
.then(_.bind(function(response) {
|
|
|
|
|
var result = _.map(response.data, _.bind(function(metricData, index) {
|
|
|
|
|
return transformMetricData(metricData, groupByTags, this.targets[index]);
|
|
|
|
|
}, this));
|
2014-03-14 12:14:16 -07:00
|
|
|
return { data: result };
|
2014-07-23 14:19:21 -04:00
|
|
|
}, options));
|
2014-03-14 12:14:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OpenTSDBDatasource.prototype.performTimeSeriesQuery = function(queries, start, end) {
|
|
|
|
|
var reqBody = {
|
|
|
|
|
start: start,
|
|
|
|
|
queries: queries
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Relative queries (e.g. last hour) don't include an end time
|
|
|
|
|
if (end) {
|
|
|
|
|
reqBody.end = end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: this.url + '/api/query',
|
|
|
|
|
data: reqBody
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return $http(options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OpenTSDBDatasource.prototype.performSuggestQuery = function(query, type) {
|
|
|
|
|
var options = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: this.url + '/api/suggest',
|
|
|
|
|
params: {
|
|
|
|
|
type: type,
|
|
|
|
|
q: query
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return $http(options).then(function(result) {
|
|
|
|
|
return result.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-23 14:19:21 -04:00
|
|
|
function transformMetricData(md, groupByTags, options) {
|
|
|
|
|
var dps = [],
|
|
|
|
|
tagData = [],
|
|
|
|
|
metricLabel = null;
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(md.tags)) {
|
|
|
|
|
_.each(_.pairs(md.tags), function(tag) {
|
|
|
|
|
if (_.has(groupByTags, tag[0])) {
|
|
|
|
|
tagData.push(tag[0] + "=" + tag[1]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricLabel = createMetricLabel(md.metric, tagData, options);
|
2014-03-14 12:14:16 -07:00
|
|
|
|
|
|
|
|
// TSDB returns datapoints has a hash of ts => value.
|
|
|
|
|
// Can't use _.pairs(invert()) because it stringifies keys/values
|
|
|
|
|
_.each(md.dps, function (v, k) {
|
2014-11-12 08:39:04 +01:00
|
|
|
dps.push([v, k * 1000]);
|
2014-03-14 12:14:16 -07:00
|
|
|
});
|
|
|
|
|
|
2014-07-23 14:19:21 -04:00
|
|
|
return { target: metricLabel, datapoints: dps };
|
|
|
|
|
}
|
2014-03-14 12:14:16 -07:00
|
|
|
|
2014-07-23 14:19:21 -04:00
|
|
|
function createMetricLabel(metric, tagData, options) {
|
2014-08-08 14:58:03 +02:00
|
|
|
if (!_.isUndefined(options) && options.alias) {
|
2014-07-31 12:27:49 +02:00
|
|
|
return options.alias;
|
2014-07-23 14:19:21 -04:00
|
|
|
}
|
2014-03-14 12:14:16 -07:00
|
|
|
|
2014-07-23 14:19:21 -04:00
|
|
|
if (!_.isEmpty(tagData)) {
|
|
|
|
|
metric += "{" + tagData.join(", ") + "}";
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-23 14:19:21 -04:00
|
|
|
return metric;
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
2015-02-06 18:15:27 +02:00
|
|
|
function convertTargetToQuery(target, interval) {
|
2014-03-14 12:14:16 -07:00
|
|
|
if (!target.metric) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = {
|
2014-10-09 16:59:29 +02:00
|
|
|
metric: templateSrv.replace(target.metric),
|
2014-03-14 12:14:16 -07:00
|
|
|
aggregator: "avg"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (target.aggregator) {
|
2014-10-09 16:59:29 +02:00
|
|
|
query.aggregator = templateSrv.replace(target.aggregator);
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (target.shouldComputeRate) {
|
|
|
|
|
query.rate = true;
|
|
|
|
|
query.rateOptions = {
|
|
|
|
|
counter: !!target.isCounter
|
|
|
|
|
};
|
2014-10-13 12:36:26 +01:00
|
|
|
|
|
|
|
|
if (target.counterMax && target.counterMax.length) {
|
2014-10-13 13:43:25 +01:00
|
|
|
query.rateOptions.counterMax = parseInt(target.counterMax);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (target.counterResetValue && target.counterResetValue.length) {
|
|
|
|
|
query.rateOptions.resetValue = parseInt(target.counterResetValue);
|
2014-10-13 12:36:26 +01:00
|
|
|
}
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
2015-02-23 12:48:07 +01:00
|
|
|
if (!target.disableDownsampling) {
|
2015-02-06 18:39:05 +02:00
|
|
|
var buf = target.downsampleInterval || interval;
|
|
|
|
|
query.downsample = templateSrv.replace(buf) + "-" + target.downsampleAggregator;
|
2014-03-14 12:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query.tags = angular.copy(target.tags);
|
2014-10-09 16:59:29 +02:00
|
|
|
if(query.tags){
|
|
|
|
|
for(var key in query.tags){
|
|
|
|
|
query.tags[key] = templateSrv.replace(query.tags[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-14 12:14:16 -07:00
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertToTSDBTime(date) {
|
|
|
|
|
if (date === 'now') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
date = kbn.parseDate(date);
|
|
|
|
|
|
|
|
|
|
return date.getTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return OpenTSDBDatasource;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|