From 2e010b920b8da0a24aef45e8f56ca3307ddfd762 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 27 Apr 2017 12:38:38 +0200 Subject: [PATCH] dashboard: sort search with dash folder first --- pkg/services/search/handlers_test.go | 31 +++++++++++++++------------- pkg/services/search/models.go | 18 +++++++++++++--- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/pkg/services/search/handlers_test.go b/pkg/services/search/handlers_test.go index c5ba735291a..8535cc48e82 100644 --- a/pkg/services/search/handlers_test.go +++ b/pkg/services/search/handlers_test.go @@ -16,10 +16,11 @@ func TestSearch(t *testing.T) { bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error { query.Result = HitList{ - &Hit{Id: 16, Title: "CCAA", Tags: []string{"BB", "AA"}}, - &Hit{Id: 10, Title: "AABB", Tags: []string{"CC", "AA"}}, - &Hit{Id: 15, Title: "BBAA", Tags: []string{"EE", "AA", "BB"}}, - &Hit{Id: 17, Title: "FOLDER", Dashboards: []Hit{ + &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", Dashboards: []Hit{ {Id: 18, Title: "ZZAA", Tags: []string{"ZZ"}}, }}, } @@ -36,15 +37,17 @@ func TestSearch(t *testing.T) { So(err, ShouldBeNil) Convey("should return sorted results", func() { - So(query.Result[0].Title, ShouldEqual, "AABB") - So(query.Result[1].Title, ShouldEqual, "BBAA") - So(query.Result[2].Title, ShouldEqual, "CCAA") + 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[1].Tags[0], ShouldEqual, "AA") - So(query.Result[1].Tags[1], ShouldEqual, "BB") - So(query.Result[1].Tags[2], ShouldEqual, "EE") + So(query.Result[3].Tags[0], ShouldEqual, "AA") + So(query.Result[3].Tags[1], ShouldEqual, "BB") + So(query.Result[3].Tags[2], ShouldEqual, "EE") }) }) @@ -54,9 +57,9 @@ func TestSearch(t *testing.T) { So(err, ShouldBeNil) 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[1].Title, ShouldEqual, "CCAA") + So(query.Result[2].Title, ShouldEqual, "CCAA") }) }) @@ -67,8 +70,8 @@ func TestSearch(t *testing.T) { So(err, ShouldBeNil) Convey("should return correct results", func() { - So(query.Result[3].Title, ShouldEqual, "FOLDER") - So(len(query.Result[3].Dashboards), ShouldEqual, 1) + So(query.Result[0].Title, ShouldEqual, "FOLDER") + So(len(query.Result[0].Dashboards), ShouldEqual, 1) }) }) diff --git a/pkg/services/search/models.go b/pkg/services/search/models.go index a9297f900b1..d641a4e8fd6 100644 --- a/pkg/services/search/models.go +++ b/pkg/services/search/models.go @@ -1,5 +1,7 @@ package search +import "strings" + type HitType string const ( @@ -23,9 +25,19 @@ type Hit struct { type HitList []*Hit -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) Less(i, j int) bool { return s[i].Title < s[j].Title } +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) 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 { Title string