Commit 2e010b92 by Daniel Lee

dashboard: sort search with dash folder first

parent 9cbaf072
...@@ -16,10 +16,11 @@ func TestSearch(t *testing.T) { ...@@ -16,10 +16,11 @@ func TestSearch(t *testing.T) {
bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error { bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error {
query.Result = HitList{ query.Result = HitList{
&Hit{Id: 16, Title: "CCAA", Tags: []string{"BB", "AA"}}, &Hit{Id: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
&Hit{Id: 10, Title: "AABB", Tags: []string{"CC", "AA"}}, &Hit{Id: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
&Hit{Id: 15, Title: "BBAA", Tags: []string{"EE", "AA", "BB"}}, &Hit{Id: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 17, Title: "FOLDER", Dashboards: []Hit{ &Hit{Id: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&Hit{Id: 17, Title: "FOLDER", Type: "dash-folder", Dashboards: []Hit{
{Id: 18, Title: "ZZAA", Tags: []string{"ZZ"}}, {Id: 18, Title: "ZZAA", Tags: []string{"ZZ"}},
}}, }},
} }
...@@ -36,15 +37,17 @@ func TestSearch(t *testing.T) { ...@@ -36,15 +37,17 @@ func TestSearch(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
Convey("should return sorted results", func() { Convey("should return sorted results", func() {
So(query.Result[0].Title, ShouldEqual, "AABB") So(query.Result[0].Title, ShouldEqual, "FOLDER")
So(query.Result[1].Title, ShouldEqual, "BBAA") So(query.Result[1].Title, ShouldEqual, "AABB")
So(query.Result[2].Title, ShouldEqual, "CCAA") 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() { Convey("should return sorted tags", func() {
So(query.Result[1].Tags[0], ShouldEqual, "AA") So(query.Result[3].Tags[0], ShouldEqual, "AA")
So(query.Result[1].Tags[1], ShouldEqual, "BB") So(query.Result[3].Tags[1], ShouldEqual, "BB")
So(query.Result[1].Tags[2], ShouldEqual, "EE") So(query.Result[3].Tags[2], ShouldEqual, "EE")
}) })
}) })
...@@ -54,9 +57,9 @@ func TestSearch(t *testing.T) { ...@@ -54,9 +57,9 @@ func TestSearch(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
Convey("should return correct results", func() { Convey("should return correct results", func() {
So(len(query.Result), ShouldEqual, 2) So(len(query.Result), ShouldEqual, 3)
So(query.Result[0].Title, ShouldEqual, "BBAA") So(query.Result[0].Title, ShouldEqual, "BBAA")
So(query.Result[1].Title, ShouldEqual, "CCAA") So(query.Result[2].Title, ShouldEqual, "CCAA")
}) })
}) })
...@@ -67,8 +70,8 @@ func TestSearch(t *testing.T) { ...@@ -67,8 +70,8 @@ func TestSearch(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
Convey("should return correct results", func() { Convey("should return correct results", func() {
So(query.Result[3].Title, ShouldEqual, "FOLDER") So(query.Result[0].Title, ShouldEqual, "FOLDER")
So(len(query.Result[3].Dashboards), ShouldEqual, 1) So(len(query.Result[0].Dashboards), ShouldEqual, 1)
}) })
}) })
......
package search package search
import "strings"
type HitType string type HitType string
const ( const (
...@@ -23,9 +25,19 @@ type Hit struct { ...@@ -23,9 +25,19 @@ type Hit struct {
type HitList []*Hit type HitList []*Hit
func (s HitList) Len() int { return len(s) } func (s HitList) Len() int { return len(s) }
func (s HitList) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s HitList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s HitList) Less(i, j int) bool { return s[i].Title < s[j].Title } func (s HitList) Less(i, j int) bool {
if s[i].Type == "dash-folder" && s[j].Type == "dash-db" {
return true
}
if s[i].Type == "dash-db" && s[j].Type == "dash-folder" {
return false
}
return strings.ToLower(s[i].Title) < strings.ToLower(s[j].Title)
}
type Query struct { type Query struct {
Title string Title string
......
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