query-impact.js
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function uniqueNodes(nodes) {
const byId = new Map();
nodes.forEach((node) => {
if (node) {
byId.set(node.id, node);
}
});
return [...byId.values()].sort((left, right) => left.id.localeCompare(right.id, "en"));
}
function searchableText(node) {
return JSON.stringify({
id: node.id,
name: node.name,
path: node.path,
meta: node.meta,
}).toLowerCase();
}
function createAdjacency(graph) {
const incoming = new Map();
const outgoing = new Map();
for (const edge of graph.edges) {
if (!incoming.has(edge.to)) {
incoming.set(edge.to, []);
}
if (!outgoing.has(edge.from)) {
outgoing.set(edge.from, []);
}
incoming.get(edge.to).push(edge);
outgoing.get(edge.from).push(edge);
}
return { incoming, outgoing };
}
function traverseDirection(graph, seedIds, direction, depth, adjacency = createAdjacency(graph)) {
const nodeById = new Map(graph.nodes.map(node => [node.id, node]));
const visited = new Set(seedIds);
let frontier = [...seedIds];
const result = [];
const evidence = [];
for (let level = 0; level < depth && frontier.length; level += 1) {
const next = [];
for (const currentId of frontier) {
const candidateEdges = direction === "upstream"
? adjacency.incoming.get(currentId) || []
: adjacency.outgoing.get(currentId) || [];
for (const edge of candidateEdges) {
const relatedId = direction === "upstream" ? edge.from : edge.to;
evidence.push(edge);
if (!visited.has(relatedId)) {
visited.add(relatedId);
next.push(relatedId);
if (nodeById.has(relatedId)) {
result.push(nodeById.get(relatedId));
}
}
}
}
frontier = next;
}
return { nodes: uniqueNodes(result), evidence };
}
function traverseUndirected(graph, seedIds, depth, adjacency) {
const nodeById = new Map(graph.nodes.map(node => [node.id, node]));
const visited = new Set(seedIds);
let frontier = [...seedIds];
const result = [];
const evidence = [];
for (let level = 0; level < depth && frontier.length; level += 1) {
const next = [];
for (const currentId of frontier) {
const edges = [
...(adjacency.incoming.get(currentId) || []),
...(adjacency.outgoing.get(currentId) || []),
];
for (const edge of edges) {
const relatedId = edge.from === currentId ? edge.to : edge.from;
evidence.push(edge);
if (!visited.has(relatedId)) {
visited.add(relatedId);
next.push(relatedId);
if (nodeById.has(relatedId)) {
result.push(nodeById.get(relatedId));
}
}
}
}
frontier = next;
}
return { nodes: uniqueNodes(result), evidence };
}
function collectLinkedType(graph, relatedIds, edgeType, nodeType) {
const nodeById = new Map(graph.nodes.map(node => [node.id, node]));
return uniqueNodes(graph.edges
.filter(edge => edge.type === edgeType && relatedIds.has(edge.from))
.map(edge => nodeById.get(edge.to))
.filter(node => node?.type === nodeType));
}
function queryImpact(graph, options = {}) {
const keywords = (options.keywords || []).map(keyword => String(keyword).trim().toLowerCase()).filter(Boolean);
const depth = Number.isInteger(options.depth) ? Math.max(0, options.depth) : 2;
const direction = options.direction || "both";
const directMatches = keywords.length
? graph.nodes.filter(node => keywords.some(keyword => searchableText(node).includes(keyword)))
: [];
const seeds = uniqueNodes(directMatches);
const seedIds = seeds.map(node => node.id);
const adjacency = createAdjacency(graph);
const upstream = ["both", "upstream"].includes(direction)
? traverseDirection(graph, seedIds, "upstream", depth, adjacency)
: { nodes: [], evidence: [] };
const downstream = ["both", "downstream"].includes(direction)
? traverseDirection(graph, seedIds, "downstream", depth, adjacency)
: { nodes: [], evidence: [] };
const neighborhood = direction === "both"
? traverseUndirected(graph, seedIds, depth, adjacency)
: { nodes: [], evidence: [] };
if (direction === "both") {
const upstreamIds = new Set(upstream.nodes.map(node => node.id));
downstream.nodes = uniqueNodes([
...downstream.nodes,
...neighborhood.nodes.filter(node => !upstreamIds.has(node.id)),
]);
downstream.evidence.push(...neighborhood.evidence);
}
const relatedNodes = uniqueNodes([...seeds, ...upstream.nodes, ...downstream.nodes]);
const relatedIds = new Set(relatedNodes.map(node => node.id));
const domainSet = new Set(relatedNodes.flatMap(node => node.domainIds || []));
const crossDomainRisks = domainSet.size > 1
? relatedNodes.filter(node => (node.domainIds || []).length > 1 || node.type === "domain")
: [];
const evidenceByKey = new Map();
for (const edge of [...upstream.evidence, ...downstream.evidence]) {
evidenceByKey.set(JSON.stringify(edge), edge);
}
const evidence = [...evidenceByKey.values()]
.sort((left, right) => JSON.stringify(left).localeCompare(JSON.stringify(right), "en"));
const relatedPaths = new Set(relatedNodes.map(node => node.path).filter(Boolean));
return {
seeds,
directMatches: seeds,
upstreamConsumers: upstream.nodes,
downstreamDependencies: downstream.nodes,
crossDomainRisks: uniqueNodes(crossDomainRisks),
testCandidates: collectLinkedType(graph, relatedIds, "coveredBy", "test"),
documents: collectLinkedType(graph, relatedIds, "documentedBy", "document"),
unresolved: graph.unresolved.filter(item => relatedPaths.has(item.path)),
evidence,
};
}
module.exports = {
queryImpact,
};