Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
1d6413bf
Commit
1d6413bf
authored
Jan 29, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More work on backend for user favorites
parent
e02e6017
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
138 additions
and
59 deletions
+138
-59
conf/grafana.ini
+1
-1
grafana
+1
-1
pkg/api/api.go
+2
-49
pkg/api/dashboard.go
+7
-2
pkg/api/dtos/models.go
+5
-0
pkg/api/favorite.go
+35
-0
pkg/api/index.go
+55
-0
pkg/api/login.go
+1
-1
pkg/api/render.go
+2
-2
pkg/middleware/auth.go
+3
-3
pkg/middleware/session.go
+6
-0
pkg/models/favorite.go
+12
-0
pkg/services/sqlstore/dashboard.go
+1
-0
pkg/services/sqlstore/favorite.go
+7
-0
No files found.
conf/grafana.ini
View file @
1d6413bf
...
...
@@ -59,7 +59,7 @@ default_role = Editor
[auth.anonymous]
; enable anonymous access
enabled
=
tru
e
enabled
=
fals
e
; specify account name that should be used for unauthenticated users
account_name
=
main
; specify role for unauthenticated users
...
...
grafana
@
3b5c813b
Subproject commit
9d0982f2f7552a08053b66d4b17ee0f583e8339e
Subproject commit
3b5c813be71c4816f3c2ef40e4c1439a8026236f
pkg/api/api.go
View file @
1d6413bf
...
...
@@ -6,7 +6,6 @@ import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
)
// Register adds http routes
...
...
@@ -46,6 +45,8 @@ func Register(r *macaron.Macaron) {
r
.
Put
(
"/"
,
bind
(
m
.
UpdateUserCommand
{}),
UpdateUser
)
r
.
Post
(
"/using/:id"
,
SetUsingAccount
)
r
.
Get
(
"/accounts"
,
GetUserAccounts
)
r
.
Post
(
"/favorites/dashboard/:id"
,
AddAsFavorite
)
r
.
Delete
(
"/favorites/dashboard/:id"
,
RemoveAsFavorite
)
})
// account
...
...
@@ -97,51 +98,3 @@ func Register(r *macaron.Macaron) {
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"
)
}
pkg/api/dashboard.go
View file @
1d6413bf
package
api
import
(
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
...
...
@@ -17,9 +18,13 @@ func GetDashboard(c *middleware.Context) {
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
)
{
...
...
pkg/api/dtos/models.go
View file @
1d6413bf
...
...
@@ -25,6 +25,11 @@ type CurrentUser struct {
GravatarUrl
string
`json:"gravatarUrl"`
}
type
Dashboard
struct
{
IsFavorite
bool
`json:"isFavorite"`
Dashboard
map
[
string
]
interface
{}
`json:"dashboard"`
}
type
DataSource
struct
{
Id
int64
`json:"id"`
AccountId
int64
`json:"accountId"`
...
...
pkg/api/favorite.go
0 → 100644
View file @
1d6413bf
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"
)
}
pkg/api/index.go
0 → 100644
View file @
1d6413bf
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"
)
}
pkg/api/login.go
View file @
1d6413bf
...
...
@@ -119,7 +119,7 @@ func loginUserWithUser(user *m.User, c *middleware.Context) {
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
)
{
...
...
pkg/api/render.go
View file @
1d6413bf
package
api
import
(
"fmt"
"net/http"
"strconv"
"github.com/torkelo/grafana-pro/pkg/components/renderer"
"github.com/torkelo/grafana-pro/pkg/middleware"
...
...
@@ -11,7 +11,7 @@ import (
func
RenderToPng
(
c
*
middleware
.
Context
)
{
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
{
Url
:
c
.
Params
(
"*"
)
+
queryParams
,
...
...
pkg/middleware/auth.go
View file @
1d6413bf
...
...
@@ -16,7 +16,7 @@ type AuthOptions struct {
}
func
getRequestUserId
(
c
*
Context
)
int64
{
userId
:=
c
.
Session
.
Get
(
"userId"
)
userId
:=
c
.
Session
.
Get
(
SESS_KEY_USERID
)
if
userId
!=
nil
{
return
userId
.
(
int64
)
...
...
@@ -24,8 +24,8 @@ func getRequestUserId(c *Context) int64 {
// TODO: figure out a way to secure this
if
c
.
Query
(
"render"
)
==
"1"
{
userId
:=
c
.
QueryInt64
(
"userId"
)
c
.
Session
.
Set
(
"userId"
,
userId
)
userId
:=
c
.
QueryInt64
(
SESS_KEY_USERID
)
c
.
Session
.
Set
(
SESS_KEY_USERID
,
userId
)
return
userId
}
...
...
pkg/middleware/session.go
0 → 100644
View file @
1d6413bf
package
middleware
const
(
SESS_KEY_USERID
=
"uid"
SESS_KEY_FAVORITES
=
"favorites"
)
pkg/models/favorite.go
View file @
1d6413bf
...
...
@@ -6,6 +6,9 @@ type Favorite struct {
DashboardId
int64
}
// ----------------------
// COMMANDS
type
AddAsFavoriteCommand
struct
{
UserId
int64
DashboardId
int64
...
...
@@ -15,3 +18,12 @@ type RemoveAsFavoriteCommand struct {
UserId
int64
DashboardId
int64
}
// ---------------------
// QUERIES
type
GetUserFavoritesQuery
struct
{
UserId
int64
Result
[]
Favorite
}
pkg/services/sqlstore/dashboard.go
View file @
1d6413bf
...
...
@@ -66,6 +66,7 @@ func GetDashboard(query *m.GetDashboardQuery) error {
return
m
.
ErrDashboardNotFound
}
dashboard
.
Data
[
"id"
]
=
dashboard
.
Id
query
.
Result
=
&
dashboard
return
nil
...
...
pkg/services/sqlstore/favorite.go
View file @
1d6413bf
...
...
@@ -10,6 +10,7 @@ import (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
AddAsFavorite
)
bus
.
AddHandler
(
"sql"
,
RemoveAsFavorite
)
bus
.
AddHandler
(
"sql"
,
GetUserFavorites
)
}
func
AddAsFavorite
(
cmd
*
m
.
AddAsFavoriteCommand
)
error
{
...
...
@@ -32,3 +33,9 @@ func RemoveAsFavorite(cmd *m.RemoveAsFavoriteCommand) error {
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment