-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphController.java
More file actions
311 lines (279 loc) · 11.9 KB
/
Copy pathGraphController.java
File metadata and controls
311 lines (279 loc) · 11.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package io.github.randomcodespace.iq.api;
import io.github.randomcodespace.iq.config.CodeIqConfig;
import io.github.randomcodespace.iq.query.QueryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.context.annotation.Profile;
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 io.github.randomcodespace.iq.intelligence.query.CapabilityMatrix;
import io.github.randomcodespace.iq.model.NodeKind;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* REST API controller matching the Python Code IQ API paths.
*/
@RestController
@RequestMapping("/api")
@Profile("serving")
public class GraphController {
private final QueryService queryService;
private final CodeIqConfig config;
public GraphController(@org.springframework.beans.factory.annotation.Autowired(required = false) QueryService queryService,
CodeIqConfig config) {
this.queryService = queryService;
this.config = config;
}
@GetMapping("/stats")
public Map<String, Object> getStats() {
requireQueryService();
return queryService.getStats();
}
@GetMapping("/stats/detailed")
public Map<String, Object> getDetailedStats(
@RequestParam(defaultValue = "all") String category) {
requireQueryService();
return queryService.getDetailedStats(category);
}
@GetMapping("/kinds")
public Map<String, Object> listKinds() {
requireQueryService();
return queryService.listKinds();
}
@GetMapping("/kinds/{kind}")
public Map<String, Object> nodesByKind(
@PathVariable String kind,
@RequestParam(defaultValue = "50") int limit,
@RequestParam(defaultValue = "0") int offset) {
requireQueryService();
validateNodeKind(kind);
return queryService.nodesByKind(kind, Math.min(limit, 1000), Math.max(0, offset));
}
@GetMapping("/nodes")
public Map<String, Object> listNodes(
@RequestParam(required = false) String kind,
@RequestParam(defaultValue = "100") int limit,
@RequestParam(defaultValue = "0") int offset) {
requireQueryService();
if (kind != null) {
validateNodeKind(kind);
}
return queryService.listNodes(kind, Math.min(limit, 1000), Math.max(0, offset));
}
@GetMapping("/nodes/{nodeId}/detail")
public Map<String, Object> nodeDetail(@PathVariable String nodeId) {
requireQueryService();
Map<String, Object> result = queryService.nodeDetailWithEdges(nodeId);
if (result == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Node not found: " + nodeId);
}
return result;
}
@GetMapping("/nodes/{nodeId}/neighbors")
public Map<String, Object> neighbors(
@PathVariable String nodeId,
@RequestParam(defaultValue = "both") String direction) {
requireQueryService();
return queryService.getNeighbors(nodeId, direction);
}
@GetMapping("/edges")
public Map<String, Object> listEdges(
@RequestParam(required = false) String kind,
@RequestParam(defaultValue = "100") int limit,
@RequestParam(defaultValue = "0") int offset) {
requireQueryService();
return queryService.listEdges(kind, Math.min(limit, 1000), Math.max(0, offset));
}
@GetMapping("/ego/{center}")
public Map<String, Object> egoGraph(
@PathVariable String center,
@RequestParam(defaultValue = "2") int radius) {
int cappedRadius = Math.min(radius, config.getMaxRadius());
requireQueryService();
return queryService.egoGraph(center, cappedRadius);
}
@GetMapping("/query/cycles")
public Map<String, Object> findCycles(@RequestParam(defaultValue = "100") int limit) {
requireQueryService();
return queryService.findCycles(limit);
}
@GetMapping("/query/shortest-path")
public Map<String, Object> shortestPath(
@RequestParam String source,
@RequestParam String target) {
requireQueryService();
Map<String, Object> result = queryService.shortestPath(source, target);
if (result == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"No path found between " + source + " and " + target);
}
return result;
}
@GetMapping("/query/consumers/{targetId}")
public Map<String, Object> consumersOf(@PathVariable String targetId) {
requireQueryService();
return queryService.consumersOf(targetId);
}
@GetMapping("/query/producers/{targetId}")
public Map<String, Object> producersOf(@PathVariable String targetId) {
requireQueryService();
return queryService.producersOf(targetId);
}
@GetMapping("/query/callers/{targetId}")
public Map<String, Object> callersOf(@PathVariable String targetId) {
requireQueryService();
return queryService.callersOf(targetId);
}
@GetMapping("/query/dependencies/{moduleId}")
public Map<String, Object> dependenciesOf(@PathVariable String moduleId) {
requireQueryService();
return queryService.dependenciesOf(moduleId);
}
@GetMapping("/query/dependents/{moduleId}")
public Map<String, Object> dependentsOf(@PathVariable String moduleId) {
requireQueryService();
return queryService.dependentsOf(moduleId);
}
@GetMapping("/query/dead-code")
public ResponseEntity<?> findDeadCode(
@RequestParam(required = false) String kind,
@RequestParam(defaultValue = "100") int limit) {
requireQueryService();
if (kind != null && !kind.isBlank()) {
validateNodeKind(kind);
}
return ResponseEntity.ok(queryService.findDeadCode(kind, Math.min(limit, 1000)));
}
@GetMapping("/triage/component")
public Map<String, Object> findComponent(@RequestParam String file) {
requireQueryService();
return queryService.findComponentByFile(file);
}
@GetMapping("/triage/impact/{nodeId}")
public Map<String, Object> traceImpact(
@PathVariable String nodeId,
@RequestParam(defaultValue = "3") int depth) {
int cappedDepth = Math.min(depth, config.getMaxDepth());
requireQueryService();
return queryService.traceImpact(nodeId, cappedDepth);
}
@GetMapping("/search")
public List<Map<String, Object>> searchGraph(
@RequestParam String q,
@RequestParam(defaultValue = "50") int limit) {
requireQueryService();
return queryService.searchGraph(q, Math.min(limit, 1000));
}
@GetMapping("/file-tree")
public Map<String, Object> getFileTree(
@RequestParam(required = false) Integer depth,
@RequestParam(required = false) Integer maxFiles,
@RequestParam(defaultValue = "true") boolean excludeTests) {
requireQueryService();
// depth=null means unlimited (full tree for treemap). Otherwise cap at maxDepth.
Integer cappedDepth = (depth != null) ? Math.min(depth, config.getMaxDepth()) : null;
// Default unlimited for treemap
int limit = (maxFiles != null) ? maxFiles : Integer.MAX_VALUE;
return queryService.getFileTree(cappedDepth, limit, excludeTests);
}
@GetMapping("/capabilities")
public Map<String, Object> getCapabilities(
@RequestParam(required = false) String language) {
Map<String, Object> result = new java.util.LinkedHashMap<>();
if (language != null && !language.isBlank()) {
result.put("language", language.strip().toLowerCase());
result.put("capabilities", CapabilityMatrix.forLanguage(language).entrySet().stream()
.collect(java.util.stream.Collectors.toMap(
e -> e.getKey().name().toLowerCase(),
e -> e.getValue().name(),
(a, b) -> a,
java.util.TreeMap::new)));
} else {
result.put("matrix", CapabilityMatrix.asSerializableMap());
}
return result;
}
private void validateNodeKind(String kind) {
try {
NodeKind.fromValue(kind);
} catch (IllegalArgumentException e) {
String valid = Arrays.stream(NodeKind.values())
.map(NodeKind::getValue)
.collect(Collectors.joining(", "));
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Invalid node kind: '" + kind + "'. Valid values: " + valid);
}
}
private void requireQueryService() {
if (queryService == null) {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,
"Neo4j graph not available. This endpoint requires 'enrich' to be run first.");
}
}
@GetMapping("/file")
public ResponseEntity<String> readFile(
@RequestParam String path,
@RequestParam(required = false) Integer startLine,
@RequestParam(required = false) Integer endLine) {
Path codebaseReal;
try {
codebaseReal = Path.of(config.getRootPath()).toRealPath();
} catch (IOException e) {
return ResponseEntity.status(500)
.contentType(MediaType.TEXT_PLAIN)
.body("Failed to resolve codebase root: " + e.getMessage());
}
Path candidate = codebaseReal.resolve(path).normalize();
if (!candidate.startsWith(codebaseReal)) {
return ResponseEntity.status(403)
.contentType(MediaType.TEXT_PLAIN)
.body("Path traversal blocked");
}
Path resolvedReal;
try {
resolvedReal = candidate.toRealPath();
} catch (NoSuchFileException e) {
return ResponseEntity.notFound().build();
} catch (IOException e) {
return ResponseEntity.status(500)
.contentType(MediaType.TEXT_PLAIN)
.body("Failed to resolve file: " + e.getMessage());
}
if (!resolvedReal.startsWith(codebaseReal)) {
return ResponseEntity.status(403)
.contentType(MediaType.TEXT_PLAIN)
.body("Path traversal blocked");
}
if (!Files.isRegularFile(resolvedReal)) {
return ResponseEntity.notFound().build();
}
try {
String content = SafeFileReader.read(resolvedReal, startLine, endLine, config.getMaxFileBytes());
return ResponseEntity.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(content);
} catch (SafeFileReader.FileTooLargeException tooLarge) {
return ResponseEntity.status(HttpStatus.CONTENT_TOO_LARGE)
.contentType(MediaType.TEXT_PLAIN)
.body(tooLarge.getMessage());
} catch (IOException e) {
return ResponseEntity.status(500)
.contentType(MediaType.TEXT_PLAIN)
.body("Failed to read file: " + e.getMessage());
}
}
// POST /api/analyze removed — API/MCP server is read-only.
// Analysis is done locally via CLI: codeiq analyze / codeiq index
// Data is loaded into Neo4j on serve startup (auto-enrich).
}