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

Inject updated TransitService in real-time updaters #6018

Closed
Closed
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 @@ -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 @@ -3,12 +3,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.updater.spi.UpdateResultAssertions.assertFailure;

import java.util.Set;
import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.updater.spi.UpdateError;
import org.opentripplanner.updater.trip.RealtimeTestEnvironment;
import uk.org.siri.siri20.EstimatedTimetableDeliveryStructure;

class SiriTimetableSnapshotSourceTest {

Expand All @@ -32,16 +33,7 @@ void testCancelTrip() {
@Test
void testAddJourney() {
var env = RealtimeTestEnvironment.siri();

var updates = new SiriEtBuilder(env.getDateTimeHelper())
.withEstimatedVehicleJourneyCode("newJourney")
.withIsExtraJourney(true)
.withOperatorRef(env.operator1Id.getId())
.withLineRef(env.route1Id.getId())
.withRecordedCalls(builder -> builder.call(env.stopC1).departAimedActual("00:01", "00:02"))
.withEstimatedCalls(builder -> builder.call(env.stopD1).arriveAimedExpected("00:03", "00:04"))
.buildEstimatedTimetableDeliveries();

var updates = createValidAddedJourney(env);
var result = env.applyEstimatedTimetable(updates);

assertEquals(1, result.successful());
Expand All @@ -52,6 +44,20 @@ void testAddJourney() {
);
}

@Test
void testAddJourneyMultipleTimes() {
var env = RealtimeTestEnvironment.siri();
var updates = createValidAddedJourney(env);

int numTrips = env.getTransitService().getAllTrips().size();
var result1 = env.applyEstimatedTimetable(updates);
assertEquals(1, result1.successful());
assertEquals(numTrips + 1, env.getTransitService().getAllTrips().size());
var result2 = env.applyEstimatedTimetable(updates);
assertEquals(1, result2.successful());
assertEquals(numTrips + 1, env.getTransitService().getAllTrips().size());
}

@Test
void testAddedJourneyWithInvalidScheduledData() {
var env = RealtimeTestEnvironment.siri();
Expand Down Expand Up @@ -415,6 +421,19 @@ void testExtraUnknownStop() {
assertFailure(UpdateError.UpdateErrorType.INVALID_STOP_SEQUENCE, result);
}

private static List<EstimatedTimetableDeliveryStructure> createValidAddedJourney(
RealtimeTestEnvironment env
) {
return new SiriEtBuilder(env.getDateTimeHelper())
.withEstimatedVehicleJourneyCode("newJourney")
.withIsExtraJourney(true)
.withOperatorRef(env.operator1Id.getId())
.withLineRef(env.route1Id.getId())
.withRecordedCalls(builder -> builder.call(env.stopC1).departAimedActual("00:01", "00:02"))
.withEstimatedCalls(builder -> builder.call(env.stopD1).arriveAimedExpected("00:03", "00:04"))
.buildEstimatedTimetableDeliveries();
}

private static SiriEtBuilder updatedJourneyBuilder(RealtimeTestEnvironment env) {
return new SiriEtBuilder(env.getDateTimeHelper())
.withEstimatedCalls(builder ->
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
Loading