-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActiveQueryController.java
More file actions
175 lines (164 loc) · 7.07 KB
/
Copy pathActiveQueryController.java
File metadata and controls
175 lines (164 loc) · 7.07 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 com.dbaagent.controller;
import com.dbaagent.model.ActiveQuery;
import com.dbaagent.service.ActiveQueryService;
import com.dbaagent.service.security.AccessControlService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/active-queries")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = "*")
public class ActiveQueryController {
private final ActiveQueryService activeQueryService;
private final AccessControlService accessControlService;
/**
* Capture current active queries from the database
* POST /api/active-queries/capture/{connectionId}
*/
@PostMapping("/capture/{connectionId}")
public ResponseEntity<List<ActiveQuery>> captureActiveQueries(@PathVariable String connectionId) {
log.info("API: Capturing active queries for connection {}", connectionId);
try {
accessControlService.assertCanManageConnectionContent(connectionId);
List<ActiveQuery> queries = activeQueryService.captureActiveQueries(connectionId);
return ResponseEntity.ok(queries);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error capturing active queries", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get the latest snapshot of active queries
* GET /api/active-queries/latest/{connectionId}
*/
@GetMapping("/latest/{connectionId}")
public ResponseEntity<List<ActiveQuery>> getLatestQueries(@PathVariable String connectionId) {
log.info("API: Getting latest queries for connection {}", connectionId);
try {
accessControlService.assertCanReadConnectionContent(connectionId);
List<ActiveQuery> queries = activeQueryService.getLatestQueries(connectionId);
return ResponseEntity.ok(queries);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting latest queries", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get queries by filter
* GET /api/active-queries/filter/{connectionId}?type={filterType}&value={filterValue}
*/
@GetMapping("/filter/{connectionId}")
public ResponseEntity<List<ActiveQuery>> getQueriesByFilter(
@PathVariable String connectionId,
@RequestParam String type,
@RequestParam String value) {
log.info("API: Getting queries for connection {} filtered by {}={}", connectionId, type, value);
try {
accessControlService.assertCanReadConnectionContent(connectionId);
List<ActiveQuery> queries = activeQueryService.getQueriesByFilter(connectionId, type, value);
return ResponseEntity.ok(queries);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting filtered queries", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get statistics about active queries
* GET /api/active-queries/stats/{connectionId}
*/
@GetMapping("/stats/{connectionId}")
public ResponseEntity<Map<String, Object>> getStatistics(@PathVariable String connectionId) {
log.info("API: Getting query statistics for connection {}", connectionId);
try {
accessControlService.assertCanReadConnectionContent(connectionId);
Map<String, Object> stats = activeQueryService.getStatistics(connectionId);
return ResponseEntity.ok(stats);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting query statistics", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get available filter options
* GET /api/active-queries/filter-options/{connectionId}
*/
@GetMapping("/filter-options/{connectionId}")
public ResponseEntity<Map<String, List<String>>> getFilterOptions(@PathVariable String connectionId) {
log.info("API: Getting filter options for connection {}", connectionId);
try {
accessControlService.assertCanReadConnectionContent(connectionId);
Map<String, List<String>> options = activeQueryService.getFilterOptions(connectionId);
return ResponseEntity.ok(options);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting filter options", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Kill a specific query
* POST /api/active-queries/kill/{connectionId}/{pid}
*/
@PostMapping("/kill/{connectionId}/{pid}")
public ResponseEntity<Map<String, String>> killQuery(
@PathVariable String connectionId,
@PathVariable String pid) {
log.info("API: Killing query {} on connection {}", pid, connectionId);
try {
accessControlService.assertCanManageConnectionContent(connectionId);
activeQueryService.killQuery(connectionId, pid);
return ResponseEntity.ok(Map.of(
"message", "Successfully killed query " + pid,
"pid", pid
));
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(Map.of(
"error", e.getMessage(),
"pid", pid
));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error killing query {}", pid, e);
return ResponseEntity.status(500).body(Map.of(
"error", "Failed to kill query: " + e.getMessage(),
"pid", pid
));
}
}
/**
* Cleanup old query snapshots
* DELETE /api/active-queries/cleanup/{connectionId}
*/
@DeleteMapping("/cleanup/{connectionId}")
public ResponseEntity<Map<String, Object>> cleanupOldSnapshots(@PathVariable String connectionId) {
log.info("API: Cleaning up old snapshots for connection {}", connectionId);
try {
accessControlService.assertCanManageConnectionContent(connectionId);
int deleted = activeQueryService.cleanupOldSnapshots(connectionId);
return ResponseEntity.ok(Map.of(
"message", "Cleanup completed",
"deletedCount", deleted
));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error cleaning up snapshots", e);
return ResponseEntity.internalServerError().build();
}
}
}