diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index b80cf4cff70..3a42b33c5f3 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -116,12 +116,12 @@ func (hs *HTTPServer) GetAlerts(c *contextmodel.ReqContext) response.Response { Permission: dashboards.PERMISSION_VIEW, } - err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) + hits, err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) if err != nil { return response.Error(500, "List alerts failed", err) } - for _, d := range searchQuery.Result { + for _, d := range hits { if d.Type == model.DashHitDB && d.ID > 0 { dashboardIDs = append(dashboardIDs, d.ID) } diff --git a/pkg/api/common_test.go b/pkg/api/common_test.go index 63fd69a80bd..6919bad8576 100644 --- a/pkg/api/common_test.go +++ b/pkg/api/common_test.go @@ -320,9 +320,8 @@ type setUpConf struct { type mockSearchService struct{ ExpectedResult model.HitList } -func (mss *mockSearchService) SearchHandler(_ context.Context, q *search.Query) error { - q.Result = mss.ExpectedResult - return nil +func (mss *mockSearchService) SearchHandler(_ context.Context, q *search.Query) (model.HitList, error) { + return mss.ExpectedResult, nil } func (mss *mockSearchService) SortOptions() []model.SortOption { return nil } diff --git a/pkg/api/folder.go b/pkg/api/folder.go index 57e7881dc49..ab233d61f6d 100644 --- a/pkg/api/folder.go +++ b/pkg/api/folder.go @@ -350,13 +350,14 @@ func (hs *HTTPServer) searchFolders(c *contextmodel.ReqContext) ([]*folder.Folde Page: c.QueryInt64("page"), } - if err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery); err != nil { + hits, err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) + if err != nil { return nil, err } folders := make([]*folder.Folder, 0) - for _, hit := range searchQuery.Result { + for _, hit := range hits { folders = append(folders, &folder.Folder{ ID: hit.ID, UID: hit.UID, diff --git a/pkg/api/playlist_play.go b/pkg/api/playlist_play.go index c06649b5b54..5a33185d855 100644 --- a/pkg/api/playlist_play.go +++ b/pkg/api/playlist_play.go @@ -52,8 +52,9 @@ func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64, OrgId: orgID, } - if err := hs.SearchService.SearchHandler(ctx, &searchQuery); err == nil { - for _, item := range searchQuery.Result { + hits, err := hs.SearchService.SearchHandler(ctx, &searchQuery) + if err == nil { + for _, item := range hits { result = append(result, dtos.PlaylistDashboard{ Id: item.ID, Slug: item.Slug, diff --git a/pkg/api/search.go b/pkg/api/search.go index 0eb478fc05a..3c7cd61ae51 100644 --- a/pkg/api/search.go +++ b/pkg/api/search.go @@ -81,7 +81,7 @@ func (hs *HTTPServer) Search(c *contextmodel.ReqContext) response.Response { Sort: sort, } - err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) + hits, err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) if err != nil { return response.Error(500, "Search failed", err) } @@ -89,10 +89,10 @@ func (hs *HTTPServer) Search(c *contextmodel.ReqContext) response.Response { defer c.TimeRequest(metrics.MApiDashboardSearch) if !c.QueryBool("accesscontrol") { - return response.JSON(http.StatusOK, searchQuery.Result) + return response.JSON(http.StatusOK, hits) } - return hs.searchHitsWithMetadata(c, searchQuery.Result) + return hs.searchHitsWithMetadata(c, hits) } func (hs *HTTPServer) searchHitsWithMetadata(c *contextmodel.ReqContext, hits model.HitList) response.Response { diff --git a/pkg/services/search/service.go b/pkg/services/search/service.go index bed792f18b4..838f61e6e59 100644 --- a/pkg/services/search/service.go +++ b/pkg/services/search/service.go @@ -40,12 +40,10 @@ type Query struct { FolderIds []int64 Permission dashboards.PermissionType Sort string - - Result model.HitList } type Service interface { - SearchHandler(context.Context, *Query) error + SearchHandler(context.Context, *Query) (model.HitList, error) SortOptions() []model.SortOption } @@ -57,19 +55,18 @@ type SearchService struct { dashboardService dashboards.DashboardService } -func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error { +func (s *SearchService) SearchHandler(ctx context.Context, query *Query) (model.HitList, error) { starredQuery := star.GetUserStarsQuery{ UserID: query.SignedInUser.UserID, } staredDashIDs, err := s.starService.GetByUser(ctx, &starredQuery) if err != nil { - return err + return nil, err } // No starred dashboards will be found if query.IsStarred && len(staredDashIDs.UserStars) == 0 { - query.Result = model.HitList{} - return nil + return model.HitList{}, nil } // filter by starred dashboard IDs when starred dashboards are requested and no UID or ID filters are specified to improve query performance @@ -98,7 +95,7 @@ func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error { hits, err := s.dashboardService.SearchDashboards(ctx, &dashboardQuery) if err != nil { - return err + return nil, err } if query.Sort == "" { @@ -114,17 +111,15 @@ func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error { // filter for starred dashboards if requested if !query.IsStarred { - query.Result = hits - } else { - query.Result = model.HitList{} - for _, dashboard := range hits { - if dashboard.IsStarred { - query.Result = append(query.Result, dashboard) - } + return hits, nil + } + result := model.HitList{} + for _, dashboard := range hits { + if dashboard.IsStarred { + result = append(result, dashboard) } } - - return nil + return result, nil } func sortedHits(unsorted model.HitList) model.HitList { diff --git a/pkg/services/search/service_test.go b/pkg/services/search/service_test.go index a85aa857f81..58177e5e1a9 100644 --- a/pkg/services/search/service_test.go +++ b/pkg/services/search/service_test.go @@ -44,20 +44,20 @@ func TestSearch_SortedResults(t *testing.T) { }, } - err := svc.SearchHandler(context.Background(), query) + hits, err := svc.SearchHandler(context.Background(), query) require.Nil(t, err) // Assert results are sorted. - assert.Equal(t, "FOLDER", query.Result[0].Title) - assert.Equal(t, "AABB", query.Result[1].Title) - assert.Equal(t, "BBAA", query.Result[2].Title) - assert.Equal(t, "bbAAa", query.Result[3].Title) - assert.Equal(t, "CCAA", query.Result[4].Title) + assert.Equal(t, "FOLDER", hits[0].Title) + assert.Equal(t, "AABB", hits[1].Title) + assert.Equal(t, "BBAA", hits[2].Title) + assert.Equal(t, "bbAAa", hits[3].Title) + assert.Equal(t, "CCAA", hits[4].Title) // Assert tags are sorted. - assert.Equal(t, "AA", query.Result[3].Tags[0]) - assert.Equal(t, "BB", query.Result[3].Tags[1]) - assert.Equal(t, "EE", query.Result[3].Tags[2]) + assert.Equal(t, "AA", hits[3].Tags[0]) + assert.Equal(t, "BB", hits[3].Tags[1]) + assert.Equal(t, "EE", hits[3].Tags[2]) } func TestSearch_StarredResults(t *testing.T) { @@ -84,11 +84,11 @@ func TestSearch_StarredResults(t *testing.T) { SignedInUser: &user.SignedInUser{}, } - err := svc.SearchHandler(context.Background(), query) + hits, err := svc.SearchHandler(context.Background(), query) require.Nil(t, err) // Assert only starred dashboards are returned - assert.Equal(t, 2, query.Result.Len()) - assert.Equal(t, "A", query.Result[0].Title) - assert.Equal(t, "C", query.Result[1].Title) + assert.Equal(t, 2, hits.Len()) + assert.Equal(t, "A", hits[0].Title) + assert.Equal(t, "C", hits[1].Title) }