Skip to content

Commit

Permalink
added ability to filter in global search by "type:package" (#102754) (#…
Browse files Browse the repository at this point in the history
…102898)

Co-authored-by: Jean-Louis Leysens <jloleysens@gmail.com>
  • Loading branch information
kibanamachine and jloleysens authored Jun 22, 2021
1 parent 0c29844 commit d6a4a50
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 23 deletions.
100 changes: 100 additions & 0 deletions x-pack/plugins/fleet/public/search_provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,105 @@ describe('Package search provider', () => {

expect(sendGetPackages).toHaveBeenCalledTimes(1);
});

describe('tags', () => {
test('without packages tag, without search term', () => {
getTestScheduler().run(({ hot, expectObservable }) => {
mockSendGetPackages.mockReturnValue(
hot('--(a|)', { a: { data: { response: testResponse } } })
);
setupMock.getStartServices.mockReturnValue(
hot('--(a|)', { a: [coreMock.createStart()] }) as any
);
const packageSearchProvider = createPackageSearchProvider(setupMock);
expectObservable(
packageSearchProvider.find(
{ types: ['test'] },
{ aborted$: NEVER, maxResults: 100, preference: '' }
)
).toBe('(a|)', {
a: [],
});
});

expect(sendGetPackages).toHaveBeenCalledTimes(0);
});

test('with packages tag, with no search term', () => {
getTestScheduler().run(({ hot, expectObservable }) => {
mockSendGetPackages.mockReturnValue(
hot('--(a|)', { a: { data: { response: testResponse } } })
);
setupMock.getStartServices.mockReturnValue(
hot('--(a|)', { a: [coreMock.createStart()] }) as any
);
const packageSearchProvider = createPackageSearchProvider(setupMock);
expectObservable(
packageSearchProvider.find(
{ types: ['package'] },
{ aborted$: NEVER, maxResults: 100, preference: '' }
)
).toBe('--(a|)', {
a: [
{
id: 'test-test',
score: 80,
title: 'test',
type: 'package',
url: {
path: 'undefined#/detail/test-test/overview',
prependBasePath: false,
},
},
{
id: 'test1-test1',
score: 80,
title: 'test1',
type: 'package',
url: {
path: 'undefined#/detail/test1-test1/overview',
prependBasePath: false,
},
},
],
});
});

expect(sendGetPackages).toHaveBeenCalledTimes(1);
});

test('with packages tag, with search term', () => {
getTestScheduler().run(({ hot, expectObservable }) => {
mockSendGetPackages.mockReturnValue(
hot('--(a|)', { a: { data: { response: testResponse } } })
);
setupMock.getStartServices.mockReturnValue(
hot('--(a|)', { a: [coreMock.createStart()] }) as any
);
const packageSearchProvider = createPackageSearchProvider(setupMock);
expectObservable(
packageSearchProvider.find(
{ term: 'test1', types: ['package'] },
{ aborted$: NEVER, maxResults: 100, preference: '' }
)
).toBe('--(a|)', {
a: [
{
id: 'test1-test1',
score: 80,
title: 'test1',
type: 'package',
url: {
path: 'undefined#/detail/test1-test1/overview',
prependBasePath: false,
},
},
],
});
});

expect(sendGetPackages).toHaveBeenCalledTimes(1);
});
});
});
});
69 changes: 46 additions & 23 deletions x-pack/plugins/fleet/public/search_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { CoreSetup, CoreStart } from 'src/core/public';
import type { CoreSetup, CoreStart, ApplicationStart } from 'src/core/public';

import type { Observable } from 'rxjs';
import { from, of, combineLatest } from 'rxjs';
Expand Down Expand Up @@ -34,6 +34,26 @@ const createPackages$ = () =>
shareReplay(1)
);

const toSearchResult = (
pkg: GetPackagesResponse['response'][number],
application: ApplicationStart
) => {
const pkgkey = `${pkg.name}-${pkg.version}`;
return {
id: pkgkey,
type: packageType,
title: pkg.title,
score: 80,
url: {
// TODO: See https://github.com/elastic/kibana/issues/96134 for details about why we use '#' here. Below should be updated
// as part of migrating to non-hash based router.
// prettier-ignore
path: `${application.getUrlForApp(INTEGRATIONS_PLUGIN_ID)}#${pagePathGetters.integration_details_overview({ pkgkey })[1]}`,
prependBasePath: false,
},
};
};

export const createPackageSearchProvider = (core: CoreSetup): GlobalSearchResultProvider => {
const coreStart$ = from(core.getStartServices()).pipe(
map(([coreStart]) => coreStart),
Expand All @@ -52,38 +72,41 @@ export const createPackageSearchProvider = (core: CoreSetup): GlobalSearchResult
return {
id: 'packages',
getSearchableTypes: () => [packageType],
find: ({ term }, { maxResults, aborted$ }) => {
if (!term) {
find: ({ term, types }, { maxResults, aborted$ }) => {
if (types?.includes(packageType) === false) {
return of([]);
}

term = term.toLowerCase();
const hasTypes = Boolean(types);
const typesIncludePackage = hasTypes && types!.includes(packageType);
const noSearchTerm = !term;
const includeAllPackages = typesIncludePackage && noSearchTerm;

if (!includeAllPackages && noSearchTerm) {
return of([]);
}

if (term) {
term = term.toLowerCase();
}

const toSearchResults = (
coreStart: CoreStart,
packagesResponse: GetPackagesResponse['response']
): GlobalSearchProviderResult[] => {
const packages = packagesResponse.slice(0, maxResults);

return packages.flatMap((pkg) => {
if (!term || !pkg.title.toLowerCase().includes(term)) {
return [];
}
const pkgkey = `${pkg.name}-${pkg.version}`;
return {
id: pkgkey,
type: packageType,
title: pkg.title,
score: 80,
url: {
// TODO: See https://github.com/elastic/kibana/issues/96134 for details about why we use '#' here. Below should be updated
// as part of migrating to non-hash based router.
// prettier-ignore
path: `${coreStart.application.getUrlForApp(INTEGRATIONS_PLUGIN_ID)}#${pagePathGetters.integration_details_overview({ pkgkey })[1]}`,
prependBasePath: false,
},
};
});
return packages.flatMap(
includeAllPackages
? (pkg) => toSearchResult(pkg, coreStart.application)
: (pkg) => {
if (!term || !pkg.title.toLowerCase().includes(term)) {
return [];
}

return toSearchResult(pkg, coreStart.application);
}
);
};

return combineLatest([coreStart$, getPackages$()]).pipe(
Expand Down

0 comments on commit d6a4a50

Please sign in to comment.