Skip to content

Commit 94e5001

Browse files
committed
feat(templating): making progress on adhoc template variable, grafana#6038
1 parent 83b9db5 commit 94e5001

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

public/app/features/dashboard/ad_hoc_filters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AdHocFiltersCtrl {
2121
if (this.variable.value && !_.isArray(this.variable.value)) {
2222
}
2323

24-
for (let tag of this.variable.value) {
24+
for (let tag of this.variable.tags) {
2525
if (this.segments.length > 0) {
2626
this.segments.push(this.uiSegmentSrv.newCondition('AND'));
2727
}
@@ -130,7 +130,7 @@ export class AdHocFiltersCtrl {
130130
});
131131

132132
this.$rootScope.$broadcast('refresh');
133-
this.variable.value = tags;
133+
this.variable.tags = tags;
134134
}
135135
}
136136

public/app/features/templating/templateValuesSrv.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ function (angular, _, $, kbn) {
189189
}
190190

191191
if (variable.type === 'adhoc') {
192+
variable.current = {};
192193
variable.options = [];
193194
return;
194195
}

public/app/plugins/datasource/influxdb/datasource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class InfluxDatasource {
5656
// apply add hoc filters
5757
for (let variable of this.templateSrv.variables) {
5858
if (variable.type === 'adhoc' && variable.datasource === this.name) {
59-
for (let tag of variable.value) {
59+
for (let tag of variable.tags) {
6060
if (tag.key !== undefined && tag.value !== undefined) {
6161
target.tags.push({key: tag.key, value: tag.value, condition: 'AND'});
6262
}

public/test/specs/templateValuesSrv-specs.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,43 @@ define([
2828
});
2929

3030
describe('when template variable is present in url', function() {
31-
var variable = {
32-
name: 'apps',
33-
current: {text: "test", value: "test"},
34-
options: [{text: "test", value: "test"}]
35-
};
31+
describe('and setting simple variable', function() {
32+
var variable = {
33+
name: 'apps',
34+
current: {text: "test", value: "test"},
35+
options: [{text: "test", value: "test"}]
36+
};
3637

37-
beforeEach(function(done) {
38-
var dashboard = { templating: { list: [variable] } };
39-
var urlParams = {};
40-
urlParams["var-apps"] = "new";
41-
ctx.$location.search = sinon.stub().returns(urlParams);
42-
ctx.service.init(dashboard).then(function() { done(); });
43-
ctx.$rootScope.$digest();
38+
beforeEach(function(done) {
39+
var dashboard = { templating: { list: [variable] } };
40+
var urlParams = {};
41+
urlParams["var-apps"] = "new";
42+
ctx.$location.search = sinon.stub().returns(urlParams);
43+
ctx.service.init(dashboard).then(function() { done(); });
44+
ctx.$rootScope.$digest();
45+
});
46+
47+
it('should update current value', function() {
48+
expect(variable.current.value).to.be("new");
49+
expect(variable.current.text).to.be("new");
50+
});
4451
});
4552

46-
it('should update current value', function() {
47-
expect(variable.current.value).to.be("new");
48-
expect(variable.current.text).to.be("new");
53+
describe('and setting adhoc variable', function() {
54+
var variable = {name: 'filters', type: 'adhoc'};
55+
56+
beforeEach(function(done) {
57+
var dashboard = { templating: { list: [variable] } };
58+
var urlParams = {};
59+
urlParams["var-filters"] = "hostname|gt|server2";
60+
ctx.$location.search = sinon.stub().returns(urlParams);
61+
ctx.service.init(dashboard).then(function() { done(); });
62+
ctx.$rootScope.$digest();
63+
});
64+
65+
it('should update current value', function() {
66+
expect(variable.tags[0]).to.eq({tag: 'hostname', value: 'server2'});
67+
});
4968
});
5069
});
5170

0 commit comments

Comments
 (0)