use of io.gravitee.repository.management.model.AlertTrigger in project gravitee-management-rest-api by gravitee-io.
the class AlertAnalyticsServiceImpl method findByReference.
@Override
public AlertAnalyticsEntity findByReference(AlertReferenceType referenceType, String referenceId, AlertAnalyticsQuery analyticsQuery) {
try {
Map<String, AlertTrigger> triggersById = alertTriggerRepository.findByReference(referenceType.name(), referenceId).stream().collect(toMap(AlertTrigger::getId, trigger -> trigger));
Map<AlertTrigger, HashSet<AlertEvent>> eventsByAlert = triggersById.values().stream().map(trigger -> alertEventRepository.search(new AlertEventCriteria.Builder().alert(trigger.getId()).from(analyticsQuery.getFrom()).to(analyticsQuery.getTo()).build(), null)).filter(events -> events.getContent().size() > 0).collect(Collectors.toMap(event -> triggersById.get(event.getContent().get(0).getAlert()), event -> new HashSet<>(event.getContent())));
// by severity
Map<String, Integer> bySeverity = eventsByAlert.entrySet().stream().collect(groupingBy(entry -> entry.getKey().getSeverity(), Collectors.mapping(Map.Entry::getValue, Collectors.summingInt(HashSet::size))));
// alerts sorted by severity then by decreasing event count
List<AlertAnalyticsEntity.AlertTriggerAnalytics> alerts = eventsByAlert.entrySet().stream().sorted(Map.Entry.<AlertTrigger, HashSet<AlertEvent>>comparingByKey((a1, a2) -> compareSeverity().compare(a1.getSeverity(), a2.getSeverity())).thenComparing(Map.Entry.<AlertTrigger, HashSet<AlertEvent>>comparingByValue(comparing(Set::size)).reversed())).map(e -> {
AlertAnalyticsEntity.AlertTriggerAnalytics alertTriggerAnalytics = new AlertAnalyticsEntity.AlertTriggerAnalytics();
alertTriggerAnalytics.setId(e.getKey().getId());
alertTriggerAnalytics.setType(e.getKey().getType());
alertTriggerAnalytics.setSeverity(e.getKey().getSeverity());
alertTriggerAnalytics.setName(e.getKey().getName());
alertTriggerAnalytics.setEventsCount(e.getValue().size());
return alertTriggerAnalytics;
}).collect(toList());
return new AlertAnalyticsEntity(bySeverity, alerts);
} catch (TechnicalException ex) {
final String message = "An error occurs while trying to list alerts analytics by reference " + referenceType + '/' + referenceId;
LOGGER.error(message, ex);
throw new TechnicalManagementException(message, ex);
}
}
use of io.gravitee.repository.management.model.AlertTrigger in project gravitee-management-rest-api by gravitee-io.
the class AlertAnalyticsServiceTest method alertTriggerProvider.
private List<AlertTrigger> alertTriggerProvider() {
ArrayList<AlertTrigger> alerts = new ArrayList<>();
AlertTrigger alert1 = new AlertTrigger();
alert1.setId("alert1");
alert1.setSeverity("INFO");
alerts.add(alert1);
AlertTrigger alert2 = new AlertTrigger();
alert2.setId("alert2");
alert2.setSeverity("CRITICAL");
alerts.add(alert2);
AlertTrigger alert3 = new AlertTrigger();
alert3.setId("alert3");
alert3.setSeverity("CRITICAL");
alerts.add(alert3);
AlertTrigger alert4 = new AlertTrigger();
alert4.setId("alert4");
alert4.setSeverity("WARNING");
alerts.add(alert4);
return alerts;
}
Aggregations