Skip to content

Commit

Permalink
enable last trip messages for red line
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos committed Jan 6, 2025
1 parent 78d8391 commit eb6a5b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
23 changes: 10 additions & 13 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,18 @@ defmodule Signs.Realtime do
end

defp has_service_ended_for_source?(sign, source, current_time) do
if source.headway_group not in ["red_trunk", "red_ashmont", "red_braintree"] do
num_last_trips =
SourceConfig.sign_stop_ids(source)
|> Enum.count(&has_last_trip_departed_stop?(&1, sign, current_time)) >= 1
else
false
end
end
|> Stream.flat_map(&sign.last_trip_engine.get_recent_departures(&1))
|> Enum.count(fn {trip_id, departure_time} ->
# Use a 3 second buffer to make sure trips have fully departed
DateTime.to_unix(current_time) - DateTime.to_unix(departure_time) > 3 and
sign.last_trip_engine.is_last_trip?(trip_id)
end)

defp has_last_trip_departed_stop?(stop_id, sign, current_time) do
sign.last_trip_engine.get_recent_departures(stop_id)
|> Enum.any?(fn {trip_id, departure_time} ->
# Use a 3 second buffer to make sure trips have fully departed
DateTime.to_unix(current_time) - DateTime.to_unix(departure_time) > 3 and
sign.last_trip_engine.is_last_trip?(trip_id)
end)
# Red line trunk should wait for two last trips, one for each branch
threshold = if(source.headway_group == "red_trunk", do: 2, else: 1)
num_last_trips >= threshold
end

defp prediction_key(prediction) do
Expand Down
22 changes: 22 additions & 0 deletions test/signs/realtime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,28 @@ defmodule Signs.RealtimeTest do

Signs.Realtime.handle_info(:run_loop, sign)
end

test "Red line trunk service doesn't end after one last trip" do
expect_messages({"", ""})

Signs.Realtime.handle_info(:run_loop, %{
@sign
| source_config: %{@sign.source_config | headway_group: "red_trunk"}
})
end

test "Red line trunk service ends after two last trips" do
expect(Engine.LastTrip.Mock, :get_recent_departures, fn _ ->
%{"a" => ~U[2023-01-01 00:00:00.000Z], "b" => ~U[2023-01-01 00:00:00.000Z]}
end)

expect_messages({"Service ended", "No Southbound trains"})

Signs.Realtime.handle_info(:run_loop, %{
@sign
| source_config: %{@sign.source_config | headway_group: "red_trunk"}
})
end
end

describe "PA messages" do
Expand Down

0 comments on commit eb6a5b6

Please sign in to comment.