Browse Source

refactor(apps): more WIP work on apps

Torkel Ödegaard 10 năm trước cách đây
mục cha
commit
ad94f99d57

+ 10 - 10
pkg/api/app_plugin.go

@@ -18,11 +18,11 @@ func GetAppPlugins(c *middleware.Context) Response {
 	installedAppsMap := make(map[string]*dtos.AppPlugin)
 	for t, a := range plugins.Apps {
 		installedAppsMap[t] = &dtos.AppPlugin{
-			Type:        a.Type,
-			Enabled:     a.Enabled,
-			PinNavLinks: a.PinNavLinks,
-			Module:      a.Module,
-			JsonData:    make(map[string]interface{}),
+			Type:     a.Type,
+			Enabled:  a.Enabled,
+			Pinned:   a.Pinned,
+			Module:   a.Module,
+			JsonData: make(map[string]interface{}),
 		}
 	}
 
@@ -32,11 +32,11 @@ func GetAppPlugins(c *middleware.Context) Response {
 	for _, b := range query.Result {
 		if def, ok := installedAppsMap[b.Type]; ok {
 			result = append(result, &dtos.AppPlugin{
-				Type:        b.Type,
-				Enabled:     b.Enabled,
-				PinNavLinks: b.PinNavLinks,
-				Module:      def.Module,
-				JsonData:    b.JsonData,
+				Type:     b.Type,
+				Enabled:  b.Enabled,
+				Pinned:   b.Pinned,
+				Module:   def.Module,
+				JsonData: b.JsonData,
 			})
 			seenApps[b.Type] = true
 		}

+ 1 - 1
pkg/api/datasources.go

@@ -125,7 +125,7 @@ func GetDataSourcePlugins(c *middleware.Context) {
 	}
 	enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
 
-	for key, value := range enabledPlugins.DataSourcePlugins {
+	for key, value := range enabledPlugins.DataSources {
 		if !value.BuiltIn {
 			dsList[key] = value
 		}

+ 5 - 5
pkg/api/dtos/app_plugin.go

@@ -1,9 +1,9 @@
 package dtos
 
 type AppPlugin struct {
-	Type        string                 `json:"type"`
-	Enabled     bool                   `json:"enabled"`
-	PinNavLinks bool                   `json:"pin_nav_links"`
-	Module      string                 `json:"module"`
-	JsonData    map[string]interface{} `json:"jsonData"`
+	Type     string                 `json:"type"`
+	Enabled  bool                   `json:"enabled"`
+	Pinned   bool                   `json:"pinned"`
+	Module   string                 `json:"module"`
+	JsonData map[string]interface{} `json:"jsonData"`
 }

+ 3 - 2
pkg/api/frontendsettings.go

@@ -34,6 +34,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
 	if err != nil {
 		return nil, err
 	}
+
 	enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
 
 	for _, ds := range orgDataSources {
@@ -49,7 +50,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
 			"url":  url,
 		}
 
-		meta, exists := enabledPlugins.DataSourcePlugins[ds.Type]
+		meta, exists := enabledPlugins.DataSources[ds.Type]
 		if !exists {
 			log.Error(3, "Could not find plugin definition for data source: %v", ds.Type)
 			continue
@@ -117,7 +118,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
 	}
 
 	panels := map[string]interface{}{}
-	for _, panel := range enabledPlugins.PanelPlugins {
+	for _, panel := range enabledPlugins.Panels {
 		panels[panel.Type] = map[string]interface{}{
 			"module": panel.Module,
 			"name":   panel.Name,

+ 2 - 2
pkg/api/index.go

@@ -52,7 +52,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
 	data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
 		Text: "Dashboards",
 		Icon: "fa fa-fw fa-th-large",
-		Href: "/",
+		Url:  "/",
 	})
 
 	orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
@@ -73,7 +73,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
 		}
 
 		if plugin.Pinned && plugin.Page != nil {
-			if c.userHasRole(plugin.Page.reqRole) {
+			if c.HasUserRole(plugin.Page.ReqRole) {
 				data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
 					Text: plugin.Page.Text,
 					Url:  plugin.Page.Url,

+ 3 - 3
pkg/cmd/web.go

@@ -30,9 +30,9 @@ func newMacaron() *macaron.Macaron {
 	}
 
 	for _, route := range plugins.StaticRoutes {
-		pluginRoute := path.Join("/public/plugins/", route.Url)
-		log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Path)
-		mapStatic(m, route.Path, "", pluginRoute)
+		pluginRoute := path.Join("/public/plugins/", route.UrlFragment)
+		log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Dir)
+		mapStatic(m, route.Dir, "", pluginRoute)
 	}
 
 	mapStatic(m, setting.StaticRootPath, "", "public")

+ 1 - 1
pkg/middleware/middleware.go

@@ -254,6 +254,6 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) {
 	ctx.JSON(status, resp)
 }
 
-func (ctx *Context) hasUserRole(role m.RoleType) bool {
+func (ctx *Context) HasUserRole(role m.RoleType) bool {
 	return ctx.OrgRole.Includes(role)
 }

+ 1 - 1
pkg/plugins/models.go

@@ -44,7 +44,7 @@ type ApiPluginRoute struct {
 type AppPluginPage struct {
 	Text    string          `json:"text"`
 	Icon    string          `json:"icon"`
-	Href    string          `json:"url"`
+	Url     string          `json:"url"`
 	ReqRole models.RoleType `json:"reqRole"`
 }
 

+ 4 - 3
pkg/services/sqlstore/migrations/app_plugin.go

@@ -4,7 +4,7 @@ import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
 
 func addAppPluginMigration(mg *Migrator) {
 
-	var appPluginV1 = Table{
+	var appPluginV2 = Table{
 		Name: "app_plugin",
 		Columns: []*Column{
 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
@@ -20,8 +20,9 @@ func addAppPluginMigration(mg *Migrator) {
 			{Cols: []string{"org_id", "type"}, Type: UniqueIndex},
 		},
 	}
-	mg.AddMigration("create app_plugin table v1", NewAddTableMigration(appPluginV1))
+
+	mg.AddMigration("create app_plugin table v2", NewAddTableMigration(appPluginV2))
 
 	//-------  indexes ------------------
-	addTableIndicesMigrations(mg, "v1", appPluginV1)
+	addTableIndicesMigrations(mg, "v2", appPluginV2)
 }

+ 13 - 11
public/app/features/org/appEditCtrl.js → public/app/features/org/app_edit_ctrl.ts

@@ -1,14 +1,13 @@
-define([
-  'angular',
-  'lodash',
-  'app/core/config',
-],
-function (angular, _, config) {
-  'use strict';
+///<reference path="../../headers/common.d.ts" />
 
-  var module = angular.module('grafana.controllers');
+import config = require('app/core/config');
+import angular from 'angular';
+
+export class AppEditCtrl {
+
+  /** @ngInject */
+  constructor(private $scope: any, private appSrv: any, private $routeParams: any) {
 
-  module.controller('AppEditCtrl', function($scope, appSrv, $routeParams) {
     $scope.init = function() {
       $scope.current = {};
       $scope.getApps();
@@ -31,5 +30,8 @@ function (angular, _, config) {
     };
 
     $scope.init();
-  });
-});
+  }
+
+}
+
+angular.module('grafana.controllers').controller('AppEditCtrl', AppEditCtrl);

+ 0 - 58
public/app/features/org/app_srv.js

@@ -1,58 +0,0 @@
-define([
-  'angular',
-  'lodash',
-],
-function (angular, _) {
-  'use strict';
-
-  var module = angular.module('grafana.services');
-
-  module.service('appSrv', function($rootScope, $timeout, $q, backendSrv) {
-    var self = this;
-    this.init = function() {
-      console.log("appSrv init");
-      this.apps = {};
-    };
-
-    this.get = function(type) {
-      return $q(function(resolve) {
-        if (type in self.apps) {
-          return resolve(self.apps[type]);
-        }
-        backendSrv.get('api/org/apps').then(function(results) {
-          _.forEach(results, function(p) {
-            self.apps[p.type] = p;
-          });
-          return resolve(self.apps[type]);
-        });
-      });
-    };
-
-    this.getAll = function() {
-      return $q(function(resolve) {
-        if (!_.isEmpty(self.apps)) {
-          return resolve(self.apps);
-        }
-        backendSrv.get('api/org/apps').then(function(results) {
-          _.forEach(results, function(p) {
-            self.apps[p.type] = p;
-          });
-          return resolve(self.apps);
-        });
-      });
-    };
-
-    this.update = function(app) {
-      return $q(function(resolve, reject) {
-        backendSrv.post('api/org/apps', app).then(function(resp) {
-          self.apps[app.type] = app;
-          resolve(resp);
-        }, function(resp) {
-          reject(resp);
-        });
-      });
-    };
-
-    this.init();
-  });
-});

+ 47 - 0
public/app/features/org/app_srv.ts

@@ -0,0 +1,47 @@
+///<reference path="../../headers/common.d.ts" />
+
+import config = require('app/core/config');
+import angular from 'angular';
+
+export class AppSrv {
+  apps: any = {};
+
+  /** @ngInject */
+  constructor(
+    private $rootScope,
+    private $timeout,
+    private $q,
+    private backendSrv) {
+  }
+
+  get(type) {
+    if (this.apps[type]) {
+      return this.$q.when(this.apps[type]);
+    }
+    return this.getAll().then(() => {
+      return this.apps[type];
+    });
+  }
+
+
+  getAll() {
+    if (!_.isEmpty(this.apps)) {
+      return this.$q.when(this.apps);
+    }
+
+    return this.backendSrv.get('api/org/apps').then(results => {
+      this.apps = results.reduce((prev, current) => {
+        prev[current.type] = current;
+      }, {});
+      return this.apps;
+    });
+  }
+
+  update(app) {
+    return this.backendSrv.post('api/org/apps', app).then(resp => {
+      this.apps[app.type] = app;
+    });
+  }
+}
+
+angular.module('grafana.services').service('appSrv', AppSrv);

+ 0 - 0
public/app/features/org/partials/appEdit.html → public/app/features/org/partials/app_edit.html


+ 0 - 0
public/app/features/org/partials/apps.html → public/app/features/org/partials/app_list.html


+ 1 - 1
public/app/partials/sidemenu.html

@@ -19,7 +19,7 @@
 		</li>
 
 		<li ng-repeat="item in mainLinks">
-			<a href="{{item.href}}" class="sidemenu-item" target="{{item.target}}">
+			<a href="{{item.url}}" class="sidemenu-item" target="{{item.target}}">
 				<span class="icon-circle sidemenu-icon"><i class="{{item.icon}}"></i></span>
 				<span class="sidemenu-item-text">{{item.text}}</span>
 	   	</a>

+ 1 - 1
public/views/index.html

@@ -52,7 +52,7 @@
 		window.grafanaBootData = {
 			user:[[.User]],
 			settings: [[.Settings]],
-			pluginModules: [[.PluginJs]],
+			pluginModules: [[.PluginModules]],
 			mainNavLinks: [[.MainNavLinks]]
 		};
 	</script>