Skip to content

Commit

Permalink
Search: Always send specified kind in sql searcher (#70376)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Skarhed <tobias.skarhed@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
  • Loading branch information
3 people authored and LudoVio committed Jun 26, 2023
1 parent 61f2000 commit fee4dff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
39 changes: 20 additions & 19 deletions public/app/features/search/service/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jest.spyOn(backendSrv, 'fetch');
describe('SQLSearcher', () => {
beforeEach(() => {
searchMock.mockReset();
searchMock.mockResolvedValue([]);
});

it('should call search api with correct query for general folder', async () => {
searchMock.mockResolvedValue([]);
const sqlSearcher = new SQLSearcher();
const query = {
query: '*',
Expand All @@ -31,9 +32,7 @@ describe('SQLSearcher', () => {
});
});

it('should call search api with correct query based on its kinds', async () => {
searchMock.mockResolvedValue([]);

it('should call search api with correct folder kind when searching for *', async () => {
const sqlSearcher = new SQLSearcher();

const query = {
Expand All @@ -52,55 +51,58 @@ describe('SQLSearcher', () => {
type: DashboardSearchItemType.DashFolder,
folderIds: [0],
});
});

searchMock.mockClear();
it('should call search api with correct folder kind when searching for a specific term', async () => {
const sqlSearcher = new SQLSearcher();

const query2 = {
const query = {
query: 'test',
kind: ['folder'],
location: 'any',
sort: 'name_sort',
};

await sqlSearcher.search(query2);
await sqlSearcher.search(query);

expect(searchMock).toHaveBeenLastCalledWith('/api/search', {
limit: 1000,
sort: query2.sort,
query: query2.query,
sort: query.sort,
query: query.query,
tag: undefined,
type: DashboardSearchItemType.DashFolder,
folderIds: [0],
});
});

searchMock.mockClear();
it('should call search api with correct folder kind when searching with a specific uid', async () => {
const sqlSearcher = new SQLSearcher();

const query3 = {
const query = {
query: 'test',
kind: ['folder'],
location: 'any',
sort: 'name_sort',
uid: ['T202C0Tnk'],
};

await sqlSearcher.search(query3);
await sqlSearcher.search(query);

expect(searchMock).toHaveBeenLastCalledWith('/api/search', {
limit: 1000,
sort: query3.sort,
query: query3.query,
sort: query.sort,
query: query.query,
tag: undefined,
dashboardUID: query3.uid,
dashboardUID: query.uid,
type: DashboardSearchItemType.DashFolder,
});
});

it('starred should call search api with correct query', async () => {
searchMock.mockResolvedValue([]);

const sqlSearcher = new SQLSearcher();

const query = {
query: 'test',
kind: ['folder'],
location: 'any',
sort: 'name_sort',
uid: ['T202C0Tnk'],
Expand All @@ -126,7 +128,6 @@ describe('SQLSearcher', () => {
{ from: 50, expectedPage: 2 },
{ from: 150, expectedPage: 4 },
])('should search page $expectedPage when skipping $from results', async ({ from, expectedPage }) => {
searchMock.mockResolvedValue([]);
const sqlSearcher = new SQLSearcher();

await sqlSearcher.search({
Expand Down
11 changes: 6 additions & 5 deletions public/app/features/search/service/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ export class SQLSearcher implements GrafanaSearcher {
private async composeQuery(apiQuery: APIQuery, searchOptions: SearchQuery): Promise<APIQuery> {
const query = await replaceCurrentFolderQuery(searchOptions);

if (query.query === '*') {
if (query.kind?.length === 1 && TYPE_KIND_MAP[query.kind[0]]) {
apiQuery.type = TYPE_KIND_MAP[query.kind[0]];
}
} else if (query.query?.length) {
if (query.query?.length && query.query !== '*') {
apiQuery.query = query.query;
}

// search v1 supports only one kind
if (query.kind?.length === 1 && TYPE_KIND_MAP[query.kind[0]]) {
apiQuery.type = TYPE_KIND_MAP[query.kind[0]];
}

if (query.uid) {
apiQuery.dashboardUID = query.uid;
} else if (query.location?.length) {
Expand Down

0 comments on commit fee4dff

Please sign in to comment.