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 stop time fetching in added or replacement trips #6245

Open
wants to merge 4 commits into
base: dev-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.opentripplanner.apis.gtfs.GraphQLRequestContext;
Expand All @@ -36,7 +37,6 @@
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.Direction;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.utils.time.ServiceDateUtils;

Expand Down Expand Up @@ -132,38 +132,23 @@ public DataFetcher<Iterable<TransitAlert>> alerts() {
@Override
public DataFetcher<TripTimeOnDate> arrivalStoptime() {
return environment -> {
try {
TransitService transitService = getTransitService(environment);
TripPattern tripPattern = getTripPattern(environment);
if (tripPattern == null) {
return null;
}
Timetable timetable = tripPattern.getScheduledTimetable();

TripTimes triptimes = timetable.getTripTimes(getSource(environment));
LocalDate serviceDate = null;
Instant midnight = null;

var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
LocalDate serviceDate = null;
var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
try {
serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate());
midnight =
ServiceDateUtils
.asStartOfService(serviceDate, transitService.getTimeZone())
.toInstant();
} catch (ParseException e) {
//Invalid date format
return null;
}
}

return new TripTimeOnDate(
triptimes,
triptimes.getNumStops() - 1,
tripPattern,
serviceDate,
midnight
);
} catch (ParseException e) {
//Invalid date format
TripPattern tripPattern = getTripPattern(environment, serviceDate);
if (tripPattern == null) {
return null;
}

return getStoptimeAtIndex(environment, serviceDate, tripPattern.numberOfStops() - 1);
};
}

Expand All @@ -180,32 +165,23 @@ public DataFetcher<String> blockId() {
@Override
public DataFetcher<TripTimeOnDate> departureStoptime() {
return environment -> {
try {
TransitService transitService = getTransitService(environment);
TripPattern tripPattern = getTripPattern(environment);
if (tripPattern == null) {
return null;
}
Timetable timetable = tripPattern.getScheduledTimetable();

TripTimes triptimes = timetable.getTripTimes(getSource(environment));
LocalDate serviceDate = null;
Instant midnight = null;

var args = new GraphQLTypes.GraphQLTripDepartureStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
LocalDate serviceDate = null;
var args = new GraphQLTypes.GraphQLTripDepartureStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
try {
serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate());
midnight =
ServiceDateUtils
.asStartOfService(serviceDate, transitService.getTimeZone())
.toInstant();
} catch (ParseException e) {
//Invalid date format
return null;
}
}

return new TripTimeOnDate(triptimes, 0, tripPattern, serviceDate, midnight);
} catch (ParseException e) {
//Invalid date format
TripPattern tripPattern = getTripPattern(environment, serviceDate);
if (tripPattern == null) {
return null;
}

return getStoptimeAtIndex(environment, serviceDate, 0);
};
}

Expand Down Expand Up @@ -325,7 +301,7 @@ public DataFetcher<Iterable<TripTimeOnDate>> stoptimesForDate() {
? ServiceDateUtils.parseString(args.getGraphQLServiceDate())
: LocalDate.now(timeZone);

TripPattern tripPattern = transitService.getPatternForTrip(trip, serviceDate);
TripPattern tripPattern = getTripPattern(environment, serviceDate);
// no matching pattern found
if (tripPattern == null) {
return List.of();
Expand Down Expand Up @@ -400,6 +376,15 @@ private TripPattern getTripPattern(DataFetchingEnvironment environment) {
return getTransitService(environment).getPatternForTrip(environment.getSource());
}

private TripPattern getTripPattern(
DataFetchingEnvironment environment,
@Nullable LocalDate date
) {
return date == null
? getTripPattern(environment)
: getTransitService(environment).getPatternForTrip(environment.getSource(), date);
}

private TransitService getTransitService(DataFetchingEnvironment environment) {
return environment.<GraphQLRequestContext>getContext().transitService();
}
Expand All @@ -408,6 +393,36 @@ private RealtimeVehicleService getRealtimeVehiclesService(DataFetchingEnvironmen
return environment.<GraphQLRequestContext>getContext().realTimeVehicleService();
}

private TripTimeOnDate getStoptimeAtIndex(
DataFetchingEnvironment environment,
@Nullable LocalDate serviceDate,
int stopIndex
) {
var tripPattern = getTripPattern(environment, serviceDate);
var transitService = getTransitService(environment);
var timetable = serviceDate != null
? transitService.getTimetableForTripPattern(tripPattern, serviceDate)
: tripPattern.getScheduledTimetable();
if (timetable == null) {
return null;
}

var tripTimes = timetable.getTripTimes(getSource(environment));
if (tripTimes == null) {
return null;
}

return new TripTimeOnDate(
tripTimes,
stopIndex,
tripPattern,
serviceDate,
serviceDate == null
? null
: ServiceDateUtils.asStartOfService(serviceDate, transitService.getTimeZone()).toInstant()
);
}

private Trip getSource(DataFetchingEnvironment environment) {
return environment.getSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Nullable;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.StopLocation;
Expand All @@ -31,7 +32,10 @@ public class TripTimeOnDate {
private final int stopIndex;
// This is only needed because TripTimes has no reference to TripPattern
private final TripPattern tripPattern;

@Nullable
private final LocalDate serviceDate;

private final long midnight;

public TripTimeOnDate(TripTimes tripTimes, int stopIndex, TripPattern tripPattern) {
Expand All @@ -46,8 +50,8 @@ public TripTimeOnDate(
TripTimes tripTimes,
int stopIndex,
TripPattern tripPattern,
LocalDate serviceDate,
Instant midnight
@Nullable LocalDate serviceDate,
@Nullable Instant midnight
) {
this.tripTimes = tripTimes;
this.stopIndex = stopIndex;
Expand All @@ -59,9 +63,15 @@ public TripTimeOnDate(
/**
* Must pass in both Timetable and Trip, because TripTimes do not have a reference to
* StopPatterns.
*
* @return null if the trip does not exist in the timetable
*/
@Nullable
public static List<TripTimeOnDate> fromTripTimes(Timetable table, Trip trip) {
TripTimes times = table.getTripTimes(trip);
if (times == null) {
return null;
}
List<TripTimeOnDate> out = new ArrayList<>();
for (int i = 0; i < times.getNumStops(); ++i) {
out.add(new TripTimeOnDate(times, i, table.getPattern()));
Expand All @@ -74,14 +84,20 @@ public static List<TripTimeOnDate> fromTripTimes(Timetable table, Trip trip) {
* StopPatterns.
*
* @param serviceDate service day to set, if null none is set
* @return null if the trip does not exist in the timetable
*/

@Nullable
public static List<TripTimeOnDate> fromTripTimes(
Timetable table,
Trip trip,
LocalDate serviceDate,
Instant midnight
) {
TripTimes times = table.getTripTimes(trip);
if (times == null) {
return null;
}
List<TripTimeOnDate> out = new ArrayList<>();
for (int i = 0; i < times.getNumStops(); ++i) {
out.add(new TripTimeOnDate(times, i, table.getPattern(), serviceDate, midnight));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ public TimetableSnapshotSource(
}

/**
* Constructor is package local to allow unit-tests to provide their own clock, not using system
* time.
* Constructor to allow tests to provide their own clock, not using system time.
*/
TimetableSnapshotSource(
public TimetableSnapshotSource(
TimetableSnapshotSourceParameters parameters,
TimetableRepository timetableRepository,
Supplier<LocalDate> localDateNow
Expand Down
Loading
Loading