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';
var module = angular.module('grafana.services');
var tagList = null;
module.factory('KairosDBDatasource', function($q, $http) {
@ -92,31 +91,24 @@ function (angular, _, kbn) {
});
};
KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname, range, type, keyValue) {
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');
KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) {
var options = {
url : this.url + '/api/v1/datapoints/query/tags',
method : 'POST',
data : body
data : {
metrics : [{ name : metricname }],
cache_time : 0,
start_absolute: 0
}
};
return $http(options).then(function(results) {
tagList.results = results;
return getTagListFromResponse(results, type, keyValue);
return $http(options).then(function(response) {
if (!response.data) {
return [];
}
else {
return response.data.queries[0].results[0];
}
});
};
@ -124,21 +116,6 @@ function (angular, _, kbn) {
/// 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
* @param results

View File

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