From 430bcf192f72c0c90eba4340160f48e2dc1ab7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 19 May 2016 10:01:31 +0200 Subject: [PATCH] feat(export): templetize annotations and template vars, #5084 --- .../app/features/dashboard/export/exporter.ts | 58 +++++++++++++------ .../dashboard/specs/exporter_specs.ts | 26 ++++++++- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/public/app/features/dashboard/export/exporter.ts b/public/app/features/dashboard/export/exporter.ts index a5679e81732..25b20198a25 100644 --- a/public/app/features/dashboard/export/exporter.ts +++ b/public/app/features/dashboard/export/exporter.ts @@ -22,28 +22,33 @@ export class DashboardExporter { var datasources = {}; var promises = []; + var templateizeDatasourceUsage = obj => { + promises.push(this.datasourceSrv.get(obj.datasource).then(ds => { + var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase(); + datasources[obj.datasource] = { + name: refName, + label: ds.name, + description: '', + type: 'datasource', + pluginId: ds.meta.id, + pluginName: ds.meta.name, + }; + obj.datasource = '${' + refName +'}'; + + requires['datasource' + ds.meta.id] = { + type: 'datasource', + id: ds.meta.id, + name: ds.meta.name, + 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) { - promises.push(this.datasourceSrv.get(panel.datasource).then(ds => { - var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase(); - datasources[panel.datasource] = { - name: refName, - label: ds.name, - description: '', - type: 'datasource', - pluginId: ds.meta.id, - pluginName: ds.meta.name, - }; - panel.datasource = '${' + refName +'}'; - - requires['datasource' + ds.meta.id] = { - type: 'datasource', - id: ds.meta.id, - name: ds.meta.name, - version: ds.meta.info.version || "1.0.0", - }; - })); + templateizeDatasourceUsage(panel); } 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(() => { _.each(datasources, (value, key) => { inputs.push(value); diff --git a/public/app/features/dashboard/specs/exporter_specs.ts b/public/app/features/dashboard/specs/exporter_specs.ts index 408d8cc5bb3..26ad31eeadf 100644 --- a/public/app/features/dashboard/specs/exporter_specs.ts +++ b/public/app/features/dashboard/specs/exporter_specs.ts @@ -10,12 +10,21 @@ describe.only('given dashboard with repeated panels', function() { beforeEach(done => { dash = { rows: [], - templating: { list: [] } + templating: { list: [] }, + annotations: { list: [] }, }; + dash.templating.list.push({ name: 'apps', - current: {}, - options: [] + type: 'query', + datasource: 'gfdb', + current: {value: 'Asd', text: 'Asd'}, + options: [{value: 'Asd', text: 'Asd'}] + }); + + dash.annotations.list.push({ + name: 'logs', + datasource: 'gfdb', }); dash.rows.push({ @@ -63,6 +72,17 @@ describe.only('given dashboard with repeated panels', function() { 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() { expect(exported.__inputs[0].name).to.be("DS_GFDB"); expect(exported.__inputs[0].pluginId).to.be("testdb");