Refactor buildUtils and flowStore to improve performance

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-03 18:11:06 -03:00
commit af20ca0f6e
3 changed files with 74 additions and 37 deletions

View file

@ -444,25 +444,34 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
function handleBuildUpdate( function handleBuildUpdate(
vertexBuildData: VertexBuildTypeAPI, vertexBuildData: VertexBuildTypeAPI,
status: BuildStatus, status: BuildStatus,
buildId: string runId: string
) { ) {
if (vertexBuildData && vertexBuildData.inactive_vertices) { if (vertexBuildData && vertexBuildData.inactive_vertices) {
get().removeFromVerticesBuild(vertexBuildData.inactive_vertices); get().removeFromVerticesBuild(vertexBuildData.inactive_vertices);
} }
get().verticesBuild && if (vertexBuildData.next_vertices_ids) {
// next_vertices_ids is a list of vertices that are going to be built next
// verticesLayers is a list of list of vertices ids, where each list is a layer of vertices
// we want to add a new layer (next_vertices_ids) to the list of layers (verticesLayers)
// and the values of next_vertices_ids to the list of vertices ids (verticesIds)
const newLayers = [
...get().verticesBuild!.verticesLayers,
vertexBuildData.next_vertices_ids,
];
const newIds = [
...get().verticesBuild!.verticesIds,
...vertexBuildData.next_vertices_ids,
];
get().updateVerticesBuild({ get().updateVerticesBuild({
verticesIds: [ verticesIds: newIds,
...get().verticesBuild!.verticesIds, verticesLayers: newLayers,
vertexBuildData.id, runId: runId,
],
verticesLayers: [
...get().verticesBuild!.verticesLayers,
vertexBuildData.next_vertices_ids,
],
runId: vertexBuildData.run_id,
}); });
get().updateBuildStatus(newIds, BuildStatus.TO_BUILD);
}
get().addDataToFlowPool( get().addDataToFlowPool(
{ ...vertexBuildData, buildId }, { ...vertexBuildData, buildId: runId },
vertexBuildData.id vertexBuildData.id
); );
@ -514,6 +523,7 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
runId: string; runId: string;
} | null } | null
) => { ) => {
console.log("updateVerticesBuild", vertices);
set({ verticesBuild: vertices }); set({ verticesBuild: vertices });
}, },
verticesBuild: null, verticesBuild: null,

View file

@ -134,7 +134,7 @@ export type Component = {
}; };
export type VerticesOrderTypeAPI = { export type VerticesOrderTypeAPI = {
ids: Array<Array<string>>; ids: Array<string>;
run_id: string; run_id: string;
}; };

View file

@ -31,6 +31,8 @@ function getInactiveVertexData(vertexId: string): VertexBuildTypeAPI {
id: vertexId, id: vertexId,
data: inactiveData, data: inactiveData,
params: "Inactive", params: "Inactive",
run_id: "",
next_vertices_ids: [],
inactive_vertices: null, inactive_vertices: null,
valid: false, valid: false,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
@ -61,27 +63,26 @@ 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];
let verticesLayers: Array<Array<string>> = [];
const runId = orderResponse.data.run_id; const runId = orderResponse.data.run_id;
if (nodeId) { // if (nodeId) {
for (let i = 0; i < verticesOrder.length; i += 1) { // for (let i = 0; i < verticesOrder.length; i += 1) {
const innerArray = verticesOrder[i]; // const innerArray = verticesOrder[i];
const idIndex = innerArray.indexOf(nodeId); // const idIndex = innerArray.indexOf(nodeId);
if (idIndex !== -1) { // if (idIndex !== -1) {
// If there's a nodeId, we want to run just that component and not the entire layer // // 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 // // because a layer contains dependencies for the next layer
// and we are stopping at the layer that contains the nodeId // // and we are stopping at the layer that contains the nodeId
verticesLayers.push([innerArray[idIndex]]); // verticesLayers.push([innerArray[idIndex]]);
break; // Stop searching after finding the first occurrence // break; // Stop searching after finding the first occurrence
} // }
// If the targetId is not found, include the entire inner array // // If the targetId is not found, include the entire inner array
verticesLayers.push(innerArray); // verticesLayers.push(innerArray);
} // }
} else { // } else {
verticesLayers = verticesOrder; // verticesLayers = verticesOrder;
} // }
const verticesIds = verticesOrder.flat(); const verticesIds = orderResponse.data.ids;
useFlowStore.getState().updateVerticesBuild({ useFlowStore.getState().updateVerticesBuild({
verticesLayers, verticesLayers,
verticesIds, verticesIds,
@ -128,14 +129,36 @@ export async function buildVertices({
let currentLayerIndex = 0; // Start with the first layer let currentLayerIndex = 0; // Start with the first layer
// Set each vertex state to building // Set each vertex state to building
const buildResults: Array<boolean> = []; const buildResults: Array<boolean> = [];
console.log(verticesLayers);
while (currentLayerIndex < verticesLayers.length) { // Build each layer
const currentLayer = verticesLayers[currentLayerIndex]; while (
currentLayerIndex <
(useFlowStore.getState().verticesBuild?.verticesLayers! || []).length
) {
// Get the current layer
const currentLayer =
useFlowStore.getState().verticesBuild?.verticesLayers![currentLayerIndex];
// If there are no more layers, we are done
if (!currentLayer) {
if (onBuildComplete) {
const allNodesValid = buildResults.every((result) => result);
onBuildComplete(allNodesValid);
useFlowStore.getState().setIsBuilding(false);
}
return;
}
// If there is a callback for the start of the build, call it
if (onBuildStart) onBuildStart(currentLayer); if (onBuildStart) onBuildStart(currentLayer);
// Build each vertex in the current layer
await Promise.all( await Promise.all(
currentLayer.map(async (vertexId) => { currentLayer.map(async (vertexId) => {
// Check if id is in the list of inactive nodes // Check if id is in the list of inactive nodes
if (!verticesIds.includes(vertexId) && onBuildUpdate) { if (
!useFlowStore
.getState()
.verticesBuild?.verticesIds.includes(vertexId) &&
onBuildUpdate
) {
// If it is, skip building and set the state to inactive // If it is, skip building and set the state to inactive
onBuildUpdate( onBuildUpdate(
getInactiveVertexData(vertexId), getInactiveVertexData(vertexId),
@ -145,6 +168,7 @@ export async function buildVertices({
buildResults.push(false); buildResults.push(false);
return; return;
} }
// Build the vertex
await buildVertex({ await buildVertex({
flowId, flowId,
id: vertexId, id: vertexId,
@ -164,6 +188,9 @@ export async function buildVertices({
} }
}) })
); );
// Once the current layer is built, move to the next layer
currentLayerIndex += 1;
console.log(useFlowStore.getState().verticesBuild?.verticesLayers);
if (stop) { if (stop) {
break; break;