Skip to content

Commit 46ebae7

Browse files
committed
feat(templating): progress on template system rewrite grafana#6048
1 parent 7e8b279 commit 46ebae7

File tree

14 files changed

+412
-454
lines changed

14 files changed

+412
-454
lines changed

public/app/core/utils/kbn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function($, _, moment) {
1010
kbn.valueFormats = {};
1111

1212
kbn.regexEscape = function(value) {
13-
return value.replace(/[\\^$*+?.()|[\]{}]/g, '\\\\$&');
13+
return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&')
1414
};
1515

1616
///// HELPER FUNCTIONS /////

public/app/features/templating/all.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import './editorCtrl';
55
import {VariableSrv} from './variable_srv';
66
import {IntervalVariable} from './interval_variable';
77
import {QueryVariable} from './query_variable';
8+
import {DatasourceVariable} from './datasource_variable';
9+
import {CustomVariable} from './custom_variable';
810

911
export {
1012
VariableSrv,
1113
IntervalVariable,
1214
QueryVariable,
15+
DatasourceVariable,
16+
CustomVariable,
1317
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
///<reference path="../../headers/common.d.ts" />
2+
3+
import _ from 'lodash';
4+
import kbn from 'app/core/utils/kbn';
5+
import {Variable} from './variable';
6+
import {VariableSrv, variableConstructorMap} from './variable_srv';
7+
8+
export class CustomVariable implements Variable {
9+
query: string;
10+
options: any;
11+
includeAll: boolean;
12+
13+
/** @ngInject */
14+
constructor(private model, private timeSrv, private templateSrv) {
15+
_.extend(this, model);
16+
}
17+
18+
setValue(option) {
19+
}
20+
21+
updateOptions() {
22+
// extract options in comma separated string
23+
this.options = _.map(this.query.split(/[,]+/), function(text) {
24+
return { text: text.trim(), value: text.trim() };
25+
});
26+
27+
if (this.includeAll) {
28+
this.addAllOption();
29+
}
30+
}
31+
32+
addAllOption() {
33+
this.options.unshift({text: 'All', value: "$__all"});
34+
}
35+
36+
dependsOn(variableName) {
37+
return false;
38+
}
39+
}
40+
41+
variableConstructorMap['custom'] = CustomVariable;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
///<reference path="../../headers/common.d.ts" />
2+
3+
import _ from 'lodash';
4+
import kbn from 'app/core/utils/kbn';
5+
import {Variable} from './variable';
6+
import {VariableSrv, variableConstructorMap} from './variable_srv';
7+
8+
export class DatasourceVariable implements Variable {
9+
regex: any;
10+
query: string;
11+
options: any;
12+
13+
/** @ngInject */
14+
constructor(private model, private datasourceSrv) {
15+
_.extend(this, model);
16+
}
17+
18+
setValue(option) {
19+
}
20+
21+
updateOptions() {
22+
var options = [];
23+
var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
24+
var regex;
25+
26+
if (this.regex) {
27+
regex = kbn.stringToJsRegex(this.regex);
28+
}
29+
30+
for (var i = 0; i < sources.length; i++) {
31+
var source = sources[i];
32+
// must match on type
33+
if (source.meta.id !== this.query) {
34+
continue;
35+
}
36+
37+
if (regex && !regex.exec(source.name)) {
38+
continue;
39+
}
40+
41+
options.push({text: source.name, value: source.name});
42+
}
43+
44+
if (options.length === 0) {
45+
options.push({text: 'No data sources found', value: ''});
46+
}
47+
48+
this.options = options;
49+
}
50+
51+
dependsOn(variableName) {
52+
return false;
53+
}
54+
}
55+
56+
variableConstructorMap['datasource'] = DatasourceVariable;

public/app/features/templating/interval_variable.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class IntervalVariable implements Variable {
4343
this.updateAutoValue();
4444
}
4545
}
46+
47+
dependsOn(variableName) {
48+
return false;
49+
}
4650
}
4751

4852
variableConstructorMap['interval'] = IntervalVariable;

public/app/features/templating/query_variable.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import _ from 'lodash';
44
import kbn from 'app/core/utils/kbn';
5-
import {Variable} from './variable';
5+
import {Variable, containsVariable} from './variable';
66
import {VariableSrv, variableConstructorMap} from './variable_srv';
77

88
function getNoneOption() {
@@ -17,8 +17,9 @@ export class QueryVariable implements Variable {
1717
options: any;
1818
current: any;
1919
includeAll: boolean;
20+
refresh: number;
2021

21-
constructor(private model, private datasourceSrv, private templateSrv, private variableSrv) {
22+
constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q) {
2223
_.extend(this, model);
2324
}
2425

@@ -33,6 +34,23 @@ export class QueryVariable implements Variable {
3334
return this.variableSrv.variableUpdated(this);
3435
}
3536

37+
setValueFromUrl(urlValue) {
38+
var promise = this.$q.when();
39+
40+
if (this.refresh) {
41+
promise = this.updateOptions();
42+
}
43+
44+
return promise.then(() => {
45+
var option = _.find(this.options, op => {
46+
return op.text === urlValue || op.value === urlValue;
47+
});
48+
49+
option = option || { text: urlValue, value: urlValue };
50+
return this.setValue(option);
51+
});
52+
}
53+
3654
updateOptions() {
3755
return this.datasourceSrv.get(this.datasource)
3856
.then(this.updateOptionsFromMetricFindQuery.bind(this))
@@ -102,24 +120,30 @@ export class QueryVariable implements Variable {
102120

103121
var sortType = Math.ceil(sortOrder / 2);
104122
var reverseSort = (sortOrder % 2 === 0);
123+
105124
if (sortType === 1) {
106125
options = _.sortBy(options, 'text');
107126
} else if (sortType === 2) {
108127
options = _.sortBy(options, function(opt) {
109-
var matches = opt.text.match(new RegExp(".*?(\d+).*"));
110-
if (!matches) {
111-
return 0;
112-
} else {
113-
return parseInt(matches[1], 10);
114-
}
128+
var matches = opt.text.match(/.*?(\d+).*/);
129+
if (!matches) {
130+
return 0;
131+
} else {
132+
return parseInt(matches[1], 10);
133+
}
115134
});
116135
}
136+
117137
if (reverseSort) {
118138
options = options.reverse();
119139
}
120140

121141
return options;
122142
}
143+
144+
dependsOn(variableName) {
145+
return containsVariable(this.query, variableName) || containsVariable(this.datasource, variableName);
146+
}
123147
}
124148

125149
variableConstructorMap['query'] = QueryVariable;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
2+
3+
import {containsVariable} from '../variable';
4+
5+
describe('containsVariable', function() {
6+
7+
describe('when checking if a string contains a variable', function() {
8+
9+
it('should find it with $var syntax', function() {
10+
var contains = containsVariable('this.$test.filters', 'test');
11+
expect(contains).to.be(true);
12+
});
13+
14+
it('should not find it if only part matches with $var syntax', function() {
15+
var contains = containsVariable('this.$ServerDomain.filters', 'Server');
16+
expect(contains).to.be(false);
17+
});
18+
19+
it('should find it with [[var]] syntax', function() {
20+
var contains = containsVariable('this.[[test]].filters', 'test');
21+
expect(contains).to.be(true);
22+
});
23+
24+
it('should find it when part of segment', function() {
25+
var contains = containsVariable('metrics.$env.$group-*', 'group');
26+
expect(contains).to.be(true);
27+
});
28+
29+
it('should find it its the only thing', function() {
30+
var contains = containsVariable('$env', 'env');
31+
expect(contains).to.be(true);
32+
});
33+
});
34+
35+
});
36+

0 commit comments

Comments
 (0)