From 7c741111876d26f4eaef1808318f182bbe976ba3 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Fri, 17 Nov 2017 15:30:21 +0100 Subject: [PATCH] search: add expanded folders --- pkg/services/search/models.go | 19 ++++++++++--------- pkg/services/sqlstore/dashboard.go | 8 +++++++- pkg/services/sqlstore/dashboard_test.go | 13 +++++++++++++ pkg/services/sqlstore/search_builder.go | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/pkg/services/search/models.go b/pkg/services/search/models.go index a95a5dc3d4a..2065cc3b36a 100644 --- a/pkg/services/search/models.go +++ b/pkg/services/search/models.go @@ -54,15 +54,16 @@ type Query struct { } type FindPersistedDashboardsQuery struct { - Title string - OrgId int64 - SignedInUser *models.SignedInUser - IsStarred bool - DashboardIds []int64 - Type string - FolderId int64 - Tags []string - Limit int + Title string + OrgId int64 + SignedInUser *models.SignedInUser + IsStarred bool + DashboardIds []int64 + Type string + FolderId int64 + Tags []string + ExpandedFolders []int64 + Limit int Result HitList } diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 2569359f4cc..ce4c74ac3df 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -191,7 +191,9 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear limit = 1000 } - sb := NewSearchBuilder(query.SignedInUser, limit).WithTags(query.Tags).WithDashboardIdsIn(query.DashboardIds) + sb := NewSearchBuilder(query.SignedInUser, limit). + WithTags(query.Tags). + WithDashboardIdsIn(query.DashboardIds) if query.IsStarred { sb.IsStarred() @@ -209,6 +211,10 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear sb.WithFolderId(query.FolderId) } + if len(query.ExpandedFolders) > 0 { + sb.WithExpandedFolders(query.ExpandedFolders) + } + var res []DashboardSearchProjection sql, params := sb.ToSql() diff --git a/pkg/services/sqlstore/dashboard_test.go b/pkg/services/sqlstore/dashboard_test.go index e0473b0e38f..983b4ca1814 100644 --- a/pkg/services/sqlstore/dashboard_test.go +++ b/pkg/services/sqlstore/dashboard_test.go @@ -382,6 +382,19 @@ func TestDashboardDataAccess(t *testing.T) { currentUser := createUser("viewer", "Viewer", false) + Convey("and one folder is expanded, the other collapsed", func() { + Convey("should return dashboards in root and expanded folder", func() { + query := &search.FindPersistedDashboardsQuery{ExpandedFolders: []int64{folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1} + err := SearchDashboards(query) + So(err, ShouldBeNil) + So(len(query.Result), ShouldEqual, 4) + So(query.Result[0].Id, ShouldEqual, folder1.Id) + So(query.Result[1].Id, ShouldEqual, folder2.Id) + So(query.Result[2].Id, ShouldEqual, childDash1.Id) + So(query.Result[3].Id, ShouldEqual, dashInRoot.Id) + }) + }) + Convey("and acl is set for one dashboard folder", func() { var otherUser int64 = 999 updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT) diff --git a/pkg/services/sqlstore/search_builder.go b/pkg/services/sqlstore/search_builder.go index 0434ce40c31..f58858d5347 100644 --- a/pkg/services/sqlstore/search_builder.go +++ b/pkg/services/sqlstore/search_builder.go @@ -18,6 +18,7 @@ type SearchBuilder struct { whereTypeFolder bool whereTypeDash bool whereFolderId int64 + expandedFolders []int64 sql bytes.Buffer params []interface{} } @@ -77,6 +78,12 @@ func (sb *SearchBuilder) WithFolderId(folderId int64) *SearchBuilder { return sb } +func (sb *SearchBuilder) WithExpandedFolders(expandedFolders []int64) *SearchBuilder { + sb.expandedFolders = expandedFolders + return sb +} + +// ToSql builds the sql and returns it as a string, together with the params. func (sb *SearchBuilder) ToSql() (string, []interface{}) { sb.params = make([]interface{}, 0) @@ -209,4 +216,13 @@ func (sb *SearchBuilder) buildSearchWhereClause() { sb.sql.WriteString(" AND dashboard.folder_id = ?") sb.params = append(sb.params, sb.whereFolderId) } + + if len(sb.expandedFolders) > 0 { + sb.sql.WriteString(` AND (dashboard.folder_id IN (?` + strings.Repeat(",?", len(sb.expandedFolders)-1) + `) `) + sb.sql.WriteString(` OR dashboard.folder_id IS NULL OR dashboard.folder_id = 0)`) + + for _, ef := range sb.expandedFolders { + sb.params = append(sb.params, ef) + } + } }