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 <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-08-27 15:46:08 +03:00
parent 476adcf744
commit 1882cfee48
3 changed files with 21 additions and 31 deletions
+8
View File
@@ -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.
+1 -30
View File
@@ -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 };
}
/**
@@ -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) {