forked from RedHatInsights/notifications-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replay skipped notifications (RedHatInsights#2568)
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/redhat/cloud/notifications/routers/engine/ReplayService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.redhat.cloud.notifications.routers.engine; | ||
|
||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import static com.redhat.cloud.notifications.Constants.API_INTERNAL; | ||
|
||
@RegisterRestClient(configKey = "internal-engine") | ||
public interface ReplayService { | ||
|
||
@Path(API_INTERNAL + "/replay") | ||
@POST | ||
void replay(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
engine/src/main/java/com/redhat/cloud/notifications/routers/ReplayResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.redhat.cloud.notifications.routers; | ||
|
||
import com.redhat.cloud.notifications.events.EndpointProcessor; | ||
import com.redhat.cloud.notifications.models.Event; | ||
import io.quarkus.logging.Log; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import static com.redhat.cloud.notifications.Constants.API_INTERNAL; | ||
import static java.time.Month.MARCH; | ||
|
||
@Path(API_INTERNAL + "/replay") | ||
public class ReplayResource { | ||
|
||
private static final int MAX_RESULTS = 100; | ||
|
||
@Inject | ||
EntityManager entityManager; | ||
|
||
@Inject | ||
EndpointProcessor endpointProcessor; | ||
|
||
public List<Event> getEvents(int firstResult, int maxResults) { | ||
String query = "FROM Event WHERE created > :start AND created <= :end"; | ||
return entityManager.createQuery(query, Event.class) | ||
.setParameter("start", LocalDateTime.of(2024, MARCH, 19, 9, 13)) | ||
.setParameter("end", LocalDateTime.of(2024, MARCH, 19, 14, 23)) | ||
.setFirstResult(firstResult) | ||
.setMaxResults(maxResults) | ||
.getResultList(); | ||
} | ||
|
||
@POST | ||
public void replay() { | ||
Log.info("Replay endpoint was called"); | ||
int firstResult = 0; | ||
List<Event> events; | ||
do { | ||
Log.infof("Processing events from index %d", firstResult); | ||
events = getEvents(firstResult, MAX_RESULTS); | ||
firstResult += MAX_RESULTS; | ||
for (Event event : events) { | ||
endpointProcessor.process(event, true); | ||
} | ||
} while (MAX_RESULTS == events.size()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
engine/src/test/java/com/redhat/cloud/notifications/routers/ReplayResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.redhat.cloud.notifications.routers; | ||
|
||
import com.redhat.cloud.notifications.TestLifecycleManager; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.redhat.cloud.notifications.Constants.API_INTERNAL; | ||
import static io.restassured.RestAssured.given; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(TestLifecycleManager.class) | ||
public class ReplayResourceTest { | ||
|
||
@Test | ||
void testReplay() { | ||
given() | ||
.basePath(API_INTERNAL) | ||
.when() | ||
.post("/replay") | ||
.then() | ||
.statusCode(204); | ||
} | ||
} |