Refactoring of tag suggestion

This commit is contained in:
Masaori Koshiba 2015-04-26 18:18:43 +09:00
parent 87007994af
commit d86814b6c5
2 changed files with 42 additions and 39 deletions

View File

@ -8,7 +8,6 @@ function (angular, _, kbn) {
'use strict'; 'use strict';
var module = angular.module('grafana.services'); var module = angular.module('grafana.services');
var tagList = null;
module.factory('KairosDBDatasource', function($q, $http) { module.factory('KairosDBDatasource', function($q, $http) {
@ -92,31 +91,24 @@ function (angular, _, kbn) {
}); });
}; };
KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname, range, type, keyValue) { KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) {
if (tagList && (metricname === tagList.metricName) && (range.from === tagList.range.from) &&
(range.to === tagList.range.to)) {
return getTagListFromResponse(tagList.results, type, keyValue);
}
tagList = {
metricName: metricname,
range: range
};
var body = {
metrics : [{name : metricname}]
};
convertToKairosTime(range.from, body, 'start');
convertToKairosTime(range.to, body, 'end');
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 : body data : {
metrics : [{ name : metricname }],
cache_time : 0,
start_absolute: 0
}
}; };
return $http(options).then(function(results) { return $http(options).then(function(response) {
tagList.results = results; if (!response.data) {
return getTagListFromResponse(results, type, keyValue); return [];
}
else {
return response.data.queries[0].results[0];
}
}); });
}; };
@ -124,21 +116,6 @@ function (angular, _, kbn) {
/// Formatting methods /// Formatting methods
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
function getTagListFromResponse(results, type, keyValue) {
if (!results.data) {
return [];
}
else if (type === "key") {
return _.keys(results.data.queries[0].results[0].tags);
}
else if (type === "value" && _.has(results.data.queries[0].results[0].tags, keyValue)) {
return results.data.queries[0].results[0].tags[keyValue];
}
else {
return [];
}
}
/** /**
* Requires a verion of KairosDB with every CORS defects fixed * Requires a verion of KairosDB with every CORS defects fixed
* @param results * @param results

View File

@ -7,6 +7,7 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var metricList = []; var metricList = [];
var tagList = [];
var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']; var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
module.controller('KairosDBQueryCtrl', function($scope) { module.controller('KairosDBQueryCtrl', function($scope) {
@ -63,12 +64,37 @@ function (angular, _) {
}; };
$scope.suggestTagKeys = function(query, callback) { $scope.suggestTagKeys = function(query, callback) {
callback($scope.datasource.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'key', '')); 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.suggestTagValues = function(query, callback) { $scope.suggestTagValues = function(query, callback) {
callback($scope.datasource if (!_.isEmpty(tagList)) {
.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'value', $scope.target.currentTagKey)); 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]);
}
});
}; };
// Filter metric by tag // Filter metric by tag