From 1882cfee486bba4e72770a0af767606264f46738 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Tue, 27 Aug 2024 15:46:08 +0300 Subject: [PATCH] feat: move `getEntities` ordering to database instead sorting the entities in the client on demand, use default ordering parameters for entities when calling `getEntities`. having thousands of entities causes this function to pop up as the sorting method is not very lightweight thus moving it to database to handle. Signed-off-by: Heikki Hellgren --- .changeset/loud-brooms-pull.md | 8 +++++ packages/catalog-client/src/CatalogClient.ts | 31 +------------------ .../src/service/DefaultEntitiesCatalog.ts | 13 +++++++- 3 files changed, 21 insertions(+), 31 deletions(-) create mode 100644 .changeset/loud-brooms-pull.md diff --git a/.changeset/loud-brooms-pull.md b/.changeset/loud-brooms-pull.md new file mode 100644 index 0000000000..8ed3e22c5b --- /dev/null +++ b/.changeset/loud-brooms-pull.md @@ -0,0 +1,8 @@ +--- +'@backstage/catalog-client': patch +'@backstage/plugin-catalog-backend': patch +--- + +Moved `getEntities` ordering to utilize database instead of having it inside catalog client + +Please note that the latest version of `@backstage/catalog-client` will not order the entities in the same way as before. This is because the ordering is now done in the database query instead of in the client. If you rely on the ordering of the entities, you may need to update your backend plugin or code to handle this change. diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index 2440f043d7..2e7912b69a 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -18,7 +18,6 @@ import { CompoundEntityRef, Entity, parseEntityRef, - stringifyEntityRef, stringifyLocationRef, } from '@backstage/catalog-model'; import { ResponseError } from '@backstage/errors'; @@ -142,35 +141,7 @@ export class CatalogClient implements CatalogApi { options, ), ); - - // do not sort entities, if order is provided - if (encodedOrder.length) { - return { items: entities }; - } - - const refCompare = (a: Entity, b: Entity) => { - // in case field filtering is used, these fields might not be part of the response - if ( - a.metadata?.name === undefined || - a.kind === undefined || - b.metadata?.name === undefined || - b.kind === undefined - ) { - return 0; - } - - const aRef = stringifyEntityRef(a); - const bRef = stringifyEntityRef(b); - if (aRef < bRef) { - return -1; - } - if (aRef > bRef) { - return 1; - } - return 0; - }; - - return { items: entities.sort(refCompare) }; + return { items: entities }; } /** diff --git a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts index 9049685d38..91c7592aff 100644 --- a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts @@ -249,7 +249,18 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { ]); } }); - entitiesQuery = entitiesQuery.orderBy('final_entities.entity_id', 'asc'); // stable sort + + if (!request?.order) { + entitiesQuery = entitiesQuery + .leftOuterJoin( + 'refresh_state', + 'refresh_state.entity_id', + 'final_entities.entity_id', + ) + .orderBy('refresh_state.entity_ref', 'asc'); // default sort + } else { + entitiesQuery.orderBy('final_entities.entity_id', 'asc'); // stable sort + } const { limit, offset } = parsePagination(request?.pagination); if (limit !== undefined) {