Browse Source

Rewriting anb abstracting how dashboards are loaded, unifying db, json files, and script dashboards, #960

Torkel Ödegaard 10 years ago
parent
commit
35cc0a1cc0

+ 6 - 1
pkg/api/dashboard.go

@@ -49,7 +49,12 @@ func GetDashboard(c *middleware.Context) {
 	dash := query.Result
 	dto := dtos.DashboardFullWithMeta{
 		Dashboard: dash.Data,
-		Meta:      dtos.DashboardMeta{IsStarred: isStarred, Slug: slug, Type: m.DashTypeDB},
+		Meta: dtos.DashboardMeta{
+			IsStarred: isStarred,
+			Slug:      slug,
+			Type:      m.DashTypeDB,
+			CanSave:   c.OrgRole != m.ROLE_VIEWER,
+		},
 	}
 
 	c.JSON(200, dto)

+ 1 - 0
public/app/features/dashboard/all.js

@@ -1,5 +1,6 @@
 define([
   './dashboardCtrl',
+  './dashboardLoaderSrv',
   './dashboardNavCtrl',
   './snapshotTopNavCtrl',
   './saveDashboardAsCtrl',

+ 82 - 0
public/app/features/dashboard/dashboardLoaderSrv.js

@@ -0,0 +1,82 @@
+define([
+  'angular',
+  'moment',
+  'lodash',
+  'jquery',
+  'kbn',
+],
+function (angular, moment, _, $, kbn) {
+  'use strict';
+
+  var module = angular.module('grafana.services');
+
+  module.service('dashboardLoaderSrv', function(backendSrv,
+                                                   dashboardSrv,
+                                                   datasourceSrv,
+                                                   $http, $q, $timeout,
+                                                   contextSrv, $routeParams,
+                                                   $rootScope) {
+    var self = this;
+
+    this._dashboardLoadFailed = function(title) {
+      return {meta: {}, dashboard: {title: title}};
+    };
+
+    this.loadDashboard = function(type, slug) {
+      if (type === 'script') {
+        return this._loadScriptedDashboard(slug);
+      }
+
+      if (type === 'snapshot') {
+        return backendSrv.get('/api/snapshots/' + $routeParams.slug).then(function(result) {
+          return result;
+        }, function() {
+          return {meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}};
+        });
+      }
+
+      return backendSrv.getDashboard($routeParams.type, $routeParams.slug).catch(function() {
+        return self._dashboardLoadFailed("Not found");
+      });
+    };
+
+    this._loadScriptedDashboard = function(file) {
+      var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
+
+      return $http({ url: url, method: "GET" })
+      .then(this._executeScript).then(function(result) {
+        return { meta: { fromScript: true, canDelete: false, canSave: false}, dashboard: result.data };
+      }, function(err) {
+        console.log('Script dashboard error '+ err);
+        $rootScope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
+        return self._dashboardLoadFailed('Scripted dashboard');
+      });
+    };
+
+    this._executeScript = function(result) {
+      var services = {
+        dashboardSrv: dashboardSrv,
+        datasourceSrv: datasourceSrv,
+        $q: $q,
+      };
+
+      /*jshint -W054 */
+      var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
+      var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
+
+      // Handle async dashboard scripts
+      if (_.isFunction(script_result)) {
+        var deferred = $q.defer();
+        script_result(function(dashboard) {
+          $timeout(function() {
+            deferred.resolve({ data: dashboard });
+          });
+        });
+        return deferred.promise;
+      }
+
+      return { data: script_result };
+    };
+
+  });
+});

+ 6 - 14
public/app/features/panel/soloPanelCtrl.js

@@ -15,6 +15,7 @@ function (angular, $) {
     timeSrv,
     $location,
     templateValuesSrv,
+    dashboardLoaderSrv,
     contextSrv) {
 
     var panelId;
@@ -25,24 +26,15 @@ function (angular, $) {
       var params = $location.search();
       panelId = parseInt(params.panelId);
 
-      var request;
+      dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
+        $scope.initDashboard(result, $scope);
 
-      if ($routeParams.slug) {
-        request = backendSrv.getDashboard($routeParams.slug);
-      } else {
-        request = backendSrv.get('/api/snapshots/' + $routeParams.key);
-      }
-
-      request.then(function(dashboard) {
-        $scope.initPanelScope(dashboard);
-      }).then(null, function(err) {
-        $scope.appEvent('alert-error', ['Load panel error', err.message]);
       });
-    };
 
-    $scope.initPanelScope = function(dashData) {
-      $scope.dashboard = dashboardSrv.create(dashData.dashboard, dashData.meta);
+      $scope.onAppEvent("dashboard-loaded", $scope.initPanelScope);
+    };
 
+    $scope.initPanelScope = function() {
       $scope.row = {
         height: ($(window).height() - 10) + 'px',
       };

+ 6 - 10
public/app/routes/all.js

@@ -15,30 +15,26 @@ define([
         controller : 'LoadDashboardCtrl',
         reloadOnSearch: false,
       })
-      .when('/dashboard/import/:file', {
-        templateUrl: 'app/partials/dashboard.html',
-        controller : 'DashFromImportCtrl',
-        reloadOnSearch: false,
-      })
       .when('/dashboard/:type/:slug', {
         templateUrl: 'app/partials/dashboard.html',
         controller : 'LoadDashboardCtrl',
         reloadOnSearch: false,
       })
-      .when('/dashboard/solo/db/:slug', {
+      .when('/dashboard-solo/:type/:slug', {
         templateUrl: 'app/features/panel/partials/soloPanel.html',
         controller : 'SoloPanelCtrl',
       })
-      .when('/dashboard/solo/snapshot/:key', {
-        templateUrl: 'app/features/panel/partials/soloPanel.html',
-        controller : 'SoloPanelCtrl',
+      .when('/dashboard-import/:file', {
+        templateUrl: 'app/partials/dashboard.html',
+        controller : 'DashFromImportCtrl',
+        reloadOnSearch: false,
       })
       .when('/dashboard/new', {
         templateUrl: 'app/partials/dashboard.html',
         controller : 'NewDashboardCtrl',
         reloadOnSearch: false,
       })
-      .when('/dashboard/import', {
+      .when('/import/dashboard', {
         templateUrl: 'app/features/dashboard/partials/import.html',
         controller : 'DashboardImportCtrl',
       })

+ 3 - 75
public/app/routes/dashLoadControllers.js

@@ -5,96 +5,24 @@ define([
   'moment',
   'jquery',
 ],
-function (angular, _, kbn, moment, $) {
+function (angular) {
   "use strict";
 
   var module = angular.module('grafana.routes');
 
-  module.controller('LoadDashboardCtrl', function(
-    $scope, $routeParams, backendSrv, dashboardSrv, datasourceSrv, $http, $q, $timeout, contextSrv) {
+  module.controller('LoadDashboardCtrl', function($scope, $routeParams, dashboardLoaderSrv, backendSrv) {
 
-    function dashboardLoadFailed(title) {
-      $scope.initDashboard({meta: {}, dashboard: {title: title}}, $scope);
-    }
-
-    // Home dashboard
     if (!$routeParams.slug) {
       backendSrv.get('/api/dashboards/home').then(function(result) {
         var meta = result.meta;
         meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
         $scope.initDashboard(result, $scope);
-      },function() {
-        dashboardLoadFailed('Not found');
-      });
-      return;
-    }
-
-    // Scripted dashboards
-    var execute_script = function(result) {
-      var services = {
-        dashboardSrv: dashboardSrv,
-        datasourceSrv: datasourceSrv,
-        $q: $q,
-      };
-
-      /*jshint -W054 */
-      var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
-      var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
-
-      // Handle async dashboard scripts
-      if (_.isFunction(script_result)) {
-        var deferred = $q.defer();
-        script_result(function(dashboard) {
-          $timeout(function() {
-            deferred.resolve({ data: dashboard });
-          });
-        });
-        return deferred.promise;
-      }
-
-      return { data: script_result };
-    };
-
-    var script_load = function(file) {
-      var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
-
-      return $http({ url: url, method: "GET" })
-      .then(execute_script)
-      .then(null,function(err) {
-        console.log('Script dashboard error '+ err);
-        $scope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
-        return false;
-      });
-    };
-
-    function loadScriptedDashboard() {
-      script_load($routeParams.slug).then(function(result) {
-        $scope.initDashboard({
-          meta: {fromScript: true, canDelete: false, canSave: false},
-          dashboard: result.data
-        }, $scope);
-      });
-    }
-
-    if ($routeParams.type === 'script') {
-      loadScriptedDashboard();
-      return;
-    }
-
-    if ($routeParams.type === 'snapshot') {
-      contextSrv.sidemenu = false;
-      backendSrv.get('/api/snapshots/' + $routeParams.slug).then(function(result) {
-        $scope.initDashboard(result, $scope);
-      }, function() {
-        $scope.initDashboard({meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}}, $scope);
       });
       return;
     }
 
-    return backendSrv.getDashboard($routeParams.type, $routeParams.slug).then(function(result) {
+    dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
       $scope.initDashboard(result, $scope);
-    }, function() {
-      dashboardLoadFailed('Not found');
     });
 
   });