diff --git a/.changeset/rotten-trees-mate.md b/.changeset/rotten-trees-mate.md new file mode 100644 index 0000000000..f9d164d0fe --- /dev/null +++ b/.changeset/rotten-trees-mate.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs-backend': patch +--- + +Optimize DefaultTechDocsCollator get entities. diff --git a/plugins/techdocs-backend/src/search/DefaultTechDocsCollator.ts b/plugins/techdocs-backend/src/search/DefaultTechDocsCollator.ts index 2c8786d92e..73c030cc51 100644 --- a/plugins/techdocs-backend/src/search/DefaultTechDocsCollator.ts +++ b/plugins/techdocs-backend/src/search/DefaultTechDocsCollator.ts @@ -31,7 +31,11 @@ import { Logger } from 'winston'; import pLimit from 'p-limit'; import { Config } from '@backstage/config'; import { catalogEntityReadPermission } from '@backstage/plugin-catalog-common'; -import { CatalogApi, CatalogClient } from '@backstage/catalog-client'; +import { + CatalogApi, + CatalogClient, + CATALOG_FILTER_EXISTS, +} from '@backstage/catalog-client'; import { TechDocsDocument } from '@backstage/techdocs-common'; interface MkSearchIndexDoc { @@ -99,6 +103,10 @@ export class DefaultTechDocsCollator implements DocumentCollator { catalogClient ?? new CatalogClient({ discoveryApi: discovery }) ).getEntities( { + filter: { + 'metadata.annotations.backstage.io/techdocs-ref': + CATALOG_FILTER_EXISTS, + }, fields: [ 'kind', 'namespace', @@ -113,62 +121,60 @@ export class DefaultTechDocsCollator implements DocumentCollator { }, { token }, ); - const docPromises = entities.items - .filter(it => it.metadata?.annotations?.['backstage.io/techdocs-ref']) - .map((entity: Entity) => - limit(async (): Promise => { - const entityInfo = DefaultTechDocsCollator.handleEntityInfoCasing( - this.legacyPathCasing ?? false, + const docPromises = entities.items.map((entity: Entity) => + limit(async (): Promise => { + const entityInfo = DefaultTechDocsCollator.handleEntityInfoCasing( + this.legacyPathCasing ?? false, + { + kind: entity.kind, + namespace: entity.metadata.namespace || 'default', + name: entity.metadata.name, + }, + ); + + try { + const searchIndexResponse = await fetch( + DefaultTechDocsCollator.constructDocsIndexUrl( + techDocsBaseUrl, + entityInfo, + ), { - kind: entity.kind, - namespace: entity.metadata.namespace || 'default', - name: entity.metadata.name, + headers: { + Authorization: `Bearer ${token}`, + }, }, ); + const searchIndex = await searchIndexResponse.json(); - try { - const searchIndexResponse = await fetch( - DefaultTechDocsCollator.constructDocsIndexUrl( - techDocsBaseUrl, - entityInfo, - ), + return searchIndex.docs.map((doc: MkSearchIndexDoc) => ({ + title: unescape(doc.title), + text: unescape(doc.text || ''), + location: this.applyArgsToFormat( + locationTemplate || '/docs/:namespace/:kind/:name/:path', { - headers: { - Authorization: `Bearer ${token}`, - }, + ...entityInfo, + path: doc.location, }, - ); - const searchIndex = await searchIndexResponse.json(); - - return searchIndex.docs.map((doc: MkSearchIndexDoc) => ({ - title: unescape(doc.title), - text: unescape(doc.text || ''), - location: this.applyArgsToFormat( - locationTemplate || '/docs/:namespace/:kind/:name/:path', - { - ...entityInfo, - path: doc.location, - }, - ), - path: doc.location, - ...entityInfo, - entityTitle: entity.metadata.title, - componentType: entity.spec?.type?.toString() || 'other', - lifecycle: (entity.spec?.lifecycle as string) || '', - owner: getSimpleEntityOwnerString(entity), - authorization: { - resourceRef: stringifyEntityRef(entity), - }, - })); - } catch (e) { - logger.debug( - `Failed to retrieve tech docs search index for entity ${entityInfo.namespace}/${entityInfo.kind}/${entityInfo.name}`, - e, - ); - return []; - } - }), - ); + ), + path: doc.location, + ...entityInfo, + entityTitle: entity.metadata.title, + componentType: entity.spec?.type?.toString() || 'other', + lifecycle: (entity.spec?.lifecycle as string) || '', + owner: getSimpleEntityOwnerString(entity), + authorization: { + resourceRef: stringifyEntityRef(entity), + }, + })); + } catch (e) { + logger.debug( + `Failed to retrieve tech docs search index for entity ${entityInfo.namespace}/${entityInfo.kind}/${entityInfo.name}`, + e, + ); + return []; + } + }), + ); return (await Promise.all(docPromises)).flat(); }