(graph: GraphData, catalog: HotspotCatalog, filter = '')
| 95 | |
| 96 | /** A dedicated hotspot view keeps exact symbol identities and real connecting edges. */ |
| 97 | export function buildHotspotGraph(graph: GraphData, catalog: HotspotCatalog, filter = ''): SemanticGraph { |
| 98 | const byName = new Map<string, GraphNode[]>(); |
| 99 | for (const node of graph.nodes) if (node.qualified_name) { |
| 100 | const list = byName.get(node.qualified_name) ?? []; list.push(node); byName.set(node.qualified_name, list); |
| 101 | } |
| 102 | const match = filter.trim().toLowerCase(); |
| 103 | const nodes: SemanticNode[] = []; const byGraphId = new Map<number, string>(); const seen = new Set<string>(); |
| 104 | const matchingNodes = new Map<string, SemanticNode>(); |
| 105 | for (const finding of catalog.findings) { |
| 106 | const candidates = finding.qualifiedName ? byName.get(finding.qualifiedName) ?? [] : []; |
| 107 | const exact = candidates.filter(node => (!finding.filePath || node.file_path === finding.filePath) && ['Function', 'Method'].includes(node.label)); |
| 108 | const symbol = exact.length === 1 ? exact[0] : undefined; |
| 109 | const id = symbol ? `hotspot:${symbol.id}` : `hotspot-file:${finding.filePath ?? identity(finding)}`; |
| 110 | const projected: SemanticNode = { id, kind: symbol || !finding.filePath ? 'symbol' : 'file', label: symbol?.name ?? finding.filePath ?? finding.name, |
| 111 | detail: symbol ? hotspotSignals(finding).join(' · ') : 'Ranked hotspot; symbol identity is outside the loaded graph', |
| 112 | position: [0, 0, 0], count: 1, graphNode: symbol, filePath: finding.filePath, line: finding.line, |
| 113 | areaPath: finding.filePath ? areaOf(finding.filePath) : undefined, members: symbol ? [symbol] : [] }; |
| 114 | if (!matchingNodes.has(id) && `${finding.name} ${finding.filePath ?? ''} ${hotspotSignals(finding).join(' ')}`.toLowerCase().includes(match)) matchingNodes.set(id, projected); |
| 115 | if (seen.has(id)) continue; seen.add(id); |
| 116 | nodes.push(projected); |
| 117 | if (symbol) byGraphId.set(symbol.id, id); |
| 118 | } |
| 119 | const platforms = layoutFolderHierarchy(nodes); |
| 120 | const positions = new Map(nodes.map(node => [node.id, node.position])); |
| 121 | const matchedNodes = [...matchingNodes.values()].map(node => ({ ...node, position: positions.get(node.id)! })); |
| 122 | const allEdges = graph.edges.filter(edge => matchingNodes.has(byGraphId.get(edge.source)!) && matchingNodes.has(byGraphId.get(edge.target)!) && ['CALLS', 'IMPORTS', 'USAGE', 'DATA_FLOWS', 'INHERITS', 'IMPLEMENTS'].includes(edge.type)) |
| 123 | .sort((a, b) => compare(`${a.source}:${a.type}:${a.target}:${a.id ?? ''}`, `${b.source}:${b.type}:${b.target}:${b.id ?? ''}`)); |
| 124 | const totalNodes = matchedNodes.length; const shown = matchedNodes.slice(0, 40); const visible = new Set(shown.map(node => node.id)); |
| 125 | const graphById = new Map(graph.nodes.map(node => [node.id, node])); |
| 126 | const edges = allEdges.filter(edge => visible.has(byGraphId.get(edge.source)!) && visible.has(byGraphId.get(edge.target)!)).slice(0, 100).map((edge, index) => ({ |
| 127 | id: `hotspot-edge:${edge.id ?? index}`, source: byGraphId.get(edge.source)!, target: byGraphId.get(edge.target)!, type: edge.type, count: 1, |
| 128 | evidence: [{ source: graphById.get(edge.source)!, target: graphById.get(edge.target)!, type: edge.type, id: edge.id, strategy: edge.strategy }], |
| 129 | })); |
| 130 | return { view: 'hotspots', scopeKey: `hotspots:${filter}`, title: 'Hotspots and their connections', |
| 131 | positionMeaning: 'Platforms follow source folders; symbol positions follow file and source order, independently of rank. Gravity fields encode static fan-in, not runtime activity.', |
| 132 | nodes: shown, platforms: pruneHierarchyPlatforms(platforms, shown), edges, totalNodes, totalEdges: allEdges.length, omittedNodes: totalNodes - shown.length, omittedEdges: allEdges.length - edges.length, |
| 133 | warnings: ['Hotspots are ranked review signals, not confirmed defects. Unmarked code has not been certified safe.', ...(graph.total_nodes > graph.nodes.length ? ['Some hotspot identities or relationships are outside the loaded graph.'] : [])] }; |
| 134 | } |
no test coverage detected