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 ...@@ -36,12 +36,13 @@ KairosDB Datasource Plugin provides following functions in `Variables values que
Name | Description Name | Description
---- | ---- ---- | ----
`metrics(query)` | Returns a list of metric names. If nothing is given, returns a list of all metric names. `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. If nothing is given, returns a list of all tag 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(query)` | Returns a list of tag values. If nothing is given, returns a list of all tag values. `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. 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 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 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) - [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) { ...@@ -76,55 +76,83 @@ function (angular, _, kbn) {
* Gets the list of metrics * Gets the list of metrics
* @returns {*|Promise} * @returns {*|Promise}
*/ */
KairosDBDatasource.prototype.performMetricSuggestQuery = function() { KairosDBDatasource.prototype._performMetricSuggestQuery = function(metric) {
var options = { var options = {
url : this.url + '/api/v1/metricnames', url: this.url + '/api/v1/metricnames',
method : 'GET' method: 'GET'
}; };
return $http(options).then(function(response) { return $http(options).then(function(response) {
if (!response.data) { if (!response.data) {
return []; return $q.when([]);
}
var metrics = [];
_.each(response.data.results, function(r) {
if (r.indexOf(metric) >= 0) {
metrics.push(r);
} }
return response.data.results; });
return metrics;
}); });
}; };
KairosDBDatasource.prototype.performListTagNames = function() { KairosDBDatasource.prototype._performMetricKeyLookup = function(metric) {
if(!metric) { return $q.when([]); }
var options = { var options = {
url : this.url + '/api/v1/tagnames', method: 'POST',
method : 'GET' url: this.url + '/api/v1/datapoints/query/tags',
data: {
metrics: [{ name: metric }],
cache_time: 0,
start_absolute: 0
}
}; };
return $http(options).then(function(response) { return $http(options).then(function(result) {
if (!response.data) { if (!result.data) {
return []; 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 = { var options = {
url : this.url + '/api/v1/tagvalues', method: 'POST',
method : 'GET' url: this.url + '/api/v1/datapoints/query/tags',
data: {
metrics: [{ name: metric }],
cache_time: 0,
start_absolute: 0
}
}; };
return $http(options).then(function(response) { return $http(options).then(function(result) {
if (!response.data) { if (!result.data) {
return []; 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 = { var options = {
url : this.url + '/api/v1/datapoints/query/tags', url: this.url + '/api/v1/datapoints/query/tags',
method : 'POST', method: 'POST',
data : { data: {
metrics : [{ name : metricname }], metrics: [{ name: metric }],
cache_time : 0, cache_time: 0,
start_absolute: 0 start_absolute: 0
} }
}; };
...@@ -140,19 +168,7 @@ function (angular, _, kbn) { ...@@ -140,19 +168,7 @@ function (angular, _, kbn) {
}; };
KairosDBDatasource.prototype.metricFindQuery = function(query) { KairosDBDatasource.prototype.metricFindQuery = function(query) {
function format(results, query) { if (!query) { return $q.when([]); }
return _.chain(results)
.filter(function(result) {
return result.indexOf(query) >= 0;
})
.map(function(result) {
return {
text: result,
expandable: true
};
})
.value();
}
var interpolated; var interpolated;
try { try {
...@@ -162,30 +178,32 @@ function (angular, _, kbn) { ...@@ -162,30 +178,32 @@ function (angular, _, kbn) {
return $q.reject(err); return $q.reject(err);
} }
var responseTransform = function(result) {
return _.map(result, function(value) {
return {text: value};
});
};
var metrics_regex = /metrics\((.*)\)/; var metrics_regex = /metrics\((.*)\)/;
var tag_names_regex = /tag_names\((.*)\)/; 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); var metrics_query = interpolated.match(metrics_regex);
if (metrics_query) { if (metrics_query) {
return this.performMetricSuggestQuery().then(function(metrics) { return this._performMetricSuggestQuery(metrics_query[1]).then(responseTransform);
return format(metrics, metrics_query[1]);
});
} }
var tag_names_query = interpolated.match(tag_names_regex); var tag_names_query = interpolated.match(tag_names_regex);
if (tag_names_query) { if (tag_names_query) {
return this.performListTagNames().then(function(tag_names) { return this._performMetricKeyLookup(tag_names_query[1]).then(responseTransform);
return format(tag_names, tag_names_query[1]);
});
} }
var tag_values_query = interpolated.match(tag_values_regex); var tag_values_query = interpolated.match(tag_values_regex);
if (tag_values_query) { if (tag_values_query) {
return this.performListTagValues().then(function(tag_values) { return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform);
return format(tag_values, tag_values_query[1]);
});
} }
return $q.when([]);
}; };
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
......
...@@ -6,8 +6,6 @@ function (angular, _) { ...@@ -6,8 +6,6 @@ function (angular, _) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var metricList = [];
var tagList = [];
module.controller('KairosDBQueryCtrl', function($scope) { module.controller('KairosDBQueryCtrl', function($scope) {
...@@ -48,50 +46,26 @@ function (angular, _) { ...@@ -48,50 +46,26 @@ function (angular, _) {
_.move($scope.panel.targets, fromIndex, toIndex); _.move($scope.panel.targets, fromIndex, toIndex);
}; };
$scope.getTextValues = function(metricFindResult) {
return _.map(metricFindResult, function(value) { return value.text; });
};
$scope.suggestMetrics = function(query, callback) { $scope.suggestMetrics = function(query, callback) {
if (!_.isEmpty(metricList)) { $scope.datasource.metricFindQuery('metrics(' + query + ')')
return metricList; .then($scope.getTextValues)
} .then(callback);
else {
$scope.datasource.performMetricSuggestQuery().then(function(result) {
metricList = result;
callback(metricList);
});
}
}; };
$scope.suggestTagKeys = function(query, callback) { $scope.suggestTagKeys = function(query, callback) {
if (!_.isEmpty(tagList)) { $scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
var result = _.find(tagList, { name : $scope.target.metric }); .then($scope.getTextValues)
.then(callback);
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.suggestTagValues = function(query, callback) { $scope.suggestTagValues = function(query, callback) {
if (!_.isEmpty(tagList)) { $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
var result = _.find(tagList, { name : $scope.target.metric }); .then($scope.getTextValues)
.then(callback);
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]);
}
});
}; };
// Filter metric by tag // 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