Skip to content

Commit

Permalink
Fix a bug in temporal filtering when importing timetables.
Browse files Browse the repository at this point in the history
Until now, it has been required that the validity period of the exported route
must be valid for as long or longer than the Hastus booking record. This causes
problems when the exported route is about to expire and a new version already
exists, and when the Hastus booking record is valid further into the future
than the exported route.

Fix the problem by only requiring that the Hastus booking record must start
later (or on the same day) as the exported route.

Resolves HSLdevcom/jore4#1570
  • Loading branch information
jarkkoka committed Nov 1, 2023
1 parent f2e7da1 commit 74fd848
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/fi/hsl/jore4/hastus/graphql/GraphQLService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ class GraphQLService(

fun getJourneyPatternReferences(
routeLabels: Collection<String>,
validityStart: LocalDate,
validityEnd: LocalDate
validityStart: LocalDate
): List<JoreJourneyPatternRef> {
// It is required that the route referenced by the journey pattern reference must be valid
// before (or at) the start date of the Hastus booking record.
val journeyPatternRefsQuery = JourneyPatternRefs(
variables = JourneyPatternRefs.Variables(
route_labels = routeLabels.toList(),
validity_start = validityStart,
validity_end = validityEnd
validity_start = validityStart
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {

val journeyPatternRefs: List<JoreJourneyPatternRef> = graphQLService.getJourneyPatternReferences(
uniqueRouteLabels,
hastusBookingRecord.startDate,
hastusBookingRecord.endDate
hastusBookingRecord.startDate
)
LOGGER.debug { "Fetched journey pattern references: $journeyPatternRefs" }

Expand Down Expand Up @@ -131,28 +130,38 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
throw exception
}

val bestJourneyPatternRefMatch: JoreJourneyPatternRef =
journeyPatternRefsMatchedByStopPointLabels
.sortedByDescending {
// TODO Make sure that this is the appropriate ordering criteria when
// finding JourneyPatternRef match.
it.snapshotTime
}
.firstOrNull { journeyPatternRef ->
val joreTimingPlaceLabels: List<String?> =
journeyPatternRef.stops.map { it.timingPlaceCode }
val journeyPatternRefsMatchedByTimingPlaceLabels: List<JoreJourneyPatternRef> =
journeyPatternRefsMatchedByStopPointLabels.filter { journeyPatternRef ->
val joreTimingPlaceLabels: List<String?> =
journeyPatternRef.stops.map { it.timingPlaceCode }

joreTimingPlaceLabels == hastusPlaceLabels
}
?: run {
val exception = CannotFindJourneyPatternRefByTimingPlaceLabelsException(
hastusRouteLabelAndDirection,
hastusStopPointLabels,
hastusPlaceLabels
)
LOGGER.warn(exception.message)
throw exception
joreTimingPlaceLabels == hastusPlaceLabels
}

if (journeyPatternRefsMatchedByTimingPlaceLabels.isEmpty()) {
val exception = CannotFindJourneyPatternRefByTimingPlaceLabelsException(
hastusRouteLabelAndDirection,
hastusStopPointLabels,
hastusPlaceLabels
)
LOGGER.warn(exception.message)
throw exception
}

val bestJourneyPatternRefMatch: JoreJourneyPatternRef = journeyPatternRefsMatchedByTimingPlaceLabels
.sortedWith(
compareByDescending<JoreJourneyPatternRef> {
// By choosing the one with the latest validity start date, we are
// effectively choosing the route/journey-pattern that is currently
// active or was most recently active among the candidates.
it.routeValidityStart
}.thenByDescending {
// The last exported item with the most up-to-date route information is
// picked.
it.snapshotTime
}
)
.first()

results[hastusRouteLabelAndDirection] = bestJourneyPatternRefMatch
}
Expand Down
9 changes: 1 addition & 8 deletions src/main/resources/graphql/journeypatternrefs.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
query JourneyPatternRefs(
$route_labels: [String!]!,
$validity_start: date!,
$validity_end: date!
$validity_start: date!
) {
timetables {
timetables_journey_pattern_journey_pattern_ref(
Expand All @@ -14,12 +13,6 @@ query JourneyPatternRefs(
{ route_validity_start: { _lte: $validity_start } }
]
}
{
_or: [
{ route_validity_end: { _is_null: true } }
{ route_validity_end: { _gte: $validity_end } }
]
}
]
}
order_by: {
Expand Down

0 comments on commit 74fd848

Please sign in to comment.