Refactor graph traversal and update vertices order
This commit is contained in:
parent
3a136362c8
commit
811ab28339
3 changed files with 38 additions and 35 deletions
|
|
@ -609,17 +609,44 @@ class Graph:
|
||||||
"""Cuts the graph up to a given vertex and sorts the resulting subgraph."""
|
"""Cuts the graph up to a given vertex and sorts the resulting subgraph."""
|
||||||
# Initial setup
|
# Initial setup
|
||||||
visited = set() # To keep track of visited vertices
|
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
|
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
|
# DFS to collect all vertices that can reach the specified vertex
|
||||||
while stack:
|
while stack:
|
||||||
current_id = stack.pop()
|
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)
|
visited.add(current_id)
|
||||||
current_vertex = self.get_vertex(current_id)
|
current_vertex = self.get_vertex(current_id)
|
||||||
# Assuming get_predecessors is a method that returns all vertices with edges to current_vertex
|
# Assuming get_predecessors is a method that returns all vertices with edges to current_vertex
|
||||||
for predecessor in current_vertex.predecessors:
|
for predecessor in current_vertex.predecessors:
|
||||||
stack.append(predecessor.id)
|
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`
|
# 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]
|
vertices_to_keep = [self.get_vertex(vid) for vid in visited]
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,6 @@ export type FlowStoreType = {
|
||||||
vertices: {
|
vertices: {
|
||||||
verticesIds: string[];
|
verticesIds: string[];
|
||||||
verticesLayers: string[][];
|
verticesLayers: string[][];
|
||||||
verticesOrder: string[][];
|
|
||||||
runId: string;
|
runId: string;
|
||||||
} | null
|
} | null
|
||||||
) => void;
|
) => void;
|
||||||
|
|
@ -109,7 +108,6 @@ export type FlowStoreType = {
|
||||||
verticesBuild: {
|
verticesBuild: {
|
||||||
verticesIds: string[];
|
verticesIds: string[];
|
||||||
verticesLayers: string[][];
|
verticesLayers: string[][];
|
||||||
verticesOrder: string[][];
|
|
||||||
runId: string;
|
runId: string;
|
||||||
} | null;
|
} | null;
|
||||||
updateBuildStatus: (nodeId: string[], status: BuildStatus) => void;
|
updateBuildStatus: (nodeId: string[], status: BuildStatus) => void;
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,6 @@ export async function updateVerticesOrder(
|
||||||
): Promise<{
|
): Promise<{
|
||||||
verticesLayers: string[][];
|
verticesLayers: string[][];
|
||||||
verticesIds: string[];
|
verticesIds: string[];
|
||||||
verticesOrder: string[][];
|
|
||||||
runId: string;
|
runId: string;
|
||||||
}> {
|
}> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
|
@ -62,38 +61,16 @@ export async function updateVerticesOrder(
|
||||||
useFlowStore.getState().setIsBuilding(false);
|
useFlowStore.getState().setIsBuilding(false);
|
||||||
throw new Error("Invalid nodes");
|
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;
|
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();
|
const verticesIds = verticesLayers.flat();
|
||||||
useFlowStore
|
useFlowStore.getState().updateVerticesBuild({
|
||||||
.getState()
|
verticesLayers,
|
||||||
.updateVerticesBuild({
|
verticesIds,
|
||||||
verticesLayers,
|
runId,
|
||||||
verticesIds,
|
});
|
||||||
verticesOrder,
|
resolve({ verticesLayers, verticesIds, runId });
|
||||||
runId,
|
|
||||||
});
|
|
||||||
resolve({ verticesLayers, verticesIds, verticesOrder, runId });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,7 +91,6 @@ export async function buildVertices({
|
||||||
}
|
}
|
||||||
const verticesIds = verticesBuild?.verticesIds!;
|
const verticesIds = verticesBuild?.verticesIds!;
|
||||||
const verticesLayers = verticesBuild?.verticesLayers!;
|
const verticesLayers = verticesBuild?.verticesLayers!;
|
||||||
const verticesOrder = verticesBuild?.verticesOrder!;
|
|
||||||
const runId = verticesBuild?.runId!;
|
const runId = verticesBuild?.runId!;
|
||||||
let stop = false;
|
let stop = false;
|
||||||
|
|
||||||
|
|
@ -122,7 +98,7 @@ export async function buildVertices({
|
||||||
|
|
||||||
if (validateNodes) {
|
if (validateNodes) {
|
||||||
try {
|
try {
|
||||||
validateNodes(verticesOrder.flatMap((id) => id));
|
validateNodes(verticesIds);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -193,7 +169,9 @@ async function buildVertex({
|
||||||
stopBuild: () => void;
|
stopBuild: () => void;
|
||||||
}) {
|
}) {
|
||||||
try {
|
try {
|
||||||
|
console.log("Building vertex", id);
|
||||||
const buildRes = await postBuildVertex(flowId, id, input_value);
|
const buildRes = await postBuildVertex(flowId, id, input_value);
|
||||||
|
console.log(buildRes);
|
||||||
const buildData: VertexBuildTypeAPI = buildRes.data;
|
const buildData: VertexBuildTypeAPI = buildRes.data;
|
||||||
if (onBuildUpdate) {
|
if (onBuildUpdate) {
|
||||||
if (!buildData.valid) {
|
if (!buildData.valid) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue