Skip to content

Commit

Permalink
refactor arrival and departure stop time
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Nov 8, 2024
1 parent c8af825 commit b6646f7
Showing 1 changed file with 44 additions and 49 deletions.
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);
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);
if (tripPattern == null) {
return null;
}

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

Expand Down Expand Up @@ -408,6 +384,25 @@ private RealtimeVehicleService getRealtimeVehiclesService(DataFetchingEnvironmen
return environment.<GraphQLRequestContext>getContext().realTimeVehicleService();
}

private TripTimeOnDate getStoptimeAtIndex(
DataFetchingEnvironment environment,
@Nullable LocalDate serviceDate,
int stopIndex
) {
var tripPattern = getTripPattern(environment);
return new TripTimeOnDate(
tripPattern.getScheduledTimetable().getTripTimes(getSource(environment)),
stopIndex,
tripPattern,
serviceDate,
serviceDate == null
? null
: ServiceDateUtils
.asStartOfService(serviceDate, getTransitService(environment).getTimeZone())
.toInstant()
);
}

private Trip getSource(DataFetchingEnvironment environment) {
return environment.getSource();
}
Expand Down

0 comments on commit b6646f7

Please sign in to comment.