Browse Source

dashboards: render correct link for folder when searching for dashboards (#10763)

Fixes #10761
Marcus Efraimsson 7 years ago
parent
commit
a879dd8c0c

+ 2 - 1
pkg/services/search/models.go

@@ -21,8 +21,9 @@ type Hit struct {
 	Tags        []string `json:"tags"`
 	IsStarred   bool     `json:"isStarred"`
 	FolderId    int64    `json:"folderId,omitempty"`
+	FolderUid   string   `json:"folderUid,omitempty"`
 	FolderTitle string   `json:"folderTitle,omitempty"`
-	FolderSlug  string   `json:"folderSlug,omitempty"`
+	FolderUrl   string   `json:"folderUrl,omitempty"`
 }
 
 type HitList []*Hit

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

@@ -245,6 +245,7 @@ type DashboardSearchProjection struct {
 	Term        string
 	IsFolder    bool
 	FolderId    int64
+	FolderUid   string
 	FolderSlug  string
 	FolderTitle string
 }
@@ -323,11 +324,15 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
 				Url:         m.GetDashboardFolderUrl(item.IsFolder, item.Uid, item.Slug),
 				Type:        getHitType(item),
 				FolderId:    item.FolderId,
+				FolderUid:   item.FolderUid,
 				FolderTitle: item.FolderTitle,
-				FolderSlug:  item.FolderSlug,
 				Tags:        []string{},
 			}
 
+			if item.FolderId > 0 {
+				hit.FolderUrl = m.GetFolderUrl(item.FolderUid, item.FolderSlug)
+			}
+
 			query.Result = append(query.Result, hit)
 			hits[item.Id] = hit
 		}

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

@@ -147,6 +147,7 @@ func TestDashboardDataAccess(t *testing.T) {
 				hit := query.Result[0]
 				So(hit.Type, ShouldEqual, search.DashHitFolder)
 				So(hit.Url, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
+				So(hit.FolderTitle, ShouldEqual, "")
 			})
 
 			Convey("Should be able to search for a dashboard folder's children", func() {
@@ -163,6 +164,10 @@ func TestDashboardDataAccess(t *testing.T) {
 				hit := query.Result[0]
 				So(hit.Id, ShouldEqual, savedDash.Id)
 				So(hit.Url, ShouldEqual, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
+				So(hit.FolderId, ShouldEqual, savedFolder.Id)
+				So(hit.FolderUid, ShouldEqual, savedFolder.Uid)
+				So(hit.FolderTitle, ShouldEqual, savedFolder.Title)
+				So(hit.FolderUrl, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
 			})
 
 			Convey("Should be able to search for dashboard by dashboard ids", func() {

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

@@ -107,6 +107,7 @@ func (sb *SearchBuilder) buildSelect() {
 			dashboard_tag.term,
 			dashboard.is_folder,
 			dashboard.folder_id,
+			folder.uid as folder_uid,
 			folder.slug as folder_slug,
 			folder.title as folder_title
 		FROM `)

+ 2 - 2
public/app/core/services/search_srv.ts

@@ -150,9 +150,9 @@ export class SearchSrv {
         if (hit.folderId) {
           section = {
             id: hit.folderId,
-            uid: hit.uid,
+            uid: hit.folderUid,
             title: hit.folderTitle,
-            url: hit.url,
+            url: hit.folderUrl,
             items: [],
             icon: 'fa fa-folder-open',
             toggle: this.toggleFolder.bind(this),

+ 3 - 8
public/app/core/specs/manage_dashboards.jest.ts

@@ -20,9 +20,6 @@ describe('ManageDashboards', () => {
               icon: 'fa fa-folder',
               tags: [],
               isStarred: false,
-              folderId: 410,
-              folderTitle: 'afolder',
-              folderSlug: 'afolder',
             },
           ],
           tags: [],
@@ -77,9 +74,6 @@ describe('ManageDashboards', () => {
               icon: 'fa fa-folder',
               tags: [],
               isStarred: false,
-              folderId: 410,
-              folderTitle: 'afolder',
-              folderSlug: 'afolder',
             },
           ],
           tags: [],
@@ -112,8 +106,9 @@ describe('ManageDashboards', () => {
               tags: [],
               isStarred: false,
               folderId: 410,
-              folderTitle: 'afolder',
-              folderSlug: 'afolder',
+              folderUid: 'uid',
+              folderTitle: 'Folder',
+              folderUrl: '/dashboards/f/uid/folder',
             },
             {
               id: 500,

+ 7 - 0
public/app/core/specs/search_srv.jest.ts

@@ -190,7 +190,9 @@ describe('SearchSrv', () => {
             title: 'dash in folder1 1',
             type: 'dash-db',
             folderId: 1,
+            folderUid: 'uid',
             folderTitle: 'folder1',
+            folderUrl: '/dashboards/f/uid/folder1',
           },
         ])
       );
@@ -206,6 +208,11 @@ describe('SearchSrv', () => {
 
     it('should group results by folder', () => {
       expect(results).toHaveLength(2);
+      expect(results[0].id).toEqual(0);
+      expect(results[1].id).toEqual(1);
+      expect(results[1].uid).toEqual('uid');
+      expect(results[1].title).toEqual('folder1');
+      expect(results[1].url).toEqual('/dashboards/f/uid/folder1');
     });
   });