Skip to content

Commit 6e429b8

Browse files
committed
feat(adhoc filters): good progress on ad hoc filters and sync from to url, grafana#6038
1 parent cb522d5 commit 6e429b8

File tree

14 files changed

+352
-288
lines changed

14 files changed

+352
-288
lines changed

public/app/features/dashboard/ad_hoc_filters.ts

Lines changed: 25 additions & 13 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.tags) {
24+
for (let tag of this.variable.filters) {
2525
if (this.segments.length > 0) {
2626
this.segments.push(this.uiSegmentSrv.newCondition('AND'));
2727
}
@@ -38,7 +38,11 @@ export class AdHocFiltersCtrl {
3838

3939
getOptions(segment, index) {
4040
if (segment.type === 'operator') {
41-
return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<>', '<', '>', '=~', '!~']));
41+
return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<', '>', '=~', '!~']));
42+
}
43+
44+
if (segment.type === 'condition') {
45+
return this.$q.when([this.uiSegmentSrv.newSegment('AND')]);
4246
}
4347

4448
return this.datasourceSrv.get(this.variable.datasource).then(ds => {
@@ -100,37 +104,45 @@ export class AdHocFiltersCtrl {
100104
}
101105

102106
updateVariableModel() {
103-
var tags = [];
104-
var tagIndex = -1;
105-
var tagOperator = "";
106-
107-
this.segments.forEach((segment, index) => {
108-
if (segment.fake) {
107+
var filters = [];
108+
var filterIndex = -1;
109+
var operator = "";
110+
var hasFakes = false;
111+
112+
this.segments.forEach(segment => {
113+
if (segment.type === 'value' && segment.fake) {
114+
hasFakes = true;
109115
return;
110116
}
111117

112118
switch (segment.type) {
113119
case 'key': {
114-
tags.push({key: segment.value});
115-
tagIndex += 1;
120+
filters.push({key: segment.value});
121+
filterIndex += 1;
116122
break;
117123
}
118124
case 'value': {
119-
tags[tagIndex].value = segment.value;
125+
filters[filterIndex].value = segment.value;
120126
break;
121127
}
122128
case 'operator': {
123-
tags[tagIndex].operator = segment.value;
129+
filters[filterIndex].operator = segment.value;
124130
break;
125131
}
126132
case 'condition': {
133+
filters[filterIndex].condition = segment.value;
127134
break;
128135
}
129136
}
130137
});
131138

139+
if (hasFakes) {
140+
return;
141+
}
142+
143+
this.variable.setFilters(filters);
144+
this.$rootScope.$emit('template-variable-value-updated');
132145
this.$rootScope.$broadcast('refresh');
133-
this.variable.tags = tags;
134146
}
135147
}
136148

public/app/features/dashboard/viewStateSrv.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ function (angular, _, $) {
3434
$location.search(urlParams);
3535
});
3636

37-
$scope.onAppEvent('template-variable-value-updated', function() {
38-
self.updateUrlParamsWithCurrentVariables();
39-
});
40-
4137
$scope.onAppEvent('$routeUpdate', function() {
4238
var urlState = self.getQueryStringState();
4339
if (self.needsSync(urlState)) {
@@ -57,22 +53,6 @@ function (angular, _, $) {
5753
this.expandRowForPanel();
5854
}
5955

60-
DashboardViewState.prototype.updateUrlParamsWithCurrentVariables = function() {
61-
// update url
62-
var params = $location.search();
63-
// remove variable params
64-
_.each(params, function(value, key) {
65-
if (key.indexOf('var-') === 0) {
66-
delete params[key];
67-
}
68-
});
69-
70-
// add new values
71-
templateSrv.fillVariableValuesForUrl(params);
72-
// update url
73-
$location.search(params);
74-
};
75-
7656
DashboardViewState.prototype.expandRowForPanel = function() {
7757
if (!this.state.panelId) { return; }
7858

public/app/features/templating/adhoc_variable.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import {Variable, assignModelProperties, variableTypes} from './variable';
66
import {VariableSrv} from './variable_srv';
77

88
export class AdhocVariable implements Variable {
9+
filters: any[];
910

1011
defaults = {
1112
type: 'adhoc',
1213
name: '',
1314
label: '',
1415
hide: 0,
1516
datasource: null,
16-
options: [],
17-
current: {},
18-
tags: {},
17+
filters: [],
1918
};
2019

2120
/** @ngInject **/
@@ -41,8 +40,31 @@ export class AdhocVariable implements Variable {
4140
}
4241

4342
setValueFromUrl(urlValue) {
43+
if (!_.isArray(urlValue)) {
44+
urlValue = [urlValue];
45+
}
46+
47+
this.filters = urlValue.map(item => {
48+
var values = item.split('|');
49+
return {
50+
key: values[0],
51+
operator: values[1],
52+
value: values[2],
53+
};
54+
});
55+
4456
return Promise.resolve();
4557
}
58+
59+
getValueForUrl() {
60+
return this.filters.map(filter => {
61+
return filter.key + '|' + filter.operator + '|' + filter.value;
62+
});
63+
}
64+
65+
setFilters(filters: any[]) {
66+
this.filters = filters;
67+
}
4668
}
4769

4870
variableTypes['adhoc'] = {

public/app/features/templating/constant_variable.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {VariableSrv} from './variable_srv';
77
export class ConstantVariable implements Variable {
88
query: string;
99
options: any[];
10+
current: any;
1011

1112
defaults = {
1213
type: 'constant',
1314
name: '',
1415
hide: 2,
1516
label: '',
1617
query: '',
18+
current: {},
1719
};
1820

1921
/** @ngInject */
@@ -43,6 +45,11 @@ export class ConstantVariable implements Variable {
4345
setValueFromUrl(urlValue) {
4446
return this.variableSrv.setOptionFromUrl(this, urlValue);
4547
}
48+
49+
getValueForUrl() {
50+
return this.current.value;
51+
}
52+
4653
}
4754

4855
variableTypes['constant'] = {

public/app/features/templating/custom_variable.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ export class CustomVariable implements Variable {
1010
options: any;
1111
includeAll: boolean;
1212
multi: boolean;
13+
current: any;
1314

1415
defaults = {
1516
type: 'custom',
1617
name: '',
1718
label: '',
1819
hide: 0,
1920
options: [],
20-
current: {text: '', value: ''},
21+
current: {},
2122
query: '',
2223
includeAll: false,
2324
multi: false,
25+
allValue: null,
2426
};
2527

2628
/** @ngInject **/
@@ -61,6 +63,13 @@ export class CustomVariable implements Variable {
6163
setValueFromUrl(urlValue) {
6264
return this.variableSrv.setOptionFromUrl(this, urlValue);
6365
}
66+
67+
getValueForUrl() {
68+
if (this.current.text === 'All') {
69+
return 'All';
70+
}
71+
return this.current.value;
72+
}
6473
}
6574

6675
variableTypes['custom'] = {

public/app/features/templating/datasource_variable.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ export class DatasourceVariable implements Variable {
99
regex: any;
1010
query: string;
1111
options: any;
12+
current: any;
1213

1314
defaults = {
1415
type: 'datasource',
1516
name: '',
1617
hide: 0,
1718
label: '',
18-
current: {text: '', value: ''},
19+
current: {},
1920
regex: '',
2021
options: [],
2122
query: '',
@@ -73,6 +74,10 @@ export class DatasourceVariable implements Variable {
7374
setValueFromUrl(urlValue) {
7475
return this.variableSrv.setOptionFromUrl(this, urlValue);
7576
}
77+
78+
getValueForUrl() {
79+
return this.current.value;
80+
}
7681
}
7782

7883
variableTypes['datasource'] = {

public/app/features/templating/interval_variable.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class IntervalVariable implements Variable {
1212
auto: boolean;
1313
query: string;
1414
refresh: number;
15+
current: any;
1516

1617
defaults = {
1718
type: 'interval',
@@ -20,7 +21,7 @@ export class IntervalVariable implements Variable {
2021
label: '',
2122
refresh: 2,
2223
options: [],
23-
current: {text: '', value: ''},
24+
current: {},
2425
query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
2526
auto: false,
2627
auto_min: '10s',
@@ -75,6 +76,10 @@ export class IntervalVariable implements Variable {
7576
this.updateAutoValue();
7677
return this.variableSrv.setOptionFromUrl(this, urlValue);
7778
}
79+
80+
getValueForUrl() {
81+
return this.current.value;
82+
}
7883
}
7984

8085
variableTypes['interval'] = {

public/app/features/templating/query_variable.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ export class QueryVariable implements Variable {
3333
name: '',
3434
multi: false,
3535
includeAll: false,
36+
allValue: null,
3637
options: [],
37-
current: {text: '', value: ''},
38+
current: {},
39+
tagsQuery: null,
40+
tagValuesQuery: null,
3841
};
3942

4043
constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q) {
@@ -56,6 +59,13 @@ export class QueryVariable implements Variable {
5659
return this.variableSrv.setOptionFromUrl(this, urlValue);
5760
}
5861

62+
getValueForUrl() {
63+
if (this.current.text === 'All') {
64+
return 'All';
65+
}
66+
return this.current.value;
67+
}
68+
5969
updateOptions() {
6070
return this.datasourceSrv.get(this.datasource)
6171
.then(this.updateOptionsFromMetricFindQuery.bind(this))

0 commit comments

Comments
 (0)