A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.
feat(app): delegate layouting to web worker
dextmorgn5c3610d4f8f0
diff --git a/flowsint-app/src/components/graphs/graph-viewer.tsx b/flowsint-app/src/components/graphs/graph-viewer.tsxindex 9b99df37..30438f61 100644--- a/flowsint-app/src/components/graphs/graph-viewer.tsx+++ b/flowsint-app/src/components/graphs/graph-viewer.tsx@@ -399,35 +399,37 @@ const GraphViewer: React.FC<GraphViewerProps> = ({// Regenerate layout by removing fixed positions and reheating simulationconst regenerateLayout = useCallback((layoutType: 'force' | 'hierarchy') => {if (!graphRef.current) {- console.error('[Graph] graphRef.current is null')throw new Error('Graph reference is not available')}if (!graphData || !graphData.nodes) {- console.error('[Graph] No nodes in graph data')throw new Error('No nodes available in graph')}-- // Show blur effectsetIsRegeneratingLayout(true)- // Apply layout using dedicated hook- applyLayout({- layoutType,- nodes: graphData.nodes,- edges: graphData.links,- })+ // Run layout calculation in worker (non-blocking)+ setTimeout(async () => {+ try {+ await applyLayout({+ layoutType,+ nodes: graphData.nodes,+ edges: graphData.links,+ })- // Zoom to fit after a delay to see the new layout- setTimeout(() => {- if (graphRef.current && typeof graphRef.current.zoomToFit === 'function') {- graphRef.current.zoomToFit(400)- }- // Hide blur effect after zoom animation completes- setTimeout(() => {+ // Zoom to fit after layout is complete+ if (graphRef.current && typeof graphRef.current.zoomToFit === 'function') {+ graphRef.current.zoomToFit(400)+ }++ // Hide blur effect after zoom animation completes+ setTimeout(() => {+ setIsRegeneratingLayout(false)+ }, 400)+ } catch (error) {+ console.error('Layout calculation failed:', error)setIsRegeneratingLayout(false)- }, 400)- }, 500)+ }+ })}, [graphData, applyLayout])// Optimized graph initialization callbackdiff --git a/flowsint-app/src/hooks/use-layout.ts b/flowsint-app/src/hooks/use-layout.tsindex 1305771a..d7769a96 100644--- a/flowsint-app/src/hooks/use-layout.ts+++ b/flowsint-app/src/hooks/use-layout.ts@@ -1,11 +1,11 @@-import { useCallback } from 'react'-import { getDagreLayoutedElements, getForceLayoutedElements } from '@/lib/utils'+import { useCallback, useRef, useEffect } from 'react'import { GraphNode, GraphEdge } from '@/types'interface UseLayoutProps {forceSettings: anycontainerSize: { width: number; height: number }saveAllNodePositions: (nodes: any[], force?: boolean) => void+ onProgress?: (progress: number) => void}interface LayoutOptions {@@ -18,66 +18,110 @@ export function useLayout({forceSettings,containerSize,saveAllNodePositions,+ onProgress,}: UseLayoutProps) {+ const workerRef = useRef<Worker | null>(null)++ // Initialize worker once+ useEffect(() => {+ workerRef.current = new Worker(+ new URL('../workers/layout.worker.ts', import.meta.url),+ { type: 'module' }+ )++ return () => {+ // Cleanup worker on unmount+ workerRef.current?.terminate()+ }+ }, [])+const applyLayout = useCallback(- ({ layoutType, nodes, edges }: LayoutOptions) => {+ async ({ layoutType, nodes, edges }: LayoutOptions) => {+ if (!workerRef.current) {+ throw new Error('Layout worker not initialized')+ }+// Remove fx and fy from all nodes to allow repositioningnodes.forEach((node: any) => {delete node.fxdelete node.fy})- if (layoutType === 'hierarchy') {- // Apply dagre layout for hierarchical positioning- const { nodes: layoutedNodes } = getDagreLayoutedElements(nodes, edges, {- dagLevelDistance: forceSettings.dagLevelDistance?.value ?? 50,- })-- // Apply the calculated positions to the graph nodes- layoutedNodes.forEach((layoutedNode: any) => {- const graphNode = nodes.find((n: any) => n.id === layoutedNode.id) as any- if (graphNode && layoutedNode.x !== undefined && layoutedNode.y !== undefined) {- graphNode.x = layoutedNode.x- graphNode.y = layoutedNode.y- graphNode.fx = layoutedNode.x- graphNode.fy = layoutedNode.y- }- })-- // Save all node positions immediately after hierarchy layout- saveAllNodePositions(nodes, true)- } else {- // Force layout: calculate positions using D3-force- const { nodes: layoutedNodes } = getForceLayoutedElements(nodes, edges, {- width: containerSize.width || 800,- height: containerSize.height || 600,- chargeStrength: forceSettings.d3ForceChargeStrength?.value ?? -30,- linkDistance: forceSettings.d3ForceLinkDistance?.value ?? 30,- linkStrength: forceSettings.d3ForceLinkStrength?.value ?? 2,- alphaDecay: forceSettings.d3AlphaDecay?.value ?? 0.045,- alphaMin: forceSettings.d3AlphaMin?.value ?? 0,- velocityDecay: forceSettings.d3VelocityDecay?.value ?? 0.41,- iterations: forceSettings.cooldownTicks?.value ?? 300,- })-- // Apply the calculated positions to the graph nodes- layoutedNodes.forEach((layoutedNode: any) => {- const graphNode = nodes.find((n: any) => n.id === layoutedNode.id) as any- if (graphNode && layoutedNode.x !== undefined && layoutedNode.y !== undefined) {- graphNode.x = layoutedNode.x- graphNode.y = layoutedNode.y- graphNode.fx = layoutedNode.x- graphNode.fy = layoutedNode.y+ return new Promise<GraphNode[]>((resolve, reject) => {+ const worker = workerRef.current!++ const handleMessage = (event: MessageEvent) => {+ if (event.data.type === 'progress') {+ onProgress?.(event.data.progress)+ } else if (event.data.type === 'complete') {+ const { nodes: layoutedNodes } = event.data.result++ // Apply the calculated positions to the graph nodes+ layoutedNodes.forEach((layoutedNode: any) => {+ const graphNode = nodes.find((n: any) => n.id === layoutedNode.id) as any+ if (graphNode && layoutedNode.x !== undefined && layoutedNode.y !== undefined) {+ graphNode.x = layoutedNode.x+ graphNode.y = layoutedNode.y+ graphNode.fx = layoutedNode.x+ graphNode.fy = layoutedNode.y+ }+ })++ // Save all node positions+ saveAllNodePositions(nodes, true)++ // Cleanup listener+ worker.removeEventListener('message', handleMessage)+ worker.removeEventListener('error', handleError)++ resolve(nodes)+ } else if (event.data.type === 'error') {+ worker.removeEventListener('message', handleMessage)+ worker.removeEventListener('error', handleError)+ reject(new Error(event.data.error))}- })+ }- // Save all node positions immediately after force layout- saveAllNodePositions(nodes, true)- }+ const handleError = (error: ErrorEvent) => {+ worker.removeEventListener('message', handleMessage)+ worker.removeEventListener('error', handleError)+ reject(error)+ }++ worker.addEventListener('message', handleMessage)+ worker.addEventListener('error', handleError)- return nodes+ // Send layout task to worker+ if (layoutType === 'hierarchy') {+ worker.postMessage({+ type: 'dagre',+ nodes: nodes.map(n => ({ ...n })), // Clone to avoid reference issues+ edges: edges.map(e => ({ ...e })),+ options: {+ dagLevelDistance: forceSettings.dagLevelDistance?.value ?? 50,+ },+ })+ } else {+ worker.postMessage({+ type: 'force',+ nodes: nodes.map(n => ({ ...n })),+ edges: edges.map(e => ({ ...e })),+ options: {+ width: containerSize.width || 800,+ height: containerSize.height || 600,+ chargeStrength: forceSettings.d3ForceChargeStrength?.value ?? -30,+ linkDistance: forceSettings.d3ForceLinkDistance?.value ?? 30,+ linkStrength: forceSettings.d3ForceLinkStrength?.value ?? 2,+ alphaDecay: forceSettings.d3AlphaDecay?.value ?? 0.045,+ alphaMin: forceSettings.d3AlphaMin?.value ?? 0,+ velocityDecay: forceSettings.d3VelocityDecay?.value ?? 0.41,+ iterations: forceSettings.cooldownTicks?.value ?? 300,+ },+ })+ }+ })},- [forceSettings, containerSize, saveAllNodePositions]+ [forceSettings, containerSize, saveAllNodePositions, onProgress])return { applyLayout }diff --git a/flowsint-app/src/workers/layout.worker.ts b/flowsint-app/src/workers/layout.worker.tsnew file mode 100644index 00000000..a5e76e9d--- /dev/null+++ b/flowsint-app/src/workers/layout.worker.ts@@ -0,0 +1,206 @@+import Dagre from '@dagrejs/dagre'+import {+ forceSimulation,+ forceLink,+ forceManyBody,+ forceCenter,+} from 'd3-force'++export interface GraphNode {+ id: string+ x?: number+ y?: number+ fx?: number+ fy?: number+ [key: string]: any+}++export interface GraphEdge {+ id: string+ source: string | GraphNode+ target: string | GraphNode+ [key: string]: any+}++interface DagreLayoutOptions {+ direction?: string+ strength?: number+ distance?: number+ iterations?: number+ dagLevelDistance?: number+}++interface ForceLayoutOptions {+ width?: number+ height?: number+ chargeStrength?: number+ linkDistance?: number+ linkStrength?: number+ alphaDecay?: number+ alphaMin?: number+ velocityDecay?: number+ iterations?: number+}++interface LayoutMessage {+ type: 'dagre' | 'force'+ nodes: GraphNode[]+ edges: GraphEdge[]+ options: DagreLayoutOptions | ForceLayoutOptions+}++// Dagre layout computation+function computeDagreLayout(+ nodes: GraphNode[],+ edges: GraphEdge[],+ options: DagreLayoutOptions = {}+) {+ const {+ direction = 'TB',+ dagLevelDistance = 50,+ } = options++ const g = new Dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({}))++ // Configure dagre with proper spacing+ g.setGraph({+ rankdir: direction,+ nodesep: dagLevelDistance,+ ranksep: dagLevelDistance,+ marginx: 10,+ marginy: 10,+ })++ // Set node dimensions+ const nodeWidth = 50+ const nodeHeight = 50++ nodes.forEach((node) =>+ g.setNode(node.id, {+ width: nodeWidth,+ height: nodeHeight,+ })+ )++ edges.forEach((edge) => {+ const sourceId = typeof edge.source === 'object' ? edge.source.id : edge.source+ const targetId = typeof edge.target === 'object' ? edge.target.id : edge.target+ g.setEdge(sourceId, targetId)+ })++ Dagre.layout(g)++ const layoutedNodes = nodes.map((node) => {+ const nodeWithPosition = g.node(node.id)+ return {+ ...node,+ x: nodeWithPosition.x,+ y: nodeWithPosition.y,+ }+ })++ return { nodes: layoutedNodes, edges }+}++// Force layout computation+function computeForceLayout(+ nodes: GraphNode[],+ edges: GraphEdge[],+ options: ForceLayoutOptions = {}+) {+ const {+ width = 800,+ height = 600,+ chargeStrength = -30,+ linkDistance = 30,+ linkStrength = 2,+ alphaDecay = 0.045,+ alphaMin = 0,+ velocityDecay = 0.41,+ iterations = 300,+ } = options++ // Create simulation nodes with initial random positions+ const simNodes = nodes.map((node) => ({+ ...node,+ x: node.x ?? Math.random() * width,+ y: node.y ?? Math.random() * height,+ }))++ // Create simulation links+ const simLinks = edges.map((edge) => ({+ source: typeof edge.source === 'object' ? edge.source.id : edge.source,+ target: typeof edge.target === 'object' ? edge.target.id : edge.target,+ }))++ // Create D3 force simulation+ const simulation = forceSimulation(simNodes as any)+ .force(+ 'link',+ forceLink(simLinks)+ .id((d: any) => d.id)+ .distance(linkDistance)+ .strength(linkStrength)+ )+ .force('charge', forceManyBody().strength(chargeStrength))+ .force('center', forceCenter(width / 2, height / 2))+ .alphaDecay(alphaDecay)+ .alphaMin(alphaMin)+ .velocityDecay(velocityDecay)++ // Run simulation synchronously for specified iterations+ // Send progress updates every 10 iterations+ const progressInterval = Math.max(1, Math.floor(iterations / 10))++ for (let i = 0; i < iterations; i++) {+ simulation.tick()++ // Send progress updates+ if (i % progressInterval === 0 || i === iterations - 1) {+ self.postMessage({+ type: 'progress',+ progress: (i + 1) / iterations,+ })+ }+ }++ simulation.stop()++ return {+ nodes: simNodes.map((node) => ({+ ...node,+ x: node.x,+ y: node.y,+ })),+ edges,+ }+}++// Listen for messages from the main thread+self.addEventListener('message', (event: MessageEvent<LayoutMessage>) => {+ const { type, nodes, edges, options } = event.data++ try {+ let result++ if (type === 'dagre') {+ result = computeDagreLayout(nodes, edges, options as DagreLayoutOptions)+ } else if (type === 'force') {+ result = computeForceLayout(nodes, edges, options as ForceLayoutOptions)+ } else {+ throw new Error(`Unknown layout type: ${type}`)+ }++ // Send the result back to the main thread+ self.postMessage({+ type: 'complete',+ result,+ })+ } catch (error) {+ // Send error back to the main thread+ self.postMessage({+ type: 'error',+ error: error instanceof Error ? error.message : String(error),+ })+ }+})