Search in sources :

Example 6 with AggregationCommand

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

the class DailyEmailAggregationJob method processAggregateEmails.

List<AggregationCommand> processAggregateEmails(LocalDateTime endTime, CollectorRegistry registry) {
    final CronJobRun lastCronJobRun = emailAggregationResources.getLastCronJobRun();
    LocalDateTime startTime = lastCronJobRun.getLastRun();
    LOG.infof("Collecting email aggregation for period (%s, %s) and type %s", startTime, endTime, DAILY);
    final List<AggregationCommand> pendingAggregationCommands = emailAggregationResources.getApplicationsWithPendingAggregation(startTime, endTime).stream().map(aggregationKey -> new AggregationCommand(aggregationKey, startTime, endTime, DAILY)).collect(Collectors.toList());
    LOG.infof("Finished collecting email aggregations for period (%s, %s) and type %s after %d seconds. %d (accountIds, applications) pairs were processed", startTime, endTime, DAILY, SECONDS.between(endTime, LocalDateTime.now(UTC)), pendingAggregationCommands.size());
    pairsProcessed = Gauge.build().name("aggregator_job_accountid_application_pairs_processed").help("Number of accountId and application pairs processed.").register(registry);
    pairsProcessed.set(pendingAggregationCommands.size());
    return pendingAggregationCommands;
}
Also used : LocalDateTime(java.time.LocalDateTime) Logger(org.jboss.logging.Logger) LocalDateTime(java.time.LocalDateTime) CompletableFuture(java.util.concurrent.CompletableFuture) PushGateway(io.prometheus.client.exporter.PushGateway) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Gauge(io.prometheus.client.Gauge) CronJobRun(com.redhat.cloud.notifications.models.CronJobRun) SECONDS(java.time.temporal.ChronoUnit.SECONDS) CollectorRegistry(io.prometheus.client.CollectorRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Channel(org.eclipse.microprofile.reactive.messaging.Channel) ActivateRequestContext(javax.enterprise.context.control.ActivateRequestContext) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) DAILY(com.redhat.cloud.notifications.EmailSubscriptionType.DAILY) EmailAggregationResources(com.redhat.cloud.notifications.db.EmailAggregationResources) UTC(java.time.ZoneOffset.UTC) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) Emitter(org.eclipse.microprofile.reactive.messaging.Emitter) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) CronJobRun(com.redhat.cloud.notifications.models.CronJobRun)

Example 7 with AggregationCommand

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

the class DailyEmailAggregationJobTest method shouldProcessZeroAggregations.

@Test
@TestTransaction
void shouldProcessZeroAggregations() {
    final List<AggregationCommand> emailAggregations = testee.processAggregateEmails(LocalDateTime.now(UTC), new CollectorRegistry());
    assertEquals(0, emailAggregations.size());
}
Also used : AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) CollectorRegistry(io.prometheus.client.CollectorRegistry) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) TestTransaction(io.quarkus.test.TestTransaction)

Example 8 with AggregationCommand

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

the class EmailSubscriptionTypeProcessorTest method shouldSuccessfullySendEmail.

@Test
void shouldSuccessfullySendEmail() {
    micrometerAssertionHelper.saveCounterValuesBeforeTest(AGGREGATION_COMMAND_REJECTED_COUNTER_NAME, AGGREGATION_COMMAND_PROCESSED_COUNTER_NAME, AGGREGATION_COMMAND_ERROR_COUNTER_NAME);
    AggregationCommand aggregationCommand1 = new AggregationCommand(new EmailAggregationKey("account-1", "org-1", "bundle-1", "app-1"), LocalDateTime.now(), LocalDateTime.now().plusDays(1), DAILY);
    AggregationCommand aggregationCommand2 = new AggregationCommand(new EmailAggregationKey("account-2", "org-2", "bundle-2", "app-2"), LocalDateTime.now(ZoneOffset.UTC).plusDays(1), LocalDateTime.now(ZoneOffset.UTC).plusDays(2), DAILY);
    when(emailTemplateFactory.get(anyString(), anyString())).thenReturn(new Blank());
    inMemoryConnector.source(AGGREGATION_CHANNEL).send(Json.encode(aggregationCommand1));
    inMemoryConnector.source(AGGREGATION_CHANNEL).send(Json.encode(aggregationCommand2));
    micrometerAssertionHelper.awaitAndAssertCounterIncrement(AGGREGATION_COMMAND_PROCESSED_COUNTER_NAME, 2);
    micrometerAssertionHelper.assertCounterIncrement(AGGREGATION_COMMAND_REJECTED_COUNTER_NAME, 0);
    micrometerAssertionHelper.assertCounterIncrement(AGGREGATION_COMMAND_ERROR_COUNTER_NAME, 0);
    // Let's check that EndpointEmailSubscriptionResources#sendEmail was called for each aggregation.
    verify(emailAggregationRepository, times(1)).getEmailAggregation(eq(aggregationCommand1.getAggregationKey()), eq(aggregationCommand1.getStart()), eq(aggregationCommand1.getEnd()));
    verify(emailAggregationRepository, times(1)).purgeOldAggregation(eq(aggregationCommand1.getAggregationKey()), eq(aggregationCommand1.getEnd()));
    verify(emailAggregationRepository, times(1)).getEmailAggregation(eq(aggregationCommand2.getAggregationKey()), eq(aggregationCommand2.getStart()), eq(aggregationCommand2.getEnd()));
    verify(emailAggregationRepository, times(1)).purgeOldAggregation(eq(aggregationCommand2.getAggregationKey()), eq(aggregationCommand2.getEnd()));
    verifyNoMoreInteractions(emailAggregationRepository);
    micrometerAssertionHelper.clearSavedValues();
}
Also used : Blank(com.redhat.cloud.notifications.templates.Blank) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 9 with AggregationCommand

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

the class EmailSubscriptionTypeProcessor method consumeEmailAggregations.

@Incoming(AGGREGATION_CHANNEL)
@Acknowledgment(Acknowledgment.Strategy.PRE_PROCESSING)
@Blocking
public void consumeEmailAggregations(String aggregationCommandJson) {
    AggregationCommand aggregationCommand;
    try {
        aggregationCommand = objectMapper.readValue(aggregationCommandJson, AggregationCommand.class);
    } catch (JsonProcessingException e) {
        LOGGER.error("Kafka aggregation payload parsing failed", e);
        rejectedAggregationCommandCount.increment();
        return;
    }
    LOGGER.infof("Processing received aggregation command: %s", aggregationCommand);
    processedAggregationCommandCount.increment();
    try {
        statelessSessionFactory.withSession(statelessSession -> {
            processAggregateEmailsByAggregationKey(aggregationCommand.getAggregationKey(), aggregationCommand.getStart(), aggregationCommand.getEnd(), aggregationCommand.getSubscriptionType(), // Delete on daily
            aggregationCommand.getSubscriptionType().equals(EmailSubscriptionType.DAILY));
        });
    } catch (Exception e) {
        LOGGER.info("Error while processing aggregation", e);
        failedAggregationCommandCount.increment();
    }
}
Also used : AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment) Blocking(io.smallrye.reactive.messaging.annotations.Blocking)

Aggregations

AggregationCommand (com.redhat.cloud.notifications.models.AggregationCommand)9 CollectorRegistry (io.prometheus.client.CollectorRegistry)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 Test (org.junit.jupiter.api.Test)6 TestTransaction (io.quarkus.test.TestTransaction)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Gauge (io.prometheus.client.Gauge)2 PushGateway (io.prometheus.client.exporter.PushGateway)2 IOException (java.io.IOException)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ActivateRequestContext (javax.enterprise.context.control.ActivateRequestContext)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DAILY (com.redhat.cloud.notifications.EmailSubscriptionType.DAILY)1 EmailAggregationResources (com.redhat.cloud.notifications.db.EmailAggregationResources)1 CronJobRun (com.redhat.cloud.notifications.models.CronJobRun)1 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)1 Blank (com.redhat.cloud.notifications.templates.Blank)1