Skip to content

Commit

Permalink
remove empty contracts & cache searchable apps
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 23, 2020
1 parent 0454d35 commit 3ef8b1d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 63 deletions.
15 changes: 3 additions & 12 deletions x-pack/plugins/global_search_providers/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@
*/

import { PluginInitializer } from 'src/core/public';
import {
GlobalSearchProvidersPlugin,
GlobalSearchProvidersPluginSetupDeps,
GlobalSearchProvidersPluginStartDeps,
} from './plugin';
import { GlobalSearchProvidersPluginSetup, GlobalSearchProvidersPluginStart } from './types';
import { GlobalSearchProvidersPlugin, GlobalSearchProvidersPluginSetupDeps } from './plugin';

export const plugin: PluginInitializer<
GlobalSearchProvidersPluginSetup,
GlobalSearchProvidersPluginStart,
GlobalSearchProvidersPluginSetupDeps,
GlobalSearchProvidersPluginStartDeps
> = (context) => new GlobalSearchProvidersPlugin();
export const plugin: PluginInitializer<{}, {}, GlobalSearchProvidersPluginSetupDeps, {}> = () =>
new GlobalSearchProvidersPlugin();

export { GlobalSearchProvidersPluginSetup, GlobalSearchProvidersPluginStart };
11 changes: 1 addition & 10 deletions x-pack/plugins/global_search_providers/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@ export interface GlobalSearchProvidersPluginSetupDeps {
globalSearch: GlobalSearchPluginSetup;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface GlobalSearchProvidersPluginStartDeps {}

export class GlobalSearchProvidersPlugin
implements
Plugin<
GlobalSearchProvidersPluginSetup,
GlobalSearchProvidersPluginStart,
GlobalSearchProvidersPluginSetupDeps,
GlobalSearchProvidersPluginStartDeps
> {
implements Plugin<{}, {}, GlobalSearchProvidersPluginSetupDeps, {}> {
setup(
{
getStartServices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ describe('applicationResultProvider', () => {
]);
});

it('ignores inaccessible apps', async () => {
application.applications$ = of(
createAppMap([
createApp({ id: 'app1', title: 'App 1' }),
createApp({ id: 'disabled', title: 'disabled', status: AppStatus.inaccessible }),
])
);
const provider = createApplicationResultProvider(Promise.resolve(application));
await provider.find('term', defaultOption).toPromise();

expect(getAppResultsMock).toHaveBeenCalledWith('term', [expectApp('app1')]);
});

it('ignores chromeless apps', async () => {
application.applications$ = of(
createAppMap([
createApp({ id: 'app1', title: 'App 1' }),
createApp({ id: 'chromeless', title: 'chromeless', chromeless: true }),
])
);

const provider = createApplicationResultProvider(Promise.resolve(application));
await provider.find('term', defaultOption).toPromise();

expect(getAppResultsMock).toHaveBeenCalledWith('term', [expectApp('app1')]);
});

it('sorts the results returned by `getAppResults`', async () => {
getAppResultsMock.mockReturnValue([
createResult({ id: 'r60', score: 60 }),
Expand All @@ -96,7 +123,6 @@ describe('applicationResultProvider', () => {
]);

const provider = createApplicationResultProvider(Promise.resolve(application));

const results = await provider.find('term', defaultOption).toPromise();

expect(results).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
*/

import { from } from 'rxjs';
import { take, map, takeUntil, mergeMap } from 'rxjs/operators';
import { take, map, takeUntil, mergeMap, shareReplay } from 'rxjs/operators';
import { ApplicationStart } from 'src/core/public';
import { GlobalSearchResultProvider } from '../../../global_search/public';
import { getAppResults } from './get_app_results';

export const createApplicationResultProvider = (
applicationPromise: Promise<ApplicationStart>
): GlobalSearchResultProvider => {
const searchableApps$ = from(applicationPromise).pipe(
mergeMap((application) => application.applications$),
map((apps) =>
[...apps.values()].filter(
(app) => app.status === 0 && (app.legacy === true || app.chromeless !== true)
)
),
shareReplay(1)
);

return {
id: 'application',
find: (term, { aborted$, maxResults }) => {
return from(applicationPromise).pipe(
mergeMap((application) => application.applications$),
return searchableApps$.pipe(
takeUntil(aborted$),
take(1),
map((apps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ describe('getAppResults', () => {
expect(results.length).toBe(1);
expect(results[0]).toEqual(expect.objectContaining({ id: 'dashboard', score: 100 }));
});

it('ignores inaccessible apps', () => {
const apps = [
createApp({ id: 'dashboard', title: 'dashboard', status: AppStatus.inaccessible }),
];

const results = getAppResults('dashboard', apps);

expect(results.length).toBe(0);
});

it('ignores chromeless apps', () => {
const apps = [createApp({ id: 'dashboard', title: 'dashboard', chromeless: true })];

const results = getAppResults('dashboard', apps);

expect(results.length).toBe(0);
});
});

describe('scoreApp', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ export const getAppResults = (
term: string,
apps: Array<PublicAppInfo | PublicLegacyAppInfo>
): GlobalSearchProviderResult[] => {
return (
apps
// remove disabled and chromeless app (to avoid returning the login page for example)
.filter((app) => app.status === 0 && (app.legacy === true || app.chromeless !== true))
.map((app) => ({ app, score: scoreApp(term, app) }))
.filter(({ score }) => score > 0)
.map(({ app, score }) => appToResult(app, score))
);
return apps
.map((app) => ({ app, score: scoreApp(term, app) }))
.filter(({ score }) => score > 0)
.map(({ app, score }) => appToResult(app, score));
};

export const scoreApp = (term: string, { title }: PublicAppInfo | PublicLegacyAppInfo): number => {
Expand Down
11 changes: 0 additions & 11 deletions x-pack/plugins/global_search_providers/public/types.ts

This file was deleted.

0 comments on commit 3ef8b1d

Please sign in to comment.