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

fix(DownstreamAlerts): Exclude the target stop's alerts from downstream calc #571

Merged
merged 5 commits into from
Dec 11, 2024
Merged
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 @@ -239,8 +239,9 @@ data class Alert(
}

/**
* Gets the alerts of the first stop that is downstream of the target stop which has alerts.
* Considers only alerts that have specified stops.
* Gets the alerts of the first stop that is downstream of the target stop which has any
* alerts that are different from the alerts at the target stop. Considers only alerts that
* have specified stops.
*
* @param alerts: The full list of alerts
* @param trip: The trip used to calculate downstream stops
Expand All @@ -255,19 +256,32 @@ data class Alert(

val alerts = alerts.filter { it.hasStopsSpecified }

val targetStopAlertIds =
applicableAlerts(
alerts.toList(),
trip.directionId,
listOf(trip.routeId),
targetStopWithChildren
)
.map { it.id }
.toSet()

val indexOfTargetStopInPattern =
stopIds.indexOfFirst { targetStopWithChildren.contains(it) }
if (indexOfTargetStopInPattern != -1 && indexOfTargetStopInPattern < stopIds.size - 1) {
val downstreamStops = stopIds.subList(indexOfTargetStopInPattern + 1, stopIds.size)
val firstStopAlerts =
downstreamStops
.map { stop ->
applicableAlerts(
alerts.toList() ?: listOf(),
trip.directionId,
listOf(trip.routeId),
setOf(stop)
)
val alerts =
applicableAlerts(
alerts.toList(),
trip.directionId,
listOf(trip.routeId),
setOf(stop)
)
.filter { !targetStopAlertIds.contains(it.id) }
alerts
}
.firstOrNull { alerts -> !alerts.isNullOrEmpty() }
?: listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,70 @@ class AlertTest {
}


@Test
fun `downstreamAlerts excludes alerts affecting the target stop`() {
val objects = ObjectCollectionBuilder()
val route = objects.route()
val targetStop = objects.stop()
val downstreamStop1 = objects.stop()
val downstreamStop2 = objects.stop()

val alertAllStops =
objects.alert {
informedEntity(
listOf(Alert.InformedEntity.Activity.Board),
route = route.id,
stop = targetStop.id,
directionId = 0
)
informedEntity(
listOf(Alert.InformedEntity.Activity.Board),
route = route.id,
stop = downstreamStop1.id,
directionId = 0
)
informedEntity(
listOf(Alert.InformedEntity.Activity.Board),
route = route.id,
stop = downstreamStop2.id,
directionId = 0
)
}

val alertDownstream2Only =
objects.alert {
informedEntity(
listOf(Alert.InformedEntity.Activity.Board),
route = route.id,
stop = downstreamStop2.id,
directionId = 0
)
}

val trip =
objects.trip {
routeId = route.id
directionId = 0
stopIds =
listOf(
targetStop.id,
downstreamStop1.id,
downstreamStop2.id,
)
}

val downstreamAlerts =
Alert.downstreamAlerts(
listOf(alertAllStops, alertDownstream2Only),
trip,
setOf(targetStop.id)
)

assertEquals(listOf(alertDownstream2Only), downstreamAlerts)
}



@Test
fun `downstreamAlerts ignores alert without stops specified`() {
val objects = ObjectCollectionBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,7 @@ class NearbyResponseTest {
)
),
alertsHere = listOf(alert),
alertsDownstream = listOf(alert),
alertsDownstream = emptyList(),
hasSchedulesToday = false
),
)
Expand Down
Loading