From 3e9d8427b6d36558cf4b9eecb394abab14eea591 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Mon, 15 Feb 2021 17:36:45 +0100 Subject: [PATCH] Illustrate potential software catalog indexing. Signed-off-by: Eric Peterson --- packages/backend/package.json | 1 + packages/backend/src/index.ts | 2 + packages/backend/src/plugins/catalog.ts | 8 +++ plugins/catalog-backend/package.json | 1 + plugins/catalog-backend/src/index.ts | 1 + .../catalog-backend/src/search/Collator.ts | 51 +++++++++++++++++++ plugins/catalog-backend/src/search/index.ts | 17 +++++++ plugins/search-indexer-backend/src/index.ts | 18 +++++-- 8 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 plugins/catalog-backend/src/search/Collator.ts create mode 100644 plugins/catalog-backend/src/search/index.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index 74a86598d3..4e999b1531 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -41,6 +41,7 @@ "@backstage/plugin-rollbar-backend": "^0.1.8", "@backstage/plugin-scaffolder-backend": "^0.9.2", "@backstage/plugin-search-backend": "^0.1.1", + "@backstage/plugin-search-indexer-backend": "^0.1.1", "@backstage/plugin-techdocs-backend": "^0.6.5", "@backstage/plugin-todo-backend": "^0.1.1", "@gitbeaker/node": "^28.0.2", diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index c32c7e6895..fededf0f0a 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -110,6 +110,8 @@ async function main() { console.log(err); process.exit(1); }); + + // @todo Start Search Refresh Loop Here? } module.hot?.accept(); diff --git a/packages/backend/src/plugins/catalog.ts b/packages/backend/src/plugins/catalog.ts index eaca42717c..86375203ab 100644 --- a/packages/backend/src/plugins/catalog.ts +++ b/packages/backend/src/plugins/catalog.ts @@ -19,8 +19,10 @@ import { CatalogBuilder, createRouter, runPeriodically, + SearchCollatorFactory, } from '@backstage/plugin-catalog-backend'; import { Router } from 'express'; +import { registerCollator } from '@backstage/plugin-search-indexer-backend'; import { PluginEnvironment } from '../types'; export default async function createPlugin( @@ -39,6 +41,12 @@ export default async function createPlugin( runPeriodically(() => higherOrderOperation.refreshAllLocations(), 100000), ); + registerCollator({ + type: 'software-catalog', + defaultRefreshIntervalSeconds: 600, + collator: SearchCollatorFactory(entitiesCatalog), + }); + return await createRouter({ entitiesCatalog, locationsCatalog, diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index bad9e3c809..c99221b05f 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -35,6 +35,7 @@ "@backstage/config": "^0.1.4", "@backstage/errors": "^0.1.1", "@backstage/integration": "^0.5.1", + "@backstage/plugin-search-indexer-backend": "^0.1.1", "@octokit/graphql": "^4.5.8", "@types/express": "^4.17.6", "@types/ldapjs": "^1.0.9", diff --git a/plugins/catalog-backend/src/index.ts b/plugins/catalog-backend/src/index.ts index f621930d10..51df909d05 100644 --- a/plugins/catalog-backend/src/index.ts +++ b/plugins/catalog-backend/src/index.ts @@ -17,5 +17,6 @@ export * from './catalog'; export * from './database'; export * from './ingestion'; +export * from './search'; export * from './service'; export * from './util'; diff --git a/plugins/catalog-backend/src/search/Collator.ts b/plugins/catalog-backend/src/search/Collator.ts new file mode 100644 index 0000000000..691e99eb3a --- /dev/null +++ b/plugins/catalog-backend/src/search/Collator.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + IndexableDocument, + IndexableDocumentCollator, +} from '@backstage/plugin-search-indexer-backend'; +import { EntitiesCatalog } from '../catalog'; +import { EntityFilters } from '../service/EntityFilters'; + +export interface CatalogEntityDocument extends IndexableDocument { + componentType: string; +} + +export const SearchCollatorFactory = ( + entitiesCatalog: EntitiesCatalog, +): IndexableDocumentCollator => { + return async (): Promise => { + const filter = EntityFilters.ofQuery({ kind: 'Component' }); + const entities = await entitiesCatalog.entities(filter); + return entities.map( + (entity): CatalogEntityDocument => { + return { + title: entity.metadata.name, + location: `/catalog/${ + entity.metadata.namespace || 'default' + }/component/${entity.metadata.name}`, + text: entity.metadata.description || '', + componentType: entity.spec?.type?.toString() || 'other', + ...(entity.spec?.owner && { owner: entity.spec.owner.toString() }), + ...(entity.metadata.lifecycle && { + lifecycle: entity.metadata.lifecycle.toString(), + }), + }; + }, + ); + }; +}; diff --git a/plugins/catalog-backend/src/search/index.ts b/plugins/catalog-backend/src/search/index.ts new file mode 100644 index 0000000000..568fafaed5 --- /dev/null +++ b/plugins/catalog-backend/src/search/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './Collator'; diff --git a/plugins/search-indexer-backend/src/index.ts b/plugins/search-indexer-backend/src/index.ts index 8988e8f007..13d1a298b7 100644 --- a/plugins/search-indexer-backend/src/index.ts +++ b/plugins/search-indexer-backend/src/index.ts @@ -15,9 +15,21 @@ */ import { Registry } from './registry'; +import { + RegisterCollatorParameters, + RegisterDecoratorParameters, +} from './types'; const registry = Registry.getInstance(); -export const registerCollator = registry.addCollator; -export const registerDecorator = registry.addDecorator; +export const registerCollator = (params: RegisterCollatorParameters) => { + registry.addCollator(params); +}; +export const registerDecorator = (params: RegisterDecoratorParameters) => { + registry.addDecorator(params); +}; -export type { IndexableDocument } from './types'; +export type { + IndexableDocument, + IndexableDocumentCollator, + IndexableDocumentDecorator, +} from './types';