-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeSuggestionApplier.java
More file actions
223 lines (206 loc) · 10.2 KB
/
Copy pathCodeSuggestionApplier.java
File metadata and controls
223 lines (206 loc) · 10.2 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
package com.dbaagent.service.codescan;
import com.dbaagent.model.CompanyKnowledgeEntry;
import com.dbaagent.model.DocumentationSource;
import com.dbaagent.model.SchemaDocumentation;
import com.dbaagent.model.code.CodeKnowledgeSuggestion;
import com.dbaagent.repository.CodeKnowledgeSuggestionRepository;
import com.dbaagent.repository.SchemaDocumentationRepository;
import com.dbaagent.service.CompanyKnowledgeService;
import com.dbaagent.service.SchemaScannerService;
import com.dbaagent.service.TrainingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Materializes an APPROVED suggestion into a real
* {@link com.dbaagent.model.SchemaDocumentation} or
* {@link com.dbaagent.model.CompanyKnowledgeEntry}, then triggers the existing
* embedding upsert so the new content lands in RAG immediately.
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class CodeSuggestionApplier {
private final CodeKnowledgeSuggestionRepository suggestionRepository;
private final SchemaDocumentationRepository schemaDocRepository;
private final CompanyKnowledgeService companyKnowledgeService;
private final TrainingService trainingService;
private final SchemaScannerService schemaScannerService;
@Transactional
public CodeKnowledgeSuggestion approve(String suggestionId, String decidedBy, String note) {
CodeKnowledgeSuggestion suggestion = suggestionRepository.findById(suggestionId)
.orElseThrow(() -> new IllegalArgumentException("Suggestion not found: " + suggestionId));
if (suggestion.getStatus() == CodeKnowledgeSuggestion.Status.APPROVED) {
return suggestion;
}
if (suggestion.getStatus() == CodeKnowledgeSuggestion.Status.REJECTED
|| suggestion.getStatus() == CodeKnowledgeSuggestion.Status.SUPERSEDED) {
throw new IllegalStateException("Suggestion cannot be approved from status " + suggestion.getStatus());
}
switch (suggestion.getTargetKind()) {
case SCHEMA_DOC -> applySchemaDoc(suggestion, decidedBy);
case KNOWLEDGE_ENTRY -> applyKnowledgeEntry(suggestion, decidedBy);
}
suggestion.setStatus(CodeKnowledgeSuggestion.Status.APPROVED);
suggestion.setDecidedAt(LocalDateTime.now());
suggestion.setDecidedBy(decidedBy);
suggestion.setDecisionNote(note);
return suggestionRepository.save(suggestion);
}
@Transactional
public CodeKnowledgeSuggestion reject(String suggestionId, String decidedBy, String note) {
CodeKnowledgeSuggestion suggestion = suggestionRepository.findById(suggestionId)
.orElseThrow(() -> new IllegalArgumentException("Suggestion not found: " + suggestionId));
if (suggestion.getStatus() == CodeKnowledgeSuggestion.Status.REJECTED) {
return suggestion;
}
if (suggestion.getStatus() == CodeKnowledgeSuggestion.Status.APPROVED) {
throw new IllegalStateException("Cannot reject an already-approved suggestion");
}
suggestion.setStatus(CodeKnowledgeSuggestion.Status.REJECTED);
suggestion.setDecidedAt(LocalDateTime.now());
suggestion.setDecidedBy(decidedBy);
suggestion.setDecisionNote(note);
return suggestionRepository.save(suggestion);
}
private void applySchemaDoc(CodeKnowledgeSuggestion suggestion, String decidedBy) {
SchemaDocumentation.DocumentationType objectType = resolveObjectType(suggestion);
String objectName;
String parentObject = null;
String target = suggestion.getTargetObject();
// Never silently skip — bulk-decide would report succeeded while nothing
// was written, and the Review UI would look like a no-op approval.
if (target == null || target.isBlank()) {
throw new IllegalArgumentException(
"SCHEMA_DOC suggestion " + suggestion.getId() + " has no targetObject"
);
}
if (objectType == SchemaDocumentation.DocumentationType.COLUMN) {
int dot = target.indexOf('.');
if (dot <= 0 || dot >= target.length() - 1) {
throw new IllegalArgumentException(
"SCHEMA_DOC column suggestion " + suggestion.getId()
+ " has malformed target '" + target + "'"
);
}
parentObject = target.substring(0, dot);
objectName = target.substring(dot + 1);
// Match SchemaDescriptionService's convention: parent_object is
// database-qualified (e.g. "analytics_db.ORDER_ITEMS") so the
// upsert lookup unifies with AI_GENERATED rows for the same column
// instead of orphaning a separate row per source.
String dbName = resolveDatabaseName(suggestion.getConnectionId());
if (dbName != null && !dbName.isBlank() && !parentObject.contains(".")) {
parentObject = dbName + "." + parentObject;
}
} else {
objectName = target;
}
// Scope the upsert to our own CODE_DERIVED rows AND, for columns,
// include parent_object — same column name can exist in many tables,
// so a (connection, COLUMN, "status", CODE_DERIVED) lookup would
// otherwise return multiple rows and throw "Query did not return a
// unique result".
Optional<SchemaDocumentation> existing;
if (objectType == SchemaDocumentation.DocumentationType.COLUMN) {
existing = schemaDocRepository
.findByConnectionIdAndObjectTypeAndObjectNameAndParentObjectAndSource(
suggestion.getConnectionId(),
objectType,
objectName,
parentObject,
DocumentationSource.CODE_DERIVED
);
} else {
existing = schemaDocRepository
.findByConnectionIdAndObjectTypeAndObjectNameAndSource(
suggestion.getConnectionId(),
objectType,
objectName,
DocumentationSource.CODE_DERIVED
);
}
SchemaDocumentation doc = existing.orElseGet(SchemaDocumentation::new);
doc.setConnectionId(suggestion.getConnectionId());
doc.setObjectType(objectType);
doc.setObjectName(objectName);
doc.setParentObject(parentObject);
doc.setDescription(suggestion.getContent());
Object terms = suggestion.getPayload() == null ? null : suggestion.getPayload().get("businessTerms");
if (terms instanceof List<?> list && !list.isEmpty()) {
doc.setBusinessTerms(String.join(", ", list.stream().map(String::valueOf).toList()));
}
doc.setSource(DocumentationSource.CODE_DERIVED);
doc.setConfidence(suggestion.getConfidence());
// Carry the source-file provenance forward — the suggestion's
// {path, startLine, endLine, rationale} list lands directly on the
// documentation row so the editor can render "where did this come
// from" and so future schema-drift code can spot orphans.
doc.setSourceFiles(suggestion.getSourceFiles());
if (doc.getCreatedBy() == null) doc.setCreatedBy(decidedBy);
SchemaDocumentation saved = schemaDocRepository.save(doc);
suggestion.setAppliedDocId(saved.getId());
try {
trainingService.upsertDocumentationEmbedding(saved);
} catch (Exception e) {
log.warn("Failed to embed schema doc {} after approval: {}", saved.getId(), e.getMessage());
}
}
private void applyKnowledgeEntry(CodeKnowledgeSuggestion suggestion, String decidedBy) {
CompanyKnowledgeEntry entry = CompanyKnowledgeEntry.builder()
.connectionId(suggestion.getConnectionId())
.title(suggestion.getTitle())
.content(suggestion.getContent())
.entryType(resolveEntryType(suggestion))
.linkedTables(suggestion.getLinkedTables())
.linkedColumns(suggestion.getLinkedColumns())
.createdBy(decidedBy)
.build();
CompanyKnowledgeEntry saved = companyKnowledgeService.createEntry(entry);
suggestion.setAppliedEntryId(saved.getId());
}
/** Returns the connection's database name (e.g. "analytics_db") or null. */
private String resolveDatabaseName(String connectionId) {
try {
var schema = schemaScannerService.scanSchema(connectionId);
return schema == null ? null : schema.getDatabaseName();
} catch (Exception e) {
log.warn("could not resolve database name for {}: {}", connectionId, e.getMessage());
return null;
}
}
private static SchemaDocumentation.DocumentationType resolveObjectType(CodeKnowledgeSuggestion suggestion) {
Map<String, Object> payload = suggestion.getPayload();
Object objectKind = payload == null ? null : payload.get("objectKind");
if (objectKind != null) {
try {
return SchemaDocumentation.DocumentationType.valueOf(objectKind.toString().toUpperCase());
} catch (IllegalArgumentException ignore) {
// fall through to inference
}
}
// Heuristic: if targetObject contains a dot, treat as COLUMN.
String target = suggestion.getTargetObject();
if (target != null && target.contains(".")) {
return SchemaDocumentation.DocumentationType.COLUMN;
}
return SchemaDocumentation.DocumentationType.TABLE;
}
private static CompanyKnowledgeEntry.EntryType resolveEntryType(CodeKnowledgeSuggestion suggestion) {
Map<String, Object> payload = suggestion.getPayload();
Object t = payload == null ? null : payload.get("entryType");
if (t != null) {
try {
return CompanyKnowledgeEntry.EntryType.valueOf(t.toString().toUpperCase());
} catch (IllegalArgumentException ignore) {
// fall through
}
}
return CompanyKnowledgeEntry.EntryType.BUSINESS_RULE;
}
}