Skip to content

Commit

Permalink
Merge branch 'fix-realtime-update-service-days' into dev-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
optionsome committed Feb 27, 2023
2 parents 4d1f2a6 + 6150f21 commit 095ffe6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import org.opentripplanner.transit.model.network.RoutingTripPattern;
import org.opentripplanner.transit.model.timetable.FrequencyEntry;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A TripPattern with its TripSchedules filtered by validity on a particular date. This is to avoid
* having to do any filtering by date during the search itself.
*/
public class TripPatternForDate implements Comparable<TripPatternForDate> {

private static final Logger LOG = LoggerFactory.getLogger(TripPatternForDate.class);

/**
* The original TripPattern whose TripSchedules were filtered to produce this.tripSchedules. Its
* TripSchedules remain unchanged.
Expand Down Expand Up @@ -98,6 +102,28 @@ public TripPatternForDate(
.asDateTime(localDate, last.getArrivalTime(last.getNumStops() - 1))
.toLocalDate();
}
if (startOfRunningPeriod.isAfter(endOfRunningPeriod)) {
if (hasFrequencies()) {
var tripId = frequencies.get(0).tripTimes.getTrip().getId();
LOG.warn(
"Could not construct as start of the running period {} is after the end {} in frequency trip {}",
startOfRunningPeriod,
endOfRunningPeriod,
tripId
);
} else {
var tripId = tripTimes.get(0).getTrip().getId();
LOG.warn(
"Could not construct as start of the running period {} is after the end {} in trip {}",
startOfRunningPeriod,
endOfRunningPeriod,
tripId
);
}
throw new IllegalArgumentException(
"Start of the running period is after end of the running period"
);
}
}

public List<TripTimes> tripTimes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ public void update(
datesToBeUpdated.addAll(oldTripPatternForDate.getRunningPeriodDates());
}

TripPatternForDate newTripPatternForDate = tripPatternForDateMapper.map(
timetable,
timetable.getServiceDate()
);
TripPatternForDate newTripPatternForDate;

try {
newTripPatternForDate = tripPatternForDateMapper.map(timetable, timetable.getServiceDate());
} catch (IllegalArgumentException exception) {
// There is some issue with finding the correct running period, using old pattern instead
newTripPatternForDate = oldTripPatternForDate;
}

if (newTripPatternForDate != null) {
tripPatternsStartingOnDateMapCache.get(date).put(tripPattern, newTripPatternForDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ private Result<UpdateSuccess, UpdateError> handleScheduledTrip(
return UpdateError.result(tripId, NO_UPDATES);
}

final FeedScopedId serviceId = transitService.getTripForId(tripId).getServiceId();
final Set<LocalDate> serviceDates = transitService
.getCalendarService()
.getServiceDatesForServiceId(serviceId);
if (!serviceDates.contains(serviceDate)) {
debug(
tripId,
"SCHEDULED trip has service date {} for which trip's service is not valid, skipping.",
serviceDate.toString()
);
return UpdateError.result(tripId, NO_SERVICE_ON_DATE);
}

// If this trip_id has been used for previously ADDED/MODIFIED trip message (e.g. when the
// sequence of stops has changed, and is now changing back to the originally scheduled one),
// mark that previously created trip as DELETED.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.NO_SERVICE_ON_DATE;
import static org.opentripplanner.updater.trip.BackwardsDelayPropagationType.REQUIRED_NO_DATA;
import static org.opentripplanner.updater.trip.TimetableSnapshotSourceTest.SameAssert.NotSame;
import static org.opentripplanner.updater.trip.TimetableSnapshotSourceTest.SameAssert.Same;
Expand Down Expand Up @@ -574,6 +575,48 @@ public void scheduled() {
assertEquals(90, originalTripTimesForToday.getDepartureDelay(2));
}

/**
* This test just asserts that trip with start date that is outside the service period doesn't
* throw an exception and is ignored instead.
*/
@Test
public void invalidTripDate() {
// GIVEN

String scheduledTripId = "1.1";

var serviceDateOutsideService = SERVICE_DATE.minusYears(10);
var builder = new TripUpdateBuilder(
scheduledTripId,
serviceDateOutsideService,
SCHEDULED,
transitModel.getTimeZone()
)
.addDelayedStopTime(1, 0)
.addDelayedStopTime(2, 60, 80)
.addDelayedStopTime(3, 90, 90);

var tripUpdate = builder.build();

var updater = defaultUpdater();

// WHEN
var result = updater.applyTripUpdates(
TRIP_MATCHER_NOOP,
REQUIRED_NO_DATA,
fullDataset,
List.of(tripUpdate),
feedId
);

// THEN
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
assertNull(snapshot);
assertEquals(1, result.failed());
var errors = result.failures();
assertEquals(1, errors.get(NO_SERVICE_ON_DATE).size());
}

@Test
public void scheduledTripWithSkippedAndNoData() {
// GIVEN
Expand Down

0 comments on commit 095ffe6

Please sign in to comment.