Skip to content

Commit 43e0722

Browse files
authored
fix: Orchestrator backend - adds the average duration it takes a workflow to run to the overview API. (#3451)
* fix: add the average duration for a workflow run in the stats * squash: add changeset * squash: add a test * squash: build api reports
1 parent 3036a95 commit 43e0722

13 files changed

Lines changed: 482 additions & 370 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-orchestrator-backend': patch
3+
'@red-hat-developer-hub/backstage-plugin-orchestrator-common': patch
4+
---
5+
6+
add the average duration for a workflow run in the stats

workspaces/orchestrator/plugins/orchestrator-backend/src/service/Helper.test.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 The Backstage Authors
2+
* Copyright Red Hat, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { retryAsyncFunction } from './Helper';
16+
17+
import {
18+
ProcessInstance,
19+
ProcessInstanceState,
20+
} from '@red-hat-developer-hub/backstage-plugin-orchestrator-common';
21+
22+
import { getWorkflowRunStats, retryAsyncFunction } from './Helper';
1723

1824
describe('retryAsyncFunction', () => {
1925
const successfulResponse = 'Success';
@@ -62,3 +68,65 @@ describe('retryAsyncFunction', () => {
6268
expect(asyncFns).toHaveBeenCalledTimes(4);
6369
});
6470
});
71+
72+
describe('getWorkflowRunStats', () => {
73+
const createProcessInstance = (
74+
overrides: Partial<ProcessInstance> &
75+
Pick<ProcessInstance, 'id' | 'processId'>,
76+
): ProcessInstance => ({
77+
endpoint: 'http://example.com',
78+
nodes: [],
79+
version: '1.0',
80+
state: ProcessInstanceState.Completed,
81+
...overrides,
82+
});
83+
84+
it('calculates averageTimeToComplete from instance start and end times', () => {
85+
const tenMinutesMs = 10 * 60 * 1000;
86+
const twentyMinutesMs = 20 * 60 * 1000;
87+
88+
const result = getWorkflowRunStats({
89+
'workflow-a-1.0': [
90+
createProcessInstance({
91+
id: 'instance-1',
92+
processId: 'workflow-a',
93+
start: '2024-01-01T00:00:00.000Z',
94+
end: '2024-01-01T00:10:00.000Z',
95+
}),
96+
createProcessInstance({
97+
id: 'instance-2',
98+
processId: 'workflow-a',
99+
start: '2024-01-01T00:00:00.000Z',
100+
end: '2024-01-01T00:20:00.000Z',
101+
}),
102+
],
103+
});
104+
105+
expect(result).toHaveLength(1);
106+
expect(result[0].averageTimeToComplete).toBe(
107+
(tenMinutesMs + twentyMinutesMs) / 2,
108+
);
109+
});
110+
111+
it('treats instances without start or end as zero duration when calculating averageTimeToComplete', () => {
112+
const tenMinutesMs = 10 * 60 * 1000;
113+
114+
const result = getWorkflowRunStats({
115+
'workflow-a-1.0': [
116+
createProcessInstance({
117+
id: 'instance-1',
118+
processId: 'workflow-a',
119+
start: '2024-01-01T00:00:00.000Z',
120+
end: '2024-01-01T00:10:00.000Z',
121+
}),
122+
createProcessInstance({
123+
id: 'instance-2',
124+
processId: 'workflow-a',
125+
start: '2024-01-01T00:00:00.000Z',
126+
}),
127+
],
128+
});
129+
130+
expect(result[0].averageTimeToComplete).toBe(tenMinutesMs / 2);
131+
});
132+
});

workspaces/orchestrator/plugins/orchestrator-backend/src/service/Helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ export function getWorkflowRunStats(
108108
groupedData: Record<string, ProcessInstance[]>,
109109
) {
110110
return Object.entries(groupedData).map(([processIdVersion, items]) => {
111+
const averageTimeToComplete =
112+
items.reduce((acc, item) => {
113+
return (
114+
acc +
115+
(item.end && item.start
116+
? new Date(item.end).getTime() - new Date(item.start).getTime()
117+
: 0)
118+
);
119+
}, 0) / items.length;
111120
const successCount = items.filter(
112121
item => item.state === ProcessInstanceState.Completed,
113122
).length;
@@ -126,6 +135,7 @@ export function getWorkflowRunStats(
126135
totalCount: successCount + errorCount,
127136
successRatio: successCount / (successCount + errorCount),
128137
runsLastMonth: runsLastMonth.length,
138+
averageTimeToComplete,
129139
};
130140
});
131141
}

workspaces/orchestrator/plugins/orchestrator-backend/src/service/SonataFlowService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export class SonataFlowService {
187187
successCount: stats.successCount,
188188
errorCount: stats.errorCount,
189189
totalCount: stats.totalCount,
190+
averageTimeToComplete: stats.averageTimeToComplete,
190191
};
191192
}
192193
return overview;
@@ -450,6 +451,7 @@ export class SonataFlowService {
450451
successCount: stats.successCount,
451452
errorCount: stats.errorCount,
452453
totalCount: stats.totalCount,
454+
averageTimeToComplete: stats.averageTimeToComplete,
453455
};
454456
}
455457
}

workspaces/orchestrator/plugins/orchestrator-common/report.api.md

Lines changed: 368 additions & 360 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fc09eb12c74f66b829f6f8b8364df76293b6fc06
1+
113b6a6930902bdb566ef02b53502f50ae7645c3

workspaces/orchestrator/plugins/orchestrator-common/src/generated/api/definition.ts

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

workspaces/orchestrator/plugins/orchestrator-common/src/generated/client/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,12 @@ export interface WorkflowRunStatsDTO {
943943
* @memberof WorkflowRunStatsDTO
944944
*/
945945
'totalCount'?: number;
946+
/**
947+
* Average time to complete a workflow run in milliseconds
948+
* @type {number}
949+
* @memberof WorkflowRunStatsDTO
950+
*/
951+
'averageTimeToComplete'?: number;
946952
}
947953
/**
948954
*

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/api-doc/orchestrator-api.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ spec:
458458
totalCount:
459459
type: integer
460460
description: Total number of workflow runs
461+
averageTimeToComplete:
462+
type: number
463+
description: Average time to complete a workflow run in milliseconds
461464
WorkflowOverviewDTO:
462465
type: object
463466
properties:

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/html/index.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,10 @@
14051405
"totalCount" : {
14061406
"type" : "integer",
14071407
"description" : "Total number of workflow runs"
1408+
},
1409+
"averageTimeToComplete" : {
1410+
"type" : "number",
1411+
"description" : "Average time to complete a workflow run in milliseconds"
14081412
}
14091413
}
14101414
};
@@ -2862,8 +2866,8 @@ <h3>Usage and SDK Samples</h3>
28622866
"http://localhost/v2/workflows/instances" \
28632867
-d '{
28642868
&quot;paginationInfo&quot; : {
2865-
&quot;offset&quot; : 9.301444243932576,
2866-
&quot;pageSize&quot; : 7.061401241503109,
2869+
&quot;offset&quot; : 3.616076749251911,
2870+
&quot;pageSize&quot; : 9.301444243932576,
28672871
&quot;orderDirection&quot; : &quot;ASC&quot;,
28682872
&quot;orderBy&quot; : &quot;orderBy&quot;
28692873
},
@@ -3765,8 +3769,8 @@ <h3>Usage and SDK Samples</h3>
37653769
"http://localhost/v2/workflows/{workflowId}/instances" \
37663770
-d '{
37673771
&quot;paginationInfo&quot; : {
3768-
&quot;offset&quot; : 9.301444243932576,
3769-
&quot;pageSize&quot; : 7.061401241503109,
3772+
&quot;offset&quot; : 3.616076749251911,
3773+
&quot;pageSize&quot; : 9.301444243932576,
37703774
&quot;orderDirection&quot; : &quot;ASC&quot;,
37713775
&quot;orderBy&quot; : &quot;orderBy&quot;
37723776
},
@@ -5913,8 +5917,8 @@ <h3>Usage and SDK Samples</h3>
59135917
"http://localhost/v2/workflows/overview" \
59145918
-d '{
59155919
&quot;paginationInfo&quot; : {
5916-
&quot;offset&quot; : 9.301444243932576,
5917-
&quot;pageSize&quot; : 7.061401241503109,
5920+
&quot;offset&quot; : 3.616076749251911,
5921+
&quot;pageSize&quot; : 9.301444243932576,
59185922
&quot;orderDirection&quot; : &quot;ASC&quot;,
59195923
&quot;orderBy&quot; : &quot;orderBy&quot;
59205924
},

0 commit comments

Comments
 (0)