Skip to content

Commit

Permalink
Merge pull request #4652 from leonardehrenfried/fallback-zone-id
Browse files Browse the repository at this point in the history
Use fallback timezone if no transit data is loaded
  • Loading branch information
leonardehrenfried authored Jan 17, 2024
2 parents 91609ec + 83dd228 commit ede3472
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.framework.lang.StringUtils;
import org.opentripplanner.framework.time.DurationUtils;
import org.opentripplanner.framework.time.ZoneIdFallback;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.preference.ItineraryFilterDebugProfile;
import org.opentripplanner.routing.api.request.request.filter.SelectRequest;
Expand Down Expand Up @@ -711,7 +712,7 @@ protected RouteRequest buildRequest(MultivaluedMap<String, String> queryParamete

{
//FIXME: move into setter method on routing request
ZoneId tz = serverContext.transitService().getTimeZone();
ZoneId tz = ZoneIdFallback.zoneId(serverContext.transitService().getTimeZone());
if (date == null && time != null) { // Time was provided but not date
LOG.debug("parsing ISO datetime {}", time);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opentripplanner.api.parameter.QualifiedModeSet;
import org.opentripplanner.apis.gtfs.GraphQLRequestContext;
import org.opentripplanner.framework.graphql.GraphQLUtils;
import org.opentripplanner.framework.time.ZoneIdFallback;
import org.opentripplanner.model.GenericLocation;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.framework.CostLinearFunction;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static RouteRequest toRouteRequest(
request.setDateTime(
environment.getArgument("date"),
environment.getArgument("time"),
context.transitService().getTimeZone()
ZoneIdFallback.zoneId(context.transitService().getTimeZone())
);

callWith.argument("wheelchair", request::setWheelchair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static Throttle ofOneSecond() {
return new Throttle(1000);
}

public static Throttle ofOneMinute() {
return new Throttle(1000 * 60);
}

public String setupInfo() {
return setupInfo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opentripplanner.framework.time;

import java.time.ZoneId;
import javax.annotation.Nullable;
import org.opentripplanner.framework.logging.Throttle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class provides a fallback mechanism for retrieving a zone id (=time zone).
* If a ZoneId is not provided, it returns a default fallback ZoneId (UTC).
* <p>
* This situation happens when you don't load any transit data into the graph but want to route
* anyway, perhaps only on the street network.
*/
public class ZoneIdFallback {

private static final Logger LOG = LoggerFactory.getLogger(ZoneIdFallback.class);
private static final ZoneId FALLBACK = ZoneId.of("UTC");
private static final Throttle THROTTLE = Throttle.ofOneMinute();

/**
* Accepts a nullable zone id (time zone) and returns UTC as the fallback.
*/
public static ZoneId zoneId(@Nullable ZoneId id) {
if (id == null) {
THROTTLE.throttle(() -> {
LOG.warn(
"Your instance doesn't contain a time zone (which is usually derived from transit data). Assuming {}.",
FALLBACK
);
LOG.warn("Please double-check that transit data was correctly loaded.");
});
return FALLBACK;
} else {
return id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.framework.geometry.GeometryUtils;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.framework.time.ZoneIdFallback;
import org.opentripplanner.model.plan.ElevationProfile;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.model.plan.Leg;
Expand Down Expand Up @@ -58,7 +59,7 @@ public GraphPathToItineraryMapper(
StreetNotesService streetNotesService,
double ellipsoidToGeoidDifference
) {
this.timeZone = timeZone;
this.timeZone = ZoneIdFallback.zoneId(timeZone);
this.streetNotesService = streetNotesService;
this.ellipsoidToGeoidDifference = ellipsoidToGeoidDifference;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.ZoneId;
import org.opentripplanner.framework.application.OTPRequestTimeoutException;
import org.opentripplanner.framework.time.ZoneIdFallback;
import org.opentripplanner.framework.tostring.MultiLineToStringBuilder;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.algorithm.RoutingWorker;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class DefaultRoutingService implements RoutingService {

public DefaultRoutingService(OtpServerRequestContext serverContext) {
this.serverContext = serverContext;
this.timeZone = serverContext.transitService().getTimeZone();
this.timeZone = ZoneIdFallback.zoneId(serverContext.transitService().getTimeZone());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void enforceTextPackageDependencies() {

@Test
void enforceTimePackageDependencies() {
TIME.verify();
TIME.dependsOn(LOGGING).verify();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.opentripplanner.framework.time;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.opentripplanner._support.time.ZoneIds;

class ZoneIdFallbackTest {

@Test
void fallback() {
assertEquals(ZoneIds.UTC, ZoneIdFallback.zoneId(null));
}

@Test
void keepOriginal() {
assertEquals(ZoneIds.BERLIN, ZoneIdFallback.zoneId(ZoneIds.BERLIN));
}
}

0 comments on commit ede3472

Please sign in to comment.