2023-10-17 01:31:00 +02:00
|
|
|
|
2023-11-10 19:08:07 +01:00
|
|
|
export interface Operation {
|
|
|
|
values: {id:any};
|
|
|
|
type: string;
|
|
|
|
operations?: Operation[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function organizeWaitOperations(operations: Operation[]) {
|
2023-10-17 01:31:00 +02:00
|
|
|
|
|
|
|
// Initialize an object to store the counts and associated "done" operations
|
|
|
|
const waitCounts = {};
|
|
|
|
const doneOperations = {};
|
|
|
|
|
|
|
|
// Function to count "type: wait" operations and associate "done" operations per id
|
2023-11-10 19:08:07 +01:00
|
|
|
function countWaitOperationsAndDone(operations: Operation[]) {
|
2023-10-17 01:31:00 +02:00
|
|
|
for (const operation of operations) {
|
|
|
|
if (operation.type === "wait") {
|
|
|
|
const id = operation.values.id;
|
|
|
|
if (id in waitCounts) {
|
|
|
|
waitCounts[id]++;
|
|
|
|
} else {
|
|
|
|
waitCounts[id] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation.type === "done") {
|
|
|
|
const id = operation.values.id;
|
|
|
|
doneOperations[id] = operation;
|
|
|
|
}
|
|
|
|
if (operation.operations) {
|
|
|
|
countWaitOperationsAndDone(operation.operations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start counting and associating from the root operations
|
|
|
|
countWaitOperationsAndDone(operations);
|
|
|
|
|
|
|
|
// Combine counts and associated "done" operations
|
|
|
|
const result = {};
|
|
|
|
for (const id in waitCounts) {
|
|
|
|
result[id] = {
|
|
|
|
waitCount: waitCounts[id],
|
|
|
|
doneOperation: doneOperations[id],
|
|
|
|
input: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|