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()));
}
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();
});
}
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());
}
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();
}
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();
}
Aggregations