Skip to content

Commit bbcdc56

Browse files
authored
chore: update the orchestrator workflow overview api (#3328)
* chore: update the workflow overview api to include the success ratio for a worfklow. Also add the total runs(for the last 30 days) for each workflow * squash: add changeset * squash: change changeset to be a patch * squash: small refactor * squash: return total count, error count, success count. this will be needed for RHIDP-14335 * squash: add a workflowRunStats object to return the stats in * squash: build api reports * squash: build api reports * squash: add the stats for the detail page of the workflow. fixes RHIDP-14335 * squash: only count success and error in the total count and success ratio
1 parent daa1e8b commit bbcdc56

19 files changed

Lines changed: 1067 additions & 356 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+
update the workflow overview api to include the success ratio for a worfklow. Also add the total runs(for the last 30 days) for each workflow

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import type { Config } from '@backstage/config';
1919

2020
import fs from 'fs-extra';
2121

22+
import {
23+
ProcessInstance,
24+
ProcessInstanceState,
25+
} from '@red-hat-developer-hub/backstage-plugin-orchestrator-common';
26+
2227
import os from 'os';
2328

2429
export async function retryAsyncFunction<T>(args: {
@@ -90,3 +95,37 @@ export async function executeWithRetry(
9095
export function delay(time: number) {
9196
return new Promise(r => setTimeout(r, time));
9297
}
98+
99+
export function groupByProcessIdAndVersion(instances: ProcessInstance[]) {
100+
return instances.reduce<Record<string, ProcessInstance[]>>((acc, item) => {
101+
acc[`${item.processId}-${item.version}`] ??= [];
102+
acc[`${item.processId}-${item.version}`].push(item);
103+
return acc;
104+
}, {});
105+
}
106+
107+
export function getWorkflowRunStats(
108+
groupedData: Record<string, ProcessInstance[]>,
109+
) {
110+
return Object.entries(groupedData).map(([processIdVersion, items]) => {
111+
const successCount = items.filter(
112+
item => item.state === ProcessInstanceState.Completed,
113+
).length;
114+
const errorCount = items.filter(
115+
item => item.state === ProcessInstanceState.Error,
116+
).length;
117+
const runsLastMonth = items.filter(
118+
item =>
119+
item.start &&
120+
new Date(item.start) > new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
121+
);
122+
return {
123+
processIdVersion,
124+
successCount,
125+
errorCount,
126+
totalCount: successCount + errorCount,
127+
successRatio: successCount / (successCount + errorCount),
128+
runsLastMonth: runsLastMonth.length,
129+
};
130+
});
131+
}

0 commit comments

Comments
 (0)