From 4fee8f59e33b8928b3cb8dbfddf2286948b7ed55 Mon Sep 17 00:00:00 2001 From: Milos Protic Date: Mon, 16 May 2022 15:13:57 +0200 Subject: [PATCH] fixed tech-insights API endpoint to return the proper latest value Signed-off-by: Milos Protic --- .changeset/beige-apricots-enjoy.md | 5 ++ .../persistence/TechInsightsDatabase.test.ts | 77 +++++++++++++++++++ .../persistence/TechInsightsDatabase.ts | 8 +- 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 .changeset/beige-apricots-enjoy.md diff --git a/.changeset/beige-apricots-enjoy.md b/.changeset/beige-apricots-enjoy.md new file mode 100644 index 0000000000..bd1f932454 --- /dev/null +++ b/.changeset/beige-apricots-enjoy.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': patch +--- + +Updated tech-insights fetch/latest endpoint to return the actual latest row based on the timestamp diff --git a/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.test.ts b/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.test.ts index c69432d070..e7e8819c66 100644 --- a/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.test.ts +++ b/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.test.ts @@ -115,6 +115,56 @@ const additionalFacts = [ }, ]; +const sameFactsDiffDateSchema = { + id: 'same-fact-diff-date-test', + version: '0.0.1-test', + entityFilter: JSON.stringify([{ kind: 'service' }]), + schema: JSON.stringify({ + testStringFact: { + type: 'string', + description: 'Test fact with a string type', + }, + }), +}; + +const sameFactsDiffDateNow = DateTime.now().toISO(); +const sameFactsDiffDateNearFuture = DateTime.now() + .plus(Duration.fromMillis(555)) + .toISO(); +const sameFactsDiffDateFuture = DateTime.now() + .plus(Duration.fromMillis(1000)) + .toISO(); + +const multipleSameFacts = [ + { + timestamp: sameFactsDiffDateNow, + id: sameFactsDiffDateSchema.id, + version: '0.0.1-test', + entity: 'a:a/a', + facts: JSON.stringify({ + testNumberFact: 1, + }), + }, + { + timestamp: sameFactsDiffDateNearFuture, + id: sameFactsDiffDateSchema.id, + version: '0.0.1-test', + entity: 'a:a/a', + facts: JSON.stringify({ + testNumberFact: 2, + }), + }, + { + timestamp: sameFactsDiffDateFuture, + id: 'multiple-same-facts', + version: '0.0.1-test', + entity: 'a:a/a', + facts: JSON.stringify({ + testNumberFact: 3, + }), + }, +]; + describe('Tech Insights database', () => { const databases = TestDatabases.create(); let store: TechInsightsStore; @@ -215,6 +265,33 @@ describe('Tech Insights database', () => { expect(returnedFact['test-fact']).toMatchObject(baseAssertionFact); }); + it('should return latest fact with multiple entries', async () => { + await testDbClient.batchInsert('fact_schemas', [sameFactsDiffDateSchema]); + await testDbClient.batchInsert( + 'facts', + multipleSameFacts.map(fact => ({ + ...fact, + id: sameFactsDiffDateSchema.id, + })), + ); + + const returnedFacts = await store.getLatestFactsByIds( + ['test-fact', sameFactsDiffDateSchema.id], + 'a:a/a', + ); + + expect(returnedFacts['test-fact']).toMatchObject({ + ...baseAssertionFact, + }); + + expect(returnedFacts[sameFactsDiffDateSchema.id]).toMatchObject({ + ...baseAssertionFact, + id: sameFactsDiffDateSchema.id, + timestamp: DateTime.fromISO(sameFactsDiffDateFuture), + facts: { testNumberFact: 3 }, + }); + }); + it('should return latest facts for multiple ids', async () => { await testDbClient.batchInsert('fact_schemas', [secondSchema]); await testDbClient.batchInsert( diff --git a/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.ts b/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.ts index 3507e280a1..c6751e864a 100644 --- a/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.ts +++ b/plugins/tech-insights-backend/src/service/persistence/TechInsightsDatabase.ts @@ -131,14 +131,16 @@ export class TechInsightsDatabase implements TechInsightsStore { .and.whereIn('id', ids) .join( this.db('facts') - .max('timestamp') + .max('timestamp as maxTimestamp') .column('id as subId') .where({ entity: entityTriplet }) .and.whereIn('id', ids) .groupBy('id') .as('subQ'), - 'facts.id', - 'subQ.subId', + { + 'facts.id': 'subQ.subId', + 'facts.timestamp': 'subQ.maxTimestamp', + }, ); return this.dbFactRowsToTechInsightFacts(results); }