dashboard: generate and include uid in dashboard model. #7883

This commit is contained in:
Marcus Efraimsson
2018-01-29 17:20:18 +01:00
parent 025a14ec24
commit 5b35c694dc
6 changed files with 407 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/teris-io/shortid"
)
// Typed errors
@@ -39,6 +40,7 @@ var (
// Dashboard model
type Dashboard struct {
Id int64
Uid string
Slug string
OrgId int64
GnetId int64
@@ -61,6 +63,7 @@ type Dashboard struct {
// NewDashboard creates a new dashboard
func NewDashboard(title string) *Dashboard {
dash := &Dashboard{}
dash.Uid = DashboardUid()
dash.Data = simplejson.New()
dash.Data.Set("title", title)
dash.Title = title
@@ -107,9 +110,21 @@ func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
dash.GnetId = int64(gnetId)
}
if uid, err := dash.Data.Get("uid").String(); err == nil {
dash.Uid = uid
} else {
dash.Uid = DashboardUid()
}
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
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash := NewDashboardFromJson(cmd.Dashboard)