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

Limit headways at end of service #712

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions lib/engine/scheduled_headways.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ defmodule Engine.ScheduledHeadways do
table \\ :scheduled_headways_first_last_departures,
stop_ids,
current_time,
buffer_mins
%Engine.Config.Headway{range_high: range_high, range_low: range_low}
) do
first_last_departures = get_first_last_departures(table, stop_ids)

Expand All @@ -127,7 +127,8 @@ defmodule Engine.ScheduledHeadways do

case {earliest_first, earliest_last} do
{%DateTime{} = first, %DateTime{} = last} ->
first = DateTime.add(first, -1 * buffer_mins * 60)
first = DateTime.add(first, -1 * (range_high + 1) * 60)
last = DateTime.add(last, -1 * (range_low - 1) * 60)

DateTime.compare(current_time, first) == :gt and
DateTime.compare(current_time, last) == :lt
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/scheduled_headways_api.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Engine.ScheduledHeadwaysAPI do
@callback display_headways?([String.t()], DateTime.t(), non_neg_integer()) :: boolean()
@callback display_headways?([String.t()], DateTime.t(), Engine.Config.Headway.t()) :: boolean()
@callback get_first_scheduled_departure([binary]) :: nil | DateTime.t()
end
2 changes: 1 addition & 1 deletion lib/signs/utilities/headways.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Signs.Utilities.Headways do
with headways when not is_nil(headways) <-
sign.config_engine.headway_config(headway_group, current_time),
true <-
sign.headway_engine.display_headways?(stop_ids, current_time, headways.range_high) do
sign.headway_engine.display_headways?(stop_ids, current_time, headways) do
headways
else
_ -> nil
Expand Down
28 changes: 16 additions & 12 deletions test/engine/scheduled_headways_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -188,36 +188,36 @@ defmodule Engine.ScheduledHeadwaysTest do
test "returns true/false depending on time" do
table = :display_headways_test
stop = "stop"
buffer_mins = 12
headways = %Engine.Config.Headway{headway_id: {"test", :peak}, range_high: 12, range_low: 5}

first_departure = DateTime.from_naive!(~N[2020-03-24 10:00:00], "America/New_York")
last_departure = DateTime.from_naive!(~N[2020-03-25 01:00:00], "America/New_York")

:ets.new(table, @ets_table_params)
:ets.insert(table, {stop, {first_departure, last_departure}})

before_service = DateTime.add(first_departure, -1 * (buffer_mins + 5) * 60)
during_buffer = DateTime.add(first_departure, -1 * (buffer_mins - 5) * 60)
before_service = DateTime.add(first_departure, -1 * (headways.range_high + 5) * 60)
during_buffer = DateTime.add(first_departure, -1 * (headways.range_high - 5) * 60)
during_service = DateTime.add(first_departure, 3 * 60 * 60)
after_service = DateTime.add(last_departure, 5 * 60)

refute Engine.ScheduledHeadways.display_headways?(
table,
[stop],
before_service,
buffer_mins
headways
)

assert Engine.ScheduledHeadways.display_headways?(table, [stop], during_buffer, buffer_mins)
assert Engine.ScheduledHeadways.display_headways?(table, [stop], during_buffer, headways)

assert Engine.ScheduledHeadways.display_headways?(
table,
[stop],
during_service,
buffer_mins
headways
)

refute Engine.ScheduledHeadways.display_headways?(table, [stop], after_service, buffer_mins)
refute Engine.ScheduledHeadways.display_headways?(table, [stop], after_service, headways)
end

test "uses the earliest first and earliest last departure" do
Expand All @@ -230,6 +230,7 @@ defmodule Engine.ScheduledHeadwaysTest do
early_last_dep = DateTime.from_naive!(~N[2020-03-21 00:00:00], "America/New_York")
later_last_dep = DateTime.from_naive!(~N[2020-03-21 02:00:00], "America/New_York")
no_service_late = DateTime.from_naive!(~N[2020-03-21 01:00:00], "America/New_York")
headways = %Engine.Config.Headway{headway_id: {"test", :peak}, range_high: 0, range_low: 0}

:ets.new(table, @ets_table_params)
:ets.insert(table, {"stop1", {early_first_dep, later_last_dep}})
Expand All @@ -239,37 +240,40 @@ defmodule Engine.ScheduledHeadwaysTest do
table,
["stop1", "stop2"],
in_service_early,
0
headways
)

refute Engine.ScheduledHeadways.display_headways?(
table,
["stop1", "stop2"],
no_service_late,
0
headways
)
end

test "handles when the only departure times are nil" do
table = :dh_nil_test
datetime = DateTime.from_naive!(~N[2020-03-20 12:00:00], "America/New_York")
headways = %Engine.Config.Headway{headway_id: {"test", :peak}, range_high: 0, range_low: 0}

:ets.new(table, @ets_table_params)
:ets.insert(table, {"stop_id", {nil, nil}})
:ets.insert(table, {"stop_id", {nil, nil}})

refute Engine.ScheduledHeadways.display_headways?(table, ["stop_id"], datetime, 0)
refute Engine.ScheduledHeadways.display_headways?(table, ["stop_id"], datetime, headways)
end

test "returns false if missing first/last trip timing" do
:ets.new(:no_data, @ets_table_params)
time = DateTime.from_naive!(~N[2020-03-20 10:00:00], "America/New_York")
refute Engine.ScheduledHeadways.display_headways?(:no_data, ["no_stop"], time, 0)
headways = %Engine.Config.Headway{headway_id: {"test", :peak}, range_high: 0, range_low: 0}
refute Engine.ScheduledHeadways.display_headways?(:no_data, ["no_stop"], time, headways)
end

test "display_headways?/3 fills in ETS table name" do
time = DateTime.from_naive!(~N[2020-03-20 10:00:00], "America/New_York")
refute Engine.ScheduledHeadways.display_headways?(["no_data"], time, 0)
headways = %Engine.Config.Headway{headway_id: {"test", :peak}, range_high: 0, range_low: 0}
refute Engine.ScheduledHeadways.display_headways?(["no_data"], time, headways)
end
end

Expand Down
Loading