Skip to content

Commit 1fe8868

Browse files
committed
feat(alert): add countAllAlertsByStatus method for improved alert counting
1 parent 77116d8 commit 1fe8868

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void automaticReview() {
145145
final String ctx = CLASSNAME + ".automaticReview";
146146
try {
147147
// If no new alerts have been received, stop execution
148-
if (alertUtil.countAlertsByStatus(AlertStatus.AUTOMATIC_REVIEW.getCode()) == 0)
148+
if (alertUtil.countAllAlertsByStatus(AlertStatus.AUTOMATIC_REVIEW.getCode()) == 0)
149149
return;
150150

151151
// Getting all registered rules

backend/src/main/java/com/park/utmstack/util/AlertUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ public Long countAlertsByStatus(int status) {
4848
throw new RuntimeException(ctx + ": " + e.getMessage());
4949
}
5050
}
51+
52+
public Long countAllAlertsByStatus(int status) {
53+
final String ctx = CLASSNAME + ".countAlertsByStatus";
54+
final String AGG_NAME = "count_open_alerts";
55+
try {
56+
if (!elasticsearchService.indexExist(Constants.SYS_INDEX_PATTERN.get(SystemIndexPattern.ALERTS)))
57+
return 0L;
58+
59+
List<FilterType> filters = new ArrayList<>();
60+
filters.add(new FilterType(Constants.alertStatus, OperatorType.IS, status));
61+
filters.add(new FilterType(Constants.alertTags, OperatorType.DOES_NOT_CONTAIN, Constants.FALSE_POSITIVE_TAG));
62+
63+
SearchRequest.Builder srb = new SearchRequest.Builder();
64+
srb.query(SearchUtil.toQuery(filters))
65+
.index(Constants.SYS_INDEX_PATTERN.get(SystemIndexPattern.ALERTS))
66+
.aggregations(AGG_NAME, a -> a.valueCount(c -> c.field(Constants.alertStatus)))
67+
.size(0);
68+
69+
SearchResponse<Object> response = elasticsearchService.search(srb.build(), Object.class);
70+
return (long) response.aggregations().get(AGG_NAME).valueCount().value();
71+
} catch (Exception e) {
72+
throw new RuntimeException(ctx + ": " + e.getMessage());
73+
}
74+
}
5175
}

0 commit comments

Comments
 (0)