Skip to content

Commit

Permalink
chore: use SourceConfig.map/2 to calculate values in Signs.Realtime
Browse files Browse the repository at this point in the history
Uses the new `SourceConfig.map/2` and `SourceConfig.simplify/1`
functions to calculate `alert_status`, `first_scheduled_departures`, and
`service_end_statuses_per_source`

Functionality remains the same but the code in `Signs.Realtime` is
simpler
  • Loading branch information
sloanelybutsurely committed Jul 24, 2024
1 parent 4b38308 commit 200310d
Showing 1 changed file with 11 additions and 39 deletions.
50 changes: 11 additions & 39 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,18 @@ 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
|> then(fn
{alert_status, alert_status} -> alert_status
alert_status -> alert_status
end)
sign.source_config
|> SourceConfig.map(&get_alert_status_for_source_config(&1, sign.alerts_engine))
|> SourceConfig.simplify()

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
|> SourceConfig.map(
&{
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 +181,7 @@ 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
SourceConfig.map(sign.source_config, &has_service_ended_for_source?(sign, &1, current_time))

{new_top, new_bottom} =
Utilities.Messages.get_messages(
Expand Down

0 comments on commit 200310d

Please sign in to comment.