Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CheckMonitoredTrip): Revert a change from #288. #298

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ public boolean shouldSkipMonitoredTripCheck(boolean persist) throws Exception {

// Attempt to advance to the next monitored day, except for one-time trips
// or if tracking is ongoing or if the matching itinerary is still valid.
if (!trip.isOneTime() && !isTrackingOngoing() && !isMatchingItineraryStartTimeInTheFuture()) {
if (!trip.isOneTime() && !isTrackingOngoing()) {
advanceToNextMonitoredDay();
}

Expand Down Expand Up @@ -874,6 +874,13 @@ private void advanceToNextMonitoredDay() {
}
}

initializeTargetZonedDateTime();

// advance the trip to the next active date
advanceToNextActiveTripDate();
}

private void initializeTargetZonedDateTime() {
// Check if the CheckMonitoredTrip is being ran for the first time for this trip and if the trip's saved
// itinerary has already ended. Additionally, make sure that the saved itinerary occurred on the same
// service day. If both of these conditions are true, then there is no need to
Expand All @@ -885,9 +892,6 @@ private void advanceToNextMonitoredDay() {
) {
targetZonedDateTime = targetZonedDateTime.plusDays(1);
}

// advance the trip to the next active date
advanceToNextActiveTripDate();
}

/** Check if the previous matching itinerary was null or if it has already concluded */
Expand Down Expand Up @@ -948,6 +952,14 @@ private void advanceToNextActiveTripDate() {

LOG.info("Next matching itinerary starts at {}", matchingItinerary.startTime);

resetJourneyState();

// reset the snoozed parameter to false
trip.snoozed = false;
updateTripStatus();
}

private void resetJourneyState() {
// update journey state with baseline departure and arrival times which are the last known departure/arrival
journeyState.baselineDepartureTimeEpochMillis = matchingItinerary.startTime.getTime();
journeyState.baselineArrivalTimeEpochMillis = matchingItinerary.endTime.getTime();
Expand All @@ -960,10 +972,6 @@ private void advanceToNextActiveTripDate() {
// resent journey state's realtime data to be false as it has just been manually advanced without having checked
// the trip planner for realtime data
journeyState.hasRealtimeData = false;

// reset the snoozed parameter to false
trip.snoozed = false;
updateTripStatus();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import org.opentripplanner.middleware.utils.NotificationUtils;
import spark.Request;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Supplier;

import static org.opentripplanner.middleware.i18n.Message.TRIP_REROUTED_NOTIFICATION;
Expand Down Expand Up @@ -354,12 +357,39 @@ private static EndTrackingResponse completeJourney(TripTrackingData tripData, bo
trackedJourney.longestConsecutiveDeviatedPoints
);

resetMatchingItineraryIfNeeded(trackedJourney);

return new EndTrackingResponse(
NO_INSTRUCTION,
TripStatus.ENDED.name()
);
}

/**
* If rerouting occurred, then the matching itinerary changed with a starting point different from the
* starting location in the original itinerary.
* In that case, reset the matching itinerary, so that trip monitoring/live tracking uses the original routing.
*/
private static void resetMatchingItineraryIfNeeded(TrackedJourney trackedJourney) {
if (trackedJourney.getLastReroutingLocation() != null) {
try {
MonitoredTrip trip = trackedJourney.trip;

trip.journeyState.matchingItinerary = trip.itinerary.clone();

// TODO refactor same formula as in CheckMonitoredTrip
ZonedDateTime targetZonedDateTime = trip.tripZonedDateTime(LocalDate.parse(trip.journeyState.targetDate, DateTimeFormatter.ISO_LOCAL_DATE));
long offsetMillis = targetZonedDateTime.toInstant().toEpochMilli() - trip.journeyState.matchingItinerary.getScheduledStartTimeEpochMillis();
// update overall itinerary and leg start/end times by adding offset
trip.journeyState.matchingItinerary.offsetTimes(offsetMillis);

Persistence.monitoredTrips.replace(trip.id, trip);
} catch (CloneNotSupportedException e) {
// Do nothing if clone was not created.
}
}
}

/**
* Cancel bus notifications which are no longer needed/relevant.
*/
Expand Down
Loading
Loading