Search in sources :

Example 1 with AggregationCommand

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

the class DailyEmailAggregationJob method processDailyEmail.

@ActivateRequestContext
public void processDailyEmail() {
    CollectorRegistry registry = new CollectorRegistry();
    Gauge duration = Gauge.build().name("aggregator_job_duration_seconds").help("Duration of the aggregator job in seconds.").register(registry);
    Gauge.Timer durationTimer = duration.startTimer();
    try {
        LocalDateTime now = LocalDateTime.now(UTC);
        List<AggregationCommand> aggregationCommands = processAggregateEmails(now, registry);
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        for (AggregationCommand aggregationCommand : aggregationCommands) {
            try {
                final String payload = objectMapper.writeValueAsString(aggregationCommand);
                futures.add(emitter.send(payload).toCompletableFuture());
            } catch (JsonProcessingException e) {
                LOG.warn("Could not transform AggregationCommand to JSON object.", e);
            }
            // resolve completable futures so the Quarkus main thread doesn't stop before everything has been sent
            try {
                CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
            } catch (InterruptedException | ExecutionException ie) {
                LOG.error("Writing AggregationCommands failed", ie);
            }
        }
        emailAggregationResources.updateLastCronJobRun(now);
        Gauge lastSuccess = Gauge.build().name("aggregator_job_last_success").help("Last time the aggregator job succeeded.").register(registry);
        lastSuccess.setToCurrentTime();
    } finally {
        durationTimer.setDuration();
        PushGateway pg = new PushGateway(prometheusPushGatewayUrl);
        try {
            pg.pushAdd(registry, "aggregator_job");
        } catch (IOException e) {
            LOG.warn("Could not push metrics to Prometheus Pushgateway.", e);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) CollectorRegistry(io.prometheus.client.CollectorRegistry) IOException(java.io.IOException) Gauge(io.prometheus.client.Gauge) CompletableFuture(java.util.concurrent.CompletableFuture) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) PushGateway(io.prometheus.client.exporter.PushGateway) ExecutionException(java.util.concurrent.ExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ActivateRequestContext(javax.enterprise.context.control.ActivateRequestContext)

Example 2 with AggregationCommand

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

the class DailyEmailAggregationJobTest method shouldProcessOneSubscriptionOnly.

@Test
@TestTransaction
void shouldProcessOneSubscriptionOnly() {
    helpers.addEmailAggregation("tenant", "someOrgId", "rhel", "policies", "somePolicyId", "someHostId");
    helpers.addEmailAggregation("tenant", "someOrgId", "rhel", "policies", "somePolicyId", "someHostId");
    final List<AggregationCommand> emailAggregations = testee.processAggregateEmails(LocalDateTime.now(UTC), new CollectorRegistry());
    assertEquals(1, emailAggregations.size());
    final AggregationCommand aggregationCommand = emailAggregations.get(0);
    assertEquals("tenant", aggregationCommand.getAggregationKey().getAccountId());
    assertEquals("someOrgId", aggregationCommand.getAggregationKey().getOrgId());
    assertEquals("rhel", aggregationCommand.getAggregationKey().getBundle());
    assertEquals("policies", aggregationCommand.getAggregationKey().getApplication());
    assertEquals(DAILY, aggregationCommand.getSubscriptionType());
}
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 3 with AggregationCommand

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

the class DailyEmailAggregationJobTest method shouldNotIncreaseAggregationsWhenHostIdIsDifferent.

@Test
@TestTransaction
void shouldNotIncreaseAggregationsWhenHostIdIsDifferent() {
    helpers.addEmailAggregation("someTenant", "someOrgId", "someRhel", "somePolicies", "somePolicyId", "hostId1");
    helpers.addEmailAggregation("someTenant", "someOrgId", "someRhel", "somePolicies", "somePolicyId", "hostId2");
    final List<AggregationCommand> emailAggregations = testee.processAggregateEmails(LocalDateTime.now(UTC), new CollectorRegistry());
    assertEquals(1, 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 4 with AggregationCommand

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

the class DailyEmailAggregationJobTest method shouldProcessFourSubscriptions.

@Test
@TestTransaction
void shouldProcessFourSubscriptions() {
    helpers.addEmailAggregation("tenant", "someOrgId", "rhel", "policies", "somePolicyId", "someHostId");
    helpers.addEmailAggregation("tenant", "someOrgId", "rhel", "unknown-application", "somePolicyId", "someHostId");
    helpers.addEmailAggregation("tenant", "someOrgId", "unknown-bundle", "policies", "somePolicyId", "someHostId");
    helpers.addEmailAggregation("tenant", "someOrgId", "unknown-bundle", "unknown-application", "somePolicyId", "someHostId");
    final List<AggregationCommand> emailAggregations = testee.processAggregateEmails(LocalDateTime.now(UTC), new CollectorRegistry());
    assertEquals(4, 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 5 with AggregationCommand

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

the class DailyEmailAggregationJobTest method shouldNotIncreaseAggregationsWhenPolicyIdIsDifferent.

@Test
@TestTransaction
void shouldNotIncreaseAggregationsWhenPolicyIdIsDifferent() {
    helpers.addEmailAggregation("someTenant", "someOrgId", "someRhel", "somePolicies", "policyId1", "someHostId");
    helpers.addEmailAggregation("someTenant", "someOrgId", "someRhel", "somePolicies", "policyId2", "someHostId");
    final List<AggregationCommand> emailAggregations = testee.processAggregateEmails(LocalDateTime.now(UTC), new CollectorRegistry());
    assertEquals(1, 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)

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