From 53d11d50fc63bf294510a6d27319f8f1e2a2e386 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 8 May 2017 22:23:25 +0200 Subject: [PATCH] WIP: remove browse mode for dashboard search Dashboard folders included in all searches. If a dashboard matches a search and has a parent folder then the parent folder is appended to the search result. A hierarchy is then returned in the result with child dashboards under their parent folders. --- pkg/api/search.go | 2 - pkg/services/search/handlers.go | 1 - pkg/services/search/handlers_test.go | 12 ------ pkg/services/search/models.go | 2 - pkg/services/sqlstore/dashboard.go | 38 +++++++++++++++++-- pkg/services/sqlstore/dashboard_test.go | 29 ++++---------- public/app/core/components/search/search.html | 2 +- public/app/core/components/search/search.ts | 4 -- public/sass/components/_search.scss | 6 +-- 9 files changed, 44 insertions(+), 52 deletions(-) diff --git a/pkg/api/search.go b/pkg/api/search.go index fa0f2a35660..c68dc51e986 100644 --- a/pkg/api/search.go +++ b/pkg/api/search.go @@ -14,7 +14,6 @@ func Search(c *middleware.Context) { tags := c.QueryStrings("tag") starred := c.Query("starred") limit := c.QueryInt("limit") - browseMode := c.Query("browseMode") if limit == 0 { limit = 1000 @@ -36,7 +35,6 @@ func Search(c *middleware.Context) { IsStarred: starred == "true", OrgId: c.OrgId, DashboardIds: dbids, - BrowseMode: browseMode == "true", } err := bus.Dispatch(&searchQuery) diff --git a/pkg/services/search/handlers.go b/pkg/services/search/handlers.go index 87b34cabd90..0821c407c74 100644 --- a/pkg/services/search/handlers.go +++ b/pkg/services/search/handlers.go @@ -45,7 +45,6 @@ func searchHandler(query *Query) error { IsStarred: query.IsStarred, OrgId: query.OrgId, DashboardIds: query.DashboardIds, - BrowseMode: query.BrowseMode, } if err := bus.Dispatch(&dashQuery); err != nil { diff --git a/pkg/services/search/handlers_test.go b/pkg/services/search/handlers_test.go index fc49f9a5972..3fd7801a323 100644 --- a/pkg/services/search/handlers_test.go +++ b/pkg/services/search/handlers_test.go @@ -68,17 +68,5 @@ func TestSearch(t *testing.T) { }) }) - - Convey("That returns result in browse mode", func() { - query.BrowseMode = true - err := searchHandler(&query) - So(err, ShouldBeNil) - - Convey("should return correct results", func() { - 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 d641a4e8fd6..155806dc0e1 100644 --- a/pkg/services/search/models.go +++ b/pkg/services/search/models.go @@ -47,7 +47,6 @@ type Query struct { Limit int IsStarred bool DashboardIds []int - BrowseMode bool Result HitList } @@ -58,7 +57,6 @@ type FindPersistedDashboardsQuery struct { UserId int64 IsStarred bool DashboardIds []int - BrowseMode bool Result HitList } diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 733d467dc35..fd560c2a0c0 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -218,6 +218,11 @@ func SearchDashboards(query *search.FindPersistedDashboardsQuery) error { return err } + res, err = appendDashboardFolders(res) + if err != nil { + return err + } + query.Result = make([]*search.Hit, 0) hits := make(map[int64]*search.Hit) @@ -240,13 +245,40 @@ func SearchDashboards(query *search.FindPersistedDashboardsQuery) error { } } - if query.BrowseMode { - convertToDashboardFolders(query) - } + convertToDashboardFolders(query) return err } +// appends parent folders for any hits to the search result +func appendDashboardFolders(res []DashboardSearchProjection) ([]DashboardSearchProjection, error) { + var dashboardFolderIds []int64 + for _, item := range res { + if item.ParentId > 0 { + dashboardFolderIds = append(dashboardFolderIds, item.ParentId) + } + } + + if len(dashboardFolderIds) > 0 { + folderQuery := &m.GetDashboardsQuery{DashboardIds: dashboardFolderIds} + err := GetDashboards(folderQuery) + if err != nil { + return nil, err + } + + for _, folder := range folderQuery.Result { + res = append(res, DashboardSearchProjection{ + Id: folder.Id, + IsFolder: true, + Slug: folder.Slug, + Title: folder.Title, + }) + } + } + + return res, nil +} + func getHitType(item DashboardSearchProjection) search.HitType { var hitType search.HitType if item.IsFolder { diff --git a/pkg/services/sqlstore/dashboard_test.go b/pkg/services/sqlstore/dashboard_test.go index a080875de23..b537762ab92 100644 --- a/pkg/services/sqlstore/dashboard_test.go +++ b/pkg/services/sqlstore/dashboard_test.go @@ -114,7 +114,7 @@ func TestDashboardDataAccess(t *testing.T) { So(err, ShouldNotBeNil) }) - Convey("Should be able to search for dashboard", func() { + Convey("Should be able to search for dashboard and return in folder hierarchy", func() { query := search.FindPersistedDashboardsQuery{ Title: "test dash 23", OrgId: 1, @@ -124,10 +124,12 @@ func TestDashboardDataAccess(t *testing.T) { So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 1) - hit := query.Result[0] + hit := query.Result[0].Dashboards[0] + So(len(hit.Tags), ShouldEqual, 2) So(hit.Type, ShouldEqual, search.DashHitDB) So(hit.ParentId, ShouldBeGreaterThan, 0) + }) Convey("Should be able to search for dashboard folder", func() { @@ -144,23 +146,6 @@ func TestDashboardDataAccess(t *testing.T) { So(hit.Type, ShouldEqual, search.DashHitFolder) }) - Convey("Should be able to browse dashboard folders", func() { - query := search.FindPersistedDashboardsQuery{ - OrgId: 1, - BrowseMode: true, - } - - err := SearchDashboards(&query) - So(err, ShouldBeNil) - - So(len(query.Result), ShouldEqual, 2) - hit := query.Result[0] - So(hit.Type, ShouldEqual, search.DashHitFolder) - So(len(hit.Dashboards), ShouldEqual, 2) - So(hit.Dashboards[0].Title, ShouldEqual, "test dash 23") - - }) - Convey("Should be able to search for dashboard by dashboard ids", func() { Convey("should be able to find two dashboards by id", func() { query := search.FindPersistedDashboardsQuery{ @@ -171,12 +156,12 @@ func TestDashboardDataAccess(t *testing.T) { err := SearchDashboards(&query) So(err, ShouldBeNil) - So(len(query.Result), ShouldEqual, 2) + So(len(query.Result[0].Dashboards), ShouldEqual, 2) - hit := query.Result[0] + hit := query.Result[0].Dashboards[0] So(len(hit.Tags), ShouldEqual, 2) - hit2 := query.Result[1] + hit2 := query.Result[0].Dashboards[1] So(len(hit2.Tags), ShouldEqual, 1) }) diff --git a/public/app/core/components/search/search.html b/public/app/core/components/search/search.html index 87aa2b5b9f7..671f8834ef6 100644 --- a/public/app/core/components/search/search.html +++ b/public/app/core/components/search/search.html @@ -57,7 +57,7 @@
No dashboards matching your query were found.
- diff --git a/public/app/core/components/search/search.ts b/public/app/core/components/search/search.ts index 9a957bf3110..f6810f2bc76 100644 --- a/public/app/core/components/search/search.ts +++ b/public/app/core/components/search/search.ts @@ -20,7 +20,6 @@ export class SearchCtrl { ignoreClose: any; // triggers fade animation class openCompleted: boolean; - searchMode = 'browse'; /** @ngInject */ constructor(private $scope, private $location, private $timeout, private backendSrv, private contextSrv, private $rootScope) { @@ -105,9 +104,6 @@ export class SearchCtrl { this.currentSearchId = this.currentSearchId + 1; var localSearchId = this.currentSearchId; - this.query.browseMode = this.queryHasNoFilters(); - this.searchMode = this.queryHasNoFilters() ? 'browse': 'search'; - return this.backendSrv.search(this.query).then((results) => { if (localSearchId < this.currentSearchId) { return; } diff --git a/public/sass/components/_search.scss b/public/sass/components/_search.scss index 21272b70ea0..42527b185b6 100644 --- a/public/sass/components/_search.scss +++ b/public/sass/components/_search.scss @@ -146,14 +146,10 @@ content: "\f015"; } -.search-item-dash-folder.search-results-browse-mode > .search-result-link > .search-result-icon::before { +.search-item-dash-folder > .search-result-link > .search-result-icon::before { content: "\f07c"; } -.search-item-dash-folder.search-results-search-mode > .search-result-link > .search-result-icon::before { - content: "\f07b"; -} - .search-button-row { padding: $spacer*2; display: flex;