Browse Source

feat(plugins): import all dashboards feature

Torkel Ödegaard 10 years ago
parent
commit
61017fc204

+ 10 - 2
public/app/core/utils/emitter.ts

@@ -21,10 +21,18 @@ export class Emitter {
     this.subjects[fnName].next(data);
   }
 
-  on(name, handler) {
+  on(name, handler, $scope) {
     var fnName = createName(name);
     this.subjects[fnName] || (this.subjects[fnName] = new Subject());
-    this.subjects[fnName].subscribe(handler);
+    var subscription = this.subjects[fnName].subscribe(handler);
+
+    if ($scope) {
+      $scope.$on('$destroy', function() {
+        subscription.unsubscribe();
+      });
+    }
+
+    return subscription;
   };
 
   off(name, handler) {

+ 27 - 2
public/app/features/plugins/import_list/import_list.ts

@@ -3,18 +3,43 @@
 import angular from 'angular';
 import _ from 'lodash';
 import coreModule from 'app/core/core_module';
+import appEvents from 'app/core/app_events';
 
 export class DashImportListCtrl {
   dashboards: any[];
   plugin: any;
   datasource: any;
 
-  constructor(private $http, private backendSrv, private $rootScope) {
+  constructor($scope, private $http, private backendSrv, private $rootScope) {
     this.dashboards = [];
 
     backendSrv.get(`/api/plugins/${this.plugin.id}/dashboards`).then(dashboards => {
       this.dashboards = dashboards;
     });
+
+    appEvents.on('dashboard-list-import-all', this.importAll.bind(this), $scope);
+  }
+
+  importAll(payload) {
+    return this.importNext(0).then(() => {
+      payload.resolve("All dashboards imported");
+    }).catch(err => {
+      payload.reject(err);
+    });
+  }
+
+  importNext(index) {
+    return this.import(this.dashboards[index], true).then(() => {
+      if (index+1 < this.dashboards.length) {
+        return new Promise(resolve => {
+          setTimeout(() => {
+            this.importNext(index+1).then(() => {
+              resolve();
+            });
+          }, 500);
+        });
+      }
+    });
   }
 
   import(dash, reinstall) {
@@ -34,7 +59,7 @@ export class DashImportListCtrl {
       });
     }
 
-    this.backendSrv.post(`/api/dashboards/import`, installCmd).then(res => {
+    return this.backendSrv.post(`/api/dashboards/import`, installCmd).then(res => {
       this.$rootScope.appEvent('alert-success', ['Dashboard Installed', dash.title]);
       _.extend(dash, res);
     });

+ 41 - 29
public/app/features/plugins/plugin_edit_ctrl.ts

@@ -2,6 +2,7 @@
 
 import angular from 'angular';
 import _ from 'lodash';
+import appEvents from 'app/core/app_events';
 
 export class PluginEditCtrl {
   model: any;
@@ -17,11 +18,18 @@ export class PluginEditCtrl {
   postUpdateHook: () => any;
 
   /** @ngInject */
-  constructor(private backendSrv, private $routeParams, private $sce, private $http) {
+  constructor(private $scope,
+              private backendSrv,
+              private $routeParams,
+              private $sce,
+              private $http) {
     this.model = {};
     this.pluginId = $routeParams.pluginId;
     this.tabIndex = 0;
     this.tabs = ['Overview'];
+
+    this.preUpdateHook = () => Promise.resolve();
+    this.postUpdateHook = () => Promise.resolve();
    }
 
   init() {
@@ -71,40 +79,44 @@ export class PluginEditCtrl {
   }
 
   update() {
-    var chain = Promise.resolve();
-    var self = this;
-    // if set, handle the preUpdateHook. If this returns a promise,
-    // the next step of execution will block until the promise resolves.
-    // if the promise is rejected, this update will be aborted.
-    if (this.preUpdateHook != null) {
-      chain = self.preUpdateHook();
-    }
-
-    // Perform the core update procedure
-    chain = chain.then(function() {
+    this.preUpdateHook().then(() => {
       var updateCmd = _.extend({
-        enabled: self.model.enabled,
-        pinned: self.model.pinned,
-        jsonData: self.model.jsonData,
-        secureJsonData: self.model.secureJsonData,
+        enabled: this.model.enabled,
+        pinned: this.model.pinned,
+        jsonData: this.model.jsonData,
+        secureJsonData: this.model.secureJsonData,
       }, {});
 
-      return self.backendSrv.post(`/api/plugins/${self.pluginId}/settings`, updateCmd);
+      return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
+    })
+    .then(this.postUpdateHook)
+    .then((res) => {
+      window.location.href = window.location.href;
     });
+  }
 
-    // if set, performt he postUpdate hook. If a promise is returned it will block
-    // the final step of the update procedure (reloading the page) until the promise
-    // resolves.  If the promise is rejected the page will not be reloaded.
-    if (this.postUpdateHook != null) {
-      chain = chain.then(function() {
-        return this.postUpdateHook();
-      });
-    }
+  importDashboards() {
+    // move to dashboards tab
+    this.tabIndex = 2;
 
-    // all stesp in the update procedure are complete, so reload the page to make changes
-    // take effect.
-    chain.then(function() {
-      window.location.href = window.location.href;
+    return new Promise((resolve) => {
+      if (!this.$scope.$$phase) {
+        this.$scope.$digest();
+      }
+
+      // let angular load dashboards tab
+      setTimeout(() => {
+        resolve();
+      }, 1000);
+
+    }).then(() => {
+      return new Promise((resolve, reject) => {
+        // send event to import list component
+        appEvents.emit('dashboard-list-import-all', {
+          resolve: resolve,
+          reject: reject
+        });
+      });
     });
   }