-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvisorController.java
More file actions
125 lines (115 loc) · 5.12 KB
/
Copy pathAdvisorController.java
File metadata and controls
125 lines (115 loc) · 5.12 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
package com.dbaagent.controller;
import com.dbaagent.model.IndexRecommendation;
import com.dbaagent.model.PerformanceAnalysis;
import com.dbaagent.service.DatabaseAdvisorService;
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;
/**
* REST API for the performance advisor (analysis, missing indexes, health summary).
*
* <p><b>Authorization:</b> every endpoint here takes a caller-supplied connection id, so
* each one asserts access itself ({@code assertCanReadConnectionContent} for reads,
* {@code assertCanManageConnectionContent} for writes). {@code SecurityConfig} only
* requires an authenticated principal — nothing upstream inspects a connection id. See
* {@code ConnectionScopedAuthorizationSafetyTest}.
*/
@RestController
@RequestMapping("/advisor")
@RequiredArgsConstructor
@Slf4j
public class AdvisorController {
private final DatabaseAdvisorService advisorService;
private final AccessControlService accessControlService;
/**
* Get comprehensive performance analysis
*/
@GetMapping("/analyze/{connectionId}")
public ResponseEntity<PerformanceAnalysis> analyzePerformance(
@PathVariable String connectionId
) {
accessControlService.assertCanReadConnectionContent(connectionId);
try {
log.info("Performance analysis requested for connection: {}", connectionId);
PerformanceAnalysis analysis = advisorService.analyzePerformance(connectionId);
return ResponseEntity.ok(analysis);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error analyzing performance", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get missing index recommendations only
*/
@GetMapping("/indexes/{connectionId}")
public ResponseEntity<List<IndexRecommendation>> getMissingIndexes(
@PathVariable String connectionId
) {
accessControlService.assertCanReadConnectionContent(connectionId);
try {
log.info("Index recommendations requested for connection: {}", connectionId);
PerformanceAnalysis analysis = advisorService.analyzePerformance(connectionId);
return ResponseEntity.ok(analysis.getIndexRecommendations());
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting index recommendations", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Get health summary
*/
@GetMapping("/health/{connectionId}")
public ResponseEntity<HealthSummary> getHealthSummary(
@PathVariable String connectionId
) {
accessControlService.assertCanReadConnectionContent(connectionId);
try {
log.info("Health summary requested for connection: {}", connectionId);
PerformanceAnalysis analysis = advisorService.analyzePerformance(connectionId);
HealthSummary summary = HealthSummary.builder()
.overallHealth(analysis.getOverallHealth())
.totalRecommendations(
analysis.getIndexRecommendations().size() +
analysis.getGeneralRecommendations().size()
)
.criticalIssues((int) analysis.getIndexRecommendations().stream()
.filter(r -> r.getPriority() == IndexRecommendation.RecommendationPriority.CRITICAL)
.count() +
(int) analysis.getGeneralRecommendations().stream()
.filter(r -> r.getPriority() == com.dbaagent.model.DatabaseRecommendation.RecommendationPriority.CRITICAL)
.count())
.highPriorityIssues((int) analysis.getIndexRecommendations().stream()
.filter(r -> r.getPriority() == IndexRecommendation.RecommendationPriority.HIGH)
.count() +
(int) analysis.getGeneralRecommendations().stream()
.filter(r -> r.getPriority() == com.dbaagent.model.DatabaseRecommendation.RecommendationPriority.HIGH)
.count())
.aiSummary(analysis.getAiSummary())
.build();
return ResponseEntity.ok(summary);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error getting health summary", e);
return ResponseEntity.internalServerError().build();
}
}
@lombok.Data
@lombok.Builder
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
public static class HealthSummary {
private PerformanceAnalysis.OverallHealth overallHealth;
private Integer totalRecommendations;
private Integer criticalIssues;
private Integer highPriorityIssues;
private String aiSummary;
}
}