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 {
|
||||
|
||||
/** @ngInject */
|
||||
constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
|
||||
constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout, datasourceSrv) {
|
||||
|
||||
$scope.init = function() {
|
||||
$scope.onAppEvent('save-dashboard', $scope.saveDashboard);
|
||||
@ -172,7 +172,7 @@ export class DashNavCtrl {
|
||||
|
||||
$scope.exportDashboard = function() {
|
||||
var clone = $scope.dashboard.getSaveModelClone();
|
||||
var exporter = new DashboardExporter();
|
||||
var exporter = new DashboardExporter(datasourceSrv);
|
||||
exporter.export(clone);
|
||||
};
|
||||
|
||||
|
@ -4,6 +4,8 @@ import config from 'app/core/config';
|
||||
import angular from 'angular';
|
||||
import _ from 'lodash';
|
||||
|
||||
import coreModule from 'app/core/core_module';
|
||||
|
||||
export class DynamicDashboardSrv {
|
||||
iteration: number;
|
||||
dashboard: any;
|
||||
@ -181,3 +183,4 @@ export class DynamicDashboardSrv {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,21 +8,50 @@ import {DynamicDashboardSrv} from './dynamic_dashboard_srv';
|
||||
|
||||
export class DashboardExporter {
|
||||
|
||||
makeExportable(dashboard) {
|
||||
var dynSrv = new DynamicDashboardSrv();
|
||||
dynSrv.process(dashboard, {cleanUpOnly: true});
|
||||
constructor(private datasourceSrv) {
|
||||
}
|
||||
|
||||
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) {
|
||||
var clean = this.makeExportable(dashboard);
|
||||
var blob = new Blob([angular.toJson(clean, true)], { type: "application/json;charset=utf-8" });
|
||||
var wnd: any = window;
|
||||
wnd.saveAs(blob, clean.title + '-' + new Date().getTime());
|
||||
return this.makeExportable(dashboard).then(clean => {
|
||||
var blob = new Blob([angular.toJson(clean, true)], { type: "application/json;charset=utf-8" });
|
||||
var wnd: any = window;
|
||||
wnd.saveAs(blob, clean.title + '-' + new Date().getTime());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -5,7 +5,7 @@ import {DashboardExporter} from '../exporter';
|
||||
describe('given dashboard with repeated panels', function() {
|
||||
var dash, exported;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach((done) => {
|
||||
dash = {
|
||||
rows: [],
|
||||
templating: { list: [] }
|
||||
@ -19,7 +19,7 @@ describe('given dashboard with repeated panels', function() {
|
||||
dash.rows.push({
|
||||
repeat: 'test',
|
||||
panels: [
|
||||
{id: 2, repeat: 'apps'},
|
||||
{id: 2, repeat: 'apps', datasource: 'gfdb'},
|
||||
{id: 2, repeat: null, repeatPanelId: 2},
|
||||
]
|
||||
});
|
||||
@ -28,8 +28,18 @@ describe('given dashboard with repeated panels', function() {
|
||||
repeatRowId: 1
|
||||
});
|
||||
|
||||
var exporter = new DashboardExporter();
|
||||
exported = exporter.makeExportable(dash);
|
||||
var datasourceSrvStub = {
|
||||
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);
|
||||
});
|
||||
|
||||
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