Skip to content

Commit

Permalink
Merge pull request #263 from ibi-group/log-failed-itin-checks-and-bus…
Browse files Browse the repository at this point in the history
…-notifs

Log failed itin checks and bus notifs
  • Loading branch information
binh-dam-ibigroup authored Nov 5, 2024
2 parents dd593cd + 6f0de6e commit b4184f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ MonitoredTrip postCreateHook(MonitoredTrip monitoredTrip, Request req) {
* monitored trip job, so return the trip as found in the database after the job completes.
*/
private MonitoredTrip runCheckMonitoredTrip(MonitoredTrip monitoredTrip) throws Exception {
new CheckMonitoredTrip(monitoredTrip, false).run();
new CheckMonitoredTrip(monitoredTrip, true).run();
return Persistence.monitoredTrips.getById(monitoredTrip.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.opentripplanner.middleware.otp.response.TripPlan;
import org.opentripplanner.middleware.utils.DateTimeUtils;
import org.opentripplanner.middleware.utils.ItineraryUtils;
import org.opentripplanner.middleware.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.DayOfWeek;
import java.time.ZonedDateTime;
Expand All @@ -29,6 +32,8 @@
* particular day of the week.
*/
public class ItineraryExistence extends Model {
private static final Logger LOG = LoggerFactory.getLogger(ItineraryExistence.class);

/**
* Initial set of requests on which to base the itinerary existence checks. We do not want these persisted.
*/
Expand Down Expand Up @@ -186,7 +191,9 @@ public String getInvalidDaysOfWeekMessage() {
public void checkExistence(MonitoredTrip trip) {
// TODO: Consider multi-threading?
// Check existence of itinerary in the response for each OTP request.
int index = 0;
for (OtpRequest otpRequest : otpRequests) {
index++;
boolean hasMatchingItinerary = false;
DayOfWeek dayOfWeek = otpRequest.dateTime.getDayOfWeek();
// Get existing result for day of week if a date for that day of week has already been processed, or create
Expand All @@ -199,37 +206,59 @@ public void checkExistence(MonitoredTrip trip) {

// Send off each plan query to OTP.
OtpResponse response = this.otpResponseProvider.apply(otpRequest);
TripPlan plan = response.plan;

// Handle response if valid itineraries exist.
if (plan != null && plan.itineraries != null) {
for (Itinerary itineraryCandidate : plan.itineraries) {
// If a matching itinerary on the same service day as the request date is found,
// save the date with the matching itinerary.
// (The matching itinerary will replace the original trip.itinerary.)
if (
ItineraryUtils.occursOnSameServiceDay(itineraryCandidate, otpRequest.dateTime, tripIsArriveBy) &&
ItineraryUtils.itinerariesMatch(referenceItinerary, itineraryCandidate)
) {
result.handleValidDate(otpRequest.dateTime, itineraryCandidate);
hasMatchingItinerary = true;
if (response == null) {
LOG.warn("Itinerary existence check failed on {} for trip {} - OTP response was null.", dayOfWeek , trip.id);
} else {
TripPlan plan = response.plan;

// Handle response if valid itineraries exist.
if (plan != null && plan.itineraries != null) {
for (Itinerary itineraryCandidate : plan.itineraries) {
// If a matching itinerary on the same service day as the request date is found,
// save the date with the matching itinerary.
// (The matching itinerary will replace the original trip.itinerary.)
if (
ItineraryUtils.occursOnSameServiceDay(itineraryCandidate, otpRequest.dateTime, tripIsArriveBy) &&
ItineraryUtils.itinerariesMatch(referenceItinerary, itineraryCandidate)
) {
result.handleValidDate(otpRequest.dateTime, itineraryCandidate);
hasMatchingItinerary = true;
}
}
}

if (!hasMatchingItinerary) {
// If no match was found for the date, mark day of week as non-existent for the itinerary.
result.handleInvalidDate(otpRequest.dateTime);

// Log if the itinerary didn't exist "today".
if (index == 1 && plan != null) {
logItineraryNotFound("Itinerary existence check failed 'today'.", trip, plan, LOG);
}
}
}
if (!hasMatchingItinerary) {
// If no match was found for the date, mark day of week as non-existent for the itinerary.
result.handleInvalidDate(otpRequest.dateTime);
}
}
if (!allMonitoredDaysAreValid(trip)) {
this.message = String.format(
"The trip is not possible on the following days of the week you have selected: %s",
"The trip is not possible on the following days of the week you have selected: %s. Real-time conditions have changed since this trip was planned. Return to the trip planner, plan a new trip, and save the result.",
getInvalidDaysOfWeekMessage()
);
this.error = true;
}
}

/** Log instances of itinerary not found. */
public static void logItineraryNotFound(String message, MonitoredTrip trip, TripPlan plan, Logger logger) {
logger.warn(
"{} - Trip {} - Params: {} - Saved itinerary: {} - OTP itineraries: {}",
message,
trip.id,
JsonUtils.toJson(trip.otp2QueryParams),
JsonUtils.toJson(trip.itinerary),
JsonUtils.toJson(plan.itineraries)
);
}

private static OtpResponse getOtpResponse(OtpRequest otpRequest) {
return OtpDispatcher.sendOtpRequestWithErrorHandling(otpRequest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ private boolean isTrackingOngoing() {
*/
private boolean makeOTPRequestAndUpdateMatchingItineraryInternal() {
OtpResponse otpResponse = otpResponseProvider.get();
if (otpResponse == null) return false;
if (otpResponse == null) {
LOG.warn("No comparison itinerary found for trip {} - OTP response was null.", trip.id);
return false;
}
for (int i = 0; i < otpResponse.plan.itineraries.size(); i++) {
Itinerary candidateItinerary = otpResponse.plan.itineraries.get(i);
if (ItineraryUtils.itinerariesMatch(trip.itinerary, candidateItinerary)) {
Expand Down Expand Up @@ -301,7 +304,7 @@ private boolean makeOTPRequestAndUpdateMatchingItineraryInternal() {
}

// If this point is reached, a matching itinerary was not found
LOG.warn("No comparison itinerary found in otp response for trip");
ItineraryExistence.logItineraryNotFound("No comparison itinerary found", trip, otpResponse.plan, LOG);

if (hasReachedMaxItineraryChecks()) {
// Check whether this trip should no longer ever be checked due to not having matching itineraries on any
Expand Down

0 comments on commit b4184f8

Please sign in to comment.