From 94e5001c0ed374ecbbece54cb2c75cfe1a9ef662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 15 Sep 2016 09:18:28 +0200 Subject: [PATCH] feat(templating): making progress on adhoc template variable, #6038 --- .../app/features/dashboard/ad_hoc_filters.ts | 4 +- .../features/templating/templateValuesSrv.js | 1 + .../plugins/datasource/influxdb/datasource.ts | 2 +- public/test/specs/templateValuesSrv-specs.js | 49 +++++++++++++------ 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/public/app/features/dashboard/ad_hoc_filters.ts b/public/app/features/dashboard/ad_hoc_filters.ts index c97c5588666..426f6162467 100644 --- a/public/app/features/dashboard/ad_hoc_filters.ts +++ b/public/app/features/dashboard/ad_hoc_filters.ts @@ -21,7 +21,7 @@ export class AdHocFiltersCtrl { if (this.variable.value && !_.isArray(this.variable.value)) { } - for (let tag of this.variable.value) { + for (let tag of this.variable.tags) { if (this.segments.length > 0) { this.segments.push(this.uiSegmentSrv.newCondition('AND')); } @@ -130,7 +130,7 @@ export class AdHocFiltersCtrl { }); this.$rootScope.$broadcast('refresh'); - this.variable.value = tags; + this.variable.tags = tags; } } diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 9c60cedc055..3db7a3f5d9f 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -189,6 +189,7 @@ function (angular, _, $, kbn) { } if (variable.type === 'adhoc') { + variable.current = {}; variable.options = []; return; } diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index f451d78e117..4bb183474fb 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -56,7 +56,7 @@ export default class InfluxDatasource { // apply add hoc filters for (let variable of this.templateSrv.variables) { if (variable.type === 'adhoc' && variable.datasource === this.name) { - for (let tag of variable.value) { + for (let tag of variable.tags) { if (tag.key !== undefined && tag.value !== undefined) { target.tags.push({key: tag.key, value: tag.value, condition: 'AND'}); } diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 1742f5739c3..7e62c0f87e0 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -28,24 +28,43 @@ define([ }); describe('when template variable is present in url', function() { - var variable = { - name: 'apps', - current: {text: "test", value: "test"}, - options: [{text: "test", value: "test"}] - }; + describe('and setting simple variable', function() { + var variable = { + name: 'apps', + current: {text: "test", value: "test"}, + options: [{text: "test", value: "test"}] + }; - beforeEach(function(done) { - var dashboard = { templating: { list: [variable] } }; - var urlParams = {}; - urlParams["var-apps"] = "new"; - ctx.$location.search = sinon.stub().returns(urlParams); - ctx.service.init(dashboard).then(function() { done(); }); - ctx.$rootScope.$digest(); + beforeEach(function(done) { + var dashboard = { templating: { list: [variable] } }; + var urlParams = {}; + urlParams["var-apps"] = "new"; + ctx.$location.search = sinon.stub().returns(urlParams); + ctx.service.init(dashboard).then(function() { done(); }); + ctx.$rootScope.$digest(); + }); + + it('should update current value', function() { + expect(variable.current.value).to.be("new"); + expect(variable.current.text).to.be("new"); + }); }); - it('should update current value', function() { - expect(variable.current.value).to.be("new"); - expect(variable.current.text).to.be("new"); + describe('and setting adhoc variable', function() { + var variable = {name: 'filters', type: 'adhoc'}; + + beforeEach(function(done) { + var dashboard = { templating: { list: [variable] } }; + var urlParams = {}; + urlParams["var-filters"] = "hostname|gt|server2"; + ctx.$location.search = sinon.stub().returns(urlParams); + ctx.service.init(dashboard).then(function() { done(); }); + ctx.$rootScope.$digest(); + }); + + it('should update current value', function() { + expect(variable.tags[0]).to.eq({tag: 'hostname', value: 'server2'}); + }); }); });