-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopologyController.java
More file actions
175 lines (153 loc) · 6.22 KB
/
Copy pathTopologyController.java
File metadata and controls
175 lines (153 loc) · 6.22 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package io.github.randomcodespace.iq.api;
import io.github.randomcodespace.iq.cache.AnalysisCache;
import io.github.randomcodespace.iq.config.CodeIqConfig;
import io.github.randomcodespace.iq.graph.GraphStore;
import io.github.randomcodespace.iq.model.CodeEdge;
import io.github.randomcodespace.iq.model.CodeNode;
import io.github.randomcodespace.iq.query.TopologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* REST API controller for service topology queries.
*/
@RestController
@RequestMapping("/api/topology")
@Profile("serving")
public class TopologyController {
private final TopologyService topologyService;
private final GraphStore graphStore;
private final CodeIqConfig config;
private final AtomicReference<List<CodeNode>> cachedNodes = new AtomicReference<>();
private final AtomicReference<List<CodeEdge>> cachedEdges = new AtomicReference<>();
public TopologyController(TopologyService topologyService,
@Autowired(required = false) GraphStore graphStore,
CodeIqConfig config) {
this.topologyService = topologyService;
this.graphStore = graphStore;
this.config = config;
}
// --- Data loading: Neo4j first, H2 fallback ---
/**
* Check whether Neo4j has data available.
*/
private volatile Boolean neo4jHasData;
private boolean hasNeo4jData() {
if (graphStore == null) return false;
if (neo4jHasData != null) return neo4jHasData;
try {
neo4jHasData = graphStore.count() > 0;
} catch (Exception e) {
neo4jHasData = false;
}
return neo4jHasData;
}
/**
* Load data from Neo4j if available, otherwise from H2 cache.
*/
private synchronized void ensureDataLoaded() {
if (cachedNodes.get() != null) return;
// Try Neo4j first (has enriched data with SERVICE nodes)
if (hasNeo4jData()) {
List<CodeNode> nodes = graphStore.findAll();
cachedNodes.set(nodes);
// Collect edges from all nodes' relationship lists
cachedEdges.set(nodes.stream()
.flatMap(n -> n.getEdges().stream())
.toList());
return;
}
// Fall back to H2 cache
Path root = Path.of(config.getRootPath()).toAbsolutePath().normalize();
Path cachePath = root.resolve(config.getCacheDir()).resolve("analysis-cache.db");
Path h2File = root.resolve(config.getCacheDir()).resolve("analysis-cache.mv.db");
if (!Files.exists(h2File)) return;
try (AnalysisCache cache = new AnalysisCache(cachePath)) {
cachedNodes.set(cache.loadAllNodes());
cachedEdges.set(cache.loadAllEdges());
}
}
/**
* Invalidate the in-memory cache (e.g. after re-analysis).
*/
public synchronized void invalidateCache() {
cachedNodes.set(null);
cachedEdges.set(null);
neo4jHasData = null;
}
@GetMapping
public Map<String, Object> getTopology() {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.getTopology(nodes, cachedEdges.get());
}
@GetMapping("/services/{name}")
public Map<String, Object> serviceDetail(@PathVariable String name) {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.serviceDetail(name, nodes, cachedEdges.get());
}
@GetMapping("/services/{name}/deps")
public Map<String, Object> serviceDependencies(@PathVariable String name) {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.serviceDependencies(name, nodes, cachedEdges.get());
}
@GetMapping("/services/{name}/dependents")
public Map<String, Object> serviceDependents(@PathVariable String name) {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.serviceDependents(name, nodes, cachedEdges.get());
}
@GetMapping("/blast-radius/{nodeId}")
public Map<String, Object> blastRadius(@PathVariable String nodeId) {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.blastRadius(nodeId, nodes, cachedEdges.get());
}
@GetMapping("/path")
public List<Map<String, Object>> findPath(
@RequestParam("from") String source,
@RequestParam("to") String target) {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.findPath(source, target, nodes, cachedEdges.get());
}
@GetMapping("/bottlenecks")
public List<Map<String, Object>> findBottlenecks() {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.findBottlenecks(nodes, cachedEdges.get());
}
@GetMapping("/circular")
public List<List<String>> findCircularDeps() {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.findCircularDeps(nodes, cachedEdges.get());
}
@GetMapping("/dead")
public List<Map<String, Object>> findDeadServices() {
ensureDataLoaded();
List<CodeNode> nodes = requireCache();
return topologyService.findDeadServices(nodes, cachedEdges.get());
}
private List<CodeNode> requireCache() {
List<CodeNode> nodes = cachedNodes.get();
if (nodes == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"No analysis cache found. Run analyze first.");
}
return nodes;
}
}