Commit a88176e0 by Torkel Ödegaard

feat(preferences): lots of refactoring and changes to #3214

parent e06756bc
package dtos
type Preferences struct {
Theme string `json:"theme"`
HomeDashboardId int64 `json:"homeDashboardId"`
Timezone string `json:"timezone"`
}
package api package api
import ( import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
...@@ -29,11 +30,10 @@ func GetPreferences(c *middleware.Context) { ...@@ -29,11 +30,10 @@ func GetPreferences(c *middleware.Context) {
c.JsonApiErr(500, "Failed to get preferences", err) c.JsonApiErr(500, "Failed to get preferences", err)
} }
dto := m.PreferencesDTO{ dto := dtos.Preferences{
Id: query.Result.Id, HomeDashboardId: query.Result.HomeDashboardId,
UserId: query.Result.UserId, Timezone: query.Result.Timezone,
OrgId: query.Result.OrgId, Theme: query.Result.Theme,
Preference: query.Result.Preference,
} }
c.JSON(200, dto) c.JSON(200, dto)
......
...@@ -15,7 +15,9 @@ type Preferences struct { ...@@ -15,7 +15,9 @@ type Preferences struct {
OrgId int64 OrgId int64
UserId int64 UserId int64
Version int Version int
Preference map[string]interface{} HomeDashboardId int64
Timezone string
Theme string
Created time.Time Created time.Time
Updated time.Time Updated time.Time
} }
...@@ -33,19 +35,11 @@ type GetPreferencesQuery struct { ...@@ -33,19 +35,11 @@ type GetPreferencesQuery struct {
// --------------------- // ---------------------
// COMMANDS // COMMANDS
type SavePreferencesCommand struct { type SavePreferencesCommand struct {
Preference map[string]interface{} `json:"Preference" binding:"Required"` UserId int64
UserId int64 `json:"-"` OrgId int64
OrgId int64 `json:"-"`
}
// ----------------------
// DTO & Projections
type PreferencesDTO struct { HomeDashboardId int64
Id int64 `json:"Id"` Timezone string
UserId int64 `json:"UserId"` Theme string
OrgId int64 `json:"OrgId"`
Preference map[string]interface{} `json:"Preference"`
} }
...@@ -4,20 +4,27 @@ import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" ...@@ -4,20 +4,27 @@ import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
func addPreferencesMigrations(mg *Migrator) { func addPreferencesMigrations(mg *Migrator) {
preferencesV1 := Table{ mg.AddMigration("drop preferences table v2", NewDropTableMigration("preferences"))
preferencesV2 := Table{
Name: "preferences", Name: "preferences",
Columns: []*Column{ Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_Int, Nullable: false}, {Name: "org_id", Type: DB_Int, Nullable: true},
{Name: "user_id", Type: DB_NVarchar, Length: 255, Nullable: false}, {Name: "user_id", Type: DB_NVarchar, Length: 255, Nullable: true},
{Name: "version", Type: DB_Int, Nullable: false}, {Name: "version", Type: DB_Int, Nullable: false},
{Name: "preference", Type: DB_Text, Nullable: false}, {Name: "home_dashboard_id", Type: DB_BigInt, Nullable: true},
{Name: "timezone", Type: DB_NVarchar, Length: 50, Nullable: true},
{Name: "theme", Type: DB_NVarchar, Length: 20, Nullable: true},
{Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false}, {Name: "updated", Type: DB_DateTime, Nullable: false},
}, },
Indices: []*Index{
{Cols: []string{"org_id"}},
{Cols: []string{"user_id"}},
},
} }
// create table // create table
mg.AddMigration("create preferences table v1", NewAddTableMigration(preferencesV1)) mg.AddMigration("create preferences table v2", NewAddTableMigration(preferencesV2))
} }
...@@ -3,7 +3,6 @@ package sqlstore ...@@ -3,7 +3,6 @@ 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() {
...@@ -13,19 +12,15 @@ func init() { ...@@ -13,19 +12,15 @@ func init() {
func GetPreferences(query *m.GetPreferencesQuery) error { func GetPreferences(query *m.GetPreferencesQuery) error {
sql := `SELECT * FROM preferences WHERE user_id = ? ` + var prefs m.Preferences
`AND org_id = ?` exists, err := x.Where("org_id=? AND user_id=?", query.OrgId, query.UserId).Get(&prefs)
var prefResults = make([]m.Preferences, 0) if err != nil {
return err
resultsErr := x.Sql(sql, query.UserId, query.OrgId).Find(&prefResults)
if resultsErr != nil {
return resultsErr
} }
if len(prefResults) > 0 { if exists {
query.Result = &prefResults[0] query.Result = &prefs
} else { } else {
query.Result = new(m.Preferences) query.Result = new(m.Preferences)
} }
...@@ -36,51 +31,25 @@ func GetPreferences(query *m.GetPreferencesQuery) error { ...@@ -36,51 +31,25 @@ func GetPreferences(query *m.GetPreferencesQuery) error {
func SavePreferences(cmd *m.SavePreferencesCommand) error { func SavePreferences(cmd *m.SavePreferencesCommand) error {
return inTransaction2(func(sess *session) error { return inTransaction2(func(sess *session) error {
sql := `SELECT * FROM preferences WHERE user_id = ? ` + var prefs m.Preferences
`AND org_id = ?` exists, err := sess.Where("org_id=? AND user_id=?", cmd.OrgId, cmd.UserId).Get(&prefs)
var prefResults = make([]m.Preferences, 0)
resultsErr := sess.Sql(sql, cmd.UserId, cmd.OrgId).Find(&prefResults)
if resultsErr != nil { if !exists {
return resultsErr prefs = m.Preferences{
UserId: cmd.UserId,
OrgId: cmd.OrgId,
HomeDashboardId: cmd.HomeDashboardId,
Timezone: cmd.Timezone,
Theme: cmd.Theme,
} }
_, err = sess.Insert(&prefs)
var savePref m.Preferences return err
var affectedRows int64
var saveErr error
if len(prefResults) == 0 {
savePref.UserId = cmd.UserId
savePref.OrgId = cmd.OrgId
savePref.Preference = cmd.Preference
savePref = SetPreferencesModel(savePref, false)
affectedRows, saveErr = sess.Insert(&savePref)
} else { } else {
savePref = prefResults[0] prefs.HomeDashboardId = cmd.HomeDashboardId
savePref.Preference = cmd.Preference prefs.Timezone = cmd.Timezone
savePref = SetPreferencesModel(savePref, true) prefs.Theme = cmd.Theme
affectedRows, saveErr = sess.Id(savePref.Id).Update(&savePref) _, err = sess.Id(prefs.Id).Update(&prefs)
} return err
if affectedRows == 0 {
return m.ErrPreferencesNotFound
} }
return saveErr
}) })
} }
func SetPreferencesModel(pref m.Preferences, updating bool) m.Preferences {
if updating {
pref.Version = pref.Version + 1
} else {
pref.Version = 0
pref.Created = time.Now()
}
pref.Updated = time.Now()
return pref
}
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
<li ng-if="dashboardMeta.canEdit"><a class="pointer" ng-click="editJson();">View JSON</a></li> <li ng-if="dashboardMeta.canEdit"><a class="pointer" ng-click="editJson();">View JSON</a></li>
<li ng-if="contextSrv.isEditor && !dashboard.editable"><a class="pointer" ng-click="makeEditable();">Make Editable</a></li> <li ng-if="contextSrv.isEditor && !dashboard.editable"><a class="pointer" ng-click="makeEditable();">Make Editable</a></li>
<li ng-if="contextSrv.isEditor"><a class="pointer" ng-click="saveDashboardAs();">Save As...</a></li> <li ng-if="contextSrv.isEditor"><a class="pointer" ng-click="saveDashboardAs();">Save As...</a></li>
<li ng-if="contextSrv.isEditor"><a class="pointer" ng-click="saveDashboardAsHome();">Save As Home</a></li> <li ng-if="dashboard.id"><a class="pointer" ng-click="saveDashboardAsHome();">Set As Home</a></li>
<li ng-if="dashboardMeta.canSave"><a class="pointer" ng-click="deleteDashboard();">Delete dashboard</a></li> <li ng-if="dashboardMeta.canSave"><a class="pointer" ng-click="deleteDashboard();">Delete dashboard</a></li>
</ul> </ul>
</li> </li>
......
...@@ -104,22 +104,9 @@ export class DashNavCtrl { ...@@ -104,22 +104,9 @@ export class DashNavCtrl {
}; };
$scope.saveDashboardAsHome = function() { $scope.saveDashboardAsHome = function() {
var orgId = 'org-' + contextSrv.user.orgId; // TODO: this backend method needs to be implemented
backendSrv.get('/api/preferences').then(function(prefs) { backendSrv.post('/api/preferences/set-home-dash', {
dashboardId: $scope.dashboard.id
// Checking if the preferences already exists or not
if (prefs.userId === 0 && prefs.orgId === 0 && prefs.preference === null) {
prefs.preference = {};
}
if (prefs.preference == null) {
prefs.preference = {
home_dashboard_id: $scope.dashboard.id
};
} else {
var orgPrefs = prefs.preference;
orgPrefs.home_dashboard = $scope.dashboard.id;
}
backendSrv.put('api/preferences', prefs);
}); });
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment