Search in sources :

Example 1 with AbstractEmailPayloadAggregator

use of com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator in project notifications-backend by RedHatInsights.

the class EmailAggregator method getAggregated.

public Map<User, Map<String, Object>> getAggregated(EmailAggregationKey aggregationKey, EmailSubscriptionType emailSubscriptionType, LocalDateTime start, LocalDateTime end) {
    Map<User, AbstractEmailPayloadAggregator> aggregated = new HashMap<>();
    Set<String> subscribers = getEmailSubscribers(aggregationKey, emailSubscriptionType);
    // First, we retrieve all aggregations that match the given key.
    List<EmailAggregation> aggregations = emailAggregationRepository.getEmailAggregation(aggregationKey, start, end);
    // For each aggregation...
    for (EmailAggregation aggregation : aggregations) {
        // We need its event type to determine the target endpoints.
        String eventType = getEventType(aggregation);
        // Let's retrieve these targets.
        Set<Endpoint> endpoints = Set.copyOf(endpointRepository.getTargetEmailSubscriptionEndpoints(aggregationKey.getAccountId(), aggregationKey.getBundle(), aggregationKey.getApplication(), eventType));
        // Now we want to determine who will actually receive the aggregation email.
        // All users who subscribed to the current application and subscription type combination are recipients candidates.
        /*
             * The actual recipients list may differ from the candidates depending on the endpoint properties and the action settings.
             * The target endpoints properties will determine whether or not each candidate will actually receive an email.
             */
        Set<User> users = recipientResolver.recipientUsers(aggregationKey.getAccountId(), aggregationKey.getOrgId(), Stream.concat(endpoints.stream().map(EndpointRecipientSettings::new), getActionRecipient(aggregation).stream()).collect(Collectors.toSet()), subscribers);
        /*
             * We now have the final recipients list.
             * Let's populate the Map that will be returned by the method.
             */
        users.forEach(user -> {
            // It's aggregation time!
            fillUsers(aggregationKey, user, aggregated, aggregation);
        });
    }
    return aggregated.entrySet().stream().peek(entry -> {
        // TODO These fields could be passed to EmailPayloadAggregatorFactory.by since we know them from the beginning.
        entry.getValue().setStartTime(start);
        entry.getValue().setEndTimeKey(end);
    }).collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getContext()));
}
Also used : AbstractEmailPayloadAggregator(com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator) Endpoint(com.redhat.cloud.notifications.models.Endpoint) EmailPayloadAggregatorFactory(com.redhat.cloud.notifications.processors.email.aggregators.EmailPayloadAggregatorFactory) LocalDateTime(java.time.LocalDateTime) EndpointRepository(com.redhat.cloud.notifications.db.repositories.EndpointRepository) HashMap(java.util.HashMap) Inject(javax.inject.Inject) EmailAggregationRepository(com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Recipient(com.redhat.cloud.notifications.ingress.Recipient) User(com.redhat.cloud.notifications.recipients.User) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) ActionRecipientSettings(com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings) Set(java.util.Set) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) Collectors(java.util.stream.Collectors) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) RecipientResolver(com.redhat.cloud.notifications.recipients.RecipientResolver) ApplicationScoped(javax.enterprise.context.ApplicationScoped) EndpointRecipientSettings(com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings) User(com.redhat.cloud.notifications.recipients.User) AbstractEmailPayloadAggregator(com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator) HashMap(java.util.HashMap) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) Endpoint(com.redhat.cloud.notifications.models.Endpoint) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AbstractEmailPayloadAggregator

use of com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator in project notifications-backend by RedHatInsights.

the class EmailAggregator method fillUsers.

private void fillUsers(EmailAggregationKey aggregationKey, User user, Map<User, AbstractEmailPayloadAggregator> aggregated, EmailAggregation emailAggregation) {
    AbstractEmailPayloadAggregator aggregator = aggregated.computeIfAbsent(user, ignored -> EmailPayloadAggregatorFactory.by(aggregationKey));
    aggregator.aggregate(emailAggregation);
}
Also used : AbstractEmailPayloadAggregator(com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator)

Aggregations

AbstractEmailPayloadAggregator (com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator)2 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)1 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)1 EndpointRepository (com.redhat.cloud.notifications.db.repositories.EndpointRepository)1 Recipient (com.redhat.cloud.notifications.ingress.Recipient)1 EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)1 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 Endpoint (com.redhat.cloud.notifications.models.Endpoint)1 EmailPayloadAggregatorFactory (com.redhat.cloud.notifications.processors.email.aggregators.EmailPayloadAggregatorFactory)1 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)1 User (com.redhat.cloud.notifications.recipients.User)1 ActionRecipientSettings (com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings)1 EndpointRecipientSettings (com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 LocalDateTime (java.time.LocalDateTime)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1