(templating) add sort order option

This commit is contained in:
Mitsuhiro Tanda
2016-05-21 17:56:57 +09:00
parent 0555525f97
commit 8987cfe0a2
4 changed files with 73 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ function (angular, _) {
type: 'query', type: 'query',
datasource: null, datasource: null,
refresh: 0, refresh: 0,
sort: 1,
name: '', name: '',
hide: 0, hide: 0,
options: [], options: [],
@@ -34,6 +35,12 @@ function (angular, _) {
{value: 2, text: "On Time Range Change"}, {value: 2, text: "On Time Range Change"},
]; ];
$scope.sortOptions = [
{value: 0, text: "Without Sort"},
{value: 1, text: "Alphabetical"},
{value: 2, text: "Numerical"},
];
$scope.hideOptions = [ $scope.hideOptions = [
{value: 0, text: ""}, {value: 0, text: ""},
{value: 1, text: "Label"}, {value: 1, text: "Label"},

View File

@@ -181,6 +181,17 @@
<select class="gf-form-input" ng-model="current.refresh" ng-options="f.value as f.text for f in refreshOptions"></select> <select class="gf-form-input" ng-model="current.refresh" ng-options="f.value as f.text for f in refreshOptions"></select>
</div> </div>
</div> </div>
<div class="gf-form max-width-21">
<span class="gf-form-label width-7">
Sort
<info-popover mode="right-normal">
How to sort the values of this variable.
</info-popover>
</span>
<div class="gf-form-select-wrapper max-width-14">
<select class="gf-form-input" ng-model="current.sort" ng-options="f.value as f.text for f in sortOptions" ng-change="runQuery()"></select>
</div>
</div>
</div> </div>
<div class="gf-form"> <div class="gf-form">
<span class="gf-form-label width-7">Query</span> <span class="gf-form-label width-7">Query</span>

View File

@@ -327,7 +327,7 @@ function (angular, _, kbn) {
this.metricNamesToVariableValues = function(variable, metricNames) { this.metricNamesToVariableValues = function(variable, metricNames) {
var regex, options, i, matches; var regex, options, i, matches;
options = {}; // use object hash to remove duplicates options = [];
if (variable.regex) { if (variable.regex) {
regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex)); regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
@@ -355,10 +355,24 @@ function (angular, _, kbn) {
} }
} }
options[value] = {text: text, value: value}; options.push({text: text, value: value});
} }
options = _.uniq(options, 'value');
return _.sortBy(options, 'text'); if (variable.sort === 1) {
return _.sortBy(options, 'text');
} else if (variable.sort === 2) {
return _.sortBy(options, function(opt) {
var matches = opt.text.match(/.*?(\d+).*/);
if (!matches) {
return 0;
} else {
return parseInt(matches[1], 10);
}
});
} else {
return options;
}
}; };
this.addAllOption = function(variable) { this.addAllOption = function(variable) {

View File

@@ -386,5 +386,43 @@ define([
}); });
}); });
describeUpdateVariable('without sort', function(scenario) {
scenario.setup(function() {
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 0};
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
});
it('should return options without sort', function() {
expect(scenario.variable.options[0].text).to.be('bbb2');
expect(scenario.variable.options[1].text).to.be('aaa10');
expect(scenario.variable.options[2].text).to.be('ccc3');
});
});
describeUpdateVariable('with alphabetical sort', function(scenario) {
scenario.setup(function() {
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1};
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
});
it('should return options with alphabetical sort', function() {
expect(scenario.variable.options[0].text).to.be('aaa10');
expect(scenario.variable.options[1].text).to.be('bbb2');
expect(scenario.variable.options[2].text).to.be('ccc3');
});
});
describeUpdateVariable('with numerical sort', function(scenario) {
scenario.setup(function() {
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 2};
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
});
it('should return options with numerical sort', function() {
expect(scenario.variable.options[0].text).to.be('bbb2');
expect(scenario.variable.options[1].text).to.be('ccc3');
expect(scenario.variable.options[2].text).to.be('aaa10');
});
});
}); });
}); });