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

chore: refactor function application over source_config values #798

Merged
merged 1 commit into from
Jul 25, 2024
Merged
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
51 changes: 16 additions & 35 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,21 @@ defmodule Signs.Realtime do
current_time = sign.current_time_fn.()

alert_status =
case sign.source_config do
{top, bottom} ->
top_alert_status = get_alert_status_for_source_config(top, sign.alerts_engine)
bottom_alert_status = get_alert_status_for_source_config(bottom, sign.alerts_engine)

{top_alert_status, bottom_alert_status}

source_config ->
alert_status = get_alert_status_for_source_config(source_config, sign.alerts_engine)

alert_status
end
sign.source_config
|> map_source_config(&get_alert_status_for_source_config(&1, sign.alerts_engine))
|> then(fn
{alert_status, alert_status} -> alert_status
alert_status -> alert_status
end)

first_scheduled_departures =
case sign.source_config do
{top, bottom} ->
{
{sign.headway_engine.get_first_scheduled_departure(SourceConfig.sign_stop_ids(top)),
top.headway_destination},
{sign.headway_engine.get_first_scheduled_departure(
SourceConfig.sign_stop_ids(bottom)
), bottom.headway_destination}
}

source ->
{sign.headway_engine.get_first_scheduled_departure(
SourceConfig.sign_stop_ids(sign.source_config)
), source.headway_destination}
end
sign.source_config
|> map_source_config(
&{
sign.headway_engine.get_first_scheduled_departure(SourceConfig.sign_stop_ids(&1)),
&1.headway_destination
}
)

prev_predictions_lookup =
for prediction <- sign.prev_predictions, into: %{} do
Expand All @@ -202,14 +184,10 @@ defmodule Signs.Realtime do
end

service_end_statuses_per_source =
case sign.source_config do
{top_source, bottom_source} ->
{has_service_ended_for_source?(sign, top_source, current_time),
has_service_ended_for_source?(sign, bottom_source, current_time)}

source ->
has_service_ended_for_source?(sign, source, current_time)
end
map_source_config(
sign.source_config,
&has_service_ended_for_source?(sign, &1, current_time)
)

{new_top, new_bottom} =
Utilities.Messages.get_messages(
Expand Down Expand Up @@ -314,4 +292,7 @@ defmodule Signs.Realtime do

alert_status
end

defp map_source_config({top, bottom}, fun), do: {fun.(top), fun.(bottom)}
defp map_source_config(config, fun), do: fun.(config)
end
Loading