Browse Source

search: add expanded folders

Daniel Lee 8 years ago
parent
commit
7c74111187

+ 10 - 9
pkg/services/search/models.go

@@ -54,15 +54,16 @@ type Query struct {
 }
 }
 
 
 type FindPersistedDashboardsQuery 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
 	Result HitList
 }
 }

+ 7 - 1
pkg/services/sqlstore/dashboard.go

@@ -191,7 +191,9 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear
 		limit = 1000
 		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 {
 	if query.IsStarred {
 		sb.IsStarred()
 		sb.IsStarred()
@@ -209,6 +211,10 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear
 		sb.WithFolderId(query.FolderId)
 		sb.WithFolderId(query.FolderId)
 	}
 	}
 
 
+	if len(query.ExpandedFolders) > 0 {
+		sb.WithExpandedFolders(query.ExpandedFolders)
+	}
+
 	var res []DashboardSearchProjection
 	var res []DashboardSearchProjection
 
 
 	sql, params := sb.ToSql()
 	sql, params := sb.ToSql()

+ 13 - 0
pkg/services/sqlstore/dashboard_test.go

@@ -382,6 +382,19 @@ func TestDashboardDataAccess(t *testing.T) {
 
 
 			currentUser := createUser("viewer", "Viewer", false)
 			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() {
 			Convey("and acl is set for one dashboard folder", func() {
 				var otherUser int64 = 999
 				var otherUser int64 = 999
 				updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)
 				updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)

+ 16 - 0
pkg/services/sqlstore/search_builder.go

@@ -18,6 +18,7 @@ type SearchBuilder struct {
 	whereTypeFolder     bool
 	whereTypeFolder     bool
 	whereTypeDash       bool
 	whereTypeDash       bool
 	whereFolderId       int64
 	whereFolderId       int64
+	expandedFolders     []int64
 	sql                 bytes.Buffer
 	sql                 bytes.Buffer
 	params              []interface{}
 	params              []interface{}
 }
 }
@@ -77,6 +78,12 @@ func (sb *SearchBuilder) WithFolderId(folderId int64) *SearchBuilder {
 	return sb
 	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{}) {
 func (sb *SearchBuilder) ToSql() (string, []interface{}) {
 	sb.params = make([]interface{}, 0)
 	sb.params = make([]interface{}, 0)
 
 
@@ -209,4 +216,13 @@ func (sb *SearchBuilder) buildSearchWhereClause() {
 		sb.sql.WriteString(" AND dashboard.folder_id = ?")
 		sb.sql.WriteString(" AND dashboard.folder_id = ?")
 		sb.params = append(sb.params, sb.whereFolderId)
 		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)
+		}
+	}
 }
 }