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
a88176e0
Commit
a88176e0
authored
Mar 15, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(preferences): lots of refactoring and changes to #3214
parent
e06756bc
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
59 additions
and
95 deletions
+59
-95
pkg/api/dtos/prefs.go
+7
-0
pkg/api/preferences.go
+5
-5
pkg/models/preferences.go
+8
-14
pkg/services/sqlstore/migrations/preferences_mig.go
+13
-6
pkg/services/sqlstore/preferences.go
+22
-53
public/app/features/dashboard/dashnav/dashnav.html
+1
-1
public/app/features/dashboard/dashnav/dashnav.ts
+3
-16
No files found.
pkg/api/dtos/prefs.go
0 → 100644
View file @
a88176e0
package
dtos
type
Preferences
struct
{
Theme
string
`json:"theme"`
HomeDashboardId
int64
`json:"homeDashboardId"`
Timezone
string
`json:"timezone"`
}
pkg/api/preferences.go
View file @
a88176e0
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
)
...
...
pkg/models/preferences.go
View file @
a88176e0
...
@@ -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"`
}
}
pkg/services/sqlstore/migrations/preferences_mig.go
View file @
a88176e0
...
@@ -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
:
fals
e
},
{
Name
:
"org_id"
,
Type
:
DB_Int
,
Nullable
:
tru
e
},
{
Name
:
"user_id"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
fals
e
},
{
Name
:
"user_id"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
tru
e
},
{
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
))
}
}
pkg/services/sqlstore/preferences.go
View file @
a88176e0
...
@@ -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
=
&
pref
Results
[
0
]
query
.
Result
=
&
pref
s
}
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
}
public/app/features/dashboard/dashnav/dashnav.html
View file @
a88176e0
...
@@ -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>
...
...
public/app/features/dashboard/dashnav/dashnav.ts
View file @
a88176e0
...
@@ -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
);
});
});
};
};
...
...
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