From 8aa5b62d6dfb45fd800d9c132f9c869e7d15fced Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Wed, 8 Feb 2017 00:01:42 +0100 Subject: [PATCH] fix(panel): case insensitive sort metric sources Sorts the list of metric sources that is used in dropdown for Panel Data Source on the Metrics tab so that it is case insensitive and so that the built data sources are last in the list. --- public/app/core/services/datasource_srv.js | 13 ++++- public/test/specs/datasource_srv_specs.js | 58 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 public/test/specs/datasource_srv_specs.js diff --git a/public/app/core/services/datasource_srv.js b/public/app/core/services/datasource_srv.js index b8b94cd286d..7c876ebd941 100644 --- a/public/app/core/services/datasource_srv.js +++ b/public/app/core/services/datasource_srv.js @@ -97,10 +97,19 @@ function (angular, _, coreModule, config) { } metricSources.sort(function(a, b) { - if (a.meta.builtIn || a.name > b.name) { + if (a.meta.builtIn) { return 1; } - if (a.name < b.name) { + + if (b.meta.builtIn) { + return -1; + } + + if (a.name.toLowerCase() > b.name.toLowerCase()) { + return 1; + } + + if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } return 0; diff --git a/public/test/specs/datasource_srv_specs.js b/public/test/specs/datasource_srv_specs.js new file mode 100644 index 00000000000..be7c054acaa --- /dev/null +++ b/public/test/specs/datasource_srv_specs.js @@ -0,0 +1,58 @@ +define([ + 'app/core/config', + 'app/core/services/datasource_srv' +], function(config) { + 'use strict'; + + describe('datasource_srv', function() { + var _datasourceSrv; + var metricSources; + var templateSrv = {}; + + beforeEach(module('grafana.core')); + beforeEach(module(function($provide) { + $provide.value('templateSrv', templateSrv); + })); + beforeEach(module('grafana.services')); + beforeEach(inject(function(datasourceSrv) { + _datasourceSrv = datasourceSrv; + })); + + describe('when loading metric sources', function() { + var unsortedDatasources = { + 'mmm': { + type: 'test-db', + meta: { metrics: {m: 1} } + }, + '--Mixed--': { + type: 'test-db', + meta: {builtIn: true, metrics: {m: 1} } + }, + 'ZZZ': { + type: 'test-db', + meta: {metrics: {m: 1} } + }, + 'aaa': { + type: 'test-db', + meta: { metrics: {m: 1} } + }, + 'BBB': { + type: 'test-db', + meta: { metrics: {m: 1} } + }, + }; + beforeEach(function() { + config.datasources = unsortedDatasources; + metricSources = _datasourceSrv.getMetricSources({skipVariables: true}); + }); + + it('should return a list of sources sorted case insensitively with builtin sources last', function() { + expect(metricSources[0].name).to.be('aaa'); + expect(metricSources[1].name).to.be('BBB'); + expect(metricSources[2].name).to.be('mmm'); + expect(metricSources[3].name).to.be('ZZZ'); + expect(metricSources[4].name).to.be('--Mixed--'); + }); + }); + }); +});