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
58cfb236
Commit
58cfb236
authored
Jan 31, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
retry uid generation
parent
9aa488c0
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
11 deletions
+61
-11
pkg/middleware/dashboard_redirect_test.go
+2
-0
pkg/models/dashboards.go
+1
-5
pkg/services/sqlstore/dashboard.go
+32
-4
pkg/services/sqlstore/dashboard_test.go
+26
-2
No files found.
pkg/middleware/dashboard_redirect_test.go
View file @
58cfb236
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ 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"
"github.com/grafana/grafana/pkg/util"
.
"github.com/smartystreets/goconvey/convey"
.
"github.com/smartystreets/goconvey/convey"
)
)
...
@@ -19,6 +20,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
...
@@ -19,6 +20,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
fakeDash
.
Id
=
1
fakeDash
.
Id
=
1
fakeDash
.
FolderId
=
1
fakeDash
.
FolderId
=
1
fakeDash
.
HasAcl
=
false
fakeDash
.
HasAcl
=
false
fakeDash
.
Uid
=
util
.
GenerateShortUid
()
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetDashboardQuery
)
error
{
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetDashboardQuery
)
error
{
query
.
Result
=
fakeDash
query
.
Result
=
fakeDash
...
...
pkg/models/dashboards.go
View file @
58cfb236
...
@@ -9,7 +9,6 @@ import (
...
@@ -9,7 +9,6 @@ import (
"github.com/gosimple/slug"
"github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
)
// Typed errors
// Typed errors
...
@@ -23,6 +22,7 @@ var (
...
@@ -23,6 +22,7 @@ var (
ErrDashboardFolderCannotHaveParent
=
errors
.
New
(
"A Dashboard Folder cannot be added to another folder"
)
ErrDashboardFolderCannotHaveParent
=
errors
.
New
(
"A Dashboard Folder cannot be added to another folder"
)
ErrDashboardContainsInvalidAlertData
=
errors
.
New
(
"Invalid alert data. Cannot save dashboard"
)
ErrDashboardContainsInvalidAlertData
=
errors
.
New
(
"Invalid alert data. Cannot save dashboard"
)
ErrDashboardFailedToUpdateAlertData
=
errors
.
New
(
"Failed to save alert data"
)
ErrDashboardFailedToUpdateAlertData
=
errors
.
New
(
"Failed to save alert data"
)
ErrDashboardFailedGenerateUniqueUid
=
errors
.
New
(
"Failed to generate unique dashboard uid."
)
)
)
type
UpdatePluginDashboardError
struct
{
type
UpdatePluginDashboardError
struct
{
...
@@ -66,7 +66,6 @@ type Dashboard struct {
...
@@ -66,7 +66,6 @@ type Dashboard struct {
// NewDashboard creates a new dashboard
// NewDashboard creates a new dashboard
func
NewDashboard
(
title
string
)
*
Dashboard
{
func
NewDashboard
(
title
string
)
*
Dashboard
{
dash
:=
&
Dashboard
{}
dash
:=
&
Dashboard
{}
dash
.
Uid
=
util
.
GenerateShortUid
()
dash
.
Data
=
simplejson
.
New
()
dash
.
Data
=
simplejson
.
New
()
dash
.
Data
.
Set
(
"title"
,
title
)
dash
.
Data
.
Set
(
"title"
,
title
)
dash
.
Title
=
title
dash
.
Title
=
title
...
@@ -115,9 +114,6 @@ func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
...
@@ -115,9 +114,6 @@ func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
if
uid
,
err
:=
dash
.
Data
.
Get
(
"uid"
)
.
String
();
err
==
nil
{
if
uid
,
err
:=
dash
.
Data
.
Get
(
"uid"
)
.
String
();
err
==
nil
{
dash
.
Uid
=
uid
dash
.
Uid
=
uid
}
else
{
dash
.
Uid
=
util
.
GenerateShortUid
()
dash
.
Data
.
Set
(
"uid"
,
dash
.
Uid
)
}
}
return
dash
return
dash
...
...
pkg/services/sqlstore/dashboard.go
View file @
58cfb236
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/metrics"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/util"
)
)
func
init
()
{
func
init
()
{
...
@@ -20,6 +21,8 @@ func init() {
...
@@ -20,6 +21,8 @@ func init() {
bus
.
AddHandler
(
"sql"
,
GetDashboardsByPluginId
)
bus
.
AddHandler
(
"sql"
,
GetDashboardsByPluginId
)
}
}
var
generateNewUid
func
()
string
=
util
.
GenerateShortUid
func
SaveDashboard
(
cmd
*
m
.
SaveDashboardCommand
)
error
{
func
SaveDashboard
(
cmd
*
m
.
SaveDashboardCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
dash
:=
cmd
.
GetDashboardModel
()
dash
:=
cmd
.
GetDashboardModel
()
...
@@ -27,7 +30,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
...
@@ -27,7 +30,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
// try get existing dashboard
// try get existing dashboard
var
existing
m
.
Dashboard
var
existing
m
.
Dashboard
if
dash
.
Id
>
0
{
if
dash
.
Id
!=
0
{
dashWithIdExists
,
err
:=
sess
.
Where
(
"id=? AND org_id=?"
,
dash
.
Id
,
dash
.
OrgId
)
.
Get
(
&
existing
)
dashWithIdExists
,
err
:=
sess
.
Where
(
"id=? AND org_id=?"
,
dash
.
Id
,
dash
.
OrgId
)
.
Get
(
&
existing
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -49,8 +52,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
...
@@ -49,8 +52,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
if
existing
.
PluginId
!=
""
&&
cmd
.
Overwrite
==
false
{
if
existing
.
PluginId
!=
""
&&
cmd
.
Overwrite
==
false
{
return
m
.
UpdatePluginDashboardError
{
PluginId
:
existing
.
PluginId
}
return
m
.
UpdatePluginDashboardError
{
PluginId
:
existing
.
PluginId
}
}
}
}
}
else
if
dash
.
Uid
!=
""
{
var
sameUid
m
.
Dashboard
var
sameUid
m
.
Dashboard
sameUidExists
,
err
:=
sess
.
Where
(
"org_id=? AND uid=?"
,
dash
.
OrgId
,
dash
.
Uid
)
.
Get
(
&
sameUid
)
sameUidExists
,
err
:=
sess
.
Where
(
"org_id=? AND uid=?"
,
dash
.
OrgId
,
dash
.
Uid
)
.
Get
(
&
sameUid
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -68,8 +70,18 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
...
@@ -68,8 +70,18 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
}
}
}
}
}
}
}
if
dash
.
Uid
==
""
{
uid
,
err
:=
generateNewDashboardUid
(
sess
,
dash
.
OrgId
)
if
err
!=
nil
{
return
err
}
dash
.
Uid
=
uid
dash
.
Data
.
Set
(
"uid"
,
uid
)
}
err
=
guaranteeDashboardNameIsUniqueInFolder
(
sess
,
dash
)
err
:
=
guaranteeDashboardNameIsUniqueInFolder
(
sess
,
dash
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -144,6 +156,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
...
@@ -144,6 +156,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
return
err
return
err
})
})
}
}
func
generateNewDashboardUid
(
sess
*
DBSession
,
orgId
int64
)
(
string
,
error
)
{
for
i
:=
0
;
i
<
3
;
i
++
{
uid
:=
generateNewUid
()
exists
,
err
:=
sess
.
Where
(
"org_id=? AND uid=?"
,
orgId
,
uid
)
.
Get
(
&
m
.
Dashboard
{})
if
err
!=
nil
{
return
""
,
err
}
if
!
exists
{
return
uid
,
nil
}
}
return
""
,
m
.
ErrDashboardFailedGenerateUniqueUid
}
func
guaranteeDashboardNameIsUniqueInFolder
(
sess
*
DBSession
,
dash
*
m
.
Dashboard
)
error
{
func
guaranteeDashboardNameIsUniqueInFolder
(
sess
*
DBSession
,
dash
*
m
.
Dashboard
)
error
{
var
sameNameInFolder
m
.
Dashboard
var
sameNameInFolder
m
.
Dashboard
...
...
pkg/services/sqlstore/dashboard_test.go
View file @
58cfb236
...
@@ -5,12 +5,12 @@ import (
...
@@ -5,12 +5,12 @@ import (
"testing"
"testing"
"github.com/go-xorm/xorm"
"github.com/go-xorm/xorm"
.
"github.com/smartystreets/goconvey/convey"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
.
"github.com/smartystreets/goconvey/convey"
)
)
func
TestDashboardDataAccess
(
t
*
testing
.
T
)
{
func
TestDashboardDataAccess
(
t
*
testing
.
T
)
{
...
@@ -338,6 +338,30 @@ func TestDashboardDataAccess(t *testing.T) {
...
@@ -338,6 +338,30 @@ func TestDashboardDataAccess(t *testing.T) {
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
})
})
Convey
(
"Should retry generation of uid once if it fails."
,
func
()
{
timesCalled
:=
0
generateNewUid
=
func
()
string
{
timesCalled
+=
1
if
timesCalled
<=
2
{
return
savedDash
.
Uid
}
else
{
return
util
.
GenerateShortUid
()
}
}
cmd
:=
m
.
SaveDashboardCommand
{
OrgId
:
1
,
Dashboard
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"title"
:
"new dash 12334"
,
"tags"
:
[]
interface
{}{},
}),
}
err
:=
SaveDashboard
(
&
cmd
)
So
(
err
,
ShouldBeNil
)
generateNewUid
=
util
.
GenerateShortUid
})
Convey
(
"Should be able to update dashboard and remove folderId"
,
func
()
{
Convey
(
"Should be able to update dashboard and remove folderId"
,
func
()
{
cmd
:=
m
.
SaveDashboardCommand
{
cmd
:=
m
.
SaveDashboardCommand
{
OrgId
:
1
,
OrgId
:
1
,
...
...
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