Refactor graph traversal and update vertices order

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-01 12:12:28 -03:00
commit 811ab28339
3 changed files with 38 additions and 35 deletions

View file

@ -609,17 +609,44 @@ class Graph:
"""Cuts the graph up to a given vertex and sorts the resulting subgraph."""
# Initial setup
visited = set() # To keep track of visited vertices
excluded = set() # To keep track of vertices that should be excluded
stack = [vertex_id] # Use a list as a stack for DFS
def get_successors(vertex):
# Recursively get the successors of the current vertex
successors = vertex.successors
if not successors:
return []
successors_result = []
for successor in successors:
# Just return a list of successors
next_successors = get_successors(successor)
successors_result.extend(next_successors)
successors_result.append(successor)
return successors_result
# DFS to collect all vertices that can reach the specified vertex
while stack:
current_id = stack.pop()
if current_id not in visited:
if current_id not in visited and current_id not in excluded:
visited.add(current_id)
current_vertex = self.get_vertex(current_id)
# Assuming get_predecessors is a method that returns all vertices with edges to current_vertex
for predecessor in current_vertex.predecessors:
stack.append(predecessor.id)
if current_id != vertex_id:
# Get the successors of the current vertex
for successor in current_vertex.successors:
if successor.id not in visited:
stack.append(successor.id)
else:
# We should add to visited all the vertices that are successors of the current vertex
# and their successors and so on
for successor in current_vertex.successors:
excluded.add(successor.id)
all_successors = get_successors(successor)
for successor in all_successors:
excluded.add(successor.id)
# Filter the original graph's vertices and edges to keep only those in `visited`
vertices_to_keep = [self.get_vertex(vid) for vid in visited]

View file

@ -101,7 +101,6 @@ export type FlowStoreType = {
vertices: {
verticesIds: string[];
verticesLayers: string[][];
verticesOrder: string[][];
runId: string;
} | null
) => void;
@ -109,7 +108,6 @@ export type FlowStoreType = {
verticesBuild: {
verticesIds: string[];
verticesLayers: string[][];
verticesOrder: string[][];
runId: string;
} | null;
updateBuildStatus: (nodeId: string[], status: BuildStatus) => void;

View file

@ -45,7 +45,6 @@ export async function updateVerticesOrder(
): Promise<{
verticesLayers: string[][];
verticesIds: string[];
verticesOrder: string[][];
runId: string;
}> {
return new Promise(async (resolve, reject) => {
@ -62,38 +61,16 @@ export async function updateVerticesOrder(
useFlowStore.getState().setIsBuilding(false);
throw new Error("Invalid nodes");
}
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
let verticesLayers: Array<Array<string>> = orderResponse.data.ids;
const runId = orderResponse.data.run_id;
let verticesLayers: Array<Array<string>> = [];
if (nodeId) {
for (let i = 0; i < verticesOrder.length; i += 1) {
const innerArray = verticesOrder[i];
const idIndex = innerArray.indexOf(nodeId);
if (idIndex !== -1) {
// If there's a nodeId, we want to run just that component and not the entire layer
// because a layer contains dependencies for the next layer
// and we are stopping at the layer that contains the nodeId
verticesLayers.push([innerArray[idIndex]]);
break; // Stop searching after finding the first occurrence
}
// If the targetId is not found, include the entire inner array
verticesLayers.push(innerArray);
}
} else {
verticesLayers = verticesOrder;
}
const verticesIds = verticesLayers.flat();
useFlowStore
.getState()
.updateVerticesBuild({
verticesLayers,
verticesIds,
verticesOrder,
runId,
});
resolve({ verticesLayers, verticesIds, verticesOrder, runId });
useFlowStore.getState().updateVerticesBuild({
verticesLayers,
verticesIds,
runId,
});
resolve({ verticesLayers, verticesIds, runId });
});
}
@ -114,7 +91,6 @@ export async function buildVertices({
}
const verticesIds = verticesBuild?.verticesIds!;
const verticesLayers = verticesBuild?.verticesLayers!;
const verticesOrder = verticesBuild?.verticesOrder!;
const runId = verticesBuild?.runId!;
let stop = false;
@ -122,7 +98,7 @@ export async function buildVertices({
if (validateNodes) {
try {
validateNodes(verticesOrder.flatMap((id) => id));
validateNodes(verticesIds);
} catch (e) {
return;
}
@ -193,7 +169,9 @@ async function buildVertex({
stopBuild: () => void;
}) {
try {
console.log("Building vertex", id);
const buildRes = await postBuildVertex(flowId, id, input_value);
console.log(buildRes);
const buildData: VertexBuildTypeAPI = buildRes.data;
if (onBuildUpdate) {
if (!buildData.valid) {