Search in sources :

Example 1 with AlertAnalyticsQuery

use of io.gravitee.rest.api.model.AlertAnalyticsQuery 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);
    }
}
Also used : AlertEvent(io.gravitee.repository.management.model.AlertEvent) AlertReferenceType(io.gravitee.rest.api.model.alert.AlertReferenceType) Logger(org.slf4j.Logger) AlertTrigger(io.gravitee.repository.management.model.AlertTrigger) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) AlertEventRepository(io.gravitee.repository.management.api.AlertEventRepository) LoggerFactory(org.slf4j.LoggerFactory) AlertAnalyticsService(io.gravitee.rest.api.service.AlertAnalyticsService) Set(java.util.Set) AlertEventCriteria(io.gravitee.repository.management.api.search.AlertEventCriteria) AlertTriggerRepository(io.gravitee.repository.management.api.AlertTriggerRepository) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Component(org.springframework.stereotype.Component) AlertAnalyticsEntity(io.gravitee.rest.api.model.alert.AlertAnalyticsEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) AlertAnalyticsQuery(io.gravitee.rest.api.model.AlertAnalyticsQuery) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) AlertTrigger(io.gravitee.repository.management.model.AlertTrigger) Set(java.util.Set) HashSet(java.util.HashSet) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) AlertEvent(io.gravitee.repository.management.model.AlertEvent) AlertAnalyticsEntity(io.gravitee.rest.api.model.alert.AlertAnalyticsEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) HashSet(java.util.HashSet)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 AlertEventRepository (io.gravitee.repository.management.api.AlertEventRepository)1 AlertTriggerRepository (io.gravitee.repository.management.api.AlertTriggerRepository)1 AlertEventCriteria (io.gravitee.repository.management.api.search.AlertEventCriteria)1 AlertEvent (io.gravitee.repository.management.model.AlertEvent)1 AlertTrigger (io.gravitee.repository.management.model.AlertTrigger)1 AlertAnalyticsQuery (io.gravitee.rest.api.model.AlertAnalyticsQuery)1 AlertAnalyticsEntity (io.gravitee.rest.api.model.alert.AlertAnalyticsEntity)1 AlertReferenceType (io.gravitee.rest.api.model.alert.AlertReferenceType)1 AlertAnalyticsService (io.gravitee.rest.api.service.AlertAnalyticsService)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Comparator (java.util.Comparator)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 Collectors.toList (java.util.stream.Collectors.toList)1