Search in sources :

Example 1 with EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey 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 EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey in project notifications-backend by RedHatInsights.

the class EmailAggregationRepositoryTest method testAllMethodsWithOrgIdUsageEnabled.

@Test
void testAllMethodsWithOrgIdUsageEnabled() {
    System.setProperty("notifications.use-org-id", "true");
    LocalDateTime start = LocalDateTime.now(UTC).minusHours(1L);
    LocalDateTime end = LocalDateTime.now(UTC).plusHours(1L);
    EmailAggregationKey key = new EmailAggregationKey(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME);
    statelessSessionFactory.withSession(statelessSession -> {
        clearEmailAggregations();
        resourceHelpers.addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD1);
        resourceHelpers.addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
        resourceHelpers.addEmailAggregation("other-account", ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
        resourceHelpers.addEmailAggregation(ACCOUNT_ID, ORG_ID, "other-bundle", APP_NAME, PAYLOAD2);
        resourceHelpers.addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, "other-app", PAYLOAD2);
        List<EmailAggregation> aggregations = emailAggregationRepository.getEmailAggregation(key, start, end);
        assertEquals(2, aggregations.size());
        assertTrue(aggregations.stream().map(EmailAggregation::getAccountId).allMatch(ACCOUNT_ID::equals));
        assertTrue(aggregations.stream().map(EmailAggregation::getBundleName).allMatch(BUNDLE_NAME::equals));
        assertTrue(aggregations.stream().map(EmailAggregation::getApplicationName).allMatch(APP_NAME::equals));
        assertEquals(1, aggregations.stream().map(EmailAggregation::getPayload).filter(PAYLOAD1::equals).count());
        assertEquals(1, aggregations.stream().map(EmailAggregation::getPayload).filter(PAYLOAD2::equals).count());
        List<EmailAggregationKey> keys = getApplicationsWithPendingAggregation(start, end);
        assertEquals(4, keys.size());
        assertEquals(ORG_ID, aggregations.get(0).getOrgId());
        assertEquals(ACCOUNT_ID, keys.get(0).getAccountId());
        assertEquals(BUNDLE_NAME, keys.get(0).getBundle());
        assertEquals(APP_NAME, keys.get(0).getApplication());
        assertEquals(2, emailAggregationRepository.purgeOldAggregation(key, end));
        assertEquals(0, emailAggregationRepository.getEmailAggregation(key, start, end).size());
        assertEquals(3, getApplicationsWithPendingAggregation(start, end).size());
        clearEmailAggregations();
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 3 with EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey in project notifications-backend by RedHatInsights.

the class EmailAggregationResourcesTest method testAllMethods.

@Test
void testAllMethods() {
    LocalDateTime start = LocalDateTime.now(UTC).minusHours(1L);
    LocalDateTime end = LocalDateTime.now(UTC).plusHours(1L);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD1);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation("other-account", BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, "other-bundle", APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, "other-app", PAYLOAD2);
    EmailAggregationKey key = new EmailAggregationKey(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME);
    List<EmailAggregationKey> keys = emailAggregationResources.getApplicationsWithPendingAggregation(start, end);
    assertEquals(4, keys.size());
    assertEquals(ACCOUNT_ID, keys.get(0).getAccountId());
    assertNull(keys.get(0).getOrgId());
    assertEquals(BUNDLE_NAME, keys.get(0).getBundle());
    assertEquals(APP_NAME, keys.get(0).getApplication());
    Integer purged = resourceHelpers.purgeOldAggregation(key, end);
    assertEquals(2, purged);
    keys = emailAggregationResources.getApplicationsWithPendingAggregation(start, end);
    assertEquals(3, keys.size());
}
Also used : LocalDateTime(java.time.LocalDateTime) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 4 with EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey in project notifications-backend by RedHatInsights.

the class EmailAggregationResourcesTest method shouldNotMapOrgId.

@Test
void shouldNotMapOrgId() {
    LocalDateTime start = LocalDateTime.now(UTC).minusHours(1L);
    LocalDateTime end = LocalDateTime.now(UTC).plusHours(1L);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD1);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation("other-account", BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, "other-bundle", APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, "other-app", PAYLOAD2);
    List<EmailAggregationKey> keys = emailAggregationResources.getApplicationsWithPendingAggregation(start, end);
    assertNull(keys.get(0).getOrgId());
    clearEmailAggregations();
}
Also used : LocalDateTime(java.time.LocalDateTime) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey in project notifications-backend by RedHatInsights.

the class EmailAggregationResourcesTest method shouldMapOrgId.

@Test
void shouldMapOrgId() {
    LocalDateTime start = LocalDateTime.now(UTC).minusHours(1L);
    LocalDateTime end = LocalDateTime.now(UTC).plusHours(1L);
    addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD1);
    addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation("other-account", ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, ORG_ID, "other-bundle", APP_NAME, PAYLOAD2);
    addEmailAggregation(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, "other-app", PAYLOAD2);
    List<EmailAggregationKey> keys = emailAggregationResources.getApplicationsWithPendingAggregation(start, end);
    assertEquals("987654321", keys.get(0).getOrgId());
    clearEmailAggregations();
}
Also used : LocalDateTime(java.time.LocalDateTime) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 LocalDateTime (java.time.LocalDateTime)6 Test (org.junit.jupiter.api.Test)6 EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)3 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 AggregationCommand (com.redhat.cloud.notifications.models.AggregationCommand)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 Endpoint (com.redhat.cloud.notifications.models.Endpoint)1 AbstractEmailPayloadAggregator (com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator)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 Blank (com.redhat.cloud.notifications.templates.Blank)1 JsonArray (io.vertx.core.json.JsonArray)1