Skip to content

Commit a5a66c4

Browse files
author
Alekhya Parisha
committed
Add lookback time adjustment for Office 365 source plugin partition execution
Signed-off-by: Alekhya Parisha <aparisha@amazon.com>
1 parent 8904048 commit a5a66c4

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

  • data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src

data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/main/java/org/opensearch/dataprepper/plugins/source/microsoft_office365/service/Office365Service.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public AuditLogsResponse searchAuditLogs(final String logType,
6363
return office365RestClient.searchAuditLogs(logType, startTime, endTime, nextPageUri);
6464
}
6565

66-
// Check and adjust start time for 7-day limit
66+
// Adjust start time based on configured lookback hours
6767
Instant adjustedStartTime = startTime;
68-
Instant sevenDaysAgo = Instant.now().minus(SEVEN_DAYS);
69-
if (startTime.isBefore(sevenDaysAgo)) {
70-
adjustedStartTime = sevenDaysAgo;
68+
Instant lookBackHoursAgo = Instant.now().minus(Duration.ofHours(office365SourceConfig.getLookBackHours()));
69+
if (startTime.isBefore(lookBackHoursAgo) && lookBackHoursAgo.isBefore(endTime)) {
70+
adjustedStartTime = lookBackHoursAgo;
7171
}
7272

7373
AuditLogsResponse response =

data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/test/java/org/opensearch/dataprepper/plugins/source/microsoft_office365/service/Office365ServiceTest.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.BeforeEach;
1313
import org.junit.jupiter.api.Test;
1414
import org.junit.jupiter.api.extension.ExtendWith;
15+
import org.mockito.ArgumentCaptor;
1516
import org.mockito.Mock;
1617
import org.mockito.junit.jupiter.MockitoExtension;
1718
import org.opensearch.dataprepper.metrics.PluginMetrics;
@@ -20,21 +21,17 @@
2021
import org.opensearch.dataprepper.plugins.source.microsoft_office365.exception.Office365Exception;
2122
import org.opensearch.dataprepper.plugins.source.microsoft_office365.models.AuditLogsResponse;
2223

24+
import java.time.Clock;
2325
import java.time.Duration;
2426
import java.time.Instant;
27+
import java.time.ZoneOffset;
2528
import java.util.ArrayList;
2629
import java.util.HashMap;
2730
import java.util.List;
2831
import java.util.Map;
2932

30-
import static org.junit.jupiter.api.Assertions.assertNotNull;
31-
import static org.junit.jupiter.api.Assertions.assertEquals;
32-
import static org.junit.jupiter.api.Assertions.assertNull;
33-
import static org.junit.jupiter.api.Assertions.assertThrows;
34-
import static org.mockito.ArgumentMatchers.any;
35-
import static org.mockito.ArgumentMatchers.anyString;
36-
import static org.mockito.ArgumentMatchers.eq;
37-
import static org.mockito.ArgumentMatchers.isNull;
33+
import static org.junit.jupiter.api.Assertions.*;
34+
import static org.mockito.ArgumentMatchers.*;
3835
import static org.mockito.Mockito.verify;
3936
import static org.mockito.Mockito.when;
4037

@@ -208,6 +205,49 @@ void testGetAuditLog() {
208205
verify(office365RestClient).getAuditLog("test-id");
209206
}
210207

208+
@Test
209+
void testSearchAuditLogs_WithRange_AdjustsStartTime() {
210+
Clock fixedClock = Clock.fixed(Instant.parse("2025-11-09T21:30:00.00Z"), ZoneOffset.UTC);
211+
Instant now = Instant.now(fixedClock);
212+
213+
// Create a time window that's beyond our configured range:
214+
// Start time = current time - (4 days + 30 minutes)
215+
// This simulates the crawler creating a partition that starts slightly beyond our range
216+
Instant startTime = now.minus(Duration.ofDays(4))
217+
.minus(Duration.ofMinutes(30));
218+
// End time = start time + 1 hour
219+
Instant endTime = startTime.plus(Duration.ofHours(1));
220+
221+
String logType = "Exchange";
222+
int lookBackHours = 96; // Configure 4 days range limit
223+
224+
when(sourceConfig.getLookBackHours()).thenReturn(lookBackHours);
225+
when(office365RestClient.searchAuditLogs(
226+
any(String.class),
227+
any(Instant.class),
228+
any(Instant.class),
229+
any()
230+
)).thenReturn(new AuditLogsResponse(new ArrayList<>(), null));
231+
232+
office365Service.searchAuditLogs(logType, startTime, endTime, null);
233+
234+
// Capture the actual start time that was passed to the REST client
235+
ArgumentCaptor<Instant> startTimeCaptor = ArgumentCaptor.forClass(Instant.class);
236+
verify(office365RestClient).searchAuditLogs(
237+
eq(logType),
238+
startTimeCaptor.capture(),
239+
eq(endTime),
240+
isNull()
241+
);
242+
243+
// Verify that the service adjusted the start time correctly:
244+
Instant capturedStartTime = startTimeCaptor.getValue();
245+
Duration actualLookback = Duration.between(capturedStartTime, now);
246+
assertEquals(96, actualLookback.toHours(),
247+
"Adjusted start time should be exactly 96 hours ago");
248+
}
249+
250+
211251
private Map<String, Object> createTestItem(String contentId, Instant contentCreated) {
212252
Map<String, Object> item = new HashMap<>();
213253
item.put("contentId", contentId);

0 commit comments

Comments
 (0)