Skip to content

Commit

Permalink
chore: use map_source_config/2 to calculate values in Signs.Realtime
Browse files Browse the repository at this point in the history
Defines `map_source_config/2`, a function for applying a function to
SourceConfig values (both single and top/bottom--tuple).

Uses the new `map_source_config/2` function to calculate `alert_status`,
`first_scheduled_departures`, and `service_end_statuses_per_source`.
  • Loading branch information
sloanelybutsurely committed Jul 24, 2024
1 parent 44692dc commit 5c1af3e
Showing 1 changed file with 16 additions and 35 deletions.
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

0 comments on commit 5c1af3e

Please sign in to comment.