From b06eaf66b6077083d2353efcdd8c874697f128ad Mon Sep 17 00:00:00 2001 From: Guilherme Caulada Date: Tue, 13 Sep 2022 13:33:41 -0300 Subject: [PATCH] Public Dashboards: Replace simplejson with TimeSettings on dashboard struct (#55047) * Replace simplejson for TimeSettings on PublicDashboard struct * Implement xorm conversion interface to TimeSettings * Fix minor test assertion issue --- .../publicdashboards/database/database.go | 3 ++- .../database/database_test.go | 4 +-- .../publicdashboards/models/models.go | 26 ++++++++++++------- .../publicdashboards/models/models_test.go | 2 +- .../publicdashboards/service/service.go | 3 +-- .../publicdashboards/service/service_test.go | 9 +++---- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/pkg/services/publicdashboards/database/database.go b/pkg/services/publicdashboards/database/database.go index 7ac3ca5d017..04d92ce3fba 100644 --- a/pkg/services/publicdashboards/database/database.go +++ b/pkg/services/publicdashboards/database/database.go @@ -2,6 +2,7 @@ package database import ( "context" + "encoding/json" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" @@ -183,7 +184,7 @@ func (d *PublicDashboardStoreImpl) SavePublicDashboardConfig(ctx context.Context // updates existing public dashboard configuration func (d *PublicDashboardStoreImpl) UpdatePublicDashboardConfig(ctx context.Context, cmd SavePublicDashboardConfigCommand) error { err := d.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error { - timeSettingsJSON, err := cmd.PublicDashboard.TimeSettings.MarshalJSON() + timeSettingsJSON, err := json.Marshal(cmd.PublicDashboard.TimeSettings) if err != nil { return err } diff --git a/pkg/services/publicdashboards/database/database_test.go b/pkg/services/publicdashboards/database/database_test.go index 728565c9d34..5ac7d281ab5 100644 --- a/pkg/services/publicdashboards/database/database_test.go +++ b/pkg/services/publicdashboards/database/database_test.go @@ -19,7 +19,7 @@ import ( ) // This is what the db sets empty time settings to -var DefaultTimeSettings, _ = simplejson.NewJson([]byte(`{}`)) +var DefaultTimeSettings = &TimeSettings{} // Default time to pass in with seconds rounded var DefaultTime = time.Now().UTC().Round(time.Second) @@ -361,7 +361,7 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) { DashboardUid: savedDashboard.Uid, OrgId: savedDashboard.OrgId, IsEnabled: false, - TimeSettings: simplejson.NewFromAny(map[string]interface{}{"from": "now-8", "to": "now"}), + TimeSettings: &TimeSettings{From: "now-8", To: "now"}, UpdatedAt: time.Now().UTC().Round(time.Second), UpdatedBy: 8, } diff --git a/pkg/services/publicdashboards/models/models.go b/pkg/services/publicdashboards/models/models.go index d1ea93b129d..b814d518189 100644 --- a/pkg/services/publicdashboards/models/models.go +++ b/pkg/services/publicdashboards/models/models.go @@ -1,10 +1,10 @@ package models import ( + "encoding/json" "strconv" "time" - "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/tsdb/legacydata" ) @@ -58,12 +58,12 @@ var ( ) type PublicDashboard struct { - Uid string `json:"uid" xorm:"pk 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"` + Uid string `json:"uid" xorm:"pk uid"` + DashboardUid string `json:"dashboardUid" xorm:"dashboard_uid"` + OrgId int64 `json:"-" xorm:"org_id"` // Don't ever marshal orgId to Json + TimeSettings *TimeSettings `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"` @@ -77,8 +77,16 @@ func (pd PublicDashboard) TableName() string { } type TimeSettings struct { - From string `json:"from"` - To string `json:"to"` + From string `json:"from,omitempty"` + To string `json:"to,omitempty"` +} + +func (ts *TimeSettings) FromDB(data []byte) error { + return json.Unmarshal(data, ts) +} + +func (ts *TimeSettings) ToDB() ([]byte, error) { + return json.Marshal(ts) } // build time settings object from json on public dashboard. If empty, use diff --git a/pkg/services/publicdashboards/models/models_test.go b/pkg/services/publicdashboards/models/models_test.go index 160b0387491..01be547488e 100644 --- a/pkg/services/publicdashboards/models/models_test.go +++ b/pkg/services/publicdashboards/models/models_test.go @@ -34,7 +34,7 @@ func TestBuildTimeSettings(t *testing.T) { { name: "should use dashboard time even if pubdash time exists", dashboard: &models.Dashboard{Data: dashboardData}, - pubdash: &PublicDashboard{TimeSettings: simplejson.NewFromAny(map[string]interface{}{"from": "now-12", "to": "now"})}, + pubdash: &PublicDashboard{TimeSettings: &TimeSettings{From: "now-12", To: "now"}}, timeResult: TimeSettings{ From: fromMs, To: toMs, diff --git a/pkg/services/publicdashboards/service/service.go b/pkg/services/publicdashboards/service/service.go index a0a56f3810c..4da151365b7 100644 --- a/pkg/services/publicdashboards/service/service.go +++ b/pkg/services/publicdashboards/service/service.go @@ -8,7 +8,6 @@ import ( "github.com/google/uuid" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana/pkg/api/dtos" - "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/datasources" @@ -109,7 +108,7 @@ func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Cont // set default value for time settings if dto.PublicDashboard.TimeSettings == nil { - dto.PublicDashboard.TimeSettings = simplejson.New() + dto.PublicDashboard.TimeSettings = &TimeSettings{} } // get existing public dashboard if exists diff --git a/pkg/services/publicdashboards/service/service_test.go b/pkg/services/publicdashboards/service/service_test.go index ca0852fbf31..553599e8efb 100644 --- a/pkg/services/publicdashboards/service/service_test.go +++ b/pkg/services/publicdashboards/service/service_test.go @@ -25,8 +25,8 @@ import ( "github.com/grafana/grafana/pkg/tsdb/intervalv2" ) -var timeSettings, _ = simplejson.NewJson([]byte(`{"from": "now-12h", "to": "now"}`)) -var defaultPubdashTimeSettings, _ = simplejson.NewJson([]byte(`{}`)) +var timeSettings = &TimeSettings{From: "now-12h", To: "now"} +var defaultPubdashTimeSettings = &TimeSettings{} var dashboardData = simplejson.NewFromAny(map[string]interface{}{"time": map[string]interface{}{"from": "now-8h", "to": "now"}}) var SignedInUser = &user.SignedInUser{UserID: 1234, Login: "user@login.com"} @@ -325,10 +325,7 @@ func TestUpdatePublicDashboard(t *testing.T) { updatedPubdash, err := service.SavePublicDashboardConfig(context.Background(), SignedInUser, dto) require.NoError(t, err) - timeSettings, err := simplejson.NewJson([]byte("{}")) - require.NoError(t, err) - - assert.Equal(t, timeSettings, updatedPubdash.TimeSettings) + assert.Equal(t, &TimeSettings{}, updatedPubdash.TimeSettings) }) }