use of io.gravitee.rest.api.model.alert.AlertAnalyticsEntity 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.rest.api.model.alert.AlertAnalyticsEntity in project gravitee-management-rest-api by gravitee-io.
the class AlertAnalyticsServiceTest method shouldNotFindByReferenceWhenNoEvent.
@Test
public void shouldNotFindByReferenceWhenNoEvent() throws Exception {
when(alertTriggerRepository.findByReference(REFERENCE_TYPE, REFERENCE_ID)).thenReturn(alertTriggerProvider());
when(alertEventRepository.search(any(), any())).thenReturn(new Page(Collections.emptyList(), 0, 1, 0));
AlertAnalyticsEntity result = cut.findByReference(AlertReferenceType.PLATFORM, REFERENCE_ID, new AlertAnalyticsQuery.Builder().from(0).to(1).build());
assertThat(result.getAlerts()).isEmpty();
assertThat(result.getBySeverity()).isEmpty();
}
use of io.gravitee.rest.api.model.alert.AlertAnalyticsEntity in project gravitee-management-rest-api by gravitee-io.
the class AlertAnalyticsServiceTest method shouldNotFindByReferenceWhenNoAlert.
@Test
public void shouldNotFindByReferenceWhenNoAlert() throws Exception {
when(alertTriggerRepository.findByReference(REFERENCE_TYPE, REFERENCE_ID)).thenReturn(Collections.emptyList());
AlertAnalyticsEntity result = cut.findByReference(AlertReferenceType.PLATFORM, REFERENCE_ID, null);
assertThat(result.getAlerts()).isEmpty();
assertThat(result.getBySeverity()).isEmpty();
}
use of io.gravitee.rest.api.model.alert.AlertAnalyticsEntity in project gravitee-management-rest-api by gravitee-io.
the class AlertAnalyticsServiceTest method shouldFindByReference.
@Test
public void shouldFindByReference() throws Exception {
when(alertTriggerRepository.findByReference(REFERENCE_TYPE, REFERENCE_ID)).thenReturn(alertTriggerProvider());
when(alertEventRepository.search(any(), any())).thenReturn(alertEventsProvider(10, "alert1")).thenReturn(alertEventsProvider(50, "alert2")).thenReturn(alertEventsProvider(12, "alert3")).thenReturn(alertEventsProvider(0, "alert4"));
AlertAnalyticsEntity result = cut.findByReference(AlertReferenceType.PLATFORM, REFERENCE_ID, new AlertAnalyticsQuery.Builder().from(0).to(1).build());
assertThat(result.getAlerts()).hasSize(3);
// Checking sorting on Severity then event count
assertThat(result.getAlerts().get(0).getId()).isEqualTo("alert2");
assertThat(result.getAlerts().get(1).getId()).isEqualTo("alert3");
assertThat(result.getAlerts().get(2).getId()).isEqualTo("alert1");
assertThat(result.getBySeverity().get("CRITICAL")).isEqualTo(62);
assertThat(result.getBySeverity().get("INFO")).isEqualTo(10);
assertThat(result.getBySeverity().containsKey("WARNING")).isFalse();
}
Aggregations