From 4a1693196ce3c622bdc8ade5a8689da6ec2068be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 21 Sep 2016 14:57:19 +0200 Subject: [PATCH] fix(mixed datasource): fixed issue when exporting dashboard using mixed data source, fixes #6032 --- .../app/features/dashboard/export/exporter.ts | 16 +++++++- .../dashboard/specs/exporter_specs.ts | 39 +++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/public/app/features/dashboard/export/exporter.ts b/public/app/features/dashboard/export/exporter.ts index 958715b5cbb..b57f26ebb8b 100644 --- a/public/app/features/dashboard/export/exporter.ts +++ b/public/app/features/dashboard/export/exporter.ts @@ -24,6 +24,10 @@ export class DashboardExporter { var templateizeDatasourceUsage = obj => { promises.push(this.datasourceSrv.get(obj.datasource).then(ds => { + if (ds.meta.builtIn) { + return; + } + var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase(); datasources[refName] = { name: refName, @@ -46,11 +50,19 @@ export class DashboardExporter { // check up panel data sources for (let row of dash.rows) { - _.each(row.panels, (panel) => { + for (let panel of row.panels) { if (panel.datasource !== undefined) { templateizeDatasourceUsage(panel); } + if (panel.targets) { + for (let target of panel.targets) { + if (target.datasource !== undefined) { + templateizeDatasourceUsage(target); + } + } + } + var panelDef = config.panels[panel.type]; if (panelDef) { requires['panel' + panelDef.id] = { @@ -60,7 +72,7 @@ export class DashboardExporter { version: panelDef.info.version, }; } - }); + } } // templatize template vars diff --git a/public/app/features/dashboard/specs/exporter_specs.ts b/public/app/features/dashboard/specs/exporter_specs.ts index eac40172513..16430f13916 100644 --- a/public/app/features/dashboard/specs/exporter_specs.ts +++ b/public/app/features/dashboard/specs/exporter_specs.ts @@ -42,21 +42,34 @@ describe('given dashboard with repeated panels', function() { repeat: 'test', panels: [ {id: 2, repeat: 'apps', datasource: 'gfdb', type: 'graph'}, - {id: 2, repeat: null, repeatPanelId: 2}, + {id: 3, repeat: null, repeatPanelId: 2}, + { + id: 4, + datasource: '-- Mixed --', + targets: [{datasource: 'other'}], + }, ] }); + dash.rows.push({ repeat: null, repeatRowId: 1, panels: [], }); - var datasourceSrvStub = { - get: sinon.stub().returns(Promise.resolve({ - name: 'gfdb', - meta: {id: "testdb", info: {version: "1.2.1"}, name: "TestDB"} - })) - }; + var datasourceSrvStub = {get: sinon.stub()}; + datasourceSrvStub.get.withArgs('gfdb').returns(Promise.resolve({ + name: 'gfdb', + meta: {id: "testdb", info: {version: "1.2.1"}, name: "TestDB"} + })); + datasourceSrvStub.get.withArgs('other').returns(Promise.resolve({ + name: 'other', + meta: {id: "other", info: {version: "1.2.1"}, name: "OtherDB"} + })); + datasourceSrvStub.get.withArgs('-- Mixed --').returns(Promise.resolve({ + name: 'mixed', + meta: {id: "mixed", info: {version: "1.2.1"}, name: "Mixed", builtIn: true} + })); config.panels['graph'] = { id: "graph", @@ -72,7 +85,7 @@ describe('given dashboard with repeated panels', function() { }); it('exported dashboard should not contain repeated panels', function() { - expect(exported.rows[0].panels.length).to.be(1); + expect(exported.rows[0].panels.length).to.be(2); }); it('exported dashboard should not contain repeated rows', function() { @@ -109,6 +122,16 @@ describe('given dashboard with repeated panels', function() { expect(require.version).to.be("1.2.1"); }); + it('should not add built in datasources to required', function() { + var require = _.find(exported.__requires, {name: 'Mixed'}); + expect(require).to.be(undefined); + }); + + it('should add datasources used in mixed mode', function() { + var require = _.find(exported.__requires, {name: 'OtherDB'}); + expect(require).to.not.be(undefined); + }); + it('should add panel to required', function() { var require = _.find(exported.__requires, {name: 'Graph'}); expect(require.name).to.be("Graph");