mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Add store split for Get Dashboard version method (#49138)
* Add store split for Get Dashboard version method * Implement dashboard version service * Fix api tests * Remove GetDashboarVersion from sqlstore * Add fakes for Get dashboard version * Fix sqlstore test * Add Get Dashboard store test * Add dashver service test * Remove useless comments
This commit is contained in:
9
pkg/services/dashboardversion/dashver.go
Normal file
9
pkg/services/dashboardversion/dashver.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package dashver
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Get(ctx context.Context, query *GetDashboardVersionQuery) (*DashboardVersion, error)
|
||||
}
|
||||
30
pkg/services/dashboardversion/dashverimpl/dashver.go
Normal file
30
pkg/services/dashboardversion/dashverimpl/dashver.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB, cfg *setting.Cfg) dashver.Service {
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
version, err := s.store.Get(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
version.Data.Set("id", version.DashboardID)
|
||||
return version, nil
|
||||
}
|
||||
39
pkg/services/dashboardversion/dashverimpl/dashver_test.go
Normal file
39
pkg/services/dashboardversion/dashverimpl/dashver_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDashboardVersionService(t *testing.T) {
|
||||
dashboardVersionStore := newDashboardVersionStoreFake()
|
||||
dashboardVersionService := Service{store: dashboardVersionStore}
|
||||
|
||||
t.Run("Get dashboard version", func(t *testing.T) {
|
||||
dashboard := &dashver.DashboardVersion{
|
||||
ID: 11,
|
||||
Data: &simplejson.Json{},
|
||||
}
|
||||
dashboardVersionStore.ExpectedDashboardVersion = dashboard
|
||||
dashboardVersion, err := dashboardVersionService.Get(context.Background(), &dashver.GetDashboardVersionQuery{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dashboardVersion, dashboard)
|
||||
})
|
||||
}
|
||||
|
||||
type FakeDashboardVersionStroe struct {
|
||||
ExpectedDashboardVersion *dashver.DashboardVersion
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
func newDashboardVersionStoreFake() *FakeDashboardVersionStroe {
|
||||
return &FakeDashboardVersionStroe{}
|
||||
}
|
||||
|
||||
func (f *FakeDashboardVersionStroe) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
return f.ExpectedDashboardVersion, f.ExpectedError
|
||||
}
|
||||
40
pkg/services/dashboardversion/dashverimpl/store.go
Normal file
40
pkg/services/dashboardversion/dashverimpl/store.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
)
|
||||
|
||||
type store interface {
|
||||
Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error)
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
}
|
||||
|
||||
func (s *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
var version dashver.DashboardVersion
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
has, err := sess.Where("dashboard_version.dashboard_id=? AND dashboard_version.version=? AND dashboard.org_id=?", query.DashboardID, query.Version, query.OrgID).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Get(&version)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !has {
|
||||
return models.ErrDashboardVersionNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &version, nil
|
||||
}
|
||||
131
pkg/services/dashboardversion/dashverimpl/store_test.go
Normal file
131
pkg/services/dashboardversion/dashverimpl/store_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegrationDashboardVersion(t *testing.T) {
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
dashVerStore := sqlStore{db: ss}
|
||||
|
||||
t.Run("Get a Dashboard ID and version ID", func(t *testing.T) {
|
||||
savedDash := insertTestDashboard(t, ss, "test dash 26", 1, 0, false, "diff")
|
||||
|
||||
query := dashver.GetDashboardVersionQuery{
|
||||
DashboardID: savedDash.Id,
|
||||
Version: savedDash.Version,
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
res, err := dashVerStore.Get(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, query.DashboardID, savedDash.Id)
|
||||
require.Equal(t, query.Version, savedDash.Version)
|
||||
|
||||
dashCmd := &models.Dashboard{
|
||||
Id: res.ID,
|
||||
Uid: savedDash.Uid,
|
||||
OrgId: savedDash.OrgId,
|
||||
}
|
||||
err = getDashboard(t, ss, dashCmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.EqualValues(t, dashCmd.Data.Get("uid"), res.Data.Get("uid"))
|
||||
require.EqualValues(t, dashCmd.Data.Get("orgId"), res.Data.Get("orgId"))
|
||||
})
|
||||
|
||||
t.Run("Attempt to get a version that doesn't exist", func(t *testing.T) {
|
||||
query := dashver.GetDashboardVersionQuery{
|
||||
DashboardID: int64(999),
|
||||
Version: 123,
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
_, err := dashVerStore.Get(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, models.ErrDashboardVersionNotFound, err)
|
||||
})
|
||||
}
|
||||
|
||||
func getDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.Dashboard) error {
|
||||
t.Helper()
|
||||
return sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
has, err := sess.Get(dashboard)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
dashboard.SetId(dashboard.Id)
|
||||
dashboard.SetUid(dashboard.Uid)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string, orgId int64,
|
||||
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
|
||||
t.Helper()
|
||||
cmd := models.SaveDashboardCommand{
|
||||
OrgId: orgId,
|
||||
FolderId: folderId,
|
||||
IsFolder: isFolder,
|
||||
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||
"id": nil,
|
||||
"title": title,
|
||||
"tags": tags,
|
||||
}),
|
||||
}
|
||||
|
||||
var dash *models.Dashboard
|
||||
err := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
dash = cmd.GetDashboardModel()
|
||||
dash.SetVersion(1)
|
||||
dash.Created = time.Now()
|
||||
dash.Updated = time.Now()
|
||||
dash.Uid = util.GenerateShortUID()
|
||||
_, err := sess.Insert(dash)
|
||||
return err
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, dash)
|
||||
dash.Data.Set("id", dash.Id)
|
||||
dash.Data.Set("uid", dash.Uid)
|
||||
|
||||
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
dashVersion := &models.DashboardVersion{
|
||||
DashboardId: dash.Id,
|
||||
ParentVersion: dash.Version,
|
||||
RestoredFrom: cmd.RestoredFrom,
|
||||
Version: dash.Version,
|
||||
Created: time.Now(),
|
||||
CreatedBy: dash.UpdatedBy,
|
||||
Message: cmd.Message,
|
||||
Data: dash.Data,
|
||||
}
|
||||
|
||||
if affectedRows, err := sess.Insert(dashVersion); err != nil {
|
||||
return err
|
||||
} else if affectedRows == 0 {
|
||||
return models.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return dash
|
||||
}
|
||||
26
pkg/services/dashboardversion/dashvertest/fake.go
Normal file
26
pkg/services/dashboardversion/dashvertest/fake.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package dashvertest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
)
|
||||
|
||||
type FakeDashboardVersionService struct {
|
||||
ExpectedDashboardVersion *dashver.DashboardVersion
|
||||
ExpectedDashboardVersions []*dashver.DashboardVersion
|
||||
counter int
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
func NewDashboardVersionServiceFake() *FakeDashboardVersionService {
|
||||
return &FakeDashboardVersionService{}
|
||||
}
|
||||
|
||||
func (f *FakeDashboardVersionService) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
if len(f.ExpectedDashboardVersions) == 0 {
|
||||
return f.ExpectedDashboardVersion, f.ExpectedError
|
||||
}
|
||||
f.counter++
|
||||
return f.ExpectedDashboardVersions[f.counter-1], f.ExpectedError
|
||||
}
|
||||
27
pkg/services/dashboardversion/model.go
Normal file
27
pkg/services/dashboardversion/model.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package dashver
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
type DashboardVersion struct {
|
||||
ID int64 `json:"id"`
|
||||
DashboardID int64 `json:"dashboardId"`
|
||||
ParentVersion int `json:"parentVersion"`
|
||||
RestoredFrom int `json:"restoredFrom"`
|
||||
Version int `json:"version"`
|
||||
|
||||
Created time.Time `json:"created"`
|
||||
CreatedBy int64 `json:"createdBy"`
|
||||
|
||||
Message string `json:"message"`
|
||||
Data *simplejson.Json `json:"data"`
|
||||
}
|
||||
|
||||
type GetDashboardVersionQuery struct {
|
||||
DashboardID int64
|
||||
OrgID int64
|
||||
Version int
|
||||
}
|
||||
Reference in New Issue
Block a user