mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #4399 from utkarshcmu/preferences
Set home dashboard per user per org #3214
This commit is contained in:
commit
071237d322
@ -160,7 +160,12 @@ func Register(r *macaron.Macaron) {
|
|||||||
r.Delete("/:id", wrap(DeleteApiKey))
|
r.Delete("/:id", wrap(DeleteApiKey))
|
||||||
}, reqOrgAdmin)
|
}, 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
|
// Data sources
|
||||||
r.Group("/datasources", func() {
|
r.Group("/datasources", func() {
|
||||||
|
@ -159,6 +159,28 @@ func canEditDashboard(role m.RoleType) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetHomeDashboard(c *middleware.Context) {
|
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")
|
filePath := path.Join(setting.StaticRootPath, "dashboards/home.json")
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,6 +57,10 @@ type DashboardFullWithMeta struct {
|
|||||||
Dashboard *simplejson.Json `json:"dashboard"`
|
Dashboard *simplejson.Json `json:"dashboard"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DashboardSlug struct {
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
}
|
||||||
|
|
||||||
type DataSource struct {
|
type DataSource struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
OrgId int64 `json:"orgId"`
|
OrgId int64 `json:"orgId"`
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PUT /api/user/prefs
|
// PUT /api/preferences
|
||||||
func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
|
func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
|
||||||
|
|
||||||
cmd.UserId = c.UserId
|
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) {
|
func GetPreferences(c *middleware.Context) {
|
||||||
|
|
||||||
query := m.GetPreferencesQuery{UserId: c.UserId, OrgId: c.OrgId}
|
query := m.GetPreferencesQuery{UserId: c.UserId, OrgId: c.OrgId}
|
||||||
@ -38,3 +38,17 @@ func GetPreferences(c *middleware.Context) {
|
|||||||
|
|
||||||
c.JSON(200, dto)
|
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")
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -148,3 +148,8 @@ type GetDashboardsQuery struct {
|
|||||||
DashboardIds []int64
|
DashboardIds []int64
|
||||||
Result *[]Dashboard
|
Result *[]Dashboard
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetDashboardSlugByIdQuery struct {
|
||||||
|
Id int64
|
||||||
|
Result string
|
||||||
|
}
|
||||||
|
@ -39,7 +39,7 @@ type SavePreferencesCommand struct {
|
|||||||
UserId int64
|
UserId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
|
|
||||||
HomeDashboardId int64
|
HomeDashboardId int64 `json:"homeDashboardId"`
|
||||||
Timezone string
|
Timezone string `json:"timezone"`
|
||||||
Theme string
|
Theme string `json:"theme"`
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ func init() {
|
|||||||
bus.AddHandler("sql", DeleteDashboard)
|
bus.AddHandler("sql", DeleteDashboard)
|
||||||
bus.AddHandler("sql", SearchDashboards)
|
bus.AddHandler("sql", SearchDashboards)
|
||||||
bus.AddHandler("sql", GetDashboardTags)
|
bus.AddHandler("sql", GetDashboardTags)
|
||||||
|
bus.AddHandler("sql", GetDashboardSlugById)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
||||||
@ -255,3 +256,17 @@ func GetDashboards(query *m.GetDashboardsQuery) error {
|
|||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -41,6 +42,8 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
|
|||||||
HomeDashboardId: cmd.HomeDashboardId,
|
HomeDashboardId: cmd.HomeDashboardId,
|
||||||
Timezone: cmd.Timezone,
|
Timezone: cmd.Timezone,
|
||||||
Theme: cmd.Theme,
|
Theme: cmd.Theme,
|
||||||
|
Created: time.Now(),
|
||||||
|
Updated: time.Now(),
|
||||||
}
|
}
|
||||||
_, err = sess.Insert(&prefs)
|
_, err = sess.Insert(&prefs)
|
||||||
return err
|
return err
|
||||||
@ -48,6 +51,8 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
|
|||||||
prefs.HomeDashboardId = cmd.HomeDashboardId
|
prefs.HomeDashboardId = cmd.HomeDashboardId
|
||||||
prefs.Timezone = cmd.Timezone
|
prefs.Timezone = cmd.Timezone
|
||||||
prefs.Theme = cmd.Theme
|
prefs.Theme = cmd.Theme
|
||||||
|
prefs.Updated = time.Now()
|
||||||
|
prefs.Version += 1
|
||||||
_, err = sess.Id(prefs.Id).Update(&prefs)
|
_, err = sess.Id(prefs.Id).Update(&prefs)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,17 @@ function (coreModule) {
|
|||||||
|
|
||||||
if (!$routeParams.slug) {
|
if (!$routeParams.slug) {
|
||||||
backendSrv.get('/api/dashboards/home').then(function(result) {
|
backendSrv.get('/api/dashboards/home').then(function(result) {
|
||||||
|
if (result.slug == null) {
|
||||||
var meta = result.meta;
|
var meta = result.meta;
|
||||||
meta.canSave = meta.canShare = meta.canStar = false;
|
meta.canSave = meta.canShare = meta.canStar = false;
|
||||||
$scope.initDashboard(result, $scope);
|
$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;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ export class DashNavCtrl {
|
|||||||
$scope.saveDashboardAsHome = function() {
|
$scope.saveDashboardAsHome = function() {
|
||||||
// TODO: this backend method needs to be implemented
|
// TODO: this backend method needs to be implemented
|
||||||
backendSrv.post('/api/preferences/set-home-dash', {
|
backendSrv.post('/api/preferences/set-home-dash', {
|
||||||
dashboardId: $scope.dashboard.id
|
homeDashboardId: $scope.dashboard.id
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user