Skip to content

Commit f7abf01

Browse files
committed
Merge branch 'mtanda-template_sort'
2 parents 1c7b76c + 03fb152 commit f7abf01

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

public/app/features/templating/editorCtrl.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function (angular, _) {
1313
type: 'query',
1414
datasource: null,
1515
refresh: 0,
16+
sort: 1,
1617
name: '',
1718
hide: 0,
1819
options: [],
@@ -34,6 +35,14 @@ function (angular, _) {
3435
{value: 2, text: "On Time Range Change"},
3536
];
3637

38+
$scope.sortOptions = [
39+
{value: 0, text: "Without Sort"},
40+
{value: 1, text: "Alphabetical (asc)"},
41+
{value: 2, text: "Alphabetical (desc)"},
42+
{value: 3, text: "Numerical (asc)"},
43+
{value: 4, text: "Numerical (desc)"},
44+
];
45+
3746
$scope.hideOptions = [
3847
{value: 0, text: ""},
3948
{value: 1, text: "Label"},
@@ -114,6 +123,7 @@ function (angular, _) {
114123
$scope.currentIsNew = false;
115124
$scope.mode = 'edit';
116125

126+
$scope.current.sort = $scope.current.sort || replacementDefaults.sort;
117127
if ($scope.current.datasource === void 0) {
118128
$scope.current.datasource = null;
119129
$scope.current.type = 'query';

public/app/features/templating/partials/editor.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ <h5 class="section-heading">Query Options</h5>
181181
<select class="gf-form-input" ng-model="current.refresh" ng-options="f.value as f.text for f in refreshOptions"></select>
182182
</div>
183183
</div>
184+
<div class="gf-form max-width-21">
185+
<span class="gf-form-label width-7">
186+
Sort
187+
<info-popover mode="right-normal">
188+
How to sort the values of this variable.
189+
</info-popover>
190+
</span>
191+
<div class="gf-form-select-wrapper max-width-14">
192+
<select class="gf-form-input" ng-model="current.sort" ng-options="f.value as f.text for f in sortOptions" ng-change="runQuery()"></select>
193+
</div>
194+
</div>
184195
</div>
185196
<div class="gf-form">
186197
<span class="gf-form-label width-7">Query</span>

public/app/features/templating/templateValuesSrv.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function (angular, _, $, kbn) {
342342

343343
this.metricNamesToVariableValues = function(variable, metricNames) {
344344
var regex, options, i, matches;
345-
options = {}; // use object hash to remove duplicates
345+
options = [];
346346

347347
if (variable.regex) {
348348
regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
@@ -370,16 +370,43 @@ function (angular, _, $, kbn) {
370370
}
371371
}
372372

373-
options[value] = {text: text, value: value};
373+
options.push({text: text, value: value});
374374
}
375+
options = _.uniq(options, 'value');
375376

376-
return _.sortBy(options, 'text');
377+
return this.sortVariableValues(options, variable.sort);
377378
};
378379

379380
this.addAllOption = function(variable) {
380381
variable.options.unshift({text: 'All', value: "$__all"});
381382
};
382383

384+
this.sortVariableValues = function(options, sortOrder) {
385+
if (sortOrder === 0) {
386+
return options;
387+
}
388+
389+
var sortType = Math.ceil(sortOrder / 2);
390+
var reverseSort = (sortOrder % 2 === 0);
391+
if (sortType === 1) {
392+
options = _.sortBy(options, 'text');
393+
} else if (sortType === 2) {
394+
options = _.sortBy(options, function(opt) {
395+
var matches = opt.text.match(/.*?(\d+).*/);
396+
if (!matches) {
397+
return 0;
398+
} else {
399+
return parseInt(matches[1], 10);
400+
}
401+
});
402+
}
403+
if (reverseSort) {
404+
options = options.reverse();
405+
}
406+
407+
return options;
408+
};
409+
383410
});
384411

385412
});

public/test/specs/templateValuesSrv-specs.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,69 @@ define([
386386
});
387387
});
388388

389+
describeUpdateVariable('without sort', function(scenario) {
390+
scenario.setup(function() {
391+
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 0};
392+
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
393+
});
394+
395+
it('should return options without sort', function() {
396+
expect(scenario.variable.options[0].text).to.be('bbb2');
397+
expect(scenario.variable.options[1].text).to.be('aaa10');
398+
expect(scenario.variable.options[2].text).to.be('ccc3');
399+
});
400+
});
401+
402+
describeUpdateVariable('with alphabetical sort (asc)', function(scenario) {
403+
scenario.setup(function() {
404+
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1};
405+
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
406+
});
407+
408+
it('should return options with alphabetical sort', function() {
409+
expect(scenario.variable.options[0].text).to.be('aaa10');
410+
expect(scenario.variable.options[1].text).to.be('bbb2');
411+
expect(scenario.variable.options[2].text).to.be('ccc3');
412+
});
413+
});
414+
415+
describeUpdateVariable('with alphabetical sort (desc)', function(scenario) {
416+
scenario.setup(function() {
417+
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 2};
418+
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
419+
});
420+
421+
it('should return options with alphabetical sort', function() {
422+
expect(scenario.variable.options[0].text).to.be('ccc3');
423+
expect(scenario.variable.options[1].text).to.be('bbb2');
424+
expect(scenario.variable.options[2].text).to.be('aaa10');
425+
});
426+
});
427+
428+
describeUpdateVariable('with numerical sort (asc)', function(scenario) {
429+
scenario.setup(function() {
430+
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 3};
431+
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
432+
});
433+
434+
it('should return options with numerical sort', function() {
435+
expect(scenario.variable.options[0].text).to.be('bbb2');
436+
expect(scenario.variable.options[1].text).to.be('ccc3');
437+
expect(scenario.variable.options[2].text).to.be('aaa10');
438+
});
439+
});
440+
441+
describeUpdateVariable('with numerical sort (desc)', function(scenario) {
442+
scenario.setup(function() {
443+
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 4};
444+
scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
445+
});
446+
447+
it('should return options with numerical sort', function() {
448+
expect(scenario.variable.options[0].text).to.be('aaa10');
449+
expect(scenario.variable.options[1].text).to.be('ccc3');
450+
expect(scenario.variable.options[2].text).to.be('bbb2');
451+
});
452+
});
389453
});
390454
});

0 commit comments

Comments
 (0)