use of com.redhat.cloud.notifications.models.EmailAggregation 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.EmailAggregation in project notifications-backend by RedHatInsights.
the class ResourceHelpers method addEmailAggregation.
public void addEmailAggregation(String tenant, String orgId, String bundle, String application, String policyId, String insightsId) {
EmailAggregation aggregation = TestHelpers.createEmailAggregation(tenant, orgId, bundle, application, policyId, insightsId);
addEmailAggregation(aggregation);
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class TestHelpers method createEmailAggregation.
static EmailAggregation createEmailAggregation(String tenant, String orgId, String bundle, String application, String policyId, String inventoryId) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setBundleName(bundle);
aggregation.setApplicationName(application);
aggregation.setAccountId(tenant);
aggregation.setOrgId(orgId);
aggregation.setCreated(LocalDateTime.now(UTC).minusHours(5L));
Action emailActionMessage = new Action();
emailActionMessage.setBundle(bundle);
emailActionMessage.setApplication(application);
emailActionMessage.setTimestamp(LocalDateTime.now());
emailActionMessage.setEventType("testEmailSubscriptionInstant");
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("inventory_id", inventoryId).withAdditionalProperty("system_check_in", "2020-08-03T15:22:42.199046").withAdditionalProperty("display_name", "My test machine").withAdditionalProperty("tags", List.of()).build());
emailActionMessage.setEvents(List.of(new Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("policy_id", policyId).withAdditionalProperty("policy_name", "not-tested-name").withAdditionalProperty("policy_description", "not-used-desc").withAdditionalProperty("policy_condition", "not-used-condition").build()).build()));
emailActionMessage.setAccountId(tenant);
JsonObject payload = baseTransformer.transform(emailActionMessage);
aggregation.setPayload(payload);
return aggregation;
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class RhosakEmailAggregatorTest method createDisruptionEmailAggregation.
private static EmailAggregation createDisruptionEmailAggregation(String kafkaName, String impactedArea) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setBundleName(APPLICATION_SERVICES);
aggregation.setApplicationName(RHOSAK);
aggregation.setAccountId(ACCOUNT_ID);
Action emailActionMessage = new Action();
emailActionMessage.setBundle(APPLICATION_SERVICES);
emailActionMessage.setApplication(RHOSAK);
emailActionMessage.setTimestamp(NOW);
emailActionMessage.setEventType(DISRUPTION);
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("impacted_area", impactedArea).build());
emailActionMessage.setEvents(List.of(new Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("id", kafkaName).withAdditionalProperty("name", kafkaName).build()).build()));
emailActionMessage.setAccountId(ACCOUNT_ID);
JsonObject payload = baseTransformer.transform(emailActionMessage);
aggregation.setPayload(payload);
return aggregation;
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class RhosakEmailAggregatorTest method serviceDisruptionAggregationTests.
@Test
void serviceDisruptionAggregationTests() {
String kafkaName = "my-kafka";
String perf = "performance";
String impactedArea = "impacted_area";
// test first aggregation
EmailAggregation aggregation = createDisruptionEmailAggregation(kafkaName, perf);
aggregator.processEmailAggregation(aggregation);
assertEquals(1, disruptions.size(), "aggregator should have one disruption body");
JsonObject entry = disruptions.getJsonObject(kafkaName);
assertNotNull(entry);
assertEquals(perf, entry.getString(impactedArea));
// test second aggregation
String throughput = "throughput";
aggregation = createDisruptionEmailAggregation(kafkaName, throughput);
aggregator.processEmailAggregation(aggregation);
assertEquals(1, disruptions.size(), "aggregator should still have one disruption body");
entry = disruptions.getJsonObject(kafkaName);
assertEquals(perf + ", " + throughput, entry.getString(impactedArea));
assertNotNull(entry.getString("start_time"));
// test third aggregation
String latency = "latency";
aggregation = createDisruptionEmailAggregation(kafkaName, latency);
aggregator.processEmailAggregation(aggregation);
assertEquals(1, disruptions.size(), "aggregator should still have one disruption body");
entry = disruptions.getJsonObject(kafkaName);
assertEquals(perf + ", " + latency + ", " + throughput, entry.getString(impactedArea));
// test fourth aggregation
String impactedArea1 = "latency, performance, throughput, availability";
aggregation = createDisruptionEmailAggregation(kafkaName, impactedArea1);
aggregator.processEmailAggregation(aggregation);
assertEquals(1, disruptions.size(), "aggregator should still have one disruption body");
entry = disruptions.getJsonObject(kafkaName);
assertEquals(perf + ", " + latency + ", " + throughput + ", availability", entry.getString(impactedArea));
// test fifth aggregation with another kafka
String kafkaName1 = "another-kafka-name";
aggregation = createDisruptionEmailAggregation(kafkaName1, impactedArea1);
aggregator.processEmailAggregation(aggregation);
assertEquals(2, disruptions.size(), "aggregator should still have two disruption bodies");
entry = disruptions.getJsonObject(kafkaName1);
assertEquals(impactedArea1, entry.getString(impactedArea));
// there has been no upgrades aggregation
assertEquals(0, upgrades.size(), "service disruption only aggregator should have empty upgrades body");
}
Aggregations