Skip to content

Commit

Permalink
Merge branch 'fix-departureArrivalStopTime-npe' into aubin-test
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Nov 19, 2024
2 parents 41b2e5a + a7dc78e commit 991c09e
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
Expand All @@ -24,7 +23,6 @@
import org.opentripplanner.apis.gtfs.mapping.BikesAllowedMapper;
import org.opentripplanner.apis.gtfs.model.TripOccupancy;
import org.opentripplanner.apis.support.SemanticHash;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.TransitAlert;
Expand Down Expand Up @@ -132,23 +130,13 @@ public DataFetcher<Iterable<TransitAlert>> alerts() {
@Override
public DataFetcher<TripTimeOnDate> arrivalStoptime() {
return environment -> {
LocalDate serviceDate = null;
var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
try {
serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate());
} catch (ParseException e) {
//Invalid date format
return null;
}
}

TripPattern tripPattern = getTripPattern(environment, serviceDate);
if (tripPattern == null) {
return null;
}

return getStoptimeAtIndex(environment, serviceDate, tripPattern.numberOfStops() - 1);
var serviceDate = getOptionalServiceDateArgument(environment);
var trip = getSource(environment);
var transitService = getTransitService(environment);
var stopTimes = serviceDate
.map(date -> transitService.getTripTimeOnDates(trip, date))
.orElseGet(() -> transitService.getScheduledTripTimes(trip));
return stopTimes.map(List::getLast).orElse(null);
};
}

Expand All @@ -165,23 +153,13 @@ public DataFetcher<String> blockId() {
@Override
public DataFetcher<TripTimeOnDate> departureStoptime() {
return environment -> {
LocalDate serviceDate = null;
var args = new GraphQLTypes.GraphQLTripDepartureStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
try {
serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate());
} catch (ParseException e) {
//Invalid date format
return null;
}
}

TripPattern tripPattern = getTripPattern(environment, serviceDate);
if (tripPattern == null) {
return null;
}

return getStoptimeAtIndex(environment, serviceDate, 0);
var serviceDate = getOptionalServiceDateArgument(environment);
var trip = getSource(environment);
var transitService = getTransitService(environment);
var stopTimes = serviceDate
.map(date -> transitService.getTripTimeOnDates(trip, date))
.orElseGet(() -> transitService.getScheduledTripTimes(trip));
return stopTimes.map(List::getFirst).orElse(null);
};
}

Expand Down Expand Up @@ -276,43 +254,23 @@ public DataFetcher<Iterable<Object>> stops() {

@Override
public DataFetcher<Iterable<TripTimeOnDate>> stoptimes() {
return environment -> {
TripPattern tripPattern = getTripPattern(environment);
if (tripPattern == null) {
return List.of();
}
return TripTimeOnDate.fromTripTimes(
tripPattern.getScheduledTimetable(),
getSource(environment)
);
};
return environment ->
getTransitService(environment).getScheduledTripTimes(getSource(environment)).orElse(null);
}

@Override
public DataFetcher<Iterable<TripTimeOnDate>> stoptimesForDate() {
return environment -> {
try {
TransitService transitService = getTransitService(environment);
Trip trip = getSource(environment);
var args = new GraphQLTypes.GraphQLTripStoptimesForDateArgs(environment.getArguments());

ZoneId timeZone = transitService.getTimeZone();
LocalDate serviceDate = args.getGraphQLServiceDate() != null
? ServiceDateUtils.parseString(args.getGraphQLServiceDate())
: LocalDate.now(timeZone);

TripPattern tripPattern = getTripPattern(environment, serviceDate);
// no matching pattern found
if (tripPattern == null) {
return List.of();
}

Instant midnight = ServiceDateUtils.asStartOfService(serviceDate, timeZone).toInstant();
Timetable timetable = transitService.getTimetableForTripPattern(tripPattern, serviceDate);
return TripTimeOnDate.fromTripTimes(timetable, trip, serviceDate, midnight);
} catch (ParseException e) {
return null; // Invalid date format
}
TransitService transitService = getTransitService(environment);
Trip trip = getSource(environment);
var args = new GraphQLTypes.GraphQLTripStoptimesForDateArgs(environment.getArguments());

ZoneId timeZone = transitService.getTimeZone();
LocalDate serviceDate = args.getGraphQLServiceDate() != null
? ServiceDateUtils.parseString(args.getGraphQLServiceDate())
: LocalDate.now(timeZone);

return transitService.getTripTimeOnDates(trip, serviceDate).orElse(null);
};
}

Expand Down Expand Up @@ -393,34 +351,14 @@ 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;
private static Optional<LocalDate> getOptionalServiceDateArgument(
DataFetchingEnvironment environment
) throws ParseException {
var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments());
if (args.getGraphQLServiceDate() != null) {
return Optional.of(ServiceDateUtils.parseString(args.getGraphQLServiceDate()));
}

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()
);
return Optional.empty();
}

private Trip getSource(DataFetchingEnvironment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.opentripplanner.apis.transmodel.model.framework.TransmodelDirectives;
import org.opentripplanner.apis.transmodel.model.framework.TransmodelScalars;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.routing.TripTimeOnDateHelper;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
Expand Down Expand Up @@ -150,11 +149,10 @@ public static GraphQLObjectType create(
)
.dataFetcher(environment -> {
TripOnServiceDate tripOnServiceDate = tripOnServiceDate(environment);
return TripTimeOnDateHelper.getTripTimeOnDates(
GqlUtil.getTransitService(environment),
tripOnServiceDate.getTrip(),
tripOnServiceDate.getServiceDate()
);
return GqlUtil
.getTransitService(environment)
.getTripTimeOnDates(tripOnServiceDate.getTrip(), tripOnServiceDate.getServiceDate())
.orElse(null);
})
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.opentripplanner.apis.transmodel.model.framework.TransmodelScalars;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.framework.geometry.EncodedPolyline;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.routing.TripTimeOnDateHelper;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.Trip;
Expand Down Expand Up @@ -240,14 +238,7 @@ public static GraphQLObjectType create(
.description(
"Returns scheduled passing times only - without real-time-updates, for realtime-data use 'estimatedCalls'"
)
.dataFetcher(env -> {
Trip trip = trip(env);
TripPattern tripPattern = GqlUtil.getTransitService(env).getPatternForTrip(trip);
if (tripPattern == null) {
return List.of();
}
return TripTimeOnDate.fromTripTimes(tripPattern.getScheduledTimetable(), trip);
})
.dataFetcher(env -> GqlUtil.getTransitService(env).getScheduledTripTimes(trip(env)))
.build()
)
.field(
Expand All @@ -274,11 +265,10 @@ public static GraphQLObjectType create(
.ofNullable(environment.getArgument("date"))
.map(LocalDate.class::cast)
.orElse(LocalDate.now(GqlUtil.getTransitService(environment).getTimeZone()));
return TripTimeOnDateHelper.getTripTimeOnDates(
GqlUtil.getTransitService(environment),
trip(environment),
serviceDate
);
return GqlUtil
.getTransitService(environment)
.getTripTimeOnDates(trip(environment), serviceDate)
.orElse(null);
})
.build()
)
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 java.util.Objects;
import javax.annotation.Nullable;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.transit.model.network.TripPattern;
Expand Down Expand Up @@ -286,4 +287,22 @@ public BookingInfo getPickupBookingInfo() {
public BookingInfo getDropOffBookingInfo() {
return tripTimes.getDropOffBookingInfo(stopIndex);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
TripTimeOnDate that = (TripTimeOnDate) o;
return (
stopIndex == that.stopIndex &&
midnight == that.midnight &&
Objects.equals(tripTimes, that.tripTimes) &&
Objects.equals(tripPattern, that.tripPattern) &&
Objects.equals(serviceDate, that.serviceDate)
);
}

@Override
public int hashCode() {
return Objects.hash(tripTimes, stopIndex, tripPattern, serviceDate, midnight);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.updater.GraphUpdaterStatus;
import org.opentripplanner.utils.collection.CollectionsView;
import org.opentripplanner.utils.time.ServiceDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Default implementation of the Transit Service and Transit Editor Service.
Expand All @@ -68,6 +72,7 @@
*/
public class DefaultTransitService implements TransitEditorService {

private static final Logger LOG = LoggerFactory.getLogger(DefaultTransitService.class);
private final TimetableRepository timetableRepository;

private final TimetableRepositoryIndex timetableRepositoryIndex;
Expand All @@ -91,6 +96,45 @@ public DefaultTransitService(
this.timetableSnapshot = timetableSnapshotBuffer;
}

public Optional<List<TripTimeOnDate>> getScheduledTripTimes(Trip trip) {
TripPattern tripPattern = getPatternForTrip(trip);
if (tripPattern == null) {
return Optional.empty();
}
return Optional.ofNullable(
TripTimeOnDate.fromTripTimes(tripPattern.getScheduledTimetable(), trip)
);
}

public Optional<List<TripTimeOnDate>> getTripTimeOnDates(Trip trip, LocalDate serviceDate) {
TripPattern pattern = getPatternForTrip(trip, serviceDate);

Timetable timetable = getTimetableForTripPattern(pattern, serviceDate);

// If realtime moved pattern back to original trip, fetch it instead
if (timetable.getTripIndex(trip.getId()) == -1) {
LOG.warn(
"Trip {} not found in realtime pattern. This should not happen, and indicates a bug.",
trip
);
pattern = getPatternForTrip(trip);
timetable = getTimetableForTripPattern(pattern, serviceDate);
}

// This check is made here to avoid changing TripTimeOnDate.fromTripTimes
TripTimes times = timetable.getTripTimes(trip);
if (!this.getServiceCodesRunningForDate(serviceDate).contains(times.getServiceCode())) {
return Optional.empty();
} else {
Instant midnight = ServiceDateUtils
.asStartOfService(serviceDate, this.getTimeZone())
.toInstant();
return Optional.ofNullable(
TripTimeOnDate.fromTripTimes(timetable, trip, serviceDate, midnight)
);
}
}

@Override
public Collection<String> getFeedIds() {
return this.timetableRepository.getFeedIds();
Expand Down
Loading

0 comments on commit 991c09e

Please sign in to comment.