dashboards: extract short uid generator to util package. #7883
This commit is contained in:
@@ -7,7 +7,7 @@ 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/teris-io/shortid"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Typed errors
|
// Typed errors
|
||||||
@@ -63,7 +63,7 @@ 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 = DashboardUid()
|
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
|
||||||
@@ -113,18 +113,12 @@ 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 {
|
} else {
|
||||||
dash.Uid = DashboardUid()
|
dash.Uid, _ = util.GenerateShortUid()
|
||||||
}
|
}
|
||||||
|
|
||||||
return dash
|
return dash
|
||||||
}
|
}
|
||||||
|
|
||||||
func DashboardUid() string {
|
|
||||||
gen, _ := shortid.New(1, shortid.DefaultABC, 1)
|
|
||||||
uid, _ := gen.Generate()
|
|
||||||
return uid
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDashboardModel turns the command into the savable model
|
// GetDashboardModel turns the command into the savable model
|
||||||
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
||||||
dash := NewDashboardFromJson(cmd.Dashboard)
|
dash := NewDashboardFromJson(cmd.Dashboard)
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/teris-io/shortid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gen, _ := shortid.New(1, shortid.DefaultABC, 1)
|
||||||
|
shortid.SetDefault(gen)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateShortUid generates a short unique identifier.
|
||||||
|
func GenerateShortUid() (uid string, err error) {
|
||||||
|
if uid, err = shortid.Generate(); err != nil {
|
||||||
|
if uid, err = shortid.Generate(); err != nil {
|
||||||
|
if uid, err = shortid.Generate(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return uid, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user