Skip to content

Commit

Permalink
fix(TripSurveySenderJob): Send survey notifications within 30 mins of…
Browse files Browse the repository at this point in the history
… trip completion.
  • Loading branch information
binh-dam-ibigroup committed Oct 25, 2024
1 parent 653b62c commit eb59c11
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public static void main(String[] args) throws IOException, InterruptedException
TimeUnit.MINUTES
);

// Schedule recurring job for post-trip surveys, once every hour to catch recently completed trips.
// Schedule recurring job for post-trip surveys, once every half-hour to catch recently completed trips.
// TODO: Determine whether this should go in some other process.
TripSurveySenderJob tripSurveySenderJob = new TripSurveySenderJob();
Scheduler.scheduleJob(
tripSurveySenderJob,
0,
1,
TimeUnit.HOURS
30,
TimeUnit.MINUTES
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void run() {
List<OtpUser> usersWithNotificationsOverAWeekAgo = getUsersWithNotificationsOverAWeekAgo();

// Collect journeys that were completed/terminated in the past 24-48 hrs. (skip ongoing journeys).
List<TrackedJourney> journeysCompletedInPast24To48Hours = getCompletedJourneysInPast24To48Hours();
List<TrackedJourney> journeysCompletedInPast24To48Hours = getCompletedJourneysInPastHour();

// Map users to journeys.
Map<OtpUser, List<TrackedJourney>> usersToJourneys = mapJourneysToUsers(journeysCompletedInPast24To48Hours, usersWithNotificationsOverAWeekAgo);
Expand Down Expand Up @@ -88,14 +88,14 @@ public static List<OtpUser> getUsersWithNotificationsOverAWeekAgo() {
}

/**
* Gets tracked journeys for all users that were completed in the past 24 hours.
* Gets tracked journeys for all users that were completed in the past hour.
*/
public static List<TrackedJourney> getCompletedJourneysInPast24To48Hours() {
Date twentyFourHoursAgo = Date.from(Instant.now().minus(24, ChronoUnit.HOURS));
Date fortyEightHoursAgo = Date.from(Instant.now().minus(48, ChronoUnit.HOURS));
public static List<TrackedJourney> getCompletedJourneysInPastHour() {
Date now = new Date();
Date oneHourAgo = Date.from(Instant.now().minus(1, ChronoUnit.HOURS));
Bson dateFilter = Filters.and(
Filters.gte(END_TIME_FIELD_NAME, fortyEightHoursAgo),
Filters.lte(END_TIME_FIELD_NAME, twentyFourHoursAgo)
Filters.gte(END_TIME_FIELD_NAME, oneHourAgo),
Filters.lte(END_TIME_FIELD_NAME, now)
);
Bson completeFilter = Filters.eq(END_CONDITION_FIELD_NAME, TERMINATED_BY_USER);
Bson terminatedFilter = Filters.eq(END_CONDITION_FIELD_NAME, FORCIBLY_TERMINATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ void canGetUsersWithNotificationsOverAWeekAgo() {
}

@Test
void canGetCompletedJourneysInPast24To48Hours() {
void canGetCompletedJourneysInPastHour() {
assumeTrue(IS_END_TO_END);
createTestJourneys();

List<TrackedJourney> completedJourneys = TripSurveySenderJob.getCompletedJourneysInPast24To48Hours();
List<TrackedJourney> completedJourneys = TripSurveySenderJob.getCompletedJourneysInPastHour();
assertEquals(2, completedJourneys.size());
}

Expand Down Expand Up @@ -199,26 +199,26 @@ void canRunJob() {
}

private static void createTestJourneys() {
Instant threeHoursAgo = Instant.now().minus(3, ChronoUnit.HOURS);
Instant thirtyHoursAgo = Instant.now().minus(30, ChronoUnit.HOURS);
Instant threeMinutesInFuture = Instant.now().plus(3, ChronoUnit.MINUTES);
Instant thirtyMinutesAgo = Instant.now().minus(30, ChronoUnit.MINUTES);
Instant threeDaysAgo = Instant.now().minus(3, ChronoUnit.DAYS);

// Create journey for each trip for all users above (they will be deleted explicitly after each test).
journeys = List.of(
// Ongoing journey (should not be included)
createJourney("ongoing-journey", trip.id, null, null, 10),

// Journey completed by user 30 hours ago (should be included)
createJourney("user-terminated-journey", trip.id, thirtyHoursAgo, TERMINATED_BY_USER, 200),
// Journey completed by user 30 minutes ago (should be included)
createJourney("user-terminated-journey", trip.id, thirtyMinutesAgo, TERMINATED_BY_USER, 200),

// Journey terminated forcibly 30 hours ago (should be included)
createJourney("forcibly-terminated-journey", trip.id, thirtyHoursAgo, FORCIBLY_TERMINATED, 400),
// Journey terminated forcibly 30 minutes ago (should be included)
createJourney("forcibly-terminated-journey", trip.id, thirtyMinutesAgo, FORCIBLY_TERMINATED, 400),

// Additional journey completed over 48 hours (should not be included).
// Additional journey completed over an hour ago (should not be included).
createJourney("journey-done-3-days-ago", trip.id, threeDaysAgo, TERMINATED_BY_USER, 10),

// Additional journey completed within 24 hours (should not be included).
createJourney("journey-done-3-hours-ago", trip.id, threeHoursAgo, TERMINATED_BY_USER, 10),
// Additional journey completed with bogus time in future (should not be included).
createJourney("journey-done-3-hours-ago", trip.id, threeMinutesInFuture, TERMINATED_BY_USER, 10),

// Orphan journeys (should not be included)
createJourney("journey-3", "other-trip", null, null, 10),
Expand Down

0 comments on commit eb59c11

Please sign in to comment.