瀏覽代碼

feat(exporter): stared work on dashboard exporter that cleans up repeated panels etc

Torkel Ödegaard 9 年之前
父節點
當前提交
4d0b14fbb4

+ 0 - 4
public/app/features/dashboard/dashboard_ctrl.ts

@@ -136,10 +136,6 @@ export class DashboardCtrl {
       $scope.timezoneChanged = function() {
         $rootScope.$broadcast("refresh");
       };
-
-      $scope.formatDate = function(date) {
-        return moment(date).format('MMM Do YYYY, h:mm:ss a');
-      };
     }
 
     init(dashboard) {

+ 4 - 3
public/app/features/dashboard/dashnav/dashnav.ts

@@ -4,6 +4,8 @@ import _ from 'lodash';
 import moment from 'moment';
 import angular from 'angular';
 
+import {DashboardExporter} from '../exporter';
+
 export class DashNavCtrl {
 
   /** @ngInject */
@@ -170,9 +172,8 @@ export class DashNavCtrl {
 
     $scope.exportDashboard = function() {
       var clone = $scope.dashboard.getSaveModelClone();
-      var blob = new Blob([angular.toJson(clone, true)], { type: "application/json;charset=utf-8" });
-      var wnd: any = window;
-      wnd.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
+      var exporter = new DashboardExporter();
+      exporter.export(clone);
     };
 
     $scope.snapshot = function() {

+ 15 - 7
public/app/features/dashboard/dynamic_dashboard_srv.ts

@@ -8,30 +8,36 @@ export class DynamicDashboardSrv {
   iteration: number;
   dashboard: any;
 
+  constructor() {
+    this.iteration = new Date().getTime();
+  }
+
   init(dashboard) {
     if (dashboard.snapshot) { return; }
-
-    this.iteration = new Date().getTime();
-    this.process(dashboard);
+    this.process(dashboard, {});
   }
 
   update(dashboard) {
     if (dashboard.snapshot) { return; }
 
     this.iteration = this.iteration + 1;
-    this.process(dashboard);
+    this.process(dashboard, {});
   }
 
-  process(dashboard) {
+  process(dashboard, options) {
     if (dashboard.templating.list.length === 0) { return; }
     this.dashboard = dashboard;
 
+    var cleanUpOnly = options.cleanUpOnly;
+
     var i, j, row, panel;
     for (i = 0; i < this.dashboard.rows.length; i++) {
       row = this.dashboard.rows[i];
       // handle row repeats
       if (row.repeat) {
-        this.repeatRow(row, i);
+        if (!cleanUpOnly) {
+          this.repeatRow(row, i);
+        }
       } else if (row.repeatRowId && row.repeatIteration !== this.iteration) {
         // clean up old left overs
         this.dashboard.rows.splice(i, 1);
@@ -43,7 +49,9 @@ export class DynamicDashboardSrv {
       for (j = 0; j < row.panels.length; j++) {
         panel = row.panels[j];
         if (panel.repeat) {
-          this.repeatPanel(panel, row);
+          if (!cleanUpOnly) {
+            this.repeatPanel(panel, row);
+          }
         } else if (panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
           // clean up old left overs
           row.panels = _.without(row.panels, panel);

+ 28 - 0
public/app/features/dashboard/exporter.ts

@@ -0,0 +1,28 @@
+///<reference path="../../headers/common.d.ts" />
+
+import config from 'app/core/config';
+import angular from 'angular';
+import _ from 'lodash';
+
+import {DynamicDashboardSrv} from './dynamic_dashboard_srv';
+
+export class DashboardExporter {
+
+  makeExportable(dashboard) {
+    var dynSrv = new DynamicDashboardSrv();
+    dynSrv.process(dashboard, {cleanUpOnly: true});
+
+    return dashboard;
+  }
+
+  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());
+  }
+
+}
+
+
+

+ 2 - 2
public/app/features/dashboard/partials/settings.html

@@ -104,7 +104,7 @@
 		<div class="gf-form-group">
 			<div class="gf-form">
 				<span class="gf-form-label width-10">Last updated at:</span>
-				<span class="gf-form-label width-18">{{formatDate(dashboardMeta.updated)}}</span>
+				<span class="gf-form-label width-18">{{dashboard.formatDate(dashboardMeta.updated)}}</span>
 			</div>
 			<div class="gf-form">
 				<span class="gf-form-label width-10">Last updated by:</span>
@@ -112,7 +112,7 @@
 			</div>
 			<div class="gf-form">
 				<span class="gf-form-label width-10">Created at:</span>
-				<span class="gf-form-label width-18">{{formatDate(dashboardMeta.created)}}&nbsp;</span>
+				<span class="gf-form-label width-18">{{dashboard.formatDate(dashboardMeta.created)}}&nbsp;</span>
 			</div>
 			<div class="gf-form">
 				<span class="gf-form-label width-10">Created by:</span>

+ 1 - 1
public/test/specs/dynamic_dashboard_srv_specs.ts → public/app/features/dashboard/specs/dynamic_dashboard_srv_specs.ts

@@ -1,7 +1,7 @@
 import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
 
 import 'app/features/dashboard/dashboardSrv';
-import {DynamicDashboardSrv} from '../../app/features/dashboard/dynamic_dashboard_srv';
+import {DynamicDashboardSrv} from '../dynamic_dashboard_srv';
 
 function dynamicDashScenario(desc, func)  {
 

+ 45 - 0
public/app/features/dashboard/specs/exporter_specs.ts

@@ -0,0 +1,45 @@
+import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
+
+import {DashboardExporter} from '../exporter';
+
+describe('given dashboard with repeated panels', function() {
+  var dash, exported;
+
+  beforeEach(() => {
+    dash = {
+      rows: [],
+      templating: { list: [] }
+    };
+    dash.templating.list.push({
+      name: 'apps',
+      current: {},
+      options: []
+    });
+
+    dash.rows.push({
+      repeat: 'test',
+      panels: [
+        {id: 2, repeat: 'apps'},
+        {id: 2, repeat: null, repeatPanelId: 2},
+      ]
+    });
+    dash.rows.push({
+      repeat: null,
+      repeatRowId: 1
+    });
+
+    var exporter = new DashboardExporter();
+    exported = exporter.makeExportable(dash);
+  });
+
+
+  it('exported dashboard should not contain repeated panels', function() {
+    expect(exported.rows[0].panels.length).to.be(1);
+  });
+
+  it('exported dashboard should not contain repeated rows', function() {
+    expect(exported.rows.length).to.be(1);
+  });
+
+});
+