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
e06756bc
Commit
e06756bc
authored
Mar 15, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'preferences' of github.com:grafana/grafana into preferences
parents
9c222a44
51bde36d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
85 additions
and
37 deletions
+85
-37
pkg/api/api.go
+2
-1
pkg/api/preferences.go
+12
-11
pkg/models/preferences.go
+17
-11
pkg/services/sqlstore/migrations/preferences_mig.go
+6
-3
pkg/services/sqlstore/preferences.go
+26
-10
public/app/features/dashboard/dashnav/dashnav.html
+1
-0
public/app/features/dashboard/dashnav/dashnav.ts
+21
-1
No files found.
pkg/api/api.go
View file @
e06756bc
...
...
@@ -96,7 +96,6 @@ func Register(r *macaron.Macaron) {
r
.
Delete
(
"/stars/dashboard/:id"
,
wrap
(
UnstarDashboard
))
r
.
Put
(
"/password"
,
bind
(
m
.
ChangeUserPasswordCommand
{}),
wrap
(
ChangeUserPassword
))
r
.
Get
(
"/quotas"
,
wrap
(
GetUserQuotas
))
r
.
Combo
(
"/prefs"
)
.
Get
(
GetUserPreferences
)
.
Put
(
bind
(
m
.
SavePreferencesCommand
{}),
wrap
(
SaveUserPreferences
))
})
// users (admin permission required)
...
...
@@ -161,6 +160,8 @@ func Register(r *macaron.Macaron) {
r
.
Delete
(
"/:id"
,
wrap
(
DeleteApiKey
))
},
reqOrgAdmin
)
r
.
Combo
(
"/preferences"
)
.
Get
(
GetPreferences
)
.
Put
(
bind
(
m
.
SavePreferencesCommand
{}),
wrap
(
SavePreferences
))
// Data sources
r
.
Group
(
"/datasources"
,
func
()
{
r
.
Get
(
"/"
,
GetDataSources
)
...
...
pkg/api/preferences.go
View file @
e06756bc
...
...
@@ -7,32 +7,33 @@ import (
)
// PUT /api/user/prefs
func
Save
User
Preferences
(
c
*
middleware
.
Context
,
cmd
m
.
SavePreferencesCommand
)
Response
{
func
SavePreferences
(
c
*
middleware
.
Context
,
cmd
m
.
SavePreferencesCommand
)
Response
{
cmd
.
Pref
Id
=
c
.
UserId
cmd
.
PrefType
=
`user`
cmd
.
User
Id
=
c
.
UserId
cmd
.
OrgId
=
c
.
OrgId
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
return
ApiError
(
500
,
"Failed to save
d user
preferences"
,
err
)
return
ApiError
(
500
,
"Failed to save preferences"
,
err
)
}
return
ApiSuccess
(
"
User p
references saved"
)
return
ApiSuccess
(
"
P
references saved"
)
}
// GET /api/user/prefs
func
Get
User
Preferences
(
c
*
middleware
.
Context
)
{
func
GetPreferences
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetPreferencesQuery
{
PrefId
:
c
.
UserId
,
PrefType
:
`user`
}
query
:=
m
.
GetPreferencesQuery
{
UserId
:
c
.
UserId
,
OrgId
:
c
.
OrgId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get preferences
for user
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to get preferences"
,
err
)
}
dto
:=
m
.
PreferencesDTO
{
PrefId
:
query
.
Result
.
PrefId
,
PrefType
:
query
.
Result
.
PrefType
,
PrefData
:
query
.
Result
.
PrefData
,
Id
:
query
.
Result
.
Id
,
UserId
:
query
.
Result
.
UserId
,
OrgId
:
query
.
Result
.
OrgId
,
Preference
:
query
.
Result
.
Preference
,
}
c
.
JSON
(
200
,
dto
)
...
...
pkg/models/preferences.go
View file @
e06756bc
...
...
@@ -2,6 +2,7 @@ package models
import
(
"errors"
"time"
)
// Typed errors
...
...
@@ -11,17 +12,21 @@ var (
type
Preferences
struct
{
Id
int64
PrefId
int64
PrefType
string
PrefData
map
[
string
]
interface
{}
OrgId
int64
UserId
int64
Version
int
Preference
map
[
string
]
interface
{}
Created
time
.
Time
Updated
time
.
Time
}
// ---------------------
// QUERIES
type
GetPreferencesQuery
struct
{
PrefId
int64
PrefType
string
Id
int64
OrgId
int64
UserId
int64
Result
*
Preferences
}
...
...
@@ -30,16 +35,17 @@ type GetPreferencesQuery struct {
// COMMANDS
type
SavePreferencesCommand
struct
{
Pref
Data
map
[
string
]
interface
{}
`json:"prefData
" binding:"Required"`
PrefId
int64
`json:"-"`
PrefType
string
`json:"-"`
Pref
erence
map
[
string
]
interface
{}
`json:"Preference
" binding:"Required"`
UserId
int64
`json:"-"`
OrgId
int64
`json:"-"`
}
// ----------------------
// DTO & Projections
type
PreferencesDTO
struct
{
PrefId
int64
`json:"prefId"`
PrefType
string
`json:"prefType"`
PrefData
map
[
string
]
interface
{}
`json:"prefData"`
Id
int64
`json:"Id"`
UserId
int64
`json:"UserId"`
OrgId
int64
`json:"OrgId"`
Preference
map
[
string
]
interface
{}
`json:"Preference"`
}
pkg/services/sqlstore/migrations/preferences_mig.go
View file @
e06756bc
...
...
@@ -8,9 +8,12 @@ func addPreferencesMigrations(mg *Migrator) {
Name
:
"preferences"
,
Columns
:
[]
*
Column
{
{
Name
:
"id"
,
Type
:
DB_BigInt
,
IsPrimaryKey
:
true
,
IsAutoIncrement
:
true
},
{
Name
:
"pref_id"
,
Type
:
DB_Int
,
Nullable
:
false
},
{
Name
:
"pref_type"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
false
},
{
Name
:
"pref_data"
,
Type
:
DB_Text
,
Nullable
:
false
},
{
Name
:
"org_id"
,
Type
:
DB_Int
,
Nullable
:
false
},
{
Name
:
"user_id"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
false
},
{
Name
:
"version"
,
Type
:
DB_Int
,
Nullable
:
false
},
{
Name
:
"preference"
,
Type
:
DB_Text
,
Nullable
:
false
},
{
Name
:
"created"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
{
Name
:
"updated"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
},
}
...
...
pkg/services/sqlstore/preferences.go
View file @
e06756bc
...
...
@@ -3,6 +3,7 @@ package sqlstore
import
(
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
"time"
)
func
init
()
{
...
...
@@ -12,12 +13,12 @@ func init() {
func
GetPreferences
(
query
*
m
.
GetPreferencesQuery
)
error
{
sql
:=
`SELECT * FROM preferences WHERE
pref
_id = ? `
+
`AND
pref_type
= ?`
sql
:=
`SELECT * FROM preferences WHERE
user
_id = ? `
+
`AND
org_id
= ?`
var
prefResults
=
make
([]
m
.
Preferences
,
0
)
resultsErr
:=
x
.
Sql
(
sql
,
query
.
PrefId
,
query
.
PrefType
)
.
Find
(
&
prefResults
)
resultsErr
:=
x
.
Sql
(
sql
,
query
.
UserId
,
query
.
OrgId
)
.
Find
(
&
prefResults
)
if
resultsErr
!=
nil
{
return
resultsErr
...
...
@@ -35,12 +36,12 @@ func GetPreferences(query *m.GetPreferencesQuery) error {
func
SavePreferences
(
cmd
*
m
.
SavePreferencesCommand
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
sql
:=
`SELECT * FROM preferences WHERE
pref
_id = ? `
+
`AND
pref_type
= ?`
sql
:=
`SELECT * FROM preferences WHERE
user
_id = ? `
+
`AND
org_id
= ?`
var
prefResults
=
make
([]
m
.
Preferences
,
0
)
resultsErr
:=
sess
.
Sql
(
sql
,
cmd
.
PrefId
,
cmd
.
PrefType
)
.
Find
(
&
prefResults
)
resultsErr
:=
sess
.
Sql
(
sql
,
cmd
.
UserId
,
cmd
.
OrgId
)
.
Find
(
&
prefResults
)
if
resultsErr
!=
nil
{
return
resultsErr
...
...
@@ -51,13 +52,15 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
var
saveErr
error
if
len
(
prefResults
)
==
0
{
savePref
.
PrefId
=
cmd
.
PrefId
savePref
.
PrefType
=
cmd
.
PrefType
savePref
.
PrefData
=
cmd
.
PrefData
savePref
.
UserId
=
cmd
.
UserId
savePref
.
OrgId
=
cmd
.
OrgId
savePref
.
Preference
=
cmd
.
Preference
savePref
=
SetPreferencesModel
(
savePref
,
false
)
affectedRows
,
saveErr
=
sess
.
Insert
(
&
savePref
)
}
else
{
savePref
=
prefResults
[
0
]
savePref
.
PrefData
=
cmd
.
PrefData
savePref
.
Preference
=
cmd
.
Preference
savePref
=
SetPreferencesModel
(
savePref
,
true
)
affectedRows
,
saveErr
=
sess
.
Id
(
savePref
.
Id
)
.
Update
(
&
savePref
)
}
...
...
@@ -68,3 +71,16 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
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
}
public/app/features/dashboard/dashnav/dashnav.html
View file @
e06756bc
...
...
@@ -48,6 +48,7 @@
<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"
><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=
"dashboardMeta.canSave"
><a
class=
"pointer"
ng-click=
"deleteDashboard();"
>
Delete dashboard
</a></li>
</ul>
</li>
...
...
public/app/features/dashboard/dashnav/dashnav.ts
View file @
e06756bc
...
...
@@ -7,7 +7,7 @@ import angular from 'angular';
export
class
DashNavCtrl
{
/** @ngInject */
constructor
(
$scope
,
$rootScope
,
alertSrv
,
$location
,
playlistSrv
,
backendSrv
,
$timeout
)
{
constructor
(
$scope
,
$rootScope
,
alertSrv
,
$location
,
playlistSrv
,
backendSrv
,
contextSrv
,
$timeout
)
{
$scope
.
init
=
function
()
{
$scope
.
onAppEvent
(
'save-dashboard'
,
$scope
.
saveDashboard
);
...
...
@@ -103,6 +103,26 @@ export class DashNavCtrl {
},
$scope
.
handleSaveDashError
);
};
$scope
.
saveDashboardAsHome
=
function
()
{
var
orgId
=
'org-'
+
contextSrv
.
user
.
orgId
;
backendSrv
.
get
(
'/api/preferences'
).
then
(
function
(
prefs
)
{
// 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
);
});
};
$scope
.
handleSaveDashError
=
function
(
err
)
{
if
(
err
.
data
&&
err
.
data
.
status
===
"version-mismatch"
)
{
err
.
isHandled
=
true
;
...
...
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