Commit 60f86c3b by Marcus Olsson Committed by GitHub

Chore: Refactor GoConvey into stdlib for search service (#19765)

* Refactor GoConvey into stdlib

* Fix review comments
parent aa87f62c
package search
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestSearch(t *testing.T) {
Convey("Given search query", t, func() {
query := Query{Limit: 2000, SignedInUser: &m.SignedInUser{IsGrafanaAdmin: true}}
ss := &SearchService{}
bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error {
query.Result = HitList{
&Hit{Id: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
&Hit{Id: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
&Hit{Id: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 17, Title: "FOLDER", Type: "dash-folder"},
}
return nil
})
bus.AddHandler("test", func(query *m.GetUserStarsQuery) error {
query.Result = map[int64]bool{10: true, 12: true}
return nil
})
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
query.Result = &m.SignedInUser{IsGrafanaAdmin: true}
return nil
})
Convey("That is empty", func() {
err := ss.searchHandler(&query)
So(err, ShouldBeNil)
Convey("should return sorted results", func() {
So(query.Result[0].Title, ShouldEqual, "FOLDER")
So(query.Result[1].Title, ShouldEqual, "AABB")
So(query.Result[2].Title, ShouldEqual, "BBAA")
So(query.Result[3].Title, ShouldEqual, "bbAAa")
So(query.Result[4].Title, ShouldEqual, "CCAA")
})
Convey("should return sorted tags", func() {
So(query.Result[3].Tags[0], ShouldEqual, "AA")
So(query.Result[3].Tags[1], ShouldEqual, "BB")
So(query.Result[3].Tags[2], ShouldEqual, "EE")
})
})
})
}
package search package search
import "strings" import "strings"
import "github.com/grafana/grafana/pkg/models"
type HitType string type HitType string
...@@ -42,35 +41,3 @@ func (s HitList) Less(i, j int) bool { ...@@ -42,35 +41,3 @@ func (s HitList) Less(i, j int) bool {
return strings.ToLower(s[i].Title) < strings.ToLower(s[j].Title) return strings.ToLower(s[i].Title) < strings.ToLower(s[j].Title)
} }
type Query struct {
Title string
Tags []string
OrgId int64
SignedInUser *models.SignedInUser
Limit int64
Page int64
IsStarred bool
Type string
DashboardIds []int64
FolderIds []int64
Permission models.PermissionType
Result HitList
}
type FindPersistedDashboardsQuery struct {
Title string
OrgId int64
SignedInUser *models.SignedInUser
IsStarred bool
DashboardIds []int64
Type string
FolderIds []int64
Tags []string
Limit int64
Page int64
Permission models.PermissionType
Result HitList
}
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"sort" "sort"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry" "github.com/grafana/grafana/pkg/registry"
) )
...@@ -12,17 +12,49 @@ func init() { ...@@ -12,17 +12,49 @@ func init() {
registry.RegisterService(&SearchService{}) registry.RegisterService(&SearchService{})
} }
type Query struct {
Title string
Tags []string
OrgId int64
SignedInUser *models.SignedInUser
Limit int64
Page int64
IsStarred bool
Type string
DashboardIds []int64
FolderIds []int64
Permission models.PermissionType
Result HitList
}
type FindPersistedDashboardsQuery struct {
Title string
OrgId int64
SignedInUser *models.SignedInUser
IsStarred bool
DashboardIds []int64
Type string
FolderIds []int64
Tags []string
Limit int64
Page int64
Permission models.PermissionType
Result HitList
}
type SearchService struct { type SearchService struct {
Bus bus.Bus `inject:""` bus bus.Bus `inject:""`
} }
func (s *SearchService) Init() error { func (s *SearchService) Init() error {
s.Bus.AddHandler(s.searchHandler) s.bus.AddHandler(s.searchHandler)
return nil return nil
} }
func (s *SearchService) searchHandler(query *Query) error { func (s *SearchService) searchHandler(query *Query) error {
dashQuery := FindPersistedDashboardsQuery{ dashboardQuery := FindPersistedDashboardsQuery{
Title: query.Title, Title: query.Title,
SignedInUser: query.SignedInUser, SignedInUser: query.SignedInUser,
IsStarred: query.IsStarred, IsStarred: query.IsStarred,
...@@ -35,39 +67,46 @@ func (s *SearchService) searchHandler(query *Query) error { ...@@ -35,39 +67,46 @@ func (s *SearchService) searchHandler(query *Query) error {
Permission: query.Permission, Permission: query.Permission,
} }
if err := bus.Dispatch(&dashQuery); err != nil { if err := bus.Dispatch(&dashboardQuery); err != nil {
return err return err
} }
hits := sortedHits(dashboardQuery.Result)
if err := setStarredDashboards(query.SignedInUser.UserId, hits); err != nil {
return err
}
query.Result = hits
return nil
}
func sortedHits(unsorted HitList) HitList {
hits := make(HitList, 0) hits := make(HitList, 0)
hits = append(hits, dashQuery.Result...) hits = append(hits, unsorted...)
// sort main result array
sort.Sort(hits) sort.Sort(hits)
// sort tags
for _, hit := range hits { for _, hit := range hits {
sort.Strings(hit.Tags) sort.Strings(hit.Tags)
} }
// add isStarred info return hits
if err := setIsStarredFlagOnSearchResults(query.SignedInUser.UserId, hits); err != nil {
return err
}
query.Result = hits
return nil
} }
func setIsStarredFlagOnSearchResults(userId int64, hits []*Hit) error { func setStarredDashboards(userID int64, hits []*Hit) error {
query := m.GetUserStarsQuery{UserId: userId} query := models.GetUserStarsQuery{
UserId: userID,
}
if err := bus.Dispatch(&query); err != nil { if err := bus.Dispatch(&query); err != nil {
return err return err
} }
for _, dash := range hits { for _, dashboard := range hits {
if _, exists := query.Result[dash.Id]; exists { if _, ok := query.Result[dashboard.Id]; ok {
dash.IsStarred = true dashboard.IsStarred = true
} }
} }
......
package search
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSearch_SortedResults(t *testing.T) {
bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error {
query.Result = HitList{
&Hit{Id: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
&Hit{Id: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
&Hit{Id: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 17, Title: "FOLDER", Type: "dash-folder"},
}
return nil
})
bus.AddHandler("test", func(query *models.GetUserStarsQuery) error {
query.Result = map[int64]bool{10: true, 12: true}
return nil
})
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
query.Result = &models.SignedInUser{IsGrafanaAdmin: true}
return nil
})
svc := &SearchService{}
query := &Query{
Limit: 2000,
SignedInUser: &models.SignedInUser{
IsGrafanaAdmin: true,
},
}
err := svc.searchHandler(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 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])
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment