Skip to content

Commit 74fd848

Browse files
committed
Fix a bug in temporal filtering when importing timetables.
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
1 parent f2e7da1 commit 74fd848

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/main/kotlin/fi/hsl/jore4/hastus/graphql/GraphQLService.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ class GraphQLService(
230230

231231
fun getJourneyPatternReferences(
232232
routeLabels: Collection<String>,
233-
validityStart: LocalDate,
234-
validityEnd: LocalDate
233+
validityStart: LocalDate
235234
): List<JoreJourneyPatternRef> {
235+
// It is required that the route referenced by the journey pattern reference must be valid
236+
// before (or at) the start date of the Hastus booking record.
236237
val journeyPatternRefsQuery = JourneyPatternRefs(
237238
variables = JourneyPatternRefs.Variables(
238239
route_labels = routeLabels.toList(),
239-
validity_start = validityStart,
240-
validity_end = validityEnd
240+
validity_start = validityStart
241241
)
242242
)
243243

src/main/kotlin/fi/hsl/jore4/hastus/service/importing/ImportService.kt

+31-22
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
3838

3939
val journeyPatternRefs: List<JoreJourneyPatternRef> = graphQLService.getJourneyPatternReferences(
4040
uniqueRouteLabels,
41-
hastusBookingRecord.startDate,
42-
hastusBookingRecord.endDate
41+
hastusBookingRecord.startDate
4342
)
4443
LOGGER.debug { "Fetched journey pattern references: $journeyPatternRefs" }
4544

@@ -131,28 +130,38 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
131130
throw exception
132131
}
133132

134-
val bestJourneyPatternRefMatch: JoreJourneyPatternRef =
135-
journeyPatternRefsMatchedByStopPointLabels
136-
.sortedByDescending {
137-
// TODO Make sure that this is the appropriate ordering criteria when
138-
// finding JourneyPatternRef match.
139-
it.snapshotTime
140-
}
141-
.firstOrNull { journeyPatternRef ->
142-
val joreTimingPlaceLabels: List<String?> =
143-
journeyPatternRef.stops.map { it.timingPlaceCode }
133+
val journeyPatternRefsMatchedByTimingPlaceLabels: List<JoreJourneyPatternRef> =
134+
journeyPatternRefsMatchedByStopPointLabels.filter { journeyPatternRef ->
135+
val joreTimingPlaceLabels: List<String?> =
136+
journeyPatternRef.stops.map { it.timingPlaceCode }
144137

145-
joreTimingPlaceLabels == hastusPlaceLabels
146-
}
147-
?: run {
148-
val exception = CannotFindJourneyPatternRefByTimingPlaceLabelsException(
149-
hastusRouteLabelAndDirection,
150-
hastusStopPointLabels,
151-
hastusPlaceLabels
152-
)
153-
LOGGER.warn(exception.message)
154-
throw exception
138+
joreTimingPlaceLabels == hastusPlaceLabels
139+
}
140+
141+
if (journeyPatternRefsMatchedByTimingPlaceLabels.isEmpty()) {
142+
val exception = CannotFindJourneyPatternRefByTimingPlaceLabelsException(
143+
hastusRouteLabelAndDirection,
144+
hastusStopPointLabels,
145+
hastusPlaceLabels
146+
)
147+
LOGGER.warn(exception.message)
148+
throw exception
149+
}
150+
151+
val bestJourneyPatternRefMatch: JoreJourneyPatternRef = journeyPatternRefsMatchedByTimingPlaceLabels
152+
.sortedWith(
153+
compareByDescending<JoreJourneyPatternRef> {
154+
// By choosing the one with the latest validity start date, we are
155+
// effectively choosing the route/journey-pattern that is currently
156+
// active or was most recently active among the candidates.
157+
it.routeValidityStart
158+
}.thenByDescending {
159+
// The last exported item with the most up-to-date route information is
160+
// picked.
161+
it.snapshotTime
155162
}
163+
)
164+
.first()
156165

157166
results[hastusRouteLabelAndDirection] = bestJourneyPatternRefMatch
158167
}

src/main/resources/graphql/journeypatternrefs.graphql

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
query JourneyPatternRefs(
22
$route_labels: [String!]!,
3-
$validity_start: date!,
4-
$validity_end: date!
3+
$validity_start: date!
54
) {
65
timetables {
76
timetables_journey_pattern_journey_pattern_ref(
@@ -14,12 +13,6 @@ query JourneyPatternRefs(
1413
{ route_validity_start: { _lte: $validity_start } }
1514
]
1615
}
17-
{
18-
_or: [
19-
{ route_validity_end: { _is_null: true } }
20-
{ route_validity_end: { _gte: $validity_end } }
21-
]
22-
}
2316
]
2417
}
2518
order_by: {

0 commit comments

Comments
 (0)