mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(dash export): dashboard export can now replace datasource names with variable and add inputs section
This commit is contained in:
parent
4d0b14fbb4
commit
0f71838fdf
@ -9,7 +9,7 @@ import {DashboardExporter} from '../exporter';
|
|||||||
export class DashNavCtrl {
|
export class DashNavCtrl {
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
|
constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout, datasourceSrv) {
|
||||||
|
|
||||||
$scope.init = function() {
|
$scope.init = function() {
|
||||||
$scope.onAppEvent('save-dashboard', $scope.saveDashboard);
|
$scope.onAppEvent('save-dashboard', $scope.saveDashboard);
|
||||||
@ -172,7 +172,7 @@ export class DashNavCtrl {
|
|||||||
|
|
||||||
$scope.exportDashboard = function() {
|
$scope.exportDashboard = function() {
|
||||||
var clone = $scope.dashboard.getSaveModelClone();
|
var clone = $scope.dashboard.getSaveModelClone();
|
||||||
var exporter = new DashboardExporter();
|
var exporter = new DashboardExporter(datasourceSrv);
|
||||||
exporter.export(clone);
|
exporter.export(clone);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import config from 'app/core/config';
|
|||||||
import angular from 'angular';
|
import angular from 'angular';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
import coreModule from 'app/core/core_module';
|
||||||
|
|
||||||
export class DynamicDashboardSrv {
|
export class DynamicDashboardSrv {
|
||||||
iteration: number;
|
iteration: number;
|
||||||
dashboard: any;
|
dashboard: any;
|
||||||
@ -181,3 +183,4 @@ export class DynamicDashboardSrv {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,21 +8,50 @@ import {DynamicDashboardSrv} from './dynamic_dashboard_srv';
|
|||||||
|
|
||||||
export class DashboardExporter {
|
export class DashboardExporter {
|
||||||
|
|
||||||
makeExportable(dashboard) {
|
constructor(private datasourceSrv) {
|
||||||
var dynSrv = new DynamicDashboardSrv();
|
}
|
||||||
dynSrv.process(dashboard, {cleanUpOnly: true});
|
|
||||||
|
|
||||||
return dashboard;
|
makeExportable(dash) {
|
||||||
|
var dynSrv = new DynamicDashboardSrv();
|
||||||
|
dynSrv.process(dash, {cleanUpOnly: true});
|
||||||
|
|
||||||
|
var inputs = [];
|
||||||
|
var datasources = {};
|
||||||
|
var promises = [];
|
||||||
|
|
||||||
|
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.toUpperCase();
|
||||||
|
datasources[panel.datasource] = {
|
||||||
|
name: refName,
|
||||||
|
type: 'datasource',
|
||||||
|
pluginId: ds.meta.id,
|
||||||
|
};
|
||||||
|
panel.datasource = '${' + refName +'}';
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all(promises).then(() => {
|
||||||
|
_.each(datasources, (value, key) => {
|
||||||
|
inputs.push(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
dash["__inputs"] = inputs;
|
||||||
|
return dash;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export(dashboard) {
|
export(dashboard) {
|
||||||
var clean = this.makeExportable(dashboard);
|
return this.makeExportable(dashboard).then(clean => {
|
||||||
var blob = new Blob([angular.toJson(clean, true)], { type: "application/json;charset=utf-8" });
|
var blob = new Blob([angular.toJson(clean, true)], { type: "application/json;charset=utf-8" });
|
||||||
var wnd: any = window;
|
var wnd: any = window;
|
||||||
wnd.saveAs(blob, clean.title + '-' + new Date().getTime());
|
wnd.saveAs(blob, clean.title + '-' + new Date().getTime());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import {DashboardExporter} from '../exporter';
|
|||||||
describe('given dashboard with repeated panels', function() {
|
describe('given dashboard with repeated panels', function() {
|
||||||
var dash, exported;
|
var dash, exported;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach((done) => {
|
||||||
dash = {
|
dash = {
|
||||||
rows: [],
|
rows: [],
|
||||||
templating: { list: [] }
|
templating: { list: [] }
|
||||||
@ -19,7 +19,7 @@ describe('given dashboard with repeated panels', function() {
|
|||||||
dash.rows.push({
|
dash.rows.push({
|
||||||
repeat: 'test',
|
repeat: 'test',
|
||||||
panels: [
|
panels: [
|
||||||
{id: 2, repeat: 'apps'},
|
{id: 2, repeat: 'apps', datasource: 'gfdb'},
|
||||||
{id: 2, repeat: null, repeatPanelId: 2},
|
{id: 2, repeat: null, repeatPanelId: 2},
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
@ -28,8 +28,18 @@ describe('given dashboard with repeated panels', function() {
|
|||||||
repeatRowId: 1
|
repeatRowId: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
var exporter = new DashboardExporter();
|
var datasourceSrvStub = {
|
||||||
exported = exporter.makeExportable(dash);
|
get: sinon.stub().returns(Promise.resolve({
|
||||||
|
name: 'gfdb',
|
||||||
|
meta: {id: "testdb"}
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
|
||||||
|
var exporter = new DashboardExporter(datasourceSrvStub);
|
||||||
|
exporter.makeExportable(dash).then(clean => {
|
||||||
|
exported = clean;
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -41,5 +51,10 @@ describe('given dashboard with repeated panels', function() {
|
|||||||
expect(exported.rows.length).to.be(1);
|
expect(exported.rows.length).to.be(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should replace datasource refs', function() {
|
||||||
|
var panel = exported.rows[0].panels[0];
|
||||||
|
expect(panel.datasource).to.be("${DS_GFDB}");
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user