grafana-dash-gen 3.3.2 → 3.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/grafana/templates/custom.js +63 -24
- package/package.json +1 -1
- package/test/fixtures/override_dashboard.js +2 -2
- package/test/fixtures/templates/override_custom.js +1 -0
- package/test/fixtures/templates/override_custom_text_value.js +1 -0
- package/test/fixtures/templates/simple_custom.js +1 -0
- package/test/templates/custom.js +61 -0
|
@@ -20,30 +20,49 @@
|
|
|
20
20
|
|
|
21
21
|
'use strict';
|
|
22
22
|
|
|
23
|
+
// For some reason Grafana will interpret this as a specific string if provided
|
|
24
|
+
// as the allValue of the Custom object. In order to correctly support Grafanas
|
|
25
|
+
// fallback $__all variable, allValue must be null or empty string, and the
|
|
26
|
+
// $__all variable must be provided as the value in the "All" entry in the
|
|
27
|
+
// options list.
|
|
28
|
+
const DEFAULT_VARIABLE_ALL = '$__all';
|
|
29
|
+
|
|
23
30
|
function Custom(opts) {
|
|
24
31
|
opts = opts || {};
|
|
25
|
-
|
|
26
|
-
var defaultState = {
|
|
32
|
+
this.state = {
|
|
27
33
|
allFormat: 'glob',
|
|
28
34
|
current: null,
|
|
29
35
|
datasource: null,
|
|
30
36
|
includeAll: false,
|
|
37
|
+
allValue: '',
|
|
31
38
|
name: 'template',
|
|
32
39
|
options: [],
|
|
33
40
|
query: null,
|
|
34
41
|
'refresh_on_load': false,
|
|
35
42
|
type: 'custom'
|
|
36
43
|
};
|
|
37
|
-
this.
|
|
44
|
+
this.defaultValue = '';
|
|
38
45
|
|
|
39
46
|
// Overwrite defaults with custom values
|
|
40
|
-
Object.keys(opts).forEach(
|
|
41
|
-
|
|
47
|
+
Object.keys(opts).forEach(key => {
|
|
48
|
+
switch (key) {
|
|
49
|
+
case 'defaultValue':
|
|
50
|
+
this.defaultValue = opts[key];
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
this.state[key] = opts[key];
|
|
54
|
+
}
|
|
42
55
|
});
|
|
56
|
+
this._processAll();
|
|
57
|
+
this._processOptions();
|
|
58
|
+
}
|
|
43
59
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
Custom.prototype._processAll = function _processAll() {
|
|
61
|
+
if (!this.state.includeAll) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const allValue = !this.state.allValue ? DEFAULT_VARIABLE_ALL : this.state.allValue;
|
|
65
|
+
this.state.options = [{ text: "All", value: allValue }, ...this.state.options];
|
|
47
66
|
}
|
|
48
67
|
|
|
49
68
|
/*
|
|
@@ -51,26 +70,46 @@ function Custom(opts) {
|
|
|
51
70
|
* values.
|
|
52
71
|
*/
|
|
53
72
|
Custom.prototype._processOptions = function _processOptions() {
|
|
54
|
-
|
|
73
|
+
if (!this.state.options.length) {
|
|
74
|
+
if (this.defaultValue !== '') {
|
|
75
|
+
throw new SyntaxError("cannot define default value without any options")
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
55
79
|
|
|
56
|
-
|
|
57
|
-
|
|
80
|
+
let self = this;
|
|
81
|
+
let newOptions = [];
|
|
82
|
+
let newQuery = [];
|
|
58
83
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
84
|
+
let hasAll = false;
|
|
85
|
+
for (let i = 0; i < this.state.options.length; i++) {
|
|
86
|
+
const option = this.state.options[i];
|
|
87
|
+
const isObject = typeof option === 'object' && option.constructor === Object;
|
|
88
|
+
const opt = isObject ? option : {
|
|
89
|
+
text: option,
|
|
90
|
+
value: option
|
|
91
|
+
};
|
|
92
|
+
if (opt.value === DEFAULT_VARIABLE_ALL) {
|
|
93
|
+
if (hasAll) {
|
|
94
|
+
continue;
|
|
68
95
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
96
|
+
hasAll = true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
newOptions.push(opt);
|
|
100
|
+
newQuery.push(opt.value);
|
|
101
|
+
}
|
|
72
102
|
|
|
73
|
-
|
|
103
|
+
if (this.defaultValue !== '') {
|
|
104
|
+
const defaultOption = newOptions.find(option => option.value === this.defaultValue);
|
|
105
|
+
console.log(defaultOption);
|
|
106
|
+
if (!defaultOption) {
|
|
107
|
+
throw new SyntaxError("default value not found in options list")
|
|
108
|
+
}
|
|
109
|
+
self.state.current = defaultOption
|
|
110
|
+
} else {
|
|
111
|
+
self.state.current = newOptions[0];
|
|
112
|
+
}
|
|
74
113
|
|
|
75
114
|
this.state.options = newOptions;
|
|
76
115
|
this.state.query = newQuery.join(',');
|
package/package.json
CHANGED
|
@@ -18,8 +18,6 @@
|
|
|
18
18
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
19
|
// THE SOFTWARE.
|
|
20
20
|
|
|
21
|
-
var ExternalLink = require('../../grafana/external-link')
|
|
22
|
-
|
|
23
21
|
'use strict';
|
|
24
22
|
module.exports = {
|
|
25
23
|
id: null,
|
|
@@ -45,6 +43,7 @@ module.exports = {
|
|
|
45
43
|
},
|
|
46
44
|
datasource: null,
|
|
47
45
|
includeAll: false,
|
|
46
|
+
allValue: '',
|
|
48
47
|
name: 'myvar',
|
|
49
48
|
options: [{
|
|
50
49
|
text: 'a',
|
|
@@ -64,6 +63,7 @@ module.exports = {
|
|
|
64
63
|
},
|
|
65
64
|
datasource: null,
|
|
66
65
|
includeAll: false,
|
|
66
|
+
allValue: '',
|
|
67
67
|
name: 'smoothing',
|
|
68
68
|
options: [{
|
|
69
69
|
text: '30min',
|
package/test/templates/custom.js
CHANGED
|
@@ -94,5 +94,66 @@ test('Custom template overwrites default state', function t(assert) {
|
|
|
94
94
|
arbitraryProperty: 'foo'
|
|
95
95
|
});
|
|
96
96
|
assert.equal(customTemplate.state.includeAll, true);
|
|
97
|
+
assert.equal(customTemplate.state.allValue, '');
|
|
98
|
+
assert.equal(customTemplate.state.current.text, "All");
|
|
99
|
+
assert.equal(customTemplate.state.current.value, '$__all');
|
|
100
|
+
|
|
101
|
+
var customWithAllValue = new Custom({
|
|
102
|
+
includeAll: true,
|
|
103
|
+
arbitraryProperty: 'foo',
|
|
104
|
+
allValue: 'grafana',
|
|
105
|
+
});
|
|
106
|
+
assert.equal(customWithAllValue.state.includeAll, true);
|
|
107
|
+
assert.equal(customWithAllValue.state.allValue, 'grafana')
|
|
108
|
+
|
|
109
|
+
var allIsDefault = new Custom({
|
|
110
|
+
includeAll: true,
|
|
111
|
+
arbitraryProperty: 'foo',
|
|
112
|
+
options: [{ text: 'grafana', value: 'grafana' }]
|
|
113
|
+
});
|
|
114
|
+
assert.equal(allIsDefault.state.includeAll, true);
|
|
115
|
+
assert.equal(allIsDefault.state.allValue, '')
|
|
116
|
+
assert.equal(allIsDefault.state.current.text, "All");
|
|
117
|
+
assert.equal(allIsDefault.state.current.value, '$__all');
|
|
118
|
+
|
|
97
119
|
assert.end();
|
|
98
120
|
});
|
|
121
|
+
|
|
122
|
+
test('Custom template supports custom default', function t(assert) {
|
|
123
|
+
const defaultOption = { text: 'dash-gen', value: 'dash-gen' }
|
|
124
|
+
var definedDefault = new Custom({
|
|
125
|
+
includeAll: true,
|
|
126
|
+
defaultValue: defaultOption.value,
|
|
127
|
+
options: [{ text: 'grafana', value: 'grafana' }, defaultOption]
|
|
128
|
+
});
|
|
129
|
+
assert.equal(definedDefault.state.includeAll, true);
|
|
130
|
+
assert.equal(definedDefault.state.allValue, '')
|
|
131
|
+
assert.equal(definedDefault.state.current, defaultOption);
|
|
132
|
+
|
|
133
|
+
assert.throws(
|
|
134
|
+
() => new Custom({
|
|
135
|
+
includeAll: true,
|
|
136
|
+
defaultValue: defaultOption.value,
|
|
137
|
+
options: [{ text: 'grafana', value: 'grafana' }]
|
|
138
|
+
}),
|
|
139
|
+
new SyntaxError("default value not found in options list"),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
assert.throws(
|
|
143
|
+
() => new Custom({
|
|
144
|
+
includeAll: true,
|
|
145
|
+
defaultValue: defaultOption.value,
|
|
146
|
+
}),
|
|
147
|
+
new SyntaxError("default value not found in options list"),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
assert.throws(
|
|
151
|
+
() => new Custom({
|
|
152
|
+
defaultValue: defaultOption.value,
|
|
153
|
+
}),
|
|
154
|
+
new SyntaxError("cannot define default value without any options"),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
assert.end();
|
|
158
|
+
});
|
|
159
|
+
|