DashboardService: Consolidate constructors (#31886)
* DashboardService: Consolidate constructors Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/gtime"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/tsdbifaces"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
@@ -16,14 +15,14 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
// DashboardService service for operating on dashboards
|
||||
// DashboardService is a service for operating on dashboards.
|
||||
type DashboardService interface {
|
||||
SaveDashboard(dto *SaveDashboardDTO, allowUiUpdate bool) (*models.Dashboard, error)
|
||||
ImportDashboard(dto *SaveDashboardDTO) (*models.Dashboard, error)
|
||||
DeleteDashboard(dashboardId int64, orgId int64) error
|
||||
}
|
||||
|
||||
// DashboardProvisioningService service for operating on provisioned dashboards
|
||||
// DashboardProvisioningService is a service for operating on provisioned dashboards.
|
||||
type DashboardProvisioningService interface {
|
||||
SaveProvisionedDashboard(dto *SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error)
|
||||
SaveFolderForProvisionedDashboards(*SaveDashboardDTO) (*models.Dashboard, error)
|
||||
@@ -33,19 +32,16 @@ type DashboardProvisioningService interface {
|
||||
DeleteProvisionedDashboard(dashboardId int64, orgId int64) error
|
||||
}
|
||||
|
||||
// NewService factory for creating a new dashboard service
|
||||
var NewService = func(reqHandler tsdbifaces.RequestHandler) DashboardService {
|
||||
// NewService is a factory for creating a new dashboard service.
|
||||
var NewService = func() DashboardService {
|
||||
return &dashboardServiceImpl{
|
||||
log: log.New("dashboard-service"),
|
||||
reqHandler: reqHandler,
|
||||
log: log.New("dashboard-service"),
|
||||
}
|
||||
}
|
||||
|
||||
// NewProvisioningService factory for creating a new dashboard provisioning service
|
||||
// NewProvisioningService is a factory for creating a new dashboard provisioning service.
|
||||
var NewProvisioningService = func() DashboardProvisioningService {
|
||||
return &dashboardServiceImpl{
|
||||
log: log.New("dashboard-provisioning-service"),
|
||||
}
|
||||
return NewService().(*dashboardServiceImpl)
|
||||
}
|
||||
|
||||
type SaveDashboardDTO struct {
|
||||
@@ -58,10 +54,9 @@ type SaveDashboardDTO struct {
|
||||
}
|
||||
|
||||
type dashboardServiceImpl struct {
|
||||
orgId int64
|
||||
user *models.SignedInUser
|
||||
log log.Logger
|
||||
reqHandler tsdbifaces.RequestHandler
|
||||
orgId int64
|
||||
user *models.SignedInUser
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (dr *dashboardServiceImpl) GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error) {
|
||||
@@ -359,6 +354,8 @@ func (dr *dashboardServiceImpl) UnprovisionDashboard(dashboardId int64) error {
|
||||
}
|
||||
|
||||
type FakeDashboardService struct {
|
||||
DashboardService
|
||||
|
||||
SaveDashboardResult *models.Dashboard
|
||||
SaveDashboardError error
|
||||
SavedDashboards []*SaveDashboardDTO
|
||||
@@ -389,7 +386,7 @@ func (s *FakeDashboardService) DeleteDashboard(dashboardId int64, orgId int64) e
|
||||
}
|
||||
|
||||
func MockDashboardService(mock *FakeDashboardService) {
|
||||
NewService = func(tsdbifaces.RequestHandler) DashboardService {
|
||||
NewService = func() DashboardService {
|
||||
return mock
|
||||
}
|
||||
}
|
||||
|
||||
@@ -763,7 +763,7 @@ func createDashboard(t *testing.T, user models.SignedInUser, title string, folde
|
||||
return nil
|
||||
})
|
||||
|
||||
dashboard, err := dashboards.NewService(nil).SaveDashboard(dashItem, true)
|
||||
dashboard, err := dashboards.NewService().SaveDashboard(dashItem, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
return dashboard
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -23,18 +25,11 @@ func TestProvisionedSymlinkedFolder(t *testing.T) {
|
||||
}
|
||||
|
||||
reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
|
||||
if err != nil {
|
||||
t.Error("expected err to be nil")
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
want, err := filepath.Abs(containingID)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("expected err to be nil")
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
resolvedPath := reader.resolvedPath()
|
||||
if resolvedPath != want {
|
||||
t.Errorf("got %s want %s", resolvedPath, want)
|
||||
}
|
||||
assert.Equal(t, want, resolvedPath)
|
||||
}
|
||||
|
||||
@@ -90,6 +90,9 @@ func TestDashboardFileReader(t *testing.T) {
|
||||
Convey("Dashboard file reader", t, func() {
|
||||
bus.ClearBusHandlers()
|
||||
origNewDashboardProvisioningService := dashboards.NewProvisioningService
|
||||
Reset(func() {
|
||||
dashboards.NewProvisioningService = origNewDashboardProvisioningService
|
||||
})
|
||||
fakeService = mockDashboardProvisioningService()
|
||||
|
||||
bus.AddHandler("test", mockGetDashboardQuery)
|
||||
@@ -356,10 +359,6 @@ func TestDashboardFileReader(t *testing.T) {
|
||||
So(fakeService.inserted[0].Dashboard.Id, ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
dashboards.NewProvisioningService = origNewDashboardProvisioningService
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -403,6 +402,8 @@ func mockDashboardProvisioningService() *fakeDashboardProvisioningService {
|
||||
}
|
||||
|
||||
type fakeDashboardProvisioningService struct {
|
||||
dashboards.DashboardProvisioningService
|
||||
|
||||
inserted []*dashboards.SaveDashboardDTO
|
||||
provisioned map[string][]*models.DashboardProvisioning
|
||||
getDashboard []*models.Dashboard
|
||||
@@ -416,7 +417,8 @@ func (s *fakeDashboardProvisioningService) GetProvisionedDashboardData(name stri
|
||||
return s.provisioned[name], nil
|
||||
}
|
||||
|
||||
func (s *fakeDashboardProvisioningService) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
|
||||
func (s *fakeDashboardProvisioningService) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO,
|
||||
provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
|
||||
// Copy the structs as we need to change them but do not want to alter outside world.
|
||||
var copyProvisioning = &models.DashboardProvisioning{}
|
||||
*copyProvisioning = *provisioning
|
||||
|
||||
@@ -967,7 +967,7 @@ func callSaveWithResult(t *testing.T, cmd models.SaveDashboardCommand) *models.D
|
||||
t.Helper()
|
||||
|
||||
dto := toSaveDashboardDto(cmd)
|
||||
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
|
||||
res, err := dashboards.NewService().SaveDashboard(&dto, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
return res
|
||||
@@ -975,7 +975,7 @@ func callSaveWithResult(t *testing.T, cmd models.SaveDashboardCommand) *models.D
|
||||
|
||||
func callSaveWithError(cmd models.SaveDashboardCommand) error {
|
||||
dto := toSaveDashboardDto(cmd)
|
||||
_, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
|
||||
_, err := dashboards.NewService().SaveDashboard(&dto, false)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1001,7 +1001,7 @@ func saveTestDashboard(t *testing.T, title string, orgId int64, folderId int64)
|
||||
},
|
||||
}
|
||||
|
||||
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
|
||||
res, err := dashboards.NewService().SaveDashboard(&dto, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
return res
|
||||
@@ -1028,7 +1028,7 @@ func saveTestFolder(t *testing.T, title string, orgId int64) *models.Dashboard {
|
||||
},
|
||||
}
|
||||
|
||||
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
|
||||
res, err := dashboards.NewService().SaveDashboard(&dto, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user