Skip to content

Commit

Permalink
Inject updated TransitService in real-time updaters
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Aug 23, 2024
1 parent 8164d41 commit cea0135
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ private static TransitService makeTransitService(
transitModel.updateCalendarServiceData(true, calendarServiceData, DataImportIssueStore.NOOP);
transitModel.index();

var alertService = new TransitAlertServiceImpl(transitModel);
return new DefaultTransitService(transitModel) {
final TransitAlertService alertService = new TransitAlertServiceImpl(this);

@Override
public TransitAlertService getTransitAlertService() {
return alertService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public void setUp() throws Exception {
transitAlertService.getAllAlerts().clear();
}
if (alertsUpdateHandler == null) {
transitAlertService = new TransitAlertServiceImpl(transitModel);
transitAlertService = new TransitAlertServiceImpl(transitService);
alertsUpdateHandler =
new SiriAlertsUpdateHandler(
FEED_ID,
transitModel,
transitService,
transitAlertService,
SiriFuzzyTripMatcher.of(transitService),
Duration.ZERO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ void realtimeStopLayer() {
var transitModel = new TransitModel(new StopModel(), deduplicator);
transitModel.initTimeZone(ZoneIds.HELSINKI);
transitModel.index();
var alertService = new TransitAlertServiceImpl(transitModel);
var transitService = new DefaultTransitService(transitModel) {
final TransitAlertService alertService = new TransitAlertServiceImpl(this);

@Override
public TransitAlertService getTransitAlertService() {
return alertService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.opentripplanner.routing.alertpatch.TransitAlertBuilder;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,7 +65,7 @@ public class SiriAlertsUpdateHandler {
*/
public SiriAlertsUpdateHandler(
String feedId,
TransitModel transitModel,
TransitService transitService,
TransitAlertService transitAlertService,
SiriFuzzyTripMatcher siriFuzzyTripMatcher,
Duration earlyStart
Expand All @@ -76,7 +74,6 @@ public SiriAlertsUpdateHandler(
this.transitAlertService = transitAlertService;
this.earlyStart = earlyStart;

TransitService transitService = new DefaultTransitService(transitModel);
this.affectsMapper = new AffectsMapper(feedId, siriFuzzyTripMatcher, transitService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ public TimetableSnapshot getTimetableSnapshot() {
return snapshotManager.getTimetableSnapshot();
}

private TimetableSnapshot getTimetableSnapshotBuffer() {
/**
* @return the current timetable snapshot buffer that contains pending changes (not yet published
* in a snapshot).
* This should be used in the context of an updater to build a TransitEditorService that sees all
* the changes applied so far by real-time updates.
*/
public TimetableSnapshot getTimetableSnapshotBuffer() {
return snapshotManager.getTimetableSnapshotBuffer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.List;
import java.util.function.Consumer;
import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.spi.PollingGraphUpdater;
import org.opentripplanner.updater.spi.ResultLogger;
import org.opentripplanner.updater.spi.UpdateResult;
Expand Down Expand Up @@ -41,7 +40,7 @@ public class SiriETUpdater extends PollingGraphUpdater {

public SiriETUpdater(
SiriETUpdaterParameters config,
TransitModel transitModel,
TransitService transitService,
SiriTimetableSnapshotSource timetableSnapshotSource
) {
super(config);
Expand All @@ -62,7 +61,7 @@ public SiriETUpdater(
new EstimatedTimetableHandler(
timetableSnapshotSource,
config.fuzzyTripMatching(),
new DefaultTransitService(transitModel),
transitService,
feedId
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import org.opentripplanner.framework.retry.OtpRetryBuilder;
import org.opentripplanner.routing.impl.TransitAlertServiceImpl;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.alert.TransitAlertProvider;
import org.opentripplanner.updater.spi.PollingGraphUpdater;
import org.opentripplanner.updater.spi.WriteToGraphCallback;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class SiriSXUpdater extends PollingGraphUpdater implements TransitAlertPr
private final SiriHttpLoader siriHttpLoader;
private final OtpRetry retry;

public SiriSXUpdater(SiriSXUpdaterParameters config, TransitModel transitModel) {
public SiriSXUpdater(SiriSXUpdaterParameters config, TransitService transitService) {
super(config);
// TODO: add options to choose different patch services
this.url = config.url();
Expand All @@ -58,13 +57,13 @@ public SiriSXUpdater(SiriSXUpdaterParameters config, TransitModel transitModel)
//Keeping original requestorRef use as base for updated requestorRef to be used in retries
this.originalRequestorRef = requestorRef;
this.blockReadinessUntilInitialized = config.blockReadinessUntilInitialized();
this.transitAlertService = new TransitAlertServiceImpl(transitModel);
this.transitAlertService = new TransitAlertServiceImpl(transitService);
this.updateHandler =
new SiriAlertsUpdateHandler(
config.feedId(),
transitModel,
transitService,
transitAlertService,
SiriFuzzyTripMatcher.of(new DefaultTransitService(transitModel)),
SiriFuzzyTripMatcher.of(transitService),
config.earlyStart()
);
siriHttpLoader = new SiriHttpLoader(url, config.timeout(), config.requestHeaders());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.opentripplanner.ext.siri.SiriFuzzyTripMatcher;
import org.opentripplanner.framework.application.ApplicationShutdownSupport;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.spi.GraphUpdater;
import org.opentripplanner.updater.spi.HttpHeaders;
Expand Down Expand Up @@ -69,7 +67,10 @@ public abstract class AbstractAzureSiriUpdater implements GraphUpdater {
*/
protected final int timeout;

public AbstractAzureSiriUpdater(SiriAzureUpdaterParameters config, TransitModel transitModel) {
public AbstractAzureSiriUpdater(
SiriAzureUpdaterParameters config,
TransitService transitService
) {
this.configRef = config.configRef();
this.authenticationType = config.getAuthenticationType();
this.fullyQualifiedNamespace = config.getFullyQualifiedNamespace();
Expand All @@ -80,7 +81,6 @@ public AbstractAzureSiriUpdater(SiriAzureUpdaterParameters config, TransitModel
this.feedId = config.feedId();
this.autoDeleteOnIdle = config.getAutoDeleteOnIdle();
this.prefetchCount = config.getPrefetchCount();
TransitService transitService = new DefaultTransitService(transitModel);
this.entityResolver = new EntityResolver(transitService, feedId);
this.fuzzyTripMatcher =
config.isFuzzyTripMatching() ? SiriFuzzyTripMatcher.of(transitService) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import javax.xml.stream.XMLStreamException;
import org.apache.hc.core5.net.URIBuilder;
import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.spi.ResultLogger;
import org.opentripplanner.updater.spi.UpdateResult;
import org.opentripplanner.updater.trip.UpdateIncrementality;
Expand All @@ -40,10 +40,10 @@ public class SiriAzureETUpdater extends AbstractAzureSiriUpdater {

public SiriAzureETUpdater(
SiriAzureETUpdaterParameters config,
TransitModel transitModel,
TransitService transitService,
SiriTimetableSnapshotSource snapshotSource
) {
super(config, transitModel);
super(config, transitService);
this.fromDateTime = config.getFromDateTime();
this.snapshotSource = snapshotSource;
this.recordMetrics = TripUpdateMetrics.streaming(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.opentripplanner.ext.siri.SiriAlertsUpdateHandler;
import org.opentripplanner.routing.impl.TransitAlertServiceImpl;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.alert.TransitAlertProvider;
import org.rutebanken.siri20.util.SiriXml;
import org.slf4j.Logger;
Expand All @@ -35,15 +35,15 @@ public class SiriAzureSXUpdater extends AbstractAzureSiriUpdater implements Tran
private final LocalDate fromDateTime;
private final LocalDate toDateTime;

public SiriAzureSXUpdater(SiriAzureSXUpdaterParameters config, TransitModel transitModel) {
super(config, transitModel);
public SiriAzureSXUpdater(SiriAzureSXUpdaterParameters config, TransitService transitService) {
super(config, transitService);
this.fromDateTime = config.getFromDateTime();
this.toDateTime = config.getToDateTime();
this.transitAlertService = new TransitAlertServiceImpl(transitModel);
this.transitAlertService = new TransitAlertServiceImpl(transitService);
this.updateHandler =
new SiriAlertsUpdateHandler(
feedId,
transitModel,
transitService,
transitAlertService,
fuzzyTripMatcher(),
Duration.ZERO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import org.opentripplanner.ext.siri.updater.AsyncEstimatedTimetableProcessor;
import org.opentripplanner.ext.siri.updater.AsyncEstimatedTimetableSource;
import org.opentripplanner.ext.siri.updater.EstimatedTimetableHandler;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.spi.GraphUpdater;
import org.opentripplanner.updater.spi.UpdateResult;
import org.opentripplanner.updater.spi.WriteToGraphCallback;
Expand All @@ -27,7 +26,7 @@ public class SiriETGooglePubsubUpdater implements GraphUpdater {

public SiriETGooglePubsubUpdater(
SiriETGooglePubsubUpdaterParameters config,
TransitModel transitModel,
TransitService transitService,
SiriTimetableSnapshotSource timetableSnapshotSource
) {
configRef = config.configRef();
Expand All @@ -46,7 +45,7 @@ public SiriETGooglePubsubUpdater(
new EstimatedTimetableHandler(
timetableSnapshotSource,
config.fuzzyTripMatching(),
new DefaultTransitService(transitModel),
transitService,
config.feedId()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.Direction;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;

/**
* This is the primary implementation of TransitAlertService, which actually retains its own set
Expand All @@ -32,12 +32,12 @@
*/
public class TransitAlertServiceImpl implements TransitAlertService {

private final TransitModel transitModel;
private final TransitService transitService;

private Multimap<EntityKey, TransitAlert> alerts = HashMultimap.create();

public TransitAlertServiceImpl(TransitModel transitModel) {
this.transitModel = transitModel;
public TransitAlertServiceImpl(TransitService transitService) {
this.transitService = transitService;
}

@Override
Expand Down Expand Up @@ -85,8 +85,8 @@ public Collection<TransitAlert> getStopAlerts(
}
if (result.isEmpty()) {
// Search for alerts on parent-stop
if (transitModel != null) {
var quay = transitModel.getStopModel().getRegularStop(stopId);
if (transitService != null) {
var quay = transitService.getRegularStop(stopId);
if (quay != null) {
// TODO - SIRI: Add alerts from parent- and multimodal-stops
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import org.opentripplanner.framework.tostring.ToStringBuilder;
import org.opentripplanner.routing.impl.TransitAlertServiceImpl;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher;
import org.opentripplanner.updater.spi.HttpHeaders;
import org.opentripplanner.updater.spi.PollingGraphUpdater;
Expand All @@ -33,15 +32,15 @@ public class GtfsRealtimeAlertsUpdater extends PollingGraphUpdater implements Tr

public GtfsRealtimeAlertsUpdater(
GtfsRealtimeAlertsUpdaterParameters config,
TransitModel transitModel
TransitService transitService
) {
super(config);
this.url = config.url();
this.headers = HttpHeaders.of().acceptProtobuf().add(config.headers()).build();
TransitAlertService transitAlertService = new TransitAlertServiceImpl(transitModel);
TransitAlertService transitAlertService = new TransitAlertServiceImpl(transitService);

var fuzzyTripMatcher = config.fuzzyTripMatching()
? new GtfsRealtimeFuzzyTripMatcher(new DefaultTransitService(transitModel))
? new GtfsRealtimeFuzzyTripMatcher(transitService)
: null;

this.transitAlertService = transitAlertService;
Expand Down
Loading

0 comments on commit cea0135

Please sign in to comment.