From 8987cfe0a21b99751a2fe14eec76c89dcaca748b Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Sat, 21 May 2016 17:56:57 +0900 Subject: [PATCH 1/5] (templating) add sort order option --- public/app/features/templating/editorCtrl.js | 7 ++++ .../features/templating/partials/editor.html | 11 ++++++ .../features/templating/templateValuesSrv.js | 20 ++++++++-- public/test/specs/templateValuesSrv-specs.js | 38 +++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/public/app/features/templating/editorCtrl.js b/public/app/features/templating/editorCtrl.js index 5efebc21e30..a8181ee1db3 100644 --- a/public/app/features/templating/editorCtrl.js +++ b/public/app/features/templating/editorCtrl.js @@ -13,6 +13,7 @@ function (angular, _) { type: 'query', datasource: null, refresh: 0, + sort: 1, name: '', hide: 0, options: [], @@ -34,6 +35,12 @@ function (angular, _) { {value: 2, text: "On Time Range Change"}, ]; + $scope.sortOptions = [ + {value: 0, text: "Without Sort"}, + {value: 1, text: "Alphabetical"}, + {value: 2, text: "Numerical"}, + ]; + $scope.hideOptions = [ {value: 0, text: ""}, {value: 1, text: "Label"}, diff --git a/public/app/features/templating/partials/editor.html b/public/app/features/templating/partials/editor.html index dd5aa6fa543..b55959bad93 100644 --- a/public/app/features/templating/partials/editor.html +++ b/public/app/features/templating/partials/editor.html @@ -181,6 +181,17 @@ +
+ + Sort + + How to sort the values of this variable. + + +
+ +
+
Query diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index fb751a2ce3a..896c152ce35 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -327,7 +327,7 @@ function (angular, _, kbn) { this.metricNamesToVariableValues = function(variable, metricNames) { var regex, options, i, matches; - options = {}; // use object hash to remove duplicates + options = []; if (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) { diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 7a8d8c1fccb..df59b798f3d 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -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'); + }); + }); }); }); From 16dc095329da44a96a9d344f7faba22ccb1edc5d Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Sat, 18 Jun 2016 02:08:24 +0900 Subject: [PATCH 2/5] (templating) support asc/desc sort --- public/app/features/templating/editorCtrl.js | 6 ++-- .../features/templating/templateValuesSrv.js | 31 ++++++++++++------- public/test/specs/templateValuesSrv-specs.js | 30 ++++++++++++++++-- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/public/app/features/templating/editorCtrl.js b/public/app/features/templating/editorCtrl.js index a8181ee1db3..5c923271278 100644 --- a/public/app/features/templating/editorCtrl.js +++ b/public/app/features/templating/editorCtrl.js @@ -37,8 +37,10 @@ function (angular, _) { $scope.sortOptions = [ {value: 0, text: "Without Sort"}, - {value: 1, text: "Alphabetical"}, - {value: 2, text: "Numerical"}, + {value: 1, text: "Alphabetical (asc)"}, + {value: 2, text: "Alphabetical (desc)"}, + {value: 3, text: "Numerical (asc)"}, + {value: 4, text: "Numerical (desc)"}, ]; $scope.hideOptions = [ diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 896c152ce35..38f305f3f40 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -359,18 +359,27 @@ function (angular, _, kbn) { } options = _.uniq(options, 'value'); - 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); - } - }); + var sortType = Math.ceil(variable.sort / 2); + if (sortType === 0) { + return options; } else { + if (sortType === 1) { + options = _.sortBy(options, 'text'); + } else if (sortType === 2) { + options = _.sortBy(options, function(opt) { + var matches = opt.text.match(/.*?(\d+).*/); + if (!matches) { + return 0; + } else { + return parseInt(matches[1], 10); + } + }); + } + + if (variable.sort % 2 === 0) { + options = options.reverse(); + } + return options; } }; diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index df59b798f3d..1742f5739c3 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -399,7 +399,7 @@ define([ }); }); - describeUpdateVariable('with alphabetical sort', function(scenario) { + describeUpdateVariable('with alphabetical sort (asc)', function(scenario) { scenario.setup(function() { scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1}; scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; @@ -412,17 +412,43 @@ define([ }); }); - describeUpdateVariable('with numerical sort', function(scenario) { + describeUpdateVariable('with alphabetical sort (desc)', 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 alphabetical sort', function() { + expect(scenario.variable.options[0].text).to.be('ccc3'); + expect(scenario.variable.options[1].text).to.be('bbb2'); + expect(scenario.variable.options[2].text).to.be('aaa10'); + }); + }); + + describeUpdateVariable('with numerical sort (asc)', function(scenario) { + scenario.setup(function() { + scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 3}; + 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'); }); }); + + describeUpdateVariable('with numerical sort (desc)', function(scenario) { + scenario.setup(function() { + scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 4}; + scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; + }); + + it('should return options with numerical sort', function() { + expect(scenario.variable.options[0].text).to.be('aaa10'); + expect(scenario.variable.options[1].text).to.be('ccc3'); + expect(scenario.variable.options[2].text).to.be('bbb2'); + }); + }); }); }); From 8af3bb739c1050656e853eedc4d9aa12bacd86e8 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Sun, 26 Jun 2016 22:15:57 +0900 Subject: [PATCH 3/5] refactor --- .../features/templating/templateValuesSrv.js | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 38f305f3f40..cd74c2b665f 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -359,29 +359,29 @@ function (angular, _, kbn) { } options = _.uniq(options, 'value'); - var sortType = Math.ceil(variable.sort / 2); - if (sortType === 0) { - return options; - } else { - if (sortType === 1) { - options = _.sortBy(options, 'text'); - } else if (sortType === 2) { - options = _.sortBy(options, function(opt) { - var matches = opt.text.match(/.*?(\d+).*/); - if (!matches) { - return 0; - } else { - return parseInt(matches[1], 10); - } - }); - } - - if (variable.sort % 2 === 0) { - options = options.reverse(); - } - + if (variable.sort === 0) { return options; } + + var sortType = Math.ceil(variable.sort / 2); + var reverseSort = (variable.sort % 2 === 0); + if (sortType === 1) { + options = _.sortBy(options, 'text'); + } else if (sortType === 2) { + options = _.sortBy(options, function(opt) { + var matches = opt.text.match(/.*?(\d+).*/); + if (!matches) { + return 0; + } else { + return parseInt(matches[1], 10); + } + }); + } + if (reverseSort) { + options = options.reverse(); + } + + return options; }; this.addAllOption = function(variable) { From 9c5436c648bf300bd20e44ca24049a3fac3cca17 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 13 Sep 2016 07:39:30 +0200 Subject: [PATCH 4/5] feat(templating): set default sort order --- public/app/features/templating/editorCtrl.js | 1 + 1 file changed, 1 insertion(+) diff --git a/public/app/features/templating/editorCtrl.js b/public/app/features/templating/editorCtrl.js index 5c923271278..95f3bd2e9dd 100644 --- a/public/app/features/templating/editorCtrl.js +++ b/public/app/features/templating/editorCtrl.js @@ -123,6 +123,7 @@ function (angular, _) { $scope.currentIsNew = false; $scope.mode = 'edit'; + $scope.current.sort = $scope.current.sort || replacementDefaults.sort; if ($scope.current.datasource === void 0) { $scope.current.datasource = null; $scope.current.type = 'query'; From 03fb152d4b323c08ce216222a26f52f327bfca95 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 13 Sep 2016 08:11:04 +0200 Subject: [PATCH 5/5] tech(templates): extract sort method --- .../features/templating/templateValuesSrv.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 1059d1a19c4..6125a4b44a9 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -374,12 +374,20 @@ function (angular, _, $, kbn) { } options = _.uniq(options, 'value'); - if (variable.sort === 0) { + return this.sortVariableValues(options, variable.sort); + }; + + this.addAllOption = function(variable) { + variable.options.unshift({text: 'All', value: "$__all"}); + }; + + this.sortVariableValues = function(options, sortOrder) { + if (sortOrder === 0) { return options; } - var sortType = Math.ceil(variable.sort / 2); - var reverseSort = (variable.sort % 2 === 0); + var sortType = Math.ceil(sortOrder / 2); + var reverseSort = (sortOrder % 2 === 0); if (sortType === 1) { options = _.sortBy(options, 'text'); } else if (sortType === 2) { @@ -399,10 +407,6 @@ function (angular, _, $, kbn) { return options; }; - this.addAllOption = function(variable) { - variable.options.unshift({text: 'All', value: "$__all"}); - }; - }); });