Skip to content

Commit b573b19

Browse files
authored
Chore: Remove dashboards from models pkg (grafana#61578)
* Copy dashboard models to dashboard pkg * Use some models from current pkg instead of models * Adjust api pkg * Adjust pkg services * Fix lint * Chore: Remove dashboards models * Remove dashboards from models pkg * Fix lint in tests * Fix lint in tests 2 * Fix for import in auth * Remove newline * Revert unused fix
1 parent db0be6b commit b573b19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+497
-822
lines changed

pkg/api/alerting.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/grafana/grafana/pkg/api/response"
1212
"github.com/grafana/grafana/pkg/models"
1313
"github.com/grafana/grafana/pkg/services/alerting"
14+
"github.com/grafana/grafana/pkg/services/dashboards"
1415
"github.com/grafana/grafana/pkg/services/datasources"
1516
"github.com/grafana/grafana/pkg/services/guardian"
1617
"github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config"
@@ -147,7 +148,7 @@ func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
147148
}
148149

149150
for _, alert := range query.Result {
150-
alert.Url = models.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
151+
alert.Url = dashboards.GetDashboardURL(alert.DashboardUid, alert.DashboardSlug)
151152
}
152153

153154
return response.JSON(http.StatusOK, query.Result)

pkg/api/dashboard.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func dashboardGuardianResponse(err error) response.Response {
6464
// 401: unauthorisedError
6565
// 500: internalServerError
6666
func (hs *HTTPServer) TrimDashboard(c *models.ReqContext) response.Response {
67-
cmd := models.TrimDashboardCommand{}
67+
cmd := dashboards.TrimDashboardCommand{}
6868
if err := web.Bind(c.Req, &cmd); err != nil {
6969
return response.Error(http.StatusBadRequest, "bad request data", err)
7070
}
@@ -169,7 +169,7 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
169169
meta := dtos.DashboardMeta{
170170
IsStarred: isStarred,
171171
Slug: dash.Slug,
172-
Type: models.DashTypeDB,
172+
Type: dashboards.DashTypeDB,
173173
CanStar: c.IsSignedIn,
174174
CanSave: canSave,
175175
CanEdit: canEdit,
@@ -217,7 +217,7 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
217217

218218
meta.ProvisionedExternalId, err = filepath.Rel(
219219
hs.ProvisioningService.GetDashboardProvisionerResolvedPath(provisioningData.Name),
220-
provisioningData.ExternalId,
220+
provisioningData.ExternalID,
221221
)
222222
if err != nil {
223223
// Not sure when this could happen so not sure how to better handle this. Right now ProvisionedExternalId
@@ -429,7 +429,7 @@ func (hs *HTTPServer) postDashboard(c *models.ReqContext, cmd dashboards.SaveDas
429429
}
430430
}
431431

432-
var provisioningData *models.DashboardProvisioning
432+
var provisioningData *dashboards.DashboardProvisioning
433433
if dash.ID != 0 {
434434
data, err := hs.dashboardProvisioningService.GetProvisionedDashboardDataByDashboardID(c.Req.Context(), dash.ID)
435435
if err != nil {
@@ -536,7 +536,7 @@ func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) response.Response {
536536
slugQuery := dashboards.GetDashboardRefByIDQuery{ID: preference.HomeDashboardID}
537537
err := hs.DashboardService.GetDashboardUIDByID(c.Req.Context(), &slugQuery)
538538
if err == nil {
539-
url := models.GetDashboardUrl(slugQuery.Result.UID, slugQuery.Result.Slug)
539+
url := dashboards.GetDashboardURL(slugQuery.Result.UID, slugQuery.Result.Slug)
540540
dashRedirect := dtos.DashboardRedirect{RedirectUri: url}
541541
return response.JSON(http.StatusOK, &dashRedirect)
542542
}
@@ -786,7 +786,7 @@ func (hs *HTTPServer) GetDashboardVersion(c *models.ReqContext) response.Respons
786786
// 403: forbiddenError
787787
// 500: internalServerError
788788
func (hs *HTTPServer) ValidateDashboard(c *models.ReqContext) response.Response {
789-
cmd := models.ValidateDashboardCommand{}
789+
cmd := dashboards.ValidateDashboardCommand{}
790790

791791
if err := web.Bind(c.Req, &cmd); err != nil {
792792
return response.Error(http.StatusBadRequest, "bad request data", err)
@@ -1018,7 +1018,7 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext) response.Res
10181018
// 401: unauthorisedError
10191019
// 500: internalServerError
10201020
func (hs *HTTPServer) GetDashboardTags(c *models.ReqContext) {
1021-
query := models.GetDashboardTagsQuery{OrgId: c.OrgID}
1021+
query := dashboards.GetDashboardTagsQuery{OrgID: c.OrgID}
10221022
err := hs.DashboardService.GetDashboardTags(c.Req.Context(), &query)
10231023
if err != nil {
10241024
c.JsonApiErr(500, "Failed to get tags from database", err)
@@ -1137,7 +1137,7 @@ type DeleteDashboardByUIDParams struct {
11371137
type PostDashboardParams struct {
11381138
// in:body
11391139
// required:true
1140-
Body models.SaveDashboardCommand
1140+
Body dashboards.SaveDashboardCommand
11411141
}
11421142

11431143
// swagger:parameters calculateDashboardDiff
@@ -1160,7 +1160,7 @@ type CalcDashboardDiffParams struct {
11601160
type TrimDashboardParams struct {
11611161
// in:body
11621162
// required:true
1163-
Body models.TrimDashboardCommand
1163+
Body dashboards.TrimDashboardCommand
11641164
}
11651165

11661166
// swagger:response dashboardResponse
@@ -1249,7 +1249,7 @@ type GetHomeDashboardResponse struct {
12491249
// swagger:response getDashboardsTagsResponse
12501250
type DashboardsTagsResponse struct {
12511251
// in: body
1252-
Body []*models.DashboardTagCloudItem `json:"body"`
1252+
Body []*dashboards.DashboardTagCloudItem `json:"body"`
12531253
}
12541254

12551255
// Get home dashboard response.

pkg/api/dashboard_permission.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/grafana/grafana/pkg/api/response"
1212
"github.com/grafana/grafana/pkg/models"
1313
"github.com/grafana/grafana/pkg/services/accesscontrol"
14+
"github.com/grafana/grafana/pkg/services/dashboards"
1415
"github.com/grafana/grafana/pkg/services/guardian"
1516
"github.com/grafana/grafana/pkg/web"
1617
)
@@ -82,7 +83,7 @@ func (hs *HTTPServer) GetDashboardPermissionList(c *models.ReqContext) response.
8283
perm.TeamAvatarUrl = dtos.GetGravatarUrlWithDefault(perm.TeamEmail, perm.Team)
8384
}
8485
if perm.Slug != "" {
85-
perm.Url = models.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
86+
perm.Url = dashboards.GetDashboardFolderURL(perm.IsFolder, perm.Uid, perm.Slug)
8687
}
8788

8889
filteredACLs = append(filteredACLs, perm)

pkg/api/dashboard_snapshot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (hs *HTTPServer) GetDashboardSnapshot(c *models.ReqContext) response.Respon
216216
dto := dtos.DashboardFullWithMeta{
217217
Dashboard: snapshot.Dashboard,
218218
Meta: dtos.DashboardMeta{
219-
Type: models.DashTypeSnapshot,
219+
Type: dashboards.DashTypeSnapshot,
220220
IsSnapshot: true,
221221
Created: snapshot.Created,
222222
Expires: snapshot.Expires,

pkg/api/dashboard_test.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
578578
})
579579

580580
t.Run("Given two dashboards with the same title in different folders", func(t *testing.T) {
581-
dashOne := models.NewDashboard("dash")
582-
dashOne.Id = 2
583-
dashOne.FolderId = 1
581+
dashOne := dashboards.NewDashboard("dash")
582+
dashOne.ID = 2
583+
dashOne.FolderID = 1
584584
dashOne.HasACL = false
585585

586-
dashTwo := models.NewDashboard("dash")
587-
dashTwo.Id = 4
588-
dashTwo.FolderId = 3
586+
dashTwo := dashboards.NewDashboard("dash")
587+
dashTwo.ID = 4
588+
dashTwo.FolderID = 3
589589
dashTwo.HasACL = false
590590
})
591591

@@ -597,14 +597,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
597597
const folderID int64 = 3
598598
const dashID int64 = 2
599599

600-
cmd := models.SaveDashboardCommand{
601-
OrgId: 1,
602-
UserId: 5,
600+
cmd := dashboards.SaveDashboardCommand{
601+
OrgID: 1,
602+
UserID: 5,
603603
Dashboard: simplejson.NewFromAny(map[string]interface{}{
604604
"title": "Dash",
605605
}),
606606
Overwrite: true,
607-
FolderId: folderID,
607+
FolderID: folderID,
608608
IsFolder: false,
609609
Message: "msg",
610610
}
@@ -629,14 +629,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
629629
const folderUid string = "folderUID"
630630
const dashID int64 = 2
631631

632-
cmd := models.SaveDashboardCommand{
633-
OrgId: 1,
634-
UserId: 5,
632+
cmd := dashboards.SaveDashboardCommand{
633+
OrgID: 1,
634+
UserID: 5,
635635
Dashboard: simplejson.NewFromAny(map[string]interface{}{
636636
"title": "Dash",
637637
}),
638638
Overwrite: true,
639-
FolderUid: folderUid,
639+
FolderUID: folderUid,
640640
IsFolder: false,
641641
Message: "msg",
642642
}
@@ -662,14 +662,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
662662
})
663663

664664
t.Run("Given a request with incorrect folder uid for creating a dashboard with", func(t *testing.T) {
665-
cmd := models.SaveDashboardCommand{
666-
OrgId: 1,
667-
UserId: 5,
665+
cmd := dashboards.SaveDashboardCommand{
666+
OrgID: 1,
667+
UserID: 5,
668668
Dashboard: simplejson.NewFromAny(map[string]interface{}{
669669
"title": "Dash",
670670
}),
671671
Overwrite: true,
672-
FolderUid: "folderUID",
672+
FolderUID: "folderUID",
673673
IsFolder: false,
674674
Message: "msg",
675675
}
@@ -712,8 +712,8 @@ func TestDashboardAPIEndpoint(t *testing.T) {
712712
{SaveError: dashboards.UpdatePluginDashboardError{PluginId: "plug"}, ExpectedStatusCode: 412},
713713
}
714714

715-
cmd := models.SaveDashboardCommand{
716-
OrgId: 1,
715+
cmd := dashboards.SaveDashboardCommand{
716+
OrgID: 1,
717717
Dashboard: simplejson.NewFromAny(map[string]interface{}{
718718
"title": "",
719719
}),
@@ -736,7 +736,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
736736
sqlmock := mockstore.SQLStoreMock{}
737737

738738
t.Run("When an invalid dashboard json is posted", func(t *testing.T) {
739-
cmd := models.ValidateDashboardCommand{
739+
cmd := dashboards.ValidateDashboardCommand{
740740
Dashboard: "{\"hello\": \"world\"}",
741741
}
742742

@@ -752,7 +752,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
752752
})
753753

754754
t.Run("When a dashboard with a too-low schema version is posted", func(t *testing.T) {
755-
cmd := models.ValidateDashboardCommand{
755+
cmd := dashboards.ValidateDashboardCommand{
756756
Dashboard: "{\"schemaVersion\": 1}",
757757
}
758758

@@ -771,7 +771,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
771771
devenvDashboard, readErr := os.ReadFile("../../devenv/dev-dashboards/home.json")
772772
assert.Empty(t, readErr)
773773

774-
cmd := models.ValidateDashboardCommand{
774+
cmd := dashboards.ValidateDashboardCommand{
775775
Dashboard: string(devenvDashboard),
776776
}
777777

@@ -930,7 +930,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
930930
t.Run("Given provisioned dashboard", func(t *testing.T) {
931931
mockSQLStore := mockstore.NewSQLStoreMock()
932932
dashboardStore := dashboards.NewFakeDashboardStore(t)
933-
dashboardStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(&models.DashboardProvisioning{ExternalId: "/dashboard1.json"}, nil).Once()
933+
dashboardStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(&dashboards.DashboardProvisioning{ExternalID: "/dashboard1.json"}, nil).Once()
934934

935935
teamService := &teamtest.FakeService{}
936936
dashboardService := dashboards.NewFakeDashboardService(t)
@@ -1086,7 +1086,7 @@ func callPostDashboardShouldReturnSuccess(sc *scenarioContext) {
10861086
assert.Equal(sc.t, 200, sc.resp.Code)
10871087
}
10881088

1089-
func postDashboardScenario(t *testing.T, desc string, url string, routePattern string, cmd models.SaveDashboardCommand, dashboardService dashboards.DashboardService, folderService folder.Service, fn scenarioFunc) {
1089+
func postDashboardScenario(t *testing.T, desc string, url string, routePattern string, cmd dashboards.SaveDashboardCommand, dashboardService dashboards.DashboardService, folderService folder.Service, fn scenarioFunc) {
10901090
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
10911091
cfg := setting.NewCfg()
10921092
hs := HTTPServer{
@@ -1109,7 +1109,7 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s
11091109
c.Req.Body = mockRequestBody(cmd)
11101110
c.Req.Header.Add("Content-Type", "application/json")
11111111
sc.context = c
1112-
sc.context.SignedInUser = &user.SignedInUser{OrgID: cmd.OrgId, UserID: cmd.UserId}
1112+
sc.context.SignedInUser = &user.SignedInUser{OrgID: cmd.OrgID, UserID: cmd.UserID}
11131113

11141114
return hs.PostDashboard(c)
11151115
})
@@ -1120,7 +1120,7 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s
11201120
})
11211121
}
11221122

1123-
func postValidateScenario(t *testing.T, desc string, url string, routePattern string, cmd models.ValidateDashboardCommand,
1123+
func postValidateScenario(t *testing.T, desc string, url string, routePattern string, cmd dashboards.ValidateDashboardCommand,
11241124
role org.RoleType, fn scenarioFunc, sqlmock db.DB) {
11251125
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
11261126
cfg := setting.NewCfg()
@@ -1250,7 +1250,7 @@ type mockDashboardProvisioningService struct {
12501250
}
12511251

12521252
func (s mockDashboardProvisioningService) GetProvisionedDashboardDataByDashboardID(ctx context.Context, dashboardID int64) (
1253-
*models.DashboardProvisioning, error) {
1253+
*dashboards.DashboardProvisioning, error) {
12541254
return nil, nil
12551255
}
12561256

pkg/api/folder_permission.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (hs *HTTPServer) GetFolderPermissionList(c *models.ReqContext) response.Res
6464
}
6565

6666
if perm.Slug != "" {
67-
perm.Url = models.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
67+
perm.Url = dashboards.GetDashboardFolderURL(perm.IsFolder, perm.Uid, perm.Slug)
6868
}
6969

7070
filteredACLs = append(filteredACLs, perm)

pkg/api/playlist_play.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/grafana/grafana/pkg/api/dtos"
1010
_ "github.com/grafana/grafana/pkg/infra/log"
11-
"github.com/grafana/grafana/pkg/models"
1211
"github.com/grafana/grafana/pkg/services/dashboards"
1312
"github.com/grafana/grafana/pkg/services/playlist"
1413
"github.com/grafana/grafana/pkg/services/search"
@@ -30,7 +29,7 @@ func (hs *HTTPServer) populateDashboardsByID(ctx context.Context, dashboardByIDs
3029
Slug: item.Slug,
3130
Title: item.Title,
3231
Uri: "db/" + item.Slug,
33-
Url: models.GetDashboardUrl(item.UID, item.Slug),
32+
Url: dashboards.GetDashboardURL(item.UID, item.Slug),
3433
Order: dashboardIDOrder[item.ID],
3534
})
3635
}

0 commit comments

Comments
 (0)