Skip to content

Commit

Permalink
Merge pull request #84 from ydayagi/flpath1893
Browse files Browse the repository at this point in the history
fix(orchestrator): fix fetchWorkflowOverviewBySource to fetch less instances
  • Loading branch information
batzionb authored Nov 27, 2024
2 parents 4aa876e + 9547093 commit 53b5e33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
5 changes: 5 additions & 0 deletions workspaces/orchestrator/.changeset/red-boats-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-orchestrator-backend': major
---

fix SonataFlowService.ts:fetchWorkflowOverviewBySource to fetch less instances
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class DataIndexService {
limit: number;
offset: number;
}): Promise<ProcessInstance[]> {
const graphQlQuery = `{ ProcessInstances(where: {processId: {equal: "${args.definitionId}" } }, pagination: {limit: ${args.limit}, offset: ${args.offset}}) { id, processName, state, start, end } }`;
const graphQlQuery = `{ ProcessInstances(where: {processId: {equal: "${args.definitionId}" } }, orderBy: {start:DESC}, pagination: {limit: ${args.limit}, offset: ${args.offset}}) { id, processName, state, start, end } }`;

const result = await this.client.query(graphQlQuery, {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
Filter,
fromWorkflowSource,
getWorkflowCategory,
ProcessInstance,
ProcessInstanceStateValues,
ProcessInstanceVariables,
WorkflowDefinition,
Expand Down Expand Up @@ -171,43 +170,25 @@ export class SonataFlowService {
private async fetchWorkflowOverviewBySource(
source: string,
): Promise<WorkflowOverview | undefined> {
let processInstances: ProcessInstance[] = [];
const limit = 10;
let offset: number = 0;

let lastTriggered: Date = new Date(0);
let lastRunStatus: ProcessInstanceStateValues | undefined;
let lastRunId: string | undefined;
let counter = 0;
let totalDuration = 0;
const definition = fromWorkflowSource(source);

do {
processInstances =
await this.dataIndexService.fetchInstancesByDefinitionId({
definitionId: definition.id,
limit,
offset,
});
const processInstances =
await this.dataIndexService.fetchInstancesByDefinitionId({
definitionId: definition.id,
limit: 1,
offset: 0,
});

const pInstance = processInstances[0];

for (const pInstance of processInstances) {
if (!pInstance.start) {
continue;
}
if (new Date(pInstance.start) > lastTriggered) {
lastRunId = pInstance.id;
lastTriggered = new Date(pInstance.start);
lastRunStatus = pInstance.state;
}
if (pInstance.end) {
const start: Date = new Date(pInstance.start);
const end: Date = new Date(pInstance.end);
totalDuration += end.valueOf() - start.valueOf();
counter++;
}
}
offset += limit;
} while (processInstances.length > 0);
if (pInstance.start) {
lastRunId = pInstance.id;
lastTriggered = new Date(pInstance.start);
lastRunStatus = pInstance.state;
}

return {
workflowId: definition.id,
Expand All @@ -217,7 +198,6 @@ export class SonataFlowService {
lastTriggeredMs: lastTriggered.getTime(),
lastRunStatus,
category: getWorkflowCategory(definition),
avgDurationMs: counter ? totalDuration / counter : undefined,
description: definition.description,
};
}
Expand Down

0 comments on commit 53b5e33

Please sign in to comment.