Skip to content

Commit 70581f2

Browse files
committed
Created a generic method to centralize the logic for wrapping adjustedStartTime
Signed-off-by: enugraju <enugraju@amazon.com>
1 parent 8cf5caa commit 70581f2

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public AuditLogsResponse searchAuditLogs(final String logType,
6464
}
6565

6666
// Adjust start time based on configured lookback period (supports Instant-based granularity)
67-
Instant adjustedStartTime = startTime;
68-
Instant lookBackDuration = office365SourceConfig.getLookBackDuration(Instant.now());
69-
if (startTime.isBefore(lookBackDuration) && lookBackDuration.isBefore(endTime)) {
70-
adjustedStartTime = lookBackDuration;
71-
}
67+
Instant lookBackStartTime = office365SourceConfig.getLookBackDuration(Instant.now());
68+
Instant adjustedStartTime = office365SourceConfig.getAdjustedStartTime(startTime, endTime, lookBackStartTime);
7269

7370
AuditLogsResponse response =
7471
office365RestClient.searchAuditLogs(logType, adjustedStartTime, endTime, null);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void testGetLookBackDuration_withMinuteRange() throws Exception {
111111
setField(config, "range", fifteenMinutes);
112112

113113
Instant now = Instant.now();
114-
Instant lookBackDuration = config.getLookBackDuration(Instant.now());
114+
Instant lookBackDuration = config.getLookBackDuration(now);
115115
// Verify the duration is approximately 15 minutes before now (within 1 second tolerance)
116116
Duration actualDuration = Duration.between(lookBackDuration, now);
117117
assertEquals(15, actualDuration.toMinutes());
@@ -123,7 +123,7 @@ void testGetLookBackDuration_with30MinuteRange() throws Exception {
123123
setField(config, "range", thirtyMinutes);
124124

125125
Instant now = Instant.now();
126-
Instant lookBackDuration = config.getLookBackDuration(Instant.now());
126+
Instant lookBackDuration = config.getLookBackDuration(now);
127127
// Verify the duration is approximately 30 minutes before now
128128
Duration actualDuration = Duration.between(lookBackDuration, now);
129129
assertEquals(30, actualDuration.toMinutes());
@@ -135,7 +135,7 @@ void testGetLookBackDuration_with45MinuteRange() throws Exception {
135135
setField(config, "range", fortyFiveMinutes);
136136

137137
Instant now = Instant.now();
138-
Instant lookBackDuration = config.getLookBackDuration(Instant.now());
138+
Instant lookBackDuration = config.getLookBackDuration(now);
139139
// Verify the duration is approximately 45 minutes before now
140140
Duration actualDuration = Duration.between(lookBackDuration, now);
141141
assertEquals(45, actualDuration.toMinutes());
@@ -147,7 +147,7 @@ void testGetLookBackDuration_withHourRange() throws Exception {
147147
setField(config, "range", twoHours);
148148

149149
Instant now = Instant.now();
150-
Instant lookBackDuration = config.getLookBackDuration(Instant.now());
150+
Instant lookBackDuration = config.getLookBackDuration(now);
151151
Duration actualDuration = Duration.between(lookBackDuration, now);
152152
assertEquals(2, actualDuration.toHours());
153153
assertEquals(120, actualDuration.toMinutes());
@@ -159,7 +159,7 @@ void testGetLookBackDuration_withDayRange() throws Exception {
159159
setField(config, "range", oneDay);
160160

161161
Instant now = Instant.now();
162-
Instant lookBackDuration = config.getLookBackDuration(Instant.now());
162+
Instant lookBackDuration = config.getLookBackDuration(now);
163163
Duration actualDuration = Duration.between(lookBackDuration, now);
164164
assertEquals(24, actualDuration.toHours());
165165
assertEquals(1440, actualDuration.toMinutes());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Office365ServiceTest {
6060
void setUp() {
6161
office365Service = new Office365Service(sourceConfig, office365RestClient, pluginMetrics);
6262
lenient().when(sourceConfig.getLookBackDuration(any(Instant.class))).thenReturn(Instant.now().minus(Duration.ofDays(365)));
63+
lenient().doCallRealMethod().when(sourceConfig).getAdjustedStartTime(any(Instant.class), any(Instant.class), any(Instant.class));
6364
}
6465

6566
@Test

data-prepper-plugins/saas-source-plugins/source-crawler/src/main/java/org/opensearch/dataprepper/plugins/source/source_crawler/base/CrawlerSourceConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.opensearch.dataprepper.plugins.source.source_crawler.base;
22

33
import java.time.Duration;
4+
import java.time.Instant;
45

56
/**
67
* Marker interface to all the SAAS connectors configuration
@@ -55,4 +56,24 @@ default Duration getDurationToDelayRetry() {
5556
default Duration getLeaseInterval() {
5657
return Duration.ofMinutes(1);
5758
}
59+
60+
/**
61+
* Adjusts the start time based on the configured lookback boundary.
62+
* When the requested start time is before the lookback boundary and the lookback boundary
63+
* falls within the time window, the start time is adjusted to the lookback boundary to
64+
* respect API limitations (e.g., Office 365 API's maximum lookback period).
65+
*
66+
* @param startTime the requested start time for the query
67+
* @param endTime the end time for the query
68+
* @param lookBackStartTime the earliest time to query from (e.g., from getLookBackDuration(referenceTime))
69+
* @return the adjusted start time, or the original start time if no adjustment is needed
70+
*/
71+
default Instant getAdjustedStartTime(final Instant startTime,
72+
final Instant endTime,
73+
final Instant lookBackStartTime) {
74+
if (startTime.isBefore(lookBackStartTime) && lookBackStartTime.isBefore(endTime)) {
75+
return lookBackStartTime;
76+
}
77+
return startTime;
78+
}
5879
}

0 commit comments

Comments
 (0)