Search in sources :

Example 1 with EmailAggregation

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

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);
}
Also used : EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Example 3 with EmailAggregation

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;
}
Also used : Action(com.redhat.cloud.notifications.ingress.Action) Event(com.redhat.cloud.notifications.ingress.Event) JsonObject(io.vertx.core.json.JsonObject) Payload(com.redhat.cloud.notifications.ingress.Payload) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Example 4 with EmailAggregation

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;
}
Also used : Context(com.redhat.cloud.notifications.ingress.Context) Action(com.redhat.cloud.notifications.ingress.Action) Event(com.redhat.cloud.notifications.ingress.Event) JsonObject(io.vertx.core.json.JsonObject) Payload(com.redhat.cloud.notifications.ingress.Payload) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Example 5 with EmailAggregation

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");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)19 JsonObject (io.vertx.core.json.JsonObject)13 Action (com.redhat.cloud.notifications.ingress.Action)11 Event (com.redhat.cloud.notifications.ingress.Event)9 Payload (com.redhat.cloud.notifications.ingress.Payload)8 Context (com.redhat.cloud.notifications.ingress.Context)5 QuarkusTest (io.quarkus.test.junit.QuarkusTest)5 Test (org.junit.jupiter.api.Test)5 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)4 LocalDateTime (java.time.LocalDateTime)4 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)2 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)2 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)2 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)2 User (com.redhat.cloud.notifications.recipients.User)2 ActionRecipientSettings (com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings)2 EndpointRecipientSettings (com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings)2 TemplateInstance (io.quarkus.qute.TemplateInstance)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1