Files
grafana/src/app/features/opentsdb/datasource.js

189 lines
4.8 KiB
JavaScript
Raw Normal View History

define([
'angular',
2014-08-07 14:35:19 +02:00
'lodash',
2014-10-09 16:59:29 +02:00
'kbn',
'moment',
'./queryCtrl',
],
function (angular, _, kbn) {
'use strict';
2014-07-28 18:11:52 +02:00
var module = angular.module('grafana.services');
2014-10-09 16:59:29 +02:00
module.factory('OpenTSDBDatasource', function($q, $http, templateSrv) {
function OpenTSDBDatasource(datasource) {
this.type = 'opentsdb';
this.editorSrc = 'app/features/opentsdb/partials/query.editor.html';
this.url = datasource.url;
this.name = datasource.name;
this.supportMetrics = true;
}
// Called once per panel (graph)
OpenTSDBDatasource.prototype.query = function(options) {
var start = convertToTSDBTime(options.range.from);
var end = convertToTSDBTime(options.range.to);
2015-02-06 18:39:05 +02:00
var qs = [];
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 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
});
var queries = _.compact(qs);
// 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) {
groupByTags[key] = true;
});
});
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));
return { data: result };
2014-07-23 14:19:21 -04:00
}, options));
};
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);
// 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) {
dps.push([v, k * 1000]);
});
2014-07-23 14:19:21 -04:00
return { target: metricLabel, datapoints: dps };
}
2014-07-23 14:19:21 -04:00
function createMetricLabel(metric, tagData, options) {
if (!_.isUndefined(options) && options.alias) {
return options.alias;
2014-07-23 14:19:21 -04:00
}
2014-07-23 14:19:21 -04:00
if (!_.isEmpty(tagData)) {
metric += "{" + tagData.join(", ") + "}";
}
2014-07-23 14:19:21 -04:00
return metric;
}
function convertTargetToQuery(target, interval) {
if (!target.metric) {
return null;
}
var query = {
2014-10-09 16:59:29 +02:00
metric: templateSrv.replace(target.metric),
aggregator: "avg"
};
if (target.aggregator) {
2014-10-09 16:59:29 +02:00
query.aggregator = templateSrv.replace(target.aggregator);
}
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
}
}
if (!target.disableDownsampling) {
2015-02-06 18:39:05 +02:00
var buf = target.downsampleInterval || interval;
query.downsample = templateSrv.replace(buf) + "-" + target.downsampleAggregator;
}
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]);
}
}
return query;
}
function convertToTSDBTime(date) {
if (date === 'now') {
return null;
}
date = kbn.parseDate(date);
return date.getTime();
}
return OpenTSDBDatasource;
});
});