mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SearchSrv: stop using IDs (#54296)
* SearchSrv: stop using IDs * Remove IDs from test
This commit is contained in:
@@ -4,7 +4,12 @@ import { contextSrv } from 'app/core/services/context_srv';
|
|||||||
import impressionSrv from 'app/core/services/impression_srv';
|
import impressionSrv from 'app/core/services/impression_srv';
|
||||||
import store from 'app/core/store';
|
import store from 'app/core/store';
|
||||||
import { SECTION_STORAGE_KEY } from 'app/features/search/constants';
|
import { SECTION_STORAGE_KEY } from 'app/features/search/constants';
|
||||||
import { DashboardSection, DashboardSearchItemType, DashboardSearchHit, SearchLayout } from 'app/features/search/types';
|
import {
|
||||||
|
DashboardSection,
|
||||||
|
DashboardSearchItemType,
|
||||||
|
DashboardSearchItem,
|
||||||
|
SearchLayout,
|
||||||
|
} from 'app/features/search/types';
|
||||||
import { hasFilters } from 'app/features/search/utils';
|
import { hasFilters } from 'app/features/search/utils';
|
||||||
|
|
||||||
import { backendSrv } from './backend_srv';
|
import { backendSrv } from './backend_srv';
|
||||||
@@ -30,7 +35,7 @@ export class SearchSrv {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private queryForRecentDashboards(): Promise<DashboardSearchHit[]> {
|
private queryForRecentDashboards(): Promise<DashboardSearchItem[]> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
impressionSrv.getDashboardOpened().then((uids) => {
|
impressionSrv.getDashboardOpened().then((uids) => {
|
||||||
const dashUIDs: string[] = take(uids, 30);
|
const dashUIDs: string[] = take(uids, 30);
|
||||||
@@ -42,7 +47,7 @@ export class SearchSrv {
|
|||||||
return resolve(
|
return resolve(
|
||||||
dashUIDs
|
dashUIDs
|
||||||
.map((orderId) => result.find((result) => result.uid === orderId))
|
.map((orderId) => result.find((result) => result.uid === orderId))
|
||||||
.filter((hit) => hit && !hit.isStarred) as DashboardSearchHit[]
|
.filter((hit) => hit && !hit.isStarred) as DashboardSearchItem[]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -105,7 +110,7 @@ export class SearchSrv {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleSearchResult(sections: Sections, results: DashboardSearchHit[]): any {
|
private handleSearchResult(sections: Sections, results: DashboardSearchItem[]): any {
|
||||||
if (results.length === 0) {
|
if (results.length === 0) {
|
||||||
return sections;
|
return sections;
|
||||||
}
|
}
|
||||||
@@ -113,9 +118,7 @@ export class SearchSrv {
|
|||||||
// create folder index
|
// create folder index
|
||||||
for (const hit of results) {
|
for (const hit of results) {
|
||||||
if (hit.type === 'dash-folder') {
|
if (hit.type === 'dash-folder') {
|
||||||
// FIXME: Use hit.uid instead
|
sections[hit.uid!] = {
|
||||||
sections[hit.id!] = {
|
|
||||||
id: hit.id,
|
|
||||||
uid: hit.uid,
|
uid: hit.uid,
|
||||||
title: hit.title,
|
title: hit.title,
|
||||||
expanded: false,
|
expanded: false,
|
||||||
@@ -133,11 +136,10 @@ export class SearchSrv {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let section = sections[hit.folderId || 0];
|
let section = sections[hit.folderUid || 0];
|
||||||
if (!section) {
|
if (!section) {
|
||||||
if (hit.folderId) {
|
if (hit.folderUid) {
|
||||||
section = {
|
section = {
|
||||||
id: hit.folderId,
|
|
||||||
uid: hit.folderUid,
|
uid: hit.folderUid,
|
||||||
title: hit.folderTitle,
|
title: hit.folderTitle,
|
||||||
url: hit.folderUrl,
|
url: hit.folderUrl,
|
||||||
@@ -148,7 +150,7 @@ export class SearchSrv {
|
|||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
section = {
|
section = {
|
||||||
id: 0,
|
uid: '',
|
||||||
title: 'General',
|
title: 'General',
|
||||||
items: [],
|
items: [],
|
||||||
icon: 'folder-open',
|
icon: 'folder-open',
|
||||||
@@ -157,7 +159,7 @@ export class SearchSrv {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
// add section
|
// add section
|
||||||
sections[hit.folderId || 0] = section;
|
sections[hit.folderUid || 0] = section;
|
||||||
}
|
}
|
||||||
|
|
||||||
section.expanded = true;
|
section.expanded = true;
|
||||||
|
|||||||
@@ -158,24 +158,24 @@ describe('SearchSrv', () => {
|
|||||||
{
|
{
|
||||||
title: 'folder1',
|
title: 'folder1',
|
||||||
type: 'dash-folder',
|
type: 'dash-folder',
|
||||||
id: 1,
|
uid: 'folder-1',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'dash with no folder',
|
title: 'dash with no folder',
|
||||||
type: 'dash-db',
|
type: 'dash-db',
|
||||||
id: 2,
|
uid: '2',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'dash in folder1 1',
|
title: 'dash in folder1 1',
|
||||||
type: 'dash-db',
|
type: 'dash-db',
|
||||||
id: 3,
|
uid: '3',
|
||||||
folderId: 1,
|
folderUid: 'folder-1',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'dash in folder1 2',
|
title: 'dash in folder1 2',
|
||||||
type: 'dash-db',
|
type: 'dash-db',
|
||||||
id: 4,
|
uid: '4',
|
||||||
folderId: 1,
|
folderUid: 'folder-1',
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
@@ -202,15 +202,13 @@ describe('SearchSrv', () => {
|
|||||||
searchMock.mockImplementation(
|
searchMock.mockImplementation(
|
||||||
jest.fn().mockResolvedValue([
|
jest.fn().mockResolvedValue([
|
||||||
{
|
{
|
||||||
id: 2,
|
folderUid: 'dash-with-no-folder-uid',
|
||||||
title: 'dash with no folder',
|
title: 'dash with no folder',
|
||||||
type: 'dash-db',
|
type: 'dash-db',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 3,
|
|
||||||
title: 'dash in folder1 1',
|
title: 'dash in folder1 1',
|
||||||
type: 'dash-db',
|
type: 'dash-db',
|
||||||
folderId: 1,
|
|
||||||
folderUid: 'uid',
|
folderUid: 'uid',
|
||||||
folderTitle: 'folder1',
|
folderTitle: 'folder1',
|
||||||
folderUrl: '/dashboards/f/uid/folder1',
|
folderUrl: '/dashboards/f/uid/folder1',
|
||||||
@@ -229,8 +227,7 @@ describe('SearchSrv', () => {
|
|||||||
|
|
||||||
it('should group results by folder', () => {
|
it('should group results by folder', () => {
|
||||||
expect(results).toHaveLength(2);
|
expect(results).toHaveLength(2);
|
||||||
expect(results[0].id).toEqual(0);
|
expect(results[0].uid).toEqual('dash-with-no-folder-uid');
|
||||||
expect(results[1].id).toEqual(1);
|
|
||||||
expect(results[1].uid).toEqual('uid');
|
expect(results[1].uid).toEqual('uid');
|
||||||
expect(results[1].title).toEqual('folder1');
|
expect(results[1].title).toEqual('folder1');
|
||||||
expect(results[1].url).toEqual('/dashboards/f/uid/folder1');
|
expect(results[1].url).toEqual('/dashboards/f/uid/folder1');
|
||||||
|
|||||||
Reference in New Issue
Block a user