fixed tech-insights API endpoint to return the proper latest value
Signed-off-by: Milos Protic <protic023@gmail.com>
This commit is contained in:
@@ -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
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user