From eb59c119cad8ae343db58827d8f377ce6f06f599 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:47:18 -0400 Subject: [PATCH] fix(TripSurveySenderJob): Send survey notifications within 30 mins of trip completion. --- .../middleware/OtpMiddlewareMain.java | 6 ++--- .../triptracker/TripSurveySenderJob.java | 14 ++++++------ .../triptracker/TripSurveySenderJobTest.java | 22 +++++++++---------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/OtpMiddlewareMain.java b/src/main/java/org/opentripplanner/middleware/OtpMiddlewareMain.java index 55486f74..a07babcc 100644 --- a/src/main/java/org/opentripplanner/middleware/OtpMiddlewareMain.java +++ b/src/main/java/org/opentripplanner/middleware/OtpMiddlewareMain.java @@ -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 ); } } diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJob.java b/src/main/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJob.java index 2638ce15..fa3e8f62 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJob.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJob.java @@ -49,7 +49,7 @@ public void run() { List usersWithNotificationsOverAWeekAgo = getUsersWithNotificationsOverAWeekAgo(); // Collect journeys that were completed/terminated in the past 24-48 hrs. (skip ongoing journeys). - List journeysCompletedInPast24To48Hours = getCompletedJourneysInPast24To48Hours(); + List journeysCompletedInPast24To48Hours = getCompletedJourneysInPastHour(); // Map users to journeys. Map> usersToJourneys = mapJourneysToUsers(journeysCompletedInPast24To48Hours, usersWithNotificationsOverAWeekAgo); @@ -88,14 +88,14 @@ public static List 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 getCompletedJourneysInPast24To48Hours() { - Date twentyFourHoursAgo = Date.from(Instant.now().minus(24, ChronoUnit.HOURS)); - Date fortyEightHoursAgo = Date.from(Instant.now().minus(48, ChronoUnit.HOURS)); + public static List 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); diff --git a/src/test/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJobTest.java b/src/test/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJobTest.java index a2076d5b..4617665c 100644 --- a/src/test/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJobTest.java +++ b/src/test/java/org/opentripplanner/middleware/triptracker/TripSurveySenderJobTest.java @@ -102,11 +102,11 @@ void canGetUsersWithNotificationsOverAWeekAgo() { } @Test - void canGetCompletedJourneysInPast24To48Hours() { + void canGetCompletedJourneysInPastHour() { assumeTrue(IS_END_TO_END); createTestJourneys(); - List completedJourneys = TripSurveySenderJob.getCompletedJourneysInPast24To48Hours(); + List completedJourneys = TripSurveySenderJob.getCompletedJourneysInPastHour(); assertEquals(2, completedJourneys.size()); } @@ -199,8 +199,8 @@ 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). @@ -208,17 +208,17 @@ private static void createTestJourneys() { // 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),