public dashboards: finalize db schema & v1 feature complete (#50467)
This PR completes public dashboards v1 functionality and simplifies public dashboard conventions. It exists as a large PR so that we are not making constant changes to the database schema. models.PublicDashboardConfig model replaced with models.PublicDashboard directly dashboard_public_config table renamed to dashboard_public models.Dashboard.IsPublic removed from the dashboard and replaced with models.PublicDashboard.isEnabled Routing now uses a uuid v4 as an access token for viewing a public dashboard anonymously, PublicDashboard.Uid only used as database identifier Frontend utilizes uuid for auth'd operations and access token for anonymous access Default to time range defined on dashboard when viewing public dashboard Add audit fields to public dashboard Co-authored-by: Owen Smallwood <owen.smallwood@grafana.com>, Ezequiel Victorero <ezequiel.victorero@grafana.com>, Jesse Weaver <jesse.weaver@grafana.com>
This commit is contained in:
co-authored by
Owen Smallwood <owen.smallwood@grafana.com>, Ezequiel Victorero <ezequiel.victorero@grafana.com>, Jesse Weaver
parent
773c269084
commit
d076bedb5e
@@ -200,7 +200,6 @@ type Dashboard struct {
|
||||
FolderId int64
|
||||
IsFolder bool
|
||||
HasAcl bool
|
||||
IsPublic bool
|
||||
|
||||
Title string
|
||||
Data *simplejson.Json
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPublicDashboardFailedGenerateUniqueUid = DashboardErr{
|
||||
Reason: "Failed to generate unique dashboard id",
|
||||
Reason: "Failed to generate unique public dashboard id",
|
||||
StatusCode: 500,
|
||||
}
|
||||
ErrPublicDashboardFailedGenerateAccesstoken = DashboardErr{
|
||||
Reason: "Failed to public dashboard access token",
|
||||
StatusCode: 500,
|
||||
}
|
||||
ErrPublicDashboardNotFound = DashboardErr{
|
||||
@@ -21,20 +31,51 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
type PublicDashboardConfig struct {
|
||||
IsPublic bool `json:"isPublic"`
|
||||
PublicDashboard PublicDashboard `json:"publicDashboard"`
|
||||
}
|
||||
|
||||
type PublicDashboard struct {
|
||||
Uid string `json:"uid" xorm:"uid"`
|
||||
DashboardUid string `json:"dashboardUid" xorm:"dashboard_uid"`
|
||||
OrgId int64 `json:"orgId" xorm:"org_id"`
|
||||
TimeSettings string `json:"timeSettings" xorm:"time_settings"`
|
||||
Uid string `json:"uid" xorm:"uid"`
|
||||
DashboardUid string `json:"dashboardUid" xorm:"dashboard_uid"`
|
||||
OrgId int64 `json:"-" xorm:"org_id"` // Don't ever marshal orgId to Json
|
||||
TimeSettings *simplejson.Json `json:"timeSettings" xorm:"time_settings"`
|
||||
IsEnabled bool `json:"isEnabled" xorm:"is_enabled"`
|
||||
AccessToken string `json:"accessToken" xorm:"access_token"`
|
||||
|
||||
CreatedBy int64 `json:"createdBy" xorm:"created_by"`
|
||||
UpdatedBy int64 `json:"updatedBy" xorm:"updated_by"`
|
||||
|
||||
CreatedAt time.Time `json:"createdAt" xorm:"created_at"`
|
||||
UpdatedAt time.Time `json:"updatedAt" xorm:"updated_at"`
|
||||
}
|
||||
|
||||
func (pd PublicDashboard) TableName() string {
|
||||
return "dashboard_public_config"
|
||||
return "dashboard_public"
|
||||
}
|
||||
|
||||
type TimeSettings struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
}
|
||||
|
||||
// build time settings object from json on public dashboard. If empty, use
|
||||
// defaults on the dashboard
|
||||
func (pd PublicDashboard) BuildTimeSettings(dashboard *Dashboard) *TimeSettings {
|
||||
ts := &TimeSettings{
|
||||
From: dashboard.Data.GetPath("time", "from").MustString(),
|
||||
To: dashboard.Data.GetPath("time", "to").MustString(),
|
||||
}
|
||||
|
||||
if pd.TimeSettings == nil {
|
||||
return ts
|
||||
}
|
||||
|
||||
// merge time settings from public dashboard
|
||||
to := pd.TimeSettings.Get("to").MustString("")
|
||||
from := pd.TimeSettings.Get("from").MustString("")
|
||||
if to != "" && from != "" {
|
||||
ts.From = from
|
||||
ts.To = to
|
||||
}
|
||||
|
||||
return ts
|
||||
}
|
||||
|
||||
//
|
||||
@@ -42,7 +83,7 @@ func (pd PublicDashboard) TableName() string {
|
||||
//
|
||||
|
||||
type SavePublicDashboardConfigCommand struct {
|
||||
DashboardUid string
|
||||
OrgId int64
|
||||
PublicDashboardConfig PublicDashboardConfig
|
||||
DashboardUid string
|
||||
OrgId int64
|
||||
PublicDashboard PublicDashboard
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPublicDashboardTableName(t *testing.T) {
|
||||
assert.Equal(t, "dashboard_public", PublicDashboard{}.TableName())
|
||||
}
|
||||
|
||||
func TestBuildTimeSettings(t *testing.T) {
|
||||
var dashboardData = simplejson.NewFromAny(map[string]interface{}{"time": map[string]interface{}{"from": "now-8", "to": "now"}})
|
||||
testCases := []struct {
|
||||
name string
|
||||
dashboard *Dashboard
|
||||
pubdash *PublicDashboard
|
||||
timeResult *TimeSettings
|
||||
}{
|
||||
{
|
||||
name: "should use dashboard time if pubdash time empty",
|
||||
dashboard: &Dashboard{Data: dashboardData},
|
||||
pubdash: &PublicDashboard{},
|
||||
timeResult: &TimeSettings{
|
||||
From: "now-8",
|
||||
To: "now",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should use dashboard time if pubdash to/from empty",
|
||||
dashboard: &Dashboard{Data: dashboardData},
|
||||
pubdash: &PublicDashboard{},
|
||||
timeResult: &TimeSettings{
|
||||
From: "now-8",
|
||||
To: "now",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should use pubdash time",
|
||||
dashboard: &Dashboard{Data: dashboardData},
|
||||
pubdash: &PublicDashboard{TimeSettings: simplejson.NewFromAny(map[string]interface{}{"from": "now-12", "to": "now"})},
|
||||
timeResult: &TimeSettings{
|
||||
From: "now-12",
|
||||
To: "now",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.timeResult, test.pubdash.BuildTimeSettings(test.dashboard))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user