Skip to content

Commit 6ec2069

Browse files
committed
feat(alert): enhance UtmAlertResponseRule merging logic and update relationship management
1 parent fcc51a6 commit 6ec2069

2 files changed

Lines changed: 42 additions & 59 deletions

File tree

backend/src/main/java/com/park/utmstack/domain/alert_response_rule/UtmAlertResponseRule.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public class UtmAlertResponseRule implements Serializable {
6565
@OneToMany(mappedBy = "rule", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
6666
List<UtmAlertResponseRuleExecution> utmAlertResponseRuleExecutions;
6767

68-
69-
@ManyToMany
68+
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
7069
@JoinTable(
7170
name = "utm_alert_response_rule_template",
7271
joinColumns = @JoinColumn(name = "rule_id"),
@@ -109,30 +108,4 @@ public UtmAlertResponseRule(UtmAlertResponseRuleDTO dto) {
109108
}
110109
}
111110

112-
public void mergeInto(UtmAlertResponseRule target) {
113-
target.setRuleName(this.getRuleName());
114-
target.setRuleDescription(this.getRuleDescription());
115-
target.setRuleCmd(this.getRuleCmd());
116-
target.setRuleActive(this.getRuleActive());
117-
target.setAgentPlatform(this.getAgentPlatform());
118-
target.setDefaultAgent(this.getDefaultAgent());
119-
target.setExcludedAgents(this.getExcludedAgents());
120-
target.setRuleConditions(this.getRuleConditions());
121-
122-
if (!this.getUtmAlertResponseActionTemplates().isEmpty()) {
123-
List<UtmAlertResponseActionTemplate> targetActions = target.getUtmAlertResponseActionTemplates();
124-
targetActions.clear();
125-
targetActions.addAll(this.getUtmAlertResponseActionTemplates().stream().map(templateDto -> {
126-
UtmAlertResponseActionTemplate template = new UtmAlertResponseActionTemplate();
127-
template.setId(templateDto.getId());
128-
template.setTitle(templateDto.getTitle());
129-
template.setDescription(templateDto.getDescription());
130-
template.setCommand(templateDto.getCommand());
131-
return template;
132-
}).collect(Collectors.toList()));
133-
}
134-
135-
}
136-
137-
138111
}

backend/src/main/java/com/park/utmstack/service/alert_response_rule/UtmAlertResponseRuleService.java

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.park.utmstack.util.UtilJson;
3333
import com.park.utmstack.util.exceptions.UtmNotImplementedException;
3434
import io.grpc.stub.StreamObserver;
35+
import lombok.RequiredArgsConstructor;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738
import org.springframework.scheduling.annotation.Async;
@@ -49,6 +50,7 @@
4950
import java.util.stream.Collectors;
5051

5152
@Service
53+
@RequiredArgsConstructor
5254
@Transactional
5355
public class UtmAlertResponseRuleService {
5456

@@ -64,30 +66,9 @@ public class UtmAlertResponseRuleService {
6466
private final UtmAlertResponseRuleExecutionRepository alertResponseRuleExecutionRepository;
6567
private final UtmIncidentVariableService utmIncidentVariableService;
6668
private final UtmAlertResponseActionTemplateRepository utmAlertResponseActionTemplateRepository;
67-
6869
private final UtmAlertResponseActionTemplateService utmAlertResponseActionTemplateService;
6970

70-
public UtmAlertResponseRuleService(UtmAlertResponseRuleRepository alertResponseRuleRepository,
71-
UtmAlertResponseRuleHistoryRepository alertResponseRuleHistoryRepository,
72-
UtmNetworkScanRepository networkScanRepository,
73-
ApplicationEventService eventService,
74-
AgentService agentService,
75-
IncidentResponseCommandService incidentResponseCommandService,
76-
UtmAlertResponseRuleExecutionRepository alertResponseRuleExecutionRepository,
77-
UtmIncidentVariableService utmIncidentVariableService,
78-
UtmAlertResponseActionTemplateRepository utmAlertResponseActionTemplateRepository,
79-
UtmAlertResponseActionTemplateService utmAlertResponseActionTemplateService) {
80-
this.alertResponseRuleRepository = alertResponseRuleRepository;
81-
this.alertResponseRuleHistoryRepository = alertResponseRuleHistoryRepository;
82-
this.networkScanRepository = networkScanRepository;
83-
this.eventService = eventService;
84-
this.agentService = agentService;
85-
this.incidentResponseCommandService = incidentResponseCommandService;
86-
this.alertResponseRuleExecutionRepository = alertResponseRuleExecutionRepository;
87-
this.utmIncidentVariableService = utmIncidentVariableService;
88-
this.utmAlertResponseActionTemplateRepository = utmAlertResponseActionTemplateRepository;
89-
this.utmAlertResponseActionTemplateService = utmAlertResponseActionTemplateService;
90-
}
71+
9172

9273
public UtmAlertResponseRule save(UtmAlertResponseRule alertResponseRule) {
9374
final String ctx = CLASSNAME + ".save";
@@ -96,19 +77,11 @@ public UtmAlertResponseRule save(UtmAlertResponseRule alertResponseRule) {
9677
String alertRuleId = String.valueOf(alertResponseRule.getId());
9778
UtmAlertResponseRule current = alertResponseRuleRepository.findById(alertResponseRule.getId())
9879
.orElseThrow(() -> new RuntimeException(String.format("Incident response rule with ID: %1$s not found", alertRuleId)));
99-
alertResponseRule.mergeInto(current);
80+
this.mergeInto(current, alertResponseRule);
10081
alertResponseRuleHistoryRepository.save(new UtmAlertResponseRuleHistory(new UtmAlertResponseRuleDTO(alertResponseRule)));
10182
alertResponseRule = current;
10283
}
10384

104-
if (alertResponseRule.getUtmAlertResponseActionTemplates() != null) {
105-
for (UtmAlertResponseActionTemplate action : alertResponseRule.getUtmAlertResponseActionTemplates()) {
106-
if (action.getId() == null || !utmAlertResponseActionTemplateRepository.existsById(action.getId())) {
107-
utmAlertResponseActionTemplateService.save(action);
108-
}
109-
}
110-
}
111-
11285
return alertResponseRuleRepository.save(alertResponseRule);
11386
} catch (Exception e) {
11487
throw new RuntimeException(ctx + ": " + e.getLocalizedMessage());
@@ -347,4 +320,41 @@ public void onCompleted() {
347320
log.error(msg);
348321
}
349322
}
323+
324+
public void mergeInto(UtmAlertResponseRule target, UtmAlertResponseRule source) {
325+
target.setRuleName(source.getRuleName());
326+
target.setRuleDescription(source.getRuleDescription());
327+
target.setRuleCmd(source.getRuleCmd());
328+
target.setRuleActive(source.getRuleActive());
329+
target.setAgentPlatform(source.getAgentPlatform());
330+
target.setDefaultAgent(source.getDefaultAgent());
331+
target.setExcludedAgents(source.getExcludedAgents());
332+
target.setRuleConditions(source.getRuleConditions());
333+
334+
target.getUtmAlertResponseActionTemplates().clear();
335+
336+
if (!source.getUtmAlertResponseActionTemplates().isEmpty()) {
337+
List<UtmAlertResponseActionTemplate> managedTemplates = source.getUtmAlertResponseActionTemplates()
338+
.stream()
339+
.map(t -> {
340+
if (t.getId() != null) {
341+
UtmAlertResponseActionTemplate existing = utmAlertResponseActionTemplateRepository.findById(t.getId())
342+
.orElseThrow(() -> new RuntimeException("Template not found: " + t.getId()));
343+
existing.setTitle(t.getTitle());
344+
existing.setDescription(t.getDescription());
345+
existing.setCommand(t.getCommand());
346+
return existing;
347+
} else {
348+
UtmAlertResponseActionTemplate newT = new UtmAlertResponseActionTemplate();
349+
newT.setTitle(t.getTitle());
350+
newT.setDescription(t.getDescription());
351+
newT.setCommand(t.getCommand());
352+
return utmAlertResponseActionTemplateService.save(newT);
353+
}
354+
})
355+
.collect(Collectors.toList());
356+
357+
target.getUtmAlertResponseActionTemplates().addAll(managedTemplates);
358+
}
359+
}
350360
}

0 commit comments

Comments
 (0)