mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(export): templetize annotations and template vars, #5084
This commit is contained in:
parent
cac723dab3
commit
430bcf192f
@ -22,12 +22,10 @@ export class DashboardExporter {
|
|||||||
var datasources = {};
|
var datasources = {};
|
||||||
var promises = [];
|
var promises = [];
|
||||||
|
|
||||||
for (let row of dash.rows) {
|
var templateizeDatasourceUsage = obj => {
|
||||||
_.each(row.panels, (panel) => {
|
promises.push(this.datasourceSrv.get(obj.datasource).then(ds => {
|
||||||
if (panel.datasource !== undefined) {
|
|
||||||
promises.push(this.datasourceSrv.get(panel.datasource).then(ds => {
|
|
||||||
var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase();
|
var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase();
|
||||||
datasources[panel.datasource] = {
|
datasources[obj.datasource] = {
|
||||||
name: refName,
|
name: refName,
|
||||||
label: ds.name,
|
label: ds.name,
|
||||||
description: '',
|
description: '',
|
||||||
@ -35,7 +33,7 @@ export class DashboardExporter {
|
|||||||
pluginId: ds.meta.id,
|
pluginId: ds.meta.id,
|
||||||
pluginName: ds.meta.name,
|
pluginName: ds.meta.name,
|
||||||
};
|
};
|
||||||
panel.datasource = '${' + refName +'}';
|
obj.datasource = '${' + refName +'}';
|
||||||
|
|
||||||
requires['datasource' + ds.meta.id] = {
|
requires['datasource' + ds.meta.id] = {
|
||||||
type: 'datasource',
|
type: 'datasource',
|
||||||
@ -44,6 +42,13 @@ export class DashboardExporter {
|
|||||||
version: ds.meta.info.version || "1.0.0",
|
version: ds.meta.info.version || "1.0.0",
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
// check up panel data sources
|
||||||
|
for (let row of dash.rows) {
|
||||||
|
_.each(row.panels, (panel) => {
|
||||||
|
if (panel.datasource !== undefined) {
|
||||||
|
templateizeDatasourceUsage(panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
var panelDef = config.panels[panel.type];
|
var panelDef = config.panels[panel.type];
|
||||||
@ -58,6 +63,21 @@ export class DashboardExporter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// templatize template vars
|
||||||
|
for (let variable of dash.templating.list) {
|
||||||
|
if (variable.type === 'query') {
|
||||||
|
templateizeDatasourceUsage(variable);
|
||||||
|
variable.options = [];
|
||||||
|
variable.current = {};
|
||||||
|
variable.refresh = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// templatize annotations vars
|
||||||
|
for (let annotationDef of dash.annotations.list) {
|
||||||
|
templateizeDatasourceUsage(annotationDef);
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.all(promises).then(() => {
|
return Promise.all(promises).then(() => {
|
||||||
_.each(datasources, (value, key) => {
|
_.each(datasources, (value, key) => {
|
||||||
inputs.push(value);
|
inputs.push(value);
|
||||||
|
@ -10,12 +10,21 @@ describe.only('given dashboard with repeated panels', function() {
|
|||||||
beforeEach(done => {
|
beforeEach(done => {
|
||||||
dash = {
|
dash = {
|
||||||
rows: [],
|
rows: [],
|
||||||
templating: { list: [] }
|
templating: { list: [] },
|
||||||
|
annotations: { list: [] },
|
||||||
};
|
};
|
||||||
|
|
||||||
dash.templating.list.push({
|
dash.templating.list.push({
|
||||||
name: 'apps',
|
name: 'apps',
|
||||||
current: {},
|
type: 'query',
|
||||||
options: []
|
datasource: 'gfdb',
|
||||||
|
current: {value: 'Asd', text: 'Asd'},
|
||||||
|
options: [{value: 'Asd', text: 'Asd'}]
|
||||||
|
});
|
||||||
|
|
||||||
|
dash.annotations.list.push({
|
||||||
|
name: 'logs',
|
||||||
|
datasource: 'gfdb',
|
||||||
});
|
});
|
||||||
|
|
||||||
dash.rows.push({
|
dash.rows.push({
|
||||||
@ -63,6 +72,17 @@ describe.only('given dashboard with repeated panels', function() {
|
|||||||
expect(panel.datasource).to.be("${DS_GFDB}");
|
expect(panel.datasource).to.be("${DS_GFDB}");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should replace datasource in variable query', function() {
|
||||||
|
expect(exported.templating.list[0].datasource).to.be("${DS_GFDB}");
|
||||||
|
expect(exported.templating.list[0].options.length).to.be(0);
|
||||||
|
expect(exported.templating.list[0].current.value).to.be(undefined);
|
||||||
|
expect(exported.templating.list[0].current.text).to.be(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace datasource in annotation query', function() {
|
||||||
|
expect(exported.annotations.list[0].datasource).to.be("${DS_GFDB}");
|
||||||
|
});
|
||||||
|
|
||||||
it('should add datasource as input', function() {
|
it('should add datasource as input', function() {
|
||||||
expect(exported.__inputs[0].name).to.be("DS_GFDB");
|
expect(exported.__inputs[0].name).to.be("DS_GFDB");
|
||||||
expect(exported.__inputs[0].pluginId).to.be("testdb");
|
expect(exported.__inputs[0].pluginId).to.be("testdb");
|
||||||
|
Loading…
Reference in New Issue
Block a user