From 9304e371a33f2e1337c7517d953f166977610bfc Mon Sep 17 00:00:00 2001 From: Brandon Shien Date: Wed, 13 Nov 2024 15:52:22 -0800 Subject: [PATCH] Changed s3 event index lambda to default index yesterday and today to match eventbridge rule change Signed-off-by: Brandon Shien --- .../lambda/GithubEventsLambda.java | 2 +- .../lambda/GithubEventsLambdaTest.java | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/opensearchmetrics/lambda/GithubEventsLambda.java b/src/main/java/org/opensearchmetrics/lambda/GithubEventsLambda.java index 033ad9d..0a43868 100644 --- a/src/main/java/org/opensearchmetrics/lambda/GithubEventsLambda.java +++ b/src/main/java/org/opensearchmetrics/lambda/GithubEventsLambda.java @@ -69,7 +69,7 @@ public Void handleRequest(Map input, Context context) { } LocalDate collectionCurrentDate = collectionStartDate; LocalDate today = LocalDate.now(ZoneOffset.UTC); - while (collectionCurrentDate.isBefore(today)) { + while (!collectionCurrentDate.isAfter(today)) { Map finalEventData = new HashMap<>(); for (GithubEvents eventToIndex : eventsToIndex) { String prefix = eventToIndex.getEventName() + "/" + collectionCurrentDate + "/"; diff --git a/src/test/java/org/opensearchmetrics/lambda/GithubEventsLambdaTest.java b/src/test/java/org/opensearchmetrics/lambda/GithubEventsLambdaTest.java index 39f99d2..cad519c 100644 --- a/src/test/java/org/opensearchmetrics/lambda/GithubEventsLambdaTest.java +++ b/src/test/java/org/opensearchmetrics/lambda/GithubEventsLambdaTest.java @@ -64,15 +64,19 @@ public void testHandleRequestYesterday() { Map input = new HashMap<>(); LocalDate yesterday = LocalDate.now(ZoneOffset.UTC).minus(1, ChronoUnit.DAYS); + LocalDate today = LocalDate.now(ZoneOffset.UTC); input.put("collectionStartDate", yesterday.toString()); // Act githubEventsLambda.handleRequest(input, mock(Context.class)); // Assert - String indexName = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy")); - verify(openSearchUtil).createIndexIfNotExists(indexName); - verify(openSearchUtil).bulkIndex(eq(indexName), any(Map.class)); + String indexNameYesterday = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy")); + String indexNameToday = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy")); + verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday); + verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday); + verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameYesterday), any(Map.class)); + verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameToday), any(Map.class)); } @Test @@ -88,8 +92,8 @@ public void testHandleRequestMonthAgo() { when(s3Util.getObjectInputStream(anyString())).thenReturn(new ResponseInputStream<>(getObjectResponse, new ByteArrayInputStream(eventJson.getBytes()))); Map input = new HashMap<>(); - LocalDate yesterday = LocalDate.now(ZoneOffset.UTC).minus(1, ChronoUnit.DAYS); - LocalDate lastMonth = yesterday.minus(1, ChronoUnit.MONTHS); + LocalDate today = LocalDate.now(ZoneOffset.UTC); + LocalDate lastMonth = today.minus(1, ChronoUnit.MONTHS); input.put("collectionStartDate", lastMonth.toString()); // Act @@ -100,7 +104,7 @@ public void testHandleRequestMonthAgo() { verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameLastMonth); verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameLastMonth), any(Map.class)); - String indexNameThisMonth = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy")); + String indexNameThisMonth = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy")); verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameThisMonth); verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameThisMonth), any(Map.class)); } @@ -119,14 +123,18 @@ public void testHandleRequestDefault() { Map input = new HashMap<>(); LocalDate yesterday = LocalDate.now(ZoneOffset.UTC).minus(1, ChronoUnit.DAYS); + LocalDate today = LocalDate.now(ZoneOffset.UTC); // Act githubEventsLambda.handleRequest(input, mock(Context.class)); // Assert - String indexName = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy")); - verify(openSearchUtil).createIndexIfNotExists(indexName); - verify(openSearchUtil).bulkIndex(eq(indexName), any(Map.class)); + String indexNameYesterday = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy")); + String indexNameToday = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy")); + verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday); + verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday); + verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameYesterday), any(Map.class)); + verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameToday), any(Map.class)); } @Test