From fc1d8416a7c4b4dfd446f74d1de9f368265e11ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 20 Nov 2017 12:47:03 +0100 Subject: [PATCH] working on dashboard search --- pkg/api/search.go | 11 ++++- pkg/services/search/handlers.go | 2 +- pkg/services/search/models.go | 21 +++++----- pkg/services/sqlstore/dashboard.go | 8 +--- pkg/services/sqlstore/dashboard_test.go | 2 +- .../sqlstore/migrations/dashboard_mig.go | 2 +- pkg/services/sqlstore/search_builder.go | 28 ++++--------- public/app/core/components/search/search.ts | 2 +- public/app/core/services/search_srv.ts | 40 ++++++++++++------- 9 files changed, 58 insertions(+), 58 deletions(-) diff --git a/pkg/api/search.go b/pkg/api/search.go index 3998fb0298c..fee062a5599 100644 --- a/pkg/api/search.go +++ b/pkg/api/search.go @@ -15,7 +15,6 @@ func Search(c *middleware.Context) { starred := c.Query("starred") limit := c.QueryInt("limit") dashboardType := c.Query("type") - folderId := c.QueryInt64("folderId") if limit == 0 { limit = 1000 @@ -29,6 +28,14 @@ func Search(c *middleware.Context) { } } + folderIds := make([]int64, 0) + for _, id := range c.QueryStrings("folderIds") { + folderId, err := strconv.ParseInt(id, 10, 64) + if err == nil { + folderIds = append(folderIds, folderId) + } + } + searchQuery := search.Query{ Title: query, Tags: tags, @@ -38,7 +45,7 @@ func Search(c *middleware.Context) { OrgId: c.OrgId, DashboardIds: dbids, Type: dashboardType, - FolderId: folderId, + FolderIds: folderIds, } err := bus.Dispatch(&searchQuery) diff --git a/pkg/services/search/handlers.go b/pkg/services/search/handlers.go index 9197c8f77e6..247585402ef 100644 --- a/pkg/services/search/handlers.go +++ b/pkg/services/search/handlers.go @@ -18,7 +18,7 @@ func searchHandler(query *Query) error { IsStarred: query.IsStarred, DashboardIds: query.DashboardIds, Type: query.Type, - FolderId: query.FolderId, + FolderIds: query.FolderIds, Tags: query.Tags, Limit: query.Limit, } diff --git a/pkg/services/search/models.go b/pkg/services/search/models.go index 2065cc3b36a..b0955593573 100644 --- a/pkg/services/search/models.go +++ b/pkg/services/search/models.go @@ -48,22 +48,21 @@ type Query struct { IsStarred bool Type string DashboardIds []int64 - FolderId int64 + FolderIds []int64 Result HitList } type FindPersistedDashboardsQuery struct { - Title string - OrgId int64 - SignedInUser *models.SignedInUser - IsStarred bool - DashboardIds []int64 - Type string - FolderId int64 - Tags []string - ExpandedFolders []int64 - Limit int + Title string + OrgId int64 + SignedInUser *models.SignedInUser + IsStarred bool + DashboardIds []int64 + Type string + FolderIds []int64 + Tags []string + Limit int Result HitList } diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index ce4c74ac3df..e808a97e8f8 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -207,12 +207,8 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear sb.WithType(query.Type) } - if query.FolderId > 0 { - sb.WithFolderId(query.FolderId) - } - - if len(query.ExpandedFolders) > 0 { - sb.WithExpandedFolders(query.ExpandedFolders) + if len(query.FolderIds) > 0 { + sb.WithFolderIds(query.FolderIds) } var res []DashboardSearchProjection diff --git a/pkg/services/sqlstore/dashboard_test.go b/pkg/services/sqlstore/dashboard_test.go index 983b4ca1814..7e3b5ddee42 100644 --- a/pkg/services/sqlstore/dashboard_test.go +++ b/pkg/services/sqlstore/dashboard_test.go @@ -384,7 +384,7 @@ func TestDashboardDataAccess(t *testing.T) { 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} + query := &search.FindPersistedDashboardsQuery{FolderIds: []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) diff --git a/pkg/services/sqlstore/migrations/dashboard_mig.go b/pkg/services/sqlstore/migrations/dashboard_mig.go index ed8d2c73a5d..4f1602be931 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -139,7 +139,7 @@ func addDashboardMigration(mg *Migrator) { // add column to store folder_id for dashboard folder structure mg.AddMigration("Add column folder_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{ - Name: "folder_id", Type: DB_BigInt, Nullable: true, + Name: "folder_id", Type: DB_BigInt, Nullable: false, Default: "0", })) mg.AddMigration("Add column isFolder in dashboard", NewAddColumnMigration(dashboardV2, &Column{ diff --git a/pkg/services/sqlstore/search_builder.go b/pkg/services/sqlstore/search_builder.go index f58858d5347..99bde21d171 100644 --- a/pkg/services/sqlstore/search_builder.go +++ b/pkg/services/sqlstore/search_builder.go @@ -17,8 +17,7 @@ type SearchBuilder struct { whereTitle string whereTypeFolder bool whereTypeDash bool - whereFolderId int64 - expandedFolders []int64 + whereFolderIds []int64 sql bytes.Buffer params []interface{} } @@ -72,14 +71,8 @@ func (sb *SearchBuilder) WithType(queryType string) *SearchBuilder { return sb } -func (sb *SearchBuilder) WithFolderId(folderId int64) *SearchBuilder { - sb.whereFolderId = folderId - - return sb -} - -func (sb *SearchBuilder) WithExpandedFolders(expandedFolders []int64) *SearchBuilder { - sb.expandedFolders = expandedFolders +func (sb *SearchBuilder) WithFolderIds(folderIds []int64) *SearchBuilder { + sb.whereFolderIds = folderIds return sb } @@ -212,17 +205,10 @@ func (sb *SearchBuilder) buildSearchWhereClause() { sb.sql.WriteString(" AND dashboard.is_folder = 0") } - if sb.whereFolderId > 0 { - 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) + if len(sb.whereFolderIds) > 0 { + sb.sql.WriteString(` AND dashboard.folder_id IN (?` + strings.Repeat(",?", len(sb.whereFolderIds)-1) + `) `) + for _, id := range sb.whereFolderIds { + sb.params = append(sb.params, id) } } } diff --git a/public/app/core/components/search/search.ts b/public/app/core/components/search/search.ts index 07931ef1844..443a3b1aa3a 100644 --- a/public/app/core/components/search/search.ts +++ b/public/app/core/components/search/search.ts @@ -37,7 +37,7 @@ export class SearchCtrl { this.giveSearchFocus = 0; this.selectedIndex = -1; this.results = []; - this.query = { query: '', tag: [], starred: false, mode: 'tree' }; + this.query = { query: '', tag: [], starred: false }; this.currentSearchId = 0; this.ignoreClose = true; diff --git a/public/app/core/services/search_srv.ts b/public/app/core/services/search_srv.ts index ecd5268c6cb..55c6996cf8a 100644 --- a/public/app/core/services/search_srv.ts +++ b/public/app/core/services/search_srv.ts @@ -7,8 +7,12 @@ export class SearchSrv { constructor(private backendSrv) { } - search(query) { - return this.backendSrv.search(query).then(results => { + search(options) { + if (!options.query) { + options.folderIds = [0]; + } + + return this.backendSrv.search(options).then(results => { let sections: any = {}; @@ -34,22 +38,30 @@ export class SearchSrv { // create folder index for (let hit of results) { - let section = sections[hit.folderId]; - if (!section) { - section = { - id: hit.folderId, - title: hit.folderTitle, + if (hit.type === 'dash-folder') { + sections[hit.id] = { + id: hit.id, + title: hit.title, items: [], - icon: 'fa fa-folder-open' + icon: 'fa fa-folder-open', + score: _.keys(sections).length, }; - // handle root - if (!hit.folderId) { - section.title = "Dashboards"; - section.icon = "fa fa-circle-o"; - } - sections[hit.folderId] = section; } + } + + sections[0] = { + id: 0, + title: 'Root', + items: [], + icon: 'fa fa-folder-open', + score: _.keys(sections).length, + }; + for (let hit of results) { + if (hit.type === 'dash-folder') { + continue; + } + let section = sections[hit.folderId || 0]; hit.url = 'dashboard/' + hit.uri; section.items.push(hit); }