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

remove unused headway message code #713

Merged
merged 1 commit into from
Oct 20, 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
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ config :realtime_signs,
sign_updater_mod: MessageQueue,
http_poster_mod: HTTPoison,
scheduled_headway_requester: Headway.Request,
headway_calculator: Headway.HeadwayDisplay,
external_config_getter: ExternalConfig.Local,
aws_client: ExAws,
s3_client: ExAws.S3,
Expand Down
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ config :realtime_signs,
api_v3_url: "https://api-dev-green.mbtace.com",
http_poster_mod: Fake.HTTPoison,
scheduled_headway_requester: Fake.Headway.Request,
headway_calculator: Fake.Headway.HeadwayDisplay,
external_config_getter: Fake.ExternalConfig.Local,
sign_config_file: "priv/config.json",
aws_client: Fake.ExAws,
Expand Down
77 changes: 14 additions & 63 deletions lib/content/audio/vehicles_to_destination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ defmodule Content.Audio.VehiclesToDestination do
alias PaEss.Utilities

@enforce_keys [:language, :destination, :headway_range]
defstruct @enforce_keys ++ [:previous_departure_mins, :routes]
defstruct @enforce_keys ++ [:routes]

@type t :: %__MODULE__{
language: Content.Audio.language(),
destination: PaEss.destination() | nil,
headway_range: Headway.HeadwayDisplay.headway_range(),
previous_departure_mins: integer() | nil,
headway_range: {non_neg_integer(), non_neg_integer()},
routes: [String.t()] | nil
}

Expand All @@ -35,10 +34,9 @@ defmodule Content.Audio.VehiclesToDestination do
@spec from_headway_message(Content.Message.t(), Content.Message.t()) :: [t()]
def from_headway_message(
%Content.Message.Headways.Top{destination: destination},
%Content.Message.Headways.Bottom{range: range} = msg
%Content.Message.Headways.Bottom{range: range}
) do
create(:english, destination, range, msg.prev_departure_mins) ++
create(:spanish, destination, range, msg.prev_departure_mins)
create(:english, destination, range) ++ create(:spanish, destination, range)
end

def from_headway_message(top, bottom) do
Expand All @@ -53,40 +51,36 @@ defmodule Content.Audio.VehiclesToDestination do
destination: destination,
range: range
}) do
create(:english, destination, range, nil)
create(:english, destination, range)
end

@spec create(
Content.Audio.language(),
PaEss.destination() | nil,
Headway.HeadwayDisplay.headway_range(),
integer() | nil
{non_neg_integer(), non_neg_integer()}
) :: [t()]

defp create(:english, nil, range, nil) do
defp create(:english, nil, range) do
[
%__MODULE__{
language: :english,
destination: nil,
headway_range: range,
previous_departure_mins: nil
headway_range: range
}
]
end

defp create(:spanish, nil, _range, nil) do
defp create(:spanish, nil, _range) do
[]
end

defp create(language, destination, headway_range, previous_departure_mins) do
if Utilities.valid_destination?(destination, language) and
not (language == :spanish and !is_nil(previous_departure_mins)) do
defp create(language, destination, headway_range) do
if Utilities.valid_destination?(destination, language) do
[
%__MODULE__{
language: language,
destination: destination,
headway_range: headway_range,
previous_departure_mins: previous_departure_mins
headway_range: headway_range
}
]
else
Expand Down Expand Up @@ -117,16 +111,14 @@ defmodule Content.Audio.VehiclesToDestination do
def to_params(%Content.Audio.VehiclesToDestination{
language: :english,
destination: nil,
headway_range: {range_low, range_high},
previous_departure_mins: nil
headway_range: {range_low, range_high}
}) do
{:ad_hoc, {"Trains every #{range_low} to #{range_high} minutes.", :audio}}
end

def to_params(
%Content.Audio.VehiclesToDestination{
headway_range: {lower_mins, higher_mins},
previous_departure_mins: nil
headway_range: {lower_mins, higher_mins}
} = audio
)
when is_integer(lower_mins) and is_integer(higher_mins) do
Expand All @@ -140,47 +132,6 @@ defmodule Content.Audio.VehiclesToDestination do
end
end

def to_params(
%Content.Audio.VehiclesToDestination{language: :english, headway_range: {x, y}} = audio
)
when (x == :up_to or is_integer(x)) and is_integer(y) do
case PaEss.Utilities.destination_to_ad_hoc_string(audio.destination) do
{:ok, destination_string} ->
vehicles_to_destination =
if audio.destination in [
:northbound,
:southbound,
:eastbound,
:westbound,
:inbound,
:outbound
] do
destination_string <> " trains"
else
"Trains to " <> destination_string
end

minutes_range =
case audio.headway_range do
{:up_to, up_to_mins} -> " up to every #{up_to_mins} minutes."
{lower_mins, higher_mins} -> " every #{lower_mins} to #{higher_mins} minutes."
end

previous_departure =
if !is_nil(audio.previous_departure_mins) and audio.previous_departure_mins > 0 do
minutes_word = if audio.previous_departure_mins == 1, do: "minute", else: "minutes"
" Previous departure #{audio.previous_departure_mins} #{minutes_word} ago."
else
""
end

{:ad_hoc, {vehicles_to_destination <> minutes_range <> previous_departure, :audio}}

{:error, :unknown} ->
nil
end
end

def to_params(_audio), do: nil

@spec message_id(Content.Audio.VehiclesToDestination.t()) :: String.t()
Expand Down
17 changes: 4 additions & 13 deletions lib/content/message/headways/bottom.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
defmodule Content.Message.Headways.Bottom do
require Logger
defstruct [:range, :prev_departure_mins]
defstruct [:range]

@type t :: %__MODULE__{
range: Headway.HeadwayDisplay.headway_range(),
prev_departure_mins: integer() | nil
}
@type t :: %__MODULE__{range: {non_neg_integer(), non_neg_integer()}}

defimpl Content.Message do
def to_string(%Content.Message.Headways.Bottom{
range: {:first_departure, range, _first_departure}
}) do
Headway.HeadwayDisplay.format_headway_range(range)
end

def to_string(%Content.Message.Headways.Bottom{} = bottom) do
Headway.HeadwayDisplay.format_bottom(bottom)
def to_string(%Content.Message.Headways.Bottom{range: {low, high}}) do
"Every #{low} to #{high} min"
end
end
end
2 changes: 1 addition & 1 deletion lib/content/message/headways/paging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Content.Message.Headways.Paging do

@type t :: %__MODULE__{
destination: PaEss.destination() | nil,
range: Headway.HeadwayDisplay.headway_range() | nil
range: {non_neg_integer(), non_neg_integer()}
}

defimpl Content.Message do
Expand Down
56 changes: 3 additions & 53 deletions lib/engine/scheduled_headways.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ defmodule Engine.ScheduledHeadways do
require Signs.Utilities.SignsConfig

@type state :: %{
headways_ets_table: term(),
first_last_departures_ets_table: term(),
schedule_data: %{String.t() => [Headway.HeadwayDisplay.schedule_map()]},
schedule_data: %{String.t() => [map]},
fetcher: module(),
fetch_ms: integer(),
fetch_chunk_size: integer(),
Expand All @@ -37,8 +36,6 @@ defmodule Engine.ScheduledHeadways do

@impl true
def init(opts) do
headways_ets_table = opts[:headways_ets_table] || :scheduled_headways

first_last_departures_ets_table =
opts[:first_last_departures_ets_table] || :scheduled_headways_first_last_departures

Expand All @@ -51,9 +48,6 @@ defmodule Engine.ScheduledHeadways do
opts[:time_fetcher] ||
fn -> Timex.shift(Timex.now(), milliseconds: div(headway_calc_ms, 2)) end

^headways_ets_table =
:ets.new(headways_ets_table, [:set, :protected, :named_table, read_concurrency: true])

^first_last_departures_ets_table =
:ets.new(first_last_departures_ets_table, [
:set,
Expand All @@ -63,7 +57,6 @@ defmodule Engine.ScheduledHeadways do
])

state = %{
headways_ets_table: headways_ets_table,
first_last_departures_ets_table: first_last_departures_ets_table,
schedule_data: %{},
fetcher: fetcher,
Expand All @@ -74,21 +67,11 @@ defmodule Engine.ScheduledHeadways do
time_fetcher: time_fetcher
}

:ets.insert(headways_ets_table, Enum.map(state.stop_ids, fn x -> {x, :none} end))

send(self(), :data_update)
send(self(), :calculation_update)

{:ok, state}
end

@spec get_headways(:ets.tab(), String.t()) :: Headway.HeadwayDisplay.headway_range()
def get_headways(table_name \\ :scheduled_headways, stop_id) do
[{_stop_id, headways}] = :ets.lookup(table_name, stop_id)

headways
end

@spec get_first_last_departures(:ets.tab(), [String.t()]) ::
[{DateTime.t() | nil, DateTime.t() | nil}]
def get_first_last_departures(table_name \\ :scheduled_headways_first_last_departures, stop_ids) do
Expand Down Expand Up @@ -155,12 +138,6 @@ defmodule Engine.ScheduledHeadways do
{:noreply, update_schedule_data(state)}
end

def handle_info(:calculation_update, state) do
schedule_calculation_update(self(), state.headway_calc_ms)

{:noreply, update_headway_data(state)}
end

def handle_info(msg, state) do
Logger.warn("#{__MODULE__} unknown message: #{inspect(msg)}")
{:noreply, state}
Expand All @@ -171,11 +148,6 @@ defmodule Engine.ScheduledHeadways do
Process.send_after(pid, :data_update, fetch_ms)
end

@spec schedule_calculation_update(pid(), integer()) :: reference()
defp schedule_calculation_update(pid, headway_calc_ms) do
Process.send_after(pid, :calculation_update, headway_calc_ms)
end

@spec update_schedule_data(state()) :: state()
defp update_schedule_data(state) do
schedule_updater = state[:fetcher]
Expand Down Expand Up @@ -206,29 +178,7 @@ defmodule Engine.ScheduledHeadways do
Map.put(state, :schedule_data, new_schedule_data)
end

@spec update_headway_data(state()) :: state()
defp update_headway_data(state) do
headway_calculator = Application.get_env(:realtime_signs, :headway_calculator)

headways =
headway_calculator.group_headways_for_stations(
state.schedule_data,
state.stop_ids,
state.time_fetcher.()
)

headways =
state.stop_ids
|> Enum.map(fn x -> {x, :none} end)
|> Map.new()
|> Map.merge(headways)

:ets.insert(state.headways_ets_table, headways |> Enum.into([]))

state
end

@spec build_first_last_departures_map(%{String.t() => [Headway.HeadwayDisplay.schedule_map()]}) ::
@spec build_first_last_departures_map(%{String.t() => [map]}) ::
%{String.t() => %{first_departure: DateTime.t(), last_departure: DateTime.t()}}
defp build_first_last_departures_map(schedule_data) do
stop_time_map = build_stop_time_map(schedule_data)
Expand Down Expand Up @@ -256,7 +206,7 @@ defmodule Engine.ScheduledHeadways do
end)
end

@spec build_stop_time_map(%{String.t() => [Headway.HeadwayDisplay.schedule_map()]}) :: %{
@spec build_stop_time_map(%{String.t() => [map]}) :: %{
String.t() => {DateTime.t() | nil, DateTime.t() | nil}
}
defp build_stop_time_map(schedule_data) do
Expand Down
8 changes: 0 additions & 8 deletions lib/fake/headway/headway_display.ex

This file was deleted.

Loading