More work on backend for user favorites

pull/1442/head
Torkel Ödegaard 11 years ago
parent e02e60171e
commit 1d6413bfae
  1. 2
      conf/grafana.ini
  2. 2
      grafana
  3. 51
      pkg/api/api.go
  4. 9
      pkg/api/dashboard.go
  5. 5
      pkg/api/dtos/models.go
  6. 35
      pkg/api/favorite.go
  7. 55
      pkg/api/index.go
  8. 2
      pkg/api/login.go
  9. 4
      pkg/api/render.go
  10. 6
      pkg/middleware/auth.go
  11. 6
      pkg/middleware/session.go
  12. 12
      pkg/models/favorite.go
  13. 1
      pkg/services/sqlstore/dashboard.go
  14. 7
      pkg/services/sqlstore/favorite.go

@ -59,7 +59,7 @@ default_role = Editor
[auth.anonymous] [auth.anonymous]
; enable anonymous access ; enable anonymous access
enabled = true enabled = false
; specify account name that should be used for unauthenticated users ; specify account name that should be used for unauthenticated users
account_name = main account_name = main
; specify role for unauthenticated users ; specify role for unauthenticated users

@ -1 +1 @@
Subproject commit 9d0982f2f7552a08053b66d4b17ee0f583e8339e Subproject commit 3b5c813be71c4816f3c2ef40e4c1439a8026236f

@ -6,7 +6,6 @@ import (
"github.com/torkelo/grafana-pro/pkg/api/dtos" "github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
) )
// Register adds http routes // Register adds http routes
@ -46,6 +45,8 @@ func Register(r *macaron.Macaron) {
r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser) r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser)
r.Post("/using/:id", SetUsingAccount) r.Post("/using/:id", SetUsingAccount)
r.Get("/accounts", GetUserAccounts) r.Get("/accounts", GetUserAccounts)
r.Post("/favorites/dashboard/:id", AddAsFavorite)
r.Delete("/favorites/dashboard/:id", RemoveAsFavorite)
}) })
// account // account
@ -97,51 +98,3 @@ func Register(r *macaron.Macaron) {
r.NotFound(NotFound) r.NotFound(NotFound)
} }
func setIndexViewData(c *middleware.Context) error {
settings, err := getFrontendSettings(c)
if err != nil {
return err
}
currentUser := &dtos.CurrentUser{
IsSignedIn: c.IsSignedIn,
Login: c.Login,
Email: c.Email,
Name: c.Name,
AccountName: c.AccountName,
AccountRole: c.AccountRole,
GravatarUrl: dtos.GetGravatarUrl(c.Email),
IsGrafanaAdmin: c.IsGrafanaAdmin,
}
c.Data["User"] = currentUser
c.Data["Settings"] = settings
c.Data["AppUrl"] = setting.AppUrl
c.Data["AppSubUrl"] = setting.AppSubUrl
return nil
}
func Index(c *middleware.Context) {
if err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
}
c.HTML(200, "index")
}
func NotFound(c *middleware.Context) {
if c.IsApiRequest() {
c.JsonApiErr(200, "Not found", nil)
return
}
if err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
}
c.HTML(404, "index")
}

@ -1,6 +1,7 @@
package api package api
import ( import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
@ -17,9 +18,13 @@ func GetDashboard(c *middleware.Context) {
return return
} }
query.Result.Data["id"] = query.Result.Id dash := query.Result
dto := dtos.Dashboard{
IsFavorite: false,
Dashboard: dash.Data,
}
c.JSON(200, query.Result.Data) c.JSON(200, dto)
} }
func DeleteDashboard(c *middleware.Context) { func DeleteDashboard(c *middleware.Context) {

@ -25,6 +25,11 @@ type CurrentUser struct {
GravatarUrl string `json:"gravatarUrl"` GravatarUrl string `json:"gravatarUrl"`
} }
type Dashboard struct {
IsFavorite bool `json:"isFavorite"`
Dashboard map[string]interface{} `json:"dashboard"`
}
type DataSource struct { type DataSource struct {
Id int64 `json:"id"` Id int64 `json:"id"`
AccountId int64 `json:"accountId"` AccountId int64 `json:"accountId"`

@ -0,0 +1,35 @@
package api
import (
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func AddAsFavorite(c *middleware.Context) {
var cmd = m.AddAsFavoriteCommand{
UserId: c.UserId,
DashboardId: c.ParamsInt64(":id"),
}
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to add favorite", err)
return
}
c.JsonOK("Dashboard marked as favorite")
}
func RemoveAsFavorite(c *middleware.Context) {
var cmd = m.RemoveAsFavoriteCommand{
UserId: c.UserId,
DashboardId: c.ParamsInt64(":id"),
}
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to remove favorite", err)
return
}
c.JsonOK("Favorite removed")
}

@ -0,0 +1,55 @@
package api
import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/setting"
)
func setIndexViewData(c *middleware.Context) error {
settings, err := getFrontendSettings(c)
if err != nil {
return err
}
currentUser := &dtos.CurrentUser{
IsSignedIn: c.IsSignedIn,
Login: c.Login,
Email: c.Email,
Name: c.Name,
AccountName: c.AccountName,
AccountRole: c.AccountRole,
GravatarUrl: dtos.GetGravatarUrl(c.Email),
IsGrafanaAdmin: c.IsGrafanaAdmin,
}
c.Data["User"] = currentUser
c.Data["Settings"] = settings
c.Data["AppUrl"] = setting.AppUrl
c.Data["AppSubUrl"] = setting.AppSubUrl
return nil
}
func Index(c *middleware.Context) {
if err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
}
c.HTML(200, "index")
}
func NotFound(c *middleware.Context) {
if c.IsApiRequest() {
c.JsonApiErr(200, "Not found", nil)
return
}
if err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
}
c.HTML(404, "index")
}

@ -119,7 +119,7 @@ func loginUserWithUser(user *m.User, c *middleware.Context) {
log.Error(3, "User login with nil user") log.Error(3, "User login with nil user")
} }
c.Session.Set("userId", user.Id) c.Session.Set(middleware.SESS_KEY_USERID, user.Id)
} }
func LogoutPost(c *middleware.Context) { func LogoutPost(c *middleware.Context) {

@ -1,8 +1,8 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"strconv"
"github.com/torkelo/grafana-pro/pkg/components/renderer" "github.com/torkelo/grafana-pro/pkg/components/renderer"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
@ -11,7 +11,7 @@ import (
func RenderToPng(c *middleware.Context) { func RenderToPng(c *middleware.Context) {
queryReader := util.NewUrlQueryReader(c.Req.URL) queryReader := util.NewUrlQueryReader(c.Req.URL)
queryParams := "?render=1&userId=" + strconv.FormatInt(c.UserId, 10) + "&" + c.Req.URL.RawQuery queryParams := fmt.Sprintf("?render=1&%s=%d&%s", middleware.SESS_KEY_USERID, c.UserId, c.Req.URL.RawQuery)
renderOpts := &renderer.RenderOpts{ renderOpts := &renderer.RenderOpts{
Url: c.Params("*") + queryParams, Url: c.Params("*") + queryParams,

@ -16,7 +16,7 @@ type AuthOptions struct {
} }
func getRequestUserId(c *Context) int64 { func getRequestUserId(c *Context) int64 {
userId := c.Session.Get("userId") userId := c.Session.Get(SESS_KEY_USERID)
if userId != nil { if userId != nil {
return userId.(int64) return userId.(int64)
@ -24,8 +24,8 @@ func getRequestUserId(c *Context) int64 {
// TODO: figure out a way to secure this // TODO: figure out a way to secure this
if c.Query("render") == "1" { if c.Query("render") == "1" {
userId := c.QueryInt64("userId") userId := c.QueryInt64(SESS_KEY_USERID)
c.Session.Set("userId", userId) c.Session.Set(SESS_KEY_USERID, userId)
return userId return userId
} }

@ -0,0 +1,6 @@
package middleware
const (
SESS_KEY_USERID = "uid"
SESS_KEY_FAVORITES = "favorites"
)

@ -6,6 +6,9 @@ type Favorite struct {
DashboardId int64 DashboardId int64
} }
// ----------------------
// COMMANDS
type AddAsFavoriteCommand struct { type AddAsFavoriteCommand struct {
UserId int64 UserId int64
DashboardId int64 DashboardId int64
@ -15,3 +18,12 @@ type RemoveAsFavoriteCommand struct {
UserId int64 UserId int64
DashboardId int64 DashboardId int64
} }
// ---------------------
// QUERIES
type GetUserFavoritesQuery struct {
UserId int64
Result []Favorite
}

@ -66,6 +66,7 @@ func GetDashboard(query *m.GetDashboardQuery) error {
return m.ErrDashboardNotFound return m.ErrDashboardNotFound
} }
dashboard.Data["id"] = dashboard.Id
query.Result = &dashboard query.Result = &dashboard
return nil return nil

@ -10,6 +10,7 @@ import (
func init() { func init() {
bus.AddHandler("sql", AddAsFavorite) bus.AddHandler("sql", AddAsFavorite)
bus.AddHandler("sql", RemoveAsFavorite) bus.AddHandler("sql", RemoveAsFavorite)
bus.AddHandler("sql", GetUserFavorites)
} }
func AddAsFavorite(cmd *m.AddAsFavoriteCommand) error { func AddAsFavorite(cmd *m.AddAsFavoriteCommand) error {
@ -32,3 +33,9 @@ func RemoveAsFavorite(cmd *m.RemoveAsFavoriteCommand) error {
return err return err
}) })
} }
func GetUserFavorites(query *m.GetUserFavoritesQuery) error {
query.Result = make([]m.Favorite, 0)
err := x.Where("user_id=?", query.UserId).Find(&query.Result)
return err
}

Loading…
Cancel
Save