mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Remove result field from search (#65583)
remove result field from search
This commit is contained in:
parent
a02091cc19
commit
f19569d61d
@ -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)
|
||||
}
|
||||
|
@ -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 }
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user