Commit fb81a4f3 by Torkel Ödegaard

Merge pull request #2299 from espenfjo/kairosdb-sensible-tag-values

KairosDB: Update the template functionality to cohere with the very closely related OpenTSDB plugin
parents 95bce397 d97f24cf
......@@ -36,12 +36,13 @@ KairosDB Datasource Plugin provides following functions in `Variables values que
Name | Description
---- | ----
`metrics(query)` | Returns a list of metric names. If nothing is given, returns a list of all metric names.
`tag_names(query)` | Returns a list of tag names. If nothing is given, returns a list of all tag names.
`tag_values(query)` | Returns a list of tag values. If nothing is given, returns a list of all tag values.
`metrics(query)` | Returns a list of metric names matching `query`. If nothing is given, returns a list of all metric names.
`tag_names(query)` | Returns a list of tag names matching `query`. If nothing is given, returns a list of all tag names.
`tag_values(metric, tag)` | Returns a list of values for `tag` from the given `metric`.
For details of `metric names`, `tag names`, and `tag values`, please refer to the KairosDB documentations.
- [List Metric Names - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListMetricNames.html)
- [List Tag Names - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListTagNames.html)
- [List Tag Values - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListTagValues.html)
- [Query Metrics - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/QueryMetrics.html).
......@@ -76,55 +76,83 @@ function (angular, _, kbn) {
* Gets the list of metrics
* @returns {*|Promise}
*/
KairosDBDatasource.prototype.performMetricSuggestQuery = function() {
KairosDBDatasource.prototype._performMetricSuggestQuery = function(metric) {
var options = {
url : this.url + '/api/v1/metricnames',
method : 'GET'
url: this.url + '/api/v1/metricnames',
method: 'GET'
};
return $http(options).then(function(response) {
if (!response.data) {
return [];
return $q.when([]);
}
return response.data.results;
var metrics = [];
_.each(response.data.results, function(r) {
if (r.indexOf(metric) >= 0) {
metrics.push(r);
}
});
return metrics;
});
};
KairosDBDatasource.prototype.performListTagNames = function() {
KairosDBDatasource.prototype._performMetricKeyLookup = function(metric) {
if(!metric) { return $q.when([]); }
var options = {
url : this.url + '/api/v1/tagnames',
method : 'GET'
method: 'POST',
url: this.url + '/api/v1/datapoints/query/tags',
data: {
metrics: [{ name: metric }],
cache_time: 0,
start_absolute: 0
}
};
return $http(options).then(function(response) {
if (!response.data) {
return [];
return $http(options).then(function(result) {
if (!result.data) {
return $q.when([]);
}
return response.data.results;
var tagks = [];
_.each(result.data.queries[0].results[0].tags, function(tagv, tagk) {
if(tagks.indexOf(tagk) === -1) {
tagks.push(tagk);
}
});
return tagks;
});
};
KairosDBDatasource.prototype.performListTagValues = function() {
KairosDBDatasource.prototype._performMetricKeyValueLookup = function(metric, key) {
if(!metric || !key) {
return $q.when([]);
}
var options = {
url : this.url + '/api/v1/tagvalues',
method : 'GET'
method: 'POST',
url: this.url + '/api/v1/datapoints/query/tags',
data: {
metrics: [{ name: metric }],
cache_time: 0,
start_absolute: 0
}
};
return $http(options).then(function(response) {
if (!response.data) {
return [];
return $http(options).then(function(result) {
if (!result.data) {
return $q.when([]);
}
return response.data.results;
return result.data.queries[0].results[0].tags[key];
});
};
KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) {
KairosDBDatasource.prototype.performTagSuggestQuery = function(metric) {
var options = {
url : this.url + '/api/v1/datapoints/query/tags',
method : 'POST',
data : {
metrics : [{ name : metricname }],
cache_time : 0,
url: this.url + '/api/v1/datapoints/query/tags',
method: 'POST',
data: {
metrics: [{ name: metric }],
cache_time: 0,
start_absolute: 0
}
};
......@@ -140,19 +168,7 @@ function (angular, _, kbn) {
};
KairosDBDatasource.prototype.metricFindQuery = function(query) {
function format(results, query) {
return _.chain(results)
.filter(function(result) {
return result.indexOf(query) >= 0;
})
.map(function(result) {
return {
text: result,
expandable: true
};
})
.value();
}
if (!query) { return $q.when([]); }
var interpolated;
try {
......@@ -162,30 +178,32 @@ function (angular, _, kbn) {
return $q.reject(err);
}
var responseTransform = function(result) {
return _.map(result, function(value) {
return {text: value};
});
};
var metrics_regex = /metrics\((.*)\)/;
var tag_names_regex = /tag_names\((.*)\)/;
var tag_values_regex = /tag_values\((.*)\)/;
var tag_values_regex = /tag_values\((.*),\s?(.*?)\)/;
var metrics_query = interpolated.match(metrics_regex);
if (metrics_query) {
return this.performMetricSuggestQuery().then(function(metrics) {
return format(metrics, metrics_query[1]);
});
return this._performMetricSuggestQuery(metrics_query[1]).then(responseTransform);
}
var tag_names_query = interpolated.match(tag_names_regex);
if (tag_names_query) {
return this.performListTagNames().then(function(tag_names) {
return format(tag_names, tag_names_query[1]);
});
return this._performMetricKeyLookup(tag_names_query[1]).then(responseTransform);
}
var tag_values_query = interpolated.match(tag_values_regex);
if (tag_values_query) {
return this.performListTagValues().then(function(tag_values) {
return format(tag_values, tag_values_query[1]);
});
return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform);
}
return $q.when([]);
};
/////////////////////////////////////////////////////////////////////////
......
......@@ -6,8 +6,6 @@ function (angular, _) {
'use strict';
var module = angular.module('grafana.controllers');
var metricList = [];
var tagList = [];
module.controller('KairosDBQueryCtrl', function($scope) {
......@@ -48,50 +46,26 @@ function (angular, _) {
_.move($scope.panel.targets, fromIndex, toIndex);
};
$scope.getTextValues = function(metricFindResult) {
return _.map(metricFindResult, function(value) { return value.text; });
};
$scope.suggestMetrics = function(query, callback) {
if (!_.isEmpty(metricList)) {
return metricList;
}
else {
$scope.datasource.performMetricSuggestQuery().then(function(result) {
metricList = result;
callback(metricList);
});
}
$scope.datasource.metricFindQuery('metrics(' + query + ')')
.then($scope.getTextValues)
.then(callback);
};
$scope.suggestTagKeys = function(query, callback) {
if (!_.isEmpty(tagList)) {
var result = _.find(tagList, { name : $scope.target.metric });
if (!_.isEmpty(result)) {
return _.keys(result.tags);
}
}
$scope.datasource.performTagSuggestQuery($scope.target.metric).then(function(result) {
if (!_.isEmpty(result)) {
tagList.push(result);
callback(_.keys(result.tags));
}
});
$scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
.then($scope.getTextValues)
.then(callback);
};
$scope.suggestTagValues = function(query, callback) {
if (!_.isEmpty(tagList)) {
var result = _.find(tagList, { name : $scope.target.metric });
if (!_.isEmpty(result)) {
return result.tags[$scope.target.currentTagKey];
}
}
$scope.datasource.performTagSuggestQuery($scope.target.metric).then(function(result) {
if (!_.isEmpty(result)) {
tagList.push(result);
callback(result.tags[$scope.target.currentTagKey]);
}
});
$scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
.then($scope.getTextValues)
.then(callback);
};
// Filter metric by tag
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment