mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
This commit is contained in:
parent
6e99d8bba5
commit
b06eaf66b6
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user