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

feat: Filter out redundant outside elevator closures #2328

Merged
merged 9 commits into from
Dec 9, 2024
4 changes: 3 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ config :screens,
headway_headsign: "Test"
}
]
}
},
elevator_redundancy_data_path:
Path.join(~w[#{File.cwd!()} test fixtures elevator_redundancy_data.json])

config :screens, ScreensWeb.AuthManager, secret_key: "test key"

Expand Down
5 changes: 5 additions & 0 deletions lib/screens/alerts/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,9 @@ defmodule Screens.Alerts.Alert do
end

def partial_station_closure?(_, _), do: false

@spec informs_stop_id?(Alert.t(), Stop.id()) :: boolean()
def informs_stop_id?(%__MODULE__{informed_entities: informed_entities}, stop_id) do
Enum.any?(informed_entities, &(&1.stop == stop_id))
end
end
34 changes: 31 additions & 3 deletions lib/screens/v2/candidate_generator/elevator/closures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
@route injected(Route)
@stop injected(Stop)

@elevator_redundancy_data :screens
|> Application.compile_env(
:elevator_redundancy_data_path,
:screens
|> :code.priv_dir()
|> Path.join("elevators/elevator_redundancy_data.json")
)
|> File.read!()
|> Jason.decode!()

@spec elevator_status_instances(Screen.t(), NormalHeader.t(), Footer.t()) ::
list(WidgetInstance.t())
def elevator_status_instances(
Expand All @@ -37,7 +47,7 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
with {:ok, %Stop{id: stop_id}} <- @facility.fetch_stop_for_facility(elevator_id),
{:ok, parent_station_map} <- @stop.fetch_parent_station_name_map(),
{:ok, alerts} <- @alert.fetch_elevator_alerts_with_facilities() do
elevator_alerts = Enum.filter(alerts, &relevant_alert?/1)
elevator_alerts = Enum.filter(alerts, &relevant_alert?(&1, stop_id))
routes_map = get_routes_map(elevator_alerts, stop_id)

{in_station_alerts, outside_alerts} =
Expand Down Expand Up @@ -75,8 +85,11 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
end
end

defp relevant_alert?(alert) do
relevant_effect?(alert) and informs_one_facility?(alert)
# All alerts at home station are relevant but only elevators without redundancies are
# relevant at other stations.
defp relevant_alert?(alert, home_stop_id) do
relevant_effect?(alert) and informs_one_facility?(alert) and
(Alert.informs_stop_id?(alert, home_stop_id) or not has_nearby_redundancy?(alert))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an alert informs a facility, does it also always inform the facility's parent station? This isn't necessarily true of GTFS in general but I'd believe that it's a guarantee of our alerts implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, yes. I think it always includes the parent + every child stop not flagged for exclusion in the properties field for the facility.

end

defp relevant_effect?(alert), do: alert.effect == :elevator_closure
Expand Down Expand Up @@ -169,4 +182,19 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
ie -> if InformedEntity.parent_station?(ie), do: ie.stop
end)
end

defp has_nearby_redundancy?(%Alert{
informed_entities: [%{facility: %{id: informed_facility_id}} | _]
digitalcora marked this conversation as resolved.
Show resolved Hide resolved
}) do
if data = @elevator_redundancy_data[informed_facility_id] do
data["nearby_redundancy?"]
else
_ =
Sentry.capture_message(
"Elevator #{informed_facility_id} does not exist in redundancy data"
)

false
end
end
end
Loading
Loading