-
Notifications
You must be signed in to change notification settings - Fork 2
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
Dynamic Rerouting #286
Open
br648
wants to merge
11
commits into
dev
Choose a base branch
from
feature/otp-1457-dynamic-rerouting
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dynamic Rerouting #286
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d49dca4
feat(Adds the ability to reroute trips): Requests a new itinerary fro…
br648 d8e002d
refactor(Removed test to call reroute end point): Unable to mock OTP …
br648 e45a6f5
refactor(Removed redundant last leg check):
br648 8f6b927
refactor(Removed missing import):
br648 ec510ae
refactor(Updated to make use of otp response provider): Addressed som…
br648 bdaa2a1
Fixed merge conflicts
br648 a2784df
revert(Reinstated the github dir): Removed by mistake
br648 7eac962
fix(Issue with localhost reference in swagger output): Weird referenc…
br648 cf7113f
refactor(Updates to address PR feedback): New reroute tracking respon…
br648 2836ef0
refactor(Updated swagger docs):
br648 1ae6f1e
refactor(Updated unit tests to use besoke trip and rerouted trip):
br648 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,45 @@ | ||
package org.opentripplanner.middleware.triptracker; | ||
|
||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.opentripplanner.middleware.OtpMiddlewareMain; | ||
import org.opentripplanner.middleware.models.LegTransitionNotification; | ||
import org.opentripplanner.middleware.models.TrackedJourney; | ||
import org.opentripplanner.middleware.otp.OtpDispatcher; | ||
import org.opentripplanner.middleware.otp.OtpGraphQLVariables; | ||
import org.opentripplanner.middleware.otp.response.Itinerary; | ||
import org.opentripplanner.middleware.otp.response.Leg; | ||
import org.opentripplanner.middleware.otp.response.OtpResponse; | ||
import org.opentripplanner.middleware.otp.response.TripPlan; | ||
import org.opentripplanner.middleware.persistence.Persistence; | ||
import org.opentripplanner.middleware.triptracker.instruction.SelfLegInstruction; | ||
import org.opentripplanner.middleware.triptracker.interactions.TripActions; | ||
import org.opentripplanner.middleware.triptracker.instruction.TripInstruction; | ||
import org.opentripplanner.middleware.triptracker.interactions.TripActions; | ||
import org.opentripplanner.middleware.triptracker.interactions.busnotifiers.BusOperatorActions; | ||
import org.opentripplanner.middleware.triptracker.response.EndTrackingResponse; | ||
import org.opentripplanner.middleware.triptracker.response.RerouteResponse; | ||
import org.opentripplanner.middleware.triptracker.response.TrackingResponse; | ||
import org.opentripplanner.middleware.utils.Coordinates; | ||
import spark.Request; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.function.Supplier; | ||
|
||
import static org.opentripplanner.middleware.otp.response.Itinerary.getShortestDuration; | ||
import static org.opentripplanner.middleware.triptracker.TravelerLocator.isAtStartOfLeg; | ||
import static org.opentripplanner.middleware.triptracker.instruction.TripInstruction.NO_INSTRUCTION; | ||
import static org.opentripplanner.middleware.triptracker.instruction.TripInstruction.TRIP_INSTRUCTION_UPCOMING_RADIUS; | ||
import static org.opentripplanner.middleware.triptracker.TravelerLocator.isAtStartOfLeg; | ||
import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsInt; | ||
import static org.opentripplanner.middleware.utils.DateTimeUtils.getTimeNowAsString; | ||
import static org.opentripplanner.middleware.utils.ItineraryUtils.getRouteGtfsIdFromLeg; | ||
import static org.opentripplanner.middleware.utils.ItineraryUtils.isBusLeg; | ||
import static org.opentripplanner.middleware.utils.ItineraryUtils.legsMatch; | ||
import static org.opentripplanner.middleware.utils.JsonUtils.logMessageAndHalt; | ||
|
||
public class ManageTripTracking { | ||
|
||
public static Supplier<OtpResponse> otpResponseProviderOverride = null; | ||
private static OtpGraphQLVariables rerouteVariables = null; | ||
|
||
private ManageTripTracking() { | ||
} | ||
|
||
|
@@ -52,7 +68,20 @@ public static TrackingResponse startTracking(Request request) { | |
return null; | ||
} | ||
|
||
private static TrackingResponse doUpdateTracking(Request request, TripTrackingData tripData, boolean create) { | ||
private static TrackingResponse doUpdateTracking( | ||
Request request, | ||
TripTrackingData tripData, | ||
boolean create | ||
) { | ||
return doUpdateTracking(request, tripData, create, false); | ||
} | ||
|
||
private static TrackingResponse doUpdateTracking( | ||
Request request, | ||
TripTrackingData tripData, | ||
boolean create, | ||
boolean rerouted | ||
) { | ||
try { | ||
TrackedJourney trackedJourney; | ||
if (create) { | ||
|
@@ -84,7 +113,11 @@ private static TrackingResponse doUpdateTracking(Request request, TripTrackingDa | |
LegTransitionNotification.checkForLegTransition(tripStatus, travelerPosition, tripData.trip); | ||
|
||
// Provide response. | ||
TripInstruction instruction = TravelerLocator.getInstruction(tripStatus, travelerPosition, create); | ||
TripInstruction instruction = TravelerLocator.getInstruction( | ||
tripStatus, | ||
travelerPosition, | ||
create || rerouted | ||
); | ||
|
||
// Perform interactions such as triggering traffic signals when approaching segments so configured. | ||
// It is assumed to be ok to repeatedly perform the interaction. | ||
|
@@ -159,6 +192,115 @@ public static EndTrackingResponse forciblyEndTracking(Request request) { | |
return null; | ||
} | ||
|
||
/** | ||
* Attempt to reroute trip and provide appropriate response. | ||
*/ | ||
public static RerouteResponse rerouteTracking(Request request) { | ||
TripTrackingData tripData = TripTrackingData.fromRequestTripId(request); | ||
if (tripData != null) { | ||
var trackedJourney = tripData.journey; | ||
if (trackedJourney != null) { | ||
var reroutedItinerary = rerouteTrip(tripData); | ||
if (reroutedItinerary != null) { | ||
return new RerouteResponse( | ||
doUpdateTracking(request, tripData, false, true), | ||
reroutedItinerary | ||
); | ||
} else { | ||
return new RerouteResponse( | ||
TRIP_TRACKING_UPDATE_FREQUENCY_SECONDS, | ||
"No itinerary found!", | ||
trackedJourney.id, | ||
TripStatus.DEVIATED.name(), | ||
null | ||
); | ||
} | ||
} else { | ||
logMessageAndHalt(request, HttpStatus.BAD_REQUEST_400, "Journey for provided trip id does not exist!"); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Attempt to reroute from the traveler's current location to the trip's original destination. | ||
*/ | ||
public static Itinerary rerouteTrip(TripTrackingData tripData) { | ||
if (tripData != null) { | ||
var trackedJourney = tripData.journey; | ||
if (trackedJourney != null) { | ||
return updateTripWithNewItinerary(trackedJourney); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably notify companions/observers of that change. |
||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Record the location and time (as Strings to appease Mongo) a trip was rerouted and reset deviations. | ||
*/ | ||
private static void registerRerouting(TrackedJourney trackedJourney) { | ||
trackedJourney.reroutings.put( | ||
new Coordinates(trackedJourney.lastLocation()).getCoordinates(), | ||
LocalDateTime.now().toString() | ||
); | ||
Persistence.trackedJourneys.replace(trackedJourney.id, trackedJourney); | ||
} | ||
|
||
/** | ||
* Retrieve a new itinerary from OTP and update the associated trip's matching itinerary. | ||
*/ | ||
private static Itinerary updateTripWithNewItinerary(TrackedJourney trackedJourney) { | ||
Itinerary itinerary = getItineraryFromOtpResponse(trackedJourney); | ||
if (itinerary != null) { | ||
trackedJourney.trip.journeyState.matchingItinerary = itinerary; | ||
Persistence.monitoredTrips.replace(trackedJourney.trip.id, trackedJourney.trip); | ||
registerRerouting(trackedJourney); | ||
return itinerary; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Get the itinerary with the shortest duration returned from OTP using the new start location and current time. | ||
*/ | ||
private static Itinerary getItineraryFromOtpResponse(TrackedJourney trackedJourney) { | ||
try { | ||
Supplier<OtpResponse> otpResponseProvider = getOtpResponseProvider(); | ||
rerouteVariables = setOtpGraphQLVariables( | ||
trackedJourney.trip.otp2QueryParams, new Coordinates(trackedJourney.lastLocation()) | ||
); | ||
TripPlan plan = otpResponseProvider.get().plan; | ||
return plan == null ? null : getShortestDuration(plan.itineraries); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Define the new reroute variables related to the traveler's current location to be passed to OTP. | ||
*/ | ||
public static OtpGraphQLVariables setOtpGraphQLVariables(OtpGraphQLVariables originalTripVariables, Coordinates from) { | ||
OtpGraphQLVariables query = originalTripVariables.clone(); | ||
query.fromPlace = from.getCoordinates(); | ||
query.time = getTimeNowAsString(); | ||
return query; | ||
} | ||
|
||
/** | ||
* Define the OTP response source. This will either be an OTP server response or a mocked response for testing. | ||
*/ | ||
private static Supplier<OtpResponse> getOtpResponseProvider() { | ||
return OtpMiddlewareMain.inTestEnvironment && otpResponseProviderOverride != null | ||
? otpResponseProviderOverride | ||
: ManageTripTracking::getOtpResponse; | ||
} | ||
|
||
/** Default implementation for OtpResponse provider that actually invokes the OTP server. */ | ||
private static OtpResponse getOtpResponse() { | ||
return OtpDispatcher.sendOtpRequestWithErrorHandling(rerouteVariables); | ||
} | ||
|
||
/** | ||
* Complete a journey by defining the ending type, time and condition. Also cancel possible upcoming bus | ||
* notification. | ||
|
22 changes: 22 additions & 0 deletions
22
src/main/java/org/opentripplanner/middleware/triptracker/response/RerouteResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.opentripplanner.middleware.triptracker.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import org.opentripplanner.middleware.otp.response.Itinerary; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class RerouteResponse extends TrackingResponse { | ||
|
||
public final Itinerary itinerary; | ||
|
||
public RerouteResponse(int frequencySeconds, String instruction, String journeyId, String tripStatus, Itinerary itinerary) { | ||
super(frequencySeconds, instruction, journeyId, tripStatus); | ||
this.itinerary = itinerary; | ||
} | ||
|
||
public RerouteResponse(TrackingResponse trackingResponse, Itinerary itinerary) { | ||
super(trackingResponse); | ||
this.itinerary = itinerary; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the traveler takes the trip, it will likely still within the active trip monitoring window, and trip monitoring will attempt to replace
journeyState.matchingItinerary
with an itinerary matching the original one. To prevent that, if rerouting has occured,getQueryParamsForTargetZonedDateTime
should also plug the most recent rerouting location that is saved inTrackedJourney
.