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