Skip to content

Commit 840afb7

Browse files
committed
feat(MailService): update alert email methods to use Event type for related logs
1 parent d5ea21c commit 840afb7

2 files changed

Lines changed: 10 additions & 14 deletions

File tree

backend/src/main/java/com/park/utmstack/service/MailService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.park.utmstack.domain.application_events.enums.ApplicationEventType;
66
import com.park.utmstack.domain.incident.UtmIncident;
77
import com.park.utmstack.domain.mail_sender.MailConfig;
8+
import com.park.utmstack.domain.shared_types.alert.Event;
89
import com.park.utmstack.domain.shared_types.alert.UtmAlert;
910
import com.park.utmstack.domain.shared_types.LogType;
1011
import com.park.utmstack.service.application_events.ApplicationEventService;
@@ -250,7 +251,7 @@ public void sendPasswordResetMail(User user) {
250251
}
251252

252253
@Async
253-
public void sendAlertEmail(List<String> emailsTo, UtmAlert alert, List<LogType> relatedLogs) {
254+
public void sendAlertEmail(List<String> emailsTo, UtmAlert alert, List<Event> relatedLogs) {
254255
final String ctx = CLASS_NAME + ".sendAlertEmail";
255256
try {
256257
JavaMailSender javaMailSender = getJavaMailSender();
@@ -333,7 +334,7 @@ public void sendIncidentEmail(List<String> emailsTo, List<UtmAlert> alerts, UtmI
333334
* @throws Exception In case of any error
334335
*/
335336
private ByteArrayResource buildAlertEmailAttachment(Context context, UtmAlert alert,
336-
List<LogType> relatedLogs) throws Exception {
337+
List<Event> relatedLogs) throws Exception {
337338
final String ctx = CLASS_NAME + ".buildAlertEmailAttachment";
338339
try {
339340
ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -351,9 +352,9 @@ private ByteArrayResource buildAlertEmailAttachment(Context context, UtmAlert al
351352
}
352353
}
353354

354-
private void buildRelatedEventCsvAttachment(List<LogType> relatedLogs, ZipOutputStream zipOut) {
355+
private void buildRelatedEventCsvAttachment(List<Event> relatedLogs, ZipOutputStream zipOut) {
355356
final String ctx = CLASS_NAME + ".buildRelatedEventCsvAttachment";
356-
Map<String, List<LogType>> evtTypes = new HashMap<>();
357+
Map<String, List<Event>> evtTypes = new HashMap<>();
357358

358359
// Separating event types
359360
relatedLogs.forEach(doc -> {

backend/src/main/java/com/park/utmstack/service/impl/UtmAlertServiceImpl.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ public void checkForNewAlerts() {
7979
UtmAlertLast initialDate = lastAlertRepository.findById(1L)
8080
.orElse(new UtmAlertLast(Instant.now().atZone(ZoneOffset.UTC).toInstant().minus(1, ChronoUnit.HOURS)));
8181

82+
Instant ts = initialDate.getLastAlertTimestamp();
83+
String formatted = ts.truncatedTo(ChronoUnit.MILLIS).toString();
84+
8285
List<FilterType> filters = new ArrayList<>();
8386
filters.add(new FilterType(Constants.alertStatus, OperatorType.IS, AlertStatus.OPEN.getCode()));
84-
filters.add(new FilterType(Constants.timestamp, OperatorType.IS_GREATER_THAN, initialDate.getLastAlertTimestamp().toString()));
87+
filters.add(new FilterType(Constants.timestamp, OperatorType.IS_GREATER_THAN, formatted));
8588

8689
SearchRequest.Builder srb = new SearchRequest.Builder();
8790
srb.query(SearchUtil.toQuery(filters))
@@ -105,14 +108,6 @@ public void checkForNewAlerts() {
105108
lastAlertRepository.save(initialDate);
106109

107110
for (UtmAlert alert : alerts) {
108-
List<LogType> relatedLogs;
109-
try {
110-
relatedLogs = getRelatedAlerts(alert.getLogs());
111-
} catch (Exception e) {
112-
log.error(ctx + ": " + e.getMessage());
113-
continue;
114-
}
115-
116111
String emails = Constants.CFG.get(Constants.PROP_ALERT_ADDRESS_TO_NOTIFY_ALERTS);
117112

118113
if (!StringUtils.hasText(emails)) {
@@ -123,7 +118,7 @@ public void checkForNewAlerts() {
123118
}
124119

125120
String[] addressToNotify = emails.replace(" ", "").split(",");
126-
mailService.sendAlertEmail(Arrays.asList(addressToNotify), alert, relatedLogs);
121+
mailService.sendAlertEmail(Arrays.asList(addressToNotify), alert, alert.getEvents());
127122
}
128123

129124
alertResponseRuleService.evaluateRules(alerts);

0 commit comments

Comments
 (0)