194 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-02-09 23:15:26 +08:00
import Graph, { DirectedGraph } from 'graphology'
import { useCallback, useEffect, useState } from 'react'
import { randomColor } from '@/lib/utils'
import * as Constants from '@/lib/constants'
2025-02-10 22:02:06 +08:00
import { useGraphStore, RawGraph } from '@/stores/graph'
2025-02-09 23:15:26 +08:00
const validateGraph = (graph: RawGraph) => {
if (!graph) {
return false
}
if (!Array.isArray(graph.nodes) || !Array.isArray(graph.edges)) {
return false
}
for (const node of graph.nodes) {
if (!node.id || !node.labels || !node.properties) {
return false
}
}
for (const edge of graph.edges) {
if (!edge.id || !edge.source || !edge.target || !edge.type || !edge.properties) {
return false
}
}
for (const edge of graph.edges) {
const source = graph.getNode(edge.source)
const target = graph.getNode(edge.target)
if (source == undefined || target == undefined) {
return false
}
}
return true
}
export type NodeType = {
x: number
y: number
label: string
size: number
color: string
highlighted?: boolean
}
export type EdgeType = { label: string }
const fetchGraph = async (label: string) => {
const response = await fetch(`/graphs?label=${label}`)
2025-02-09 23:15:26 +08:00
const rawData = await response.json()
let rawGraph = null
if (rawData) {
const nodeIdMap: Record<string, number> = {}
const edgeIdMap: Record<string, number> = {}
for (let i = 0; i < rawData.nodes.length; i++) {
const node = rawData.nodes[i]
nodeIdMap[node.id] = i
node.x = Math.random()
node.y = Math.random()
node.color = randomColor()
node.degree = 0
node.size = 10
}
for (let i = 0; i < rawData.edges.length; i++) {
const edge = rawData.edges[i]
edgeIdMap[edge.id] = i
const source = nodeIdMap[edge.source]
const target = nodeIdMap[edge.target]
if (source !== undefined && source !== undefined) {
const sourceNode = rawData.nodes[source]
const targetNode = rawData.nodes[target]
sourceNode.degree += 1
targetNode.degree += 1
}
}
// generate node size
let minDegree = Number.MAX_SAFE_INTEGER
let maxDegree = 0
for (const node of rawData.nodes) {
minDegree = Math.min(minDegree, node.degree)
maxDegree = Math.max(maxDegree, node.degree)
}
const range = maxDegree - minDegree
if (range > 0) {
const scale = Constants.maxNodeSize - Constants.minNodeSize
for (const node of rawData.nodes) {
node.size = Math.round(
Constants.minNodeSize + scale * Math.pow((node.degree - minDegree) / range, 0.5)
)
}
}
rawGraph = new RawGraph()
rawGraph.nodes = rawData.nodes
rawGraph.edges = rawData.edges
rawGraph.nodeIdMap = nodeIdMap
rawGraph.edgeIdMap = edgeIdMap
if (!validateGraph(rawGraph)) {
rawGraph = null
console.error('Invalid graph data')
}
console.log('Graph data loaded')
}
// console.debug({ data: JSON.parse(JSON.stringify(rawData)) })
return rawGraph
}
2025-02-10 22:02:06 +08:00
const createSigmaGraph = (rawGraph: RawGraph | null) => {
const graph = new DirectedGraph()
for (const rawNode of rawGraph?.nodes ?? []) {
graph.addNode(rawNode.id, {
label: rawNode.labels.join(', '),
color: rawNode.color,
x: rawNode.x,
y: rawNode.y,
size: rawNode.size,
// for node-border
borderColor: Constants.nodeBorderColor,
borderSize: 0.2
})
}
for (const rawEdge of rawGraph?.edges ?? []) {
rawEdge.dynamicId = graph.addDirectedEdge(rawEdge.source, rawEdge.target, {
label: rawEdge.type
})
}
return graph
2025-02-09 23:15:26 +08:00
}
const useLightrangeGraph = () => {
const [fetchLabel, setFetchLabel] = useState<string>('*')
2025-02-10 22:02:06 +08:00
const rawGraph = useGraphStore.use.rawGraph()
const sigmaGraph = useGraphStore.use.sigmaGraph()
const getNode = useCallback(
(nodeId: string) => {
return rawGraph?.getNode(nodeId) || null
},
[rawGraph]
)
const getEdge = useCallback(
(edgeId: string, dynamicId: boolean = true) => {
return rawGraph?.getEdge(edgeId, dynamicId) || null
},
[rawGraph]
)
2025-02-09 23:15:26 +08:00
useEffect(() => {
if (fetchLabel) {
2025-02-10 22:02:06 +08:00
const state = useGraphStore.getState()
if (state.queryLabel !== fetchLabel) {
state.reset()
2025-02-09 23:15:26 +08:00
fetchGraph(fetchLabel).then((data) => {
2025-02-10 22:02:06 +08:00
state.setQueryLabel(fetchLabel)
state.setSigmaGraph(createSigmaGraph(data))
data?.buildDynamicMap()
state.setRawGraph(data)
2025-02-09 23:15:26 +08:00
})
}
} else {
2025-02-10 22:02:06 +08:00
const state = useGraphStore.getState()
state.reset()
state.setSigmaGraph(new DirectedGraph())
2025-02-09 23:15:26 +08:00
}
2025-02-10 22:02:06 +08:00
}, [fetchLabel])
2025-02-09 23:15:26 +08:00
const lightrageGraph = useCallback(() => {
2025-02-10 22:02:06 +08:00
if (sigmaGraph) {
return sigmaGraph as Graph<NodeType, EdgeType>
2025-02-09 23:15:26 +08:00
}
const graph = new DirectedGraph()
2025-02-10 22:02:06 +08:00
useGraphStore.getState().setSigmaGraph(graph)
2025-02-09 23:15:26 +08:00
return graph as Graph<NodeType, EdgeType>
2025-02-10 22:02:06 +08:00
}, [sigmaGraph])
2025-02-09 23:15:26 +08:00
2025-02-10 22:02:06 +08:00
return { lightrageGraph, fetchLabel, setFetchLabel, getNode, getEdge }
2025-02-09 23:15:26 +08:00
}
export default useLightrangeGraph