From 7c87ff1b84de579e4373ecf59e7e00b9ad951f76 Mon Sep 17 00:00:00 2001 From: Leonor Oliveira <9090754+leonorfmartins@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:58:36 +0100 Subject: [PATCH] Folders: Use folder service to count library panels (#98802) * Use folder service to count library panels --------- Co-authored-by: suntala --- pkg/services/librarypanels/librarypanels.go | 15 ++++++--------- pkg/services/librarypanels/librarypanels_test.go | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pkg/services/librarypanels/librarypanels.go b/pkg/services/librarypanels/librarypanels.go index c7b2802edf9..e029925ed25 100644 --- a/pkg/services/librarypanels/librarypanels.go +++ b/pkg/services/librarypanels/librarypanels.go @@ -195,19 +195,16 @@ func (lps LibraryPanelService) CountInFolders(ctx context.Context, orgID int64, if len(folderUIDs) == 0 { return 0, nil } + var count int64 return count, lps.SQLStore.WithDbSession(ctx, func(sess *db.Session) error { metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryPanels).Inc() - // the sequential IDs for the respective entries of dashboard and folder tables are different, - // so we need to get the folder ID from the dashboard table - // TODO: In the future, we should consider adding a folder UID column to the library_element table - // and use that instead of the folder ID. - s := fmt.Sprintf(`SELECT COUNT(*) FROM library_element - WHERE org_id = ? AND folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid IN (%s)) AND kind = ?`, strings.Repeat("?,", len(folderUIDs)-1)+"?") + s := fmt.Sprintf(`SELECT COUNT(*) FROM library_element WHERE org_id = ? AND folder_uid IN (%s) AND kind = ?`, strings.Repeat("?,", len(folderUIDs)-1)+"?") + args := make([]interface{}, 0, len(folderUIDs)+2) - args = append(args, orgID, orgID) - for _, folderUID := range folderUIDs { - args = append(args, folderUID) + args = append(args, orgID) + for _, uid := range folderUIDs { + args = append(args, uid) } args = append(args, int64(model.PanelElement)) _, err := sess.SQL(s, args...).Get(&count) diff --git a/pkg/services/librarypanels/librarypanels_test.go b/pkg/services/librarypanels/librarypanels_test.go index a820b9659f3..413800b2bb2 100644 --- a/pkg/services/librarypanels/librarypanels_test.go +++ b/pkg/services/librarypanels/librarypanels_test.go @@ -732,7 +732,7 @@ func createDashboard(t *testing.T, sqlStore db.DB, user *user.SignedInUser, dash dashPermissionService.On("SetPermissions", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]accesscontrol.ResourcePermission{}, nil) service, err := dashboardservice.ProvideDashboardServiceImpl( cfg, dashboardStore, folderStore, - featuremgmt.WithFeatures(), acmock.NewMockedPermissionsService(), dashPermissionService, ac, + features, acmock.NewMockedPermissionsService(), dashPermissionService, ac, foldertest.NewFakeService(), folder.NewFakeStore(), nil, nil, nil, nil, quotaService, nil, )