feat(API): Return null deviation on insights summary if previous period has no data (#14193)

This commit is contained in:
Guillaume Jacquart
2025-03-26 17:56:56 +01:00
committed by GitHub
parent 41b1797a25
commit ffc0a596e0
9 changed files with 79 additions and 24 deletions

View File

@@ -34,15 +34,15 @@ describe('InsightsController', () => {
// ASSERT
expect(response).toEqual({
total: { deviation: 0, unit: 'count', value: 0 },
failed: { deviation: 0, unit: 'count', value: 0 },
failureRate: { deviation: 0, unit: 'ratio', value: 0 },
averageRunTime: { deviation: 0, unit: 'time', value: 0 },
timeSaved: { deviation: 0, unit: 'time', value: 0 },
total: { deviation: null, unit: 'count', value: 0 },
failed: { deviation: null, unit: 'count', value: 0 },
failureRate: { deviation: null, unit: 'ratio', value: 0 },
averageRunTime: { deviation: null, unit: 'time', value: 0 },
timeSaved: { deviation: null, unit: 'time', value: 0 },
});
});
it('should return the insights summary with deviation = current if insights exist only for current period', async () => {
it('should return the insights summary with null deviation if insights exist only for current period', async () => {
// ARRANGE
insightsByPeriodRepository.getPreviousAndCurrentPeriodTypeAggregates.mockResolvedValue([
{ period: 'current', type: TypeToNumber.success, total_value: 20 },
@@ -56,11 +56,11 @@ describe('InsightsController', () => {
// ASSERT
expect(response).toEqual({
total: { deviation: 30, unit: 'count', value: 30 },
failed: { deviation: 10, unit: 'count', value: 10 },
failureRate: { deviation: 0.33, unit: 'ratio', value: 0.33 },
averageRunTime: { deviation: 10, unit: 'time', value: 10 },
timeSaved: { deviation: 10, unit: 'time', value: 10 },
total: { deviation: null, unit: 'count', value: 30 },
failed: { deviation: null, unit: 'count', value: 10 },
failureRate: { deviation: null, unit: 'ratio', value: 0.33 },
averageRunTime: { deviation: null, unit: 'time', value: 10 },
timeSaved: { deviation: null, unit: 'time', value: 10 },
});
});

View File

@@ -784,4 +784,21 @@ describe('getInsightsSummary', () => {
total: { deviation: 3, unit: 'count', value: 4 },
});
});
test('no data for previous period should return null deviation', async () => {
// ARRANGE
// last 7 days
await createCompactedInsightsEvent(workflow, {
type: 'success',
value: 1,
periodUnit: 'day',
periodStart: DateTime.utc(),
});
// ACT
const summary = await insightsService.getInsightsSummary();
// ASSERT
expect(Object.values(summary).map((v) => v.deviation)).toEqual([null, null, null, null, null]);
});
});

View File

@@ -265,32 +265,36 @@ export class InsightsService {
const currentTimeSaved = getValueByType('current', 'time_saved_min');
const previousTimeSaved = getValueByType('previous', 'time_saved_min');
// If the previous period has no executions, we discard deviation
const getDeviation = (current: number, previous: number) =>
previousTotal === 0 ? null : current - previous;
// Return the formatted result
const result: InsightsSummary = {
averageRunTime: {
value: currentAvgRuntime,
unit: 'time',
deviation: currentAvgRuntime - previousAvgRuntime,
deviation: getDeviation(currentAvgRuntime, previousAvgRuntime),
},
failed: {
value: currentFailures,
unit: 'count',
deviation: currentFailures - previousFailures,
deviation: getDeviation(currentFailures, previousFailures),
},
failureRate: {
value: currentFailureRate,
unit: 'ratio',
deviation: currentFailureRate - previousFailureRate,
deviation: getDeviation(currentFailureRate, previousFailureRate),
},
timeSaved: {
value: currentTimeSaved,
unit: 'time',
deviation: currentTimeSaved - previousTimeSaved,
deviation: getDeviation(currentTimeSaved, previousTimeSaved),
},
total: {
value: currentTotal,
unit: 'count',
deviation: currentTotal - previousTotal,
deviation: getDeviation(currentTotal, previousTotal),
},
};