From 3560405cab35a7e8781dc19b9dd7314d53d61e50 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Thu, 18 Mar 2021 10:06:05 +0100 Subject: [PATCH] Call a builder a builder. Signed-off-by: Eric Peterson --- packages/backend/src/plugins/search.ts | 6 +- .../src/{registry.ts => IndexBuilder.ts} | 24 +++++-- plugins/search-backend-node/src/index.test.ts | 64 +++++++++---------- plugins/search-backend-node/src/index.ts | 2 +- 4 files changed, 55 insertions(+), 41 deletions(-) rename plugins/search-backend-node/src/{registry.ts => IndexBuilder.ts} (75%) diff --git a/packages/backend/src/plugins/search.ts b/packages/backend/src/plugins/search.ts index fa97ead727..ad5111aaf3 100644 --- a/packages/backend/src/plugins/search.ts +++ b/packages/backend/src/plugins/search.ts @@ -14,12 +14,12 @@ * limitations under the License. */ import { createRouter } from '@backstage/plugin-search-backend'; -import { Registry } from '@backstage/plugin-search-backend-node'; +import { IndexBuilder } from '@backstage/plugin-search-backend-node'; import { PluginEnvironment } from '../types'; // import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend'; export default async function createPlugin({ logger }: PluginEnvironment) { - const indexRegistry = new Registry(); + const indexBuilder = new IndexBuilder(); // TODO: Within this PR, update to use REST API instead of Catalog Builder. /* indexRegistry.addCollator({ type: 'software-catalog', @@ -28,7 +28,7 @@ export default async function createPlugin({ logger }: PluginEnvironment) { });*/ // TODO: Make this a more proper refresh loop. - indexRegistry.execute(); + indexBuilder.build(); return await createRouter({ logger, diff --git a/plugins/search-backend-node/src/registry.ts b/plugins/search-backend-node/src/IndexBuilder.ts similarity index 75% rename from plugins/search-backend-node/src/registry.ts rename to plugins/search-backend-node/src/IndexBuilder.ts index d62e9d1090..2ec3cdc951 100644 --- a/plugins/search-backend-node/src/registry.ts +++ b/plugins/search-backend-node/src/IndexBuilder.ts @@ -20,13 +20,13 @@ import { RegisterDecoratorParameters, } from './types'; -interface CollatorRegistryEntry { +interface CollatorEnvelope { collate: DocumentCollator; refreshInterval: number; } -export class Registry { - private collators: Record; +export class IndexBuilder { + private collators: Record; private decorators: Record; constructor() { @@ -34,6 +34,10 @@ export class Registry { this.decorators = {}; } + /** + * Makes the index builder aware of a collator that should be executed at the + * given refresh interval. + */ addCollator({ type, collator, @@ -45,6 +49,11 @@ export class Registry { }; } + /** + * Makes the index builder aware of a decorator. If no types are provided, it + * will be applied to documents from all known collators, otherwise it will + * only be applied to documents of the given types. + */ addDecorator({ types = ['*'], decorator, @@ -58,8 +67,13 @@ export class Registry { }); } - // TODO: But like with coordination, timing, error handling, and what have you. - async execute() { + /** + * Starts the process of executing collators and decorators and building the + * search index. + * + * TODO: But like with coordination, timing, error handling, and what have you. + */ + async build() { return Promise.all( Object.keys(this.collators).map(async type => { const decorators: DocumentDecorator[] = ( diff --git a/plugins/search-backend-node/src/index.test.ts b/plugins/search-backend-node/src/index.test.ts index 22a67af20b..1d64e80b79 100644 --- a/plugins/search-backend-node/src/index.test.ts +++ b/plugins/search-backend-node/src/index.test.ts @@ -19,7 +19,7 @@ import { DocumentDecorator, IndexableDocument, } from '@backstage/search-common'; -import { Registry } from './registry'; +import { IndexBuilder } from './IndexBuilder'; class TestDocumentCollator implements DocumentCollator { async execute() { @@ -33,56 +33,56 @@ class TestDocumentDecorator implements DocumentDecorator { } } -describe('search indexer external api', () => { - let testRegistry: Registry; +describe('IndexBuilder', () => { + let testIndexBuilder: IndexBuilder; let testCollator: DocumentCollator; let testDecorator: DocumentDecorator; beforeEach(() => { - testRegistry = new Registry(); + testIndexBuilder = new IndexBuilder(); testCollator = new TestDocumentCollator(); testDecorator = new TestDocumentDecorator(); }); - describe('registerCollator', () => { - it('registers a collator', async () => { + describe('addCollator', () => { + it('adds a collator', async () => { const collatorSpy = jest.spyOn(testCollator, 'execute'); - // Register a collator. - testRegistry.addCollator({ + // Add a collator. + testIndexBuilder.addCollator({ type: 'anything', defaultRefreshIntervalSeconds: 600, collator: testCollator, }); - // Execute the registry and ensure the collator was invoked. - await testRegistry.execute(); + // Build the index and ensure the collator was invoked. + await testIndexBuilder.build(); expect(collatorSpy).toHaveBeenCalled(); }); }); - describe('registerDecorator', () => { - it('registers a decorator', async () => { + describe('addDecorator', () => { + it('adds a decorator', async () => { const decoratorSpy = jest.spyOn(testDecorator, 'execute'); - // Register a collator. - testRegistry.addCollator({ + // Add a collator. + testIndexBuilder.addCollator({ type: 'anything', defaultRefreshIntervalSeconds: 600, collator: testCollator, }); - // Register a decorator. - testRegistry.addDecorator({ + // Add a decorator. + testIndexBuilder.addDecorator({ decorator: testDecorator, }); - // Execute the registry and ensure the decorator was invoked. - await testRegistry.execute(); + // Build the index and ensure the decorator was invoked. + await testIndexBuilder.build(); expect(decoratorSpy).toHaveBeenCalled(); }); - it('registers a type-specific decorator', async () => { + it('adds a type-specific decorator', async () => { const expectedType = 'an-expected-type'; const docFixture = { title: 'Test', @@ -94,26 +94,26 @@ describe('search indexer external api', () => { .mockImplementation(async () => [docFixture]); const decoratorSpy = jest.spyOn(testDecorator, 'execute'); - // Register a collator. - testRegistry.addCollator({ + // Add a collator. + testIndexBuilder.addCollator({ type: expectedType, defaultRefreshIntervalSeconds: 600, collator: testCollator, }); - // Register a decorator for the same type. - testRegistry.addDecorator({ + // Add a decorator for the same type. + testIndexBuilder.addDecorator({ types: [expectedType], decorator: testDecorator, }); - // Execute the registry and ensure the decorator was invoked. - await testRegistry.execute(); + // Build the index and ensure the decorator was invoked. + await testIndexBuilder.build(); expect(decoratorSpy).toHaveBeenCalled(); expect(decoratorSpy).toHaveBeenCalledWith([docFixture]); }); - it('registers a type-specific decorator that should not be called', async () => { + it('adds a type-specific decorator that should not be called', async () => { const expectedType = 'an-expected-type'; const docFixture = { title: 'Test', @@ -125,21 +125,21 @@ describe('search indexer external api', () => { .mockImplementation(async () => [docFixture]); const decoratorSpy = jest.spyOn(testDecorator, 'execute'); - // Register a collator. - testRegistry.addCollator({ + // Add a collator. + testIndexBuilder.addCollator({ type: expectedType, defaultRefreshIntervalSeconds: 600, collator: testCollator, }); - // Register a decorator for a different type. - testRegistry.addDecorator({ + // Add a decorator for a different type. + testIndexBuilder.addDecorator({ types: ['not-the-expected-type'], decorator: testDecorator, }); - // Execute the registry and ensure the decorator was not invoked. - await testRegistry.execute(); + // Build the index and ensure the decorator was not invoked. + await testIndexBuilder.build(); expect(decoratorSpy).not.toHaveBeenCalled(); }); }); diff --git a/plugins/search-backend-node/src/index.ts b/plugins/search-backend-node/src/index.ts index af6811e202..924dfbc9dc 100644 --- a/plugins/search-backend-node/src/index.ts +++ b/plugins/search-backend-node/src/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export { Registry } from './registry'; +export { IndexBuilder } from './IndexBuilder';