From a483d5f5a4d82219e4e41e764127020de02ea9c9 Mon Sep 17 00:00:00 2001 From: iris Date: Fri, 2 Dec 2022 12:05:18 +0800 Subject: [PATCH] add last_updated_at as annotation backstage.io/last_updated-at Signed-off-by: iris --- .../src/service/DefaultEntitiesCatalog.ts | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts index 14f22f862b..1a0fe98b40 100644 --- a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts @@ -210,7 +210,14 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { }; } - let entities: Entity[] = rows.map(e => JSON.parse(e.final_entity!)); + let entities: Entity[] = rows.map(e => { + const entityJson = JSON.parse(e.final_entity!); + if (e.last_updated_at) { + entityJson.metadata.annotations['backstage.io/last_updated-at'] = + e.last_updated_at; + } + return entityJson; + }); if (request?.fields) { entities = entities.map(e => request.fields!(e)); @@ -263,7 +270,12 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { query = parseFilter(request.filter, query, this.database); } for (const row of await query) { - lookup.set(row.entityRef, row.entity ? JSON.parse(row.entity) : null); + const entityJson = JSON.parse(row.entity); + if (row.entity.last_updated_at) { + entityJson.metadata.annotations['backstage.io/last_updated-at'] = + row.entity.last_updated_at; + } + lookup.set(row.entityRef, row.entity ? entityJson : null); } } @@ -373,13 +385,18 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { .where('refresh_state.entity_ref', '=', rootRef) .select({ entityJson: 'final_entities.final_entity', + last_updated_at: 'final_entities.last_updated_at', }); if (!rootRow) { throw new NotFoundError(`No such entity ${rootRef}`); } - - const rootEntity = JSON.parse(rootRow.entityJson) as Entity; + const entityJson = JSON.parse(rootRow.entityJson); + if (rootRow.last_updated_at) { + entityJson.metadata.annotations['backstage.io/last_updated-at'] = + rootRow.last_updated_at; + } + const rootEntity = entityJson as Entity; const seenEntityRefs = new Set(); const todo = new Array(); const items = new Array<{ entity: Entity; parentEntityRefs: string[] }>();