Bladeren bron

Merge pull request #4399 from utkarshcmu/preferences

Set home dashboard per user per org #3214
Torkel Ödegaard 9 jaren geleden
bovenliggende
commit
071237d322

+ 6 - 1
pkg/api/api.go

@@ -160,7 +160,12 @@ func Register(r *macaron.Macaron) {
 			r.Delete("/:id", wrap(DeleteApiKey))
 		}, reqOrgAdmin)
 
-		r.Combo("/preferences").Get(GetPreferences).Put(bind(m.SavePreferencesCommand{}), wrap(SavePreferences))
+		// Preferences
+		r.Group("/preferences", func() {
+			r.Get("/", GetPreferences)
+			r.Put("/", bind(m.SavePreferencesCommand{}), wrap(SavePreferences))
+			r.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
+		})
 
 		// Data sources
 		r.Group("/datasources", func() {

+ 22 - 0
pkg/api/dashboard.go

@@ -159,6 +159,28 @@ func canEditDashboard(role m.RoleType) bool {
 }
 
 func GetHomeDashboard(c *middleware.Context) {
+
+	// Checking if there is any preference set for home dashboard
+	query := m.GetPreferencesQuery{UserId: c.UserId, OrgId: c.OrgId}
+
+	if err := bus.Dispatch(&query); err != nil {
+		c.JsonApiErr(500, "Failed to get preferences", err)
+	}
+
+	if query.Result.HomeDashboardId != 0 {
+		query := m.GetDashboardSlugByIdQuery{Id: query.Result.HomeDashboardId}
+		err := bus.Dispatch(&query)
+		if err != nil {
+			c.JsonApiErr(500, "Failed to get slug from database", err)
+			return
+		}
+
+		slug := dtos.DashboardSlug{Slug: query.Result}
+
+		c.JSON(200, &slug)
+		return
+	}
+
 	filePath := path.Join(setting.StaticRootPath, "dashboards/home.json")
 	file, err := os.Open(filePath)
 	if err != nil {

+ 4 - 0
pkg/api/dtos/models.go

@@ -57,6 +57,10 @@ type DashboardFullWithMeta struct {
 	Dashboard *simplejson.Json `json:"dashboard"`
 }
 
+type DashboardSlug struct {
+	Slug string `json:"slug"`
+}
+
 type DataSource struct {
 	Id                int64            `json:"id"`
 	OrgId             int64            `json:"orgId"`

+ 16 - 2
pkg/api/preferences.go

@@ -7,7 +7,7 @@ import (
 	m "github.com/grafana/grafana/pkg/models"
 )
 
-// PUT /api/user/prefs
+// PUT /api/preferences
 func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
 
 	cmd.UserId = c.UserId
@@ -21,7 +21,7 @@ func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Respon
 
 }
 
-// GET /api/user/prefs
+// GET /api/preferences
 func GetPreferences(c *middleware.Context) {
 
 	query := m.GetPreferencesQuery{UserId: c.UserId, OrgId: c.OrgId}
@@ -38,3 +38,17 @@ func GetPreferences(c *middleware.Context) {
 
 	c.JSON(200, dto)
 }
+
+// POST /api/preferences/set-home-dash
+func SetHomeDashboard(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
+
+	cmd.UserId = c.UserId
+	cmd.OrgId = c.OrgId
+
+	if err := bus.Dispatch(&cmd); err != nil {
+		return ApiError(500, "Failed to set home dashboard", err)
+	}
+
+	return ApiSuccess("Home dashboard set")
+
+}

+ 5 - 0
pkg/models/dashboards.go

@@ -148,3 +148,8 @@ type GetDashboardsQuery struct {
 	DashboardIds []int64
 	Result       *[]Dashboard
 }
+
+type GetDashboardSlugByIdQuery struct {
+	Id     int64
+	Result string
+}

+ 3 - 3
pkg/models/preferences.go

@@ -39,7 +39,7 @@ type SavePreferencesCommand struct {
 	UserId int64
 	OrgId  int64
 
-	HomeDashboardId int64
-	Timezone        string
-	Theme           string
+	HomeDashboardId int64  `json:"homeDashboardId"`
+	Timezone        string `json:"timezone"`
+	Theme           string `json:"theme"`
 }

+ 15 - 0
pkg/services/sqlstore/dashboard.go

@@ -18,6 +18,7 @@ func init() {
 	bus.AddHandler("sql", DeleteDashboard)
 	bus.AddHandler("sql", SearchDashboards)
 	bus.AddHandler("sql", GetDashboardTags)
+	bus.AddHandler("sql", GetDashboardSlugById)
 }
 
 func SaveDashboard(cmd *m.SaveDashboardCommand) error {
@@ -255,3 +256,17 @@ func GetDashboards(query *m.GetDashboardsQuery) error {
 
 	return nil
 }
+
+func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error {
+	dashboard := m.Dashboard{Id: query.Id}
+	has, err := x.Get(&dashboard)
+	query.Result = dashboard.Slug
+
+	if err != nil {
+		return err
+	} else if has == false {
+		return m.ErrDashboardNotFound
+	}
+
+	return nil
+}

+ 5 - 0
pkg/services/sqlstore/preferences.go

@@ -3,6 +3,7 @@ package sqlstore
 import (
 	"github.com/grafana/grafana/pkg/bus"
 	m "github.com/grafana/grafana/pkg/models"
+	"time"
 )
 
 func init() {
@@ -41,6 +42,8 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
 				HomeDashboardId: cmd.HomeDashboardId,
 				Timezone:        cmd.Timezone,
 				Theme:           cmd.Theme,
+				Created:         time.Now(),
+				Updated:         time.Now(),
 			}
 			_, err = sess.Insert(&prefs)
 			return err
@@ -48,6 +51,8 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
 			prefs.HomeDashboardId = cmd.HomeDashboardId
 			prefs.Timezone = cmd.Timezone
 			prefs.Theme = cmd.Theme
+			prefs.Updated = time.Now()
+			prefs.Version += 1
 			_, err = sess.Id(prefs.Id).Update(&prefs)
 			return err
 		}

+ 11 - 3
public/app/core/routes/dashboard_loaders.js

@@ -8,9 +8,17 @@ function (coreModule) {
 
     if (!$routeParams.slug) {
       backendSrv.get('/api/dashboards/home').then(function(result) {
-        var meta = result.meta;
-        meta.canSave = meta.canShare = meta.canStar = false;
-        $scope.initDashboard(result, $scope);
+        if (result.slug == null) {
+          var meta = result.meta;
+          meta.canSave = meta.canShare = meta.canStar = false;
+          $scope.initDashboard(result, $scope);
+        } else {
+          $routeParams.type = 'db';
+          $routeParams.slug = result.slug;
+          dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
+            $scope.initDashboard(result, $scope);
+          });
+        }
       });
       return;
     }

+ 1 - 1
public/app/features/dashboard/dashnav/dashnav.ts

@@ -106,7 +106,7 @@ export class DashNavCtrl {
     $scope.saveDashboardAsHome = function() {
       // TODO: this backend method needs to be implemented
       backendSrv.post('/api/preferences/set-home-dash', {
-        dashboardId: $scope.dashboard.id
+        homeDashboardId: $scope.dashboard.id
       });
     };