Chore: Move dashboard models to dashboard pkg (#61458)

* Copy dashboard models to dashboard pkg

* Use some models from current pkg instead of models

* Adjust api pkg

* Adjust pkg services

* Fix lint
This commit is contained in:
idafurjes
2023-01-16 16:33:55 +01:00
committed by GitHub
parent 07bbc0716c
commit 7c2522c477
83 changed files with 1770 additions and 1444 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/libraryelements"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
@@ -28,7 +29,7 @@ func ProvideService(cfg *setting.Cfg, sqlStore db.DB, routeRegister routing.Rout
// Service is a service for operating on library panels.
type Service interface {
ConnectLibraryPanelsForDashboard(c context.Context, signedInUser *user.SignedInUser, dash *models.Dashboard) error
ConnectLibraryPanelsForDashboard(c context.Context, signedInUser *user.SignedInUser, dash *dashboards.Dashboard) error
ImportLibraryPanelsForDashboard(c context.Context, signedInUser *user.SignedInUser, libraryPanels *simplejson.Json, panels []interface{}, folderID int64) error
}
@@ -47,7 +48,7 @@ type LibraryPanelService struct {
}
// ConnectLibraryPanelsForDashboard loops through all panels in dashboard JSON and connects any library panels to the dashboard.
func (lps *LibraryPanelService) ConnectLibraryPanelsForDashboard(c context.Context, signedInUser *user.SignedInUser, dash *models.Dashboard) error {
func (lps *LibraryPanelService) ConnectLibraryPanelsForDashboard(c context.Context, signedInUser *user.SignedInUser, dash *dashboards.Dashboard) error {
panels := dash.Data.Get("panels").MustArray()
libraryPanels := make(map[string]string)
err := connectLibraryPanelsRecursively(c, panels, libraryPanels)
@@ -60,7 +61,7 @@ func (lps *LibraryPanelService) ConnectLibraryPanelsForDashboard(c context.Conte
elementUIDs = append(elementUIDs, libraryPanel)
}
return lps.LibraryElementService.ConnectElementsToDashboard(c, signedInUser, elementUIDs, dash.Id)
return lps.LibraryElementService.ConnectElementsToDashboard(c, signedInUser, elementUIDs, dash.ID)
}
func isLibraryPanelOrRow(panel *simplejson.Json, panelType string) bool {

View File

@@ -72,7 +72,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
},
}
dash := models.Dashboard{
dash := dashboards.Dashboard{
Title: "Testing ConnectLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
@@ -81,7 +81,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
err := sc.service.ConnectLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB)
require.NoError(t, err)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.Id)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.ID)
require.NoError(t, err)
require.Len(t, elements, 1)
require.Equal(t, sc.initialResult.Result.UID, elements[sc.initialResult.Result.UID].UID)
@@ -170,7 +170,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
},
}
dash := models.Dashboard{
dash := dashboards.Dashboard{
Title: "Testing ConnectLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
@@ -179,7 +179,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
err = sc.service.ConnectLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB)
require.NoError(t, err)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.Id)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.ID)
require.NoError(t, err)
require.Len(t, elements, 2)
require.Equal(t, sc.initialResult.Result.UID, elements[sc.initialResult.Result.UID].UID)
@@ -216,7 +216,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
},
}
dash := models.Dashboard{
dash := dashboards.Dashboard{
Title: "Testing ConnectLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
@@ -272,12 +272,12 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
}
dash := models.Dashboard{
dash := dashboards.Dashboard{
Title: "Testing ConnectLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
err = sc.elementService.ConnectElementsToDashboard(sc.ctx, sc.user, []string{sc.initialResult.Result.UID}, dashInDB.Id)
err = sc.elementService.ConnectElementsToDashboard(sc.ctx, sc.user, []string{sc.initialResult.Result.UID}, dashInDB.ID)
require.NoError(t, err)
panelJSON := []interface{}{
@@ -310,7 +310,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
err = sc.service.ConnectLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB)
require.NoError(t, err)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.Id)
elements, err := sc.elementService.GetElementsForDashboard(sc.ctx, dashInDB.ID)
require.NoError(t, err)
require.Len(t, elements, 1)
require.Equal(t, sc.initialResult.Result.UID, elements[sc.initialResult.Result.UID].UID)
@@ -684,12 +684,12 @@ func getExpected(t *testing.T, res libraryelements.LibraryElementDTO, UID string
}
}
func createDashboard(t *testing.T, sqlStore db.DB, user *user.SignedInUser, dash *models.Dashboard, folderID int64) *models.Dashboard {
dash.FolderId = folderID
func createDashboard(t *testing.T, sqlStore db.DB, user *user.SignedInUser, dash *dashboards.Dashboard, folderID int64) *dashboards.Dashboard {
dash.FolderID = folderID
dashItem := &dashboards.SaveDashboardDTO{
Dashboard: dash,
Message: "",
OrgId: user.OrgID,
OrgID: user.OrgID,
User: user,
Overwrite: false,
}
@@ -767,11 +767,11 @@ func scenarioWithLibraryPanel(t *testing.T, desc string, fn func(t *testing.T, s
store := dbtest.NewFakeDB()
dashSvc := dashboards.NewFakeDashboardService(t)
dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*models.GetDashboardQuery")).Run(func(args mock.Arguments) {
q := args.Get(1).(*models.GetDashboardQuery)
q.Result = &models.Dashboard{
Id: q.Id,
Uid: q.Uid,
dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardQuery")).Run(func(args mock.Arguments) {
q := args.Get(1).(*dashboards.GetDashboardQuery)
q.Result = &dashboards.Dashboard{
ID: q.ID,
UID: q.UID,
}
}).Return(nil)
guardian.InitLegacyGuardian(store, dashSvc, &teamtest.FakeService{})