Skip to content

Commit

Permalink
reference prediction directly and remove individual fields (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Oct 28, 2024
1 parent b26f990 commit 2b60188
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 146 deletions.
6 changes: 3 additions & 3 deletions lib/content/audio/approaching.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ defmodule Content.Audio.Approaching do
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
platform: Content.Utilities.stop_platform(message.stop_id),
route_id: message.route_id,
trip_id: message.prediction.trip_id,
platform: Content.Utilities.stop_platform(message.prediction.stop_id),
route_id: message.prediction.route_id,
new_cars?: message.new_cars?,
crowding_description: if(include_crowding?, do: message.crowding_description)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/content/audio/following_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ defmodule Content.Audio.FollowingTrain do
def from_predictions_message(%Content.Message.Predictions{
minutes: n,
destination: destination,
route_id: route_id,
prediction: prediction,
station_code: station_code,
terminal?: terminal
})
when is_integer(n) do
[
%__MODULE__{
destination: destination,
route_id: route_id,
route_id: prediction.route_id,
minutes: n,
verb: arrives_or_departs(terminal),
station_code: station_code
Expand Down
6 changes: 3 additions & 3 deletions lib/content/audio/next_train_countdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ defmodule Content.Audio.NextTrainCountdown do
[
%__MODULE__{
destination: message.destination,
route_id: message.route_id,
route_id: message.prediction.route_id,
minutes: if(message.minutes == :approaching, do: 1, else: message.minutes),
verb: if(message.terminal?, do: :departs, else: :arrives),
track_number: Content.Utilities.stop_track_number(message.stop_id),
platform: Content.Utilities.stop_platform(message.stop_id),
track_number: Content.Utilities.stop_track_number(message.prediction.stop_id),
platform: Content.Utilities.stop_platform(message.prediction.stop_id),
station_code: message.station_code,
zone: message.zone
}
Expand Down
4 changes: 2 additions & 2 deletions lib/content/audio/stopped_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ defmodule Content.Audio.StoppedTrain do
def from_message(%Content.Message.StoppedTrain{
destination: destination,
stops_away: stops_away,
route_id: route_id
prediction: prediction
})
when stops_away > 0 do
[%__MODULE__{destination: destination, route_id: route_id, stops_away: stops_away}]
[%__MODULE__{destination: destination, route_id: prediction.route_id, stops_away: stops_away}]
end

def from_message(%Content.Message.StoppedTrain{stops_away: 0}) do
Expand Down
6 changes: 3 additions & 3 deletions lib/content/audio/train_is_arriving.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ defmodule Content.Audio.TrainIsArriving do
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
platform: Content.Utilities.stop_platform(message.stop_id),
route_id: message.route_id,
trip_id: message.prediction.trip_id,
platform: Content.Utilities.stop_platform(message.prediction.stop_id),
route_id: message.prediction.route_id,
crowding_description: if(include_crowding?, do: message.crowding_description)
}
]
Expand Down
10 changes: 5 additions & 5 deletions lib/content/audio/train_is_boarding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ defmodule Content.Audio.TrainIsBoarding do
[
%Audio.TrackChange{
destination: message.destination,
route_id: message.route_id,
berth: message.stop_id
route_id: message.prediction.route_id,
berth: message.prediction.stop_id
}
]
else
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
route_id: message.route_id,
track_number: Content.Utilities.stop_track_number(message.stop_id)
trip_id: message.prediction.trip_id,
route_id: message.prediction.route_id,
track_number: Content.Utilities.stop_track_number(message.prediction.stop_id)
}
] ++
if message.station_code == "BBOW" && message.zone == "e" do
Expand Down
68 changes: 17 additions & 51 deletions lib/content/message/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ defmodule Content.Message.Predictions do
:destination,
:minutes,
:approximate?,
:route_id,
:prediction,
:station_code,
:stop_id,
:trip_id,
:direction_id,
:zone,
width: 18,
new_cars?: false,
terminal?: false,
certainty: nil,
crowding_data_confidence: nil,
crowding_description: nil
]
Expand All @@ -40,30 +35,18 @@ defmodule Content.Message.Predictions do
destination: PaEss.destination(),
minutes: integer() | :boarding | :arriving | :approaching,
approximate?: boolean(),
route_id: String.t(),
stop_id: String.t(),
trip_id: Predictions.Prediction.trip_id() | nil,
direction_id: 0 | 1,
width: integer(),
prediction: Predictions.Prediction.t(),
new_cars?: boolean(),
station_code: String.t() | nil,
zone: String.t() | nil,
terminal?: boolean(),
certainty: non_neg_integer() | nil,
crowding_data_confidence: :high | :low | nil,
crowding_description: {atom(), atom()} | nil
}

@spec non_terminal(
Predictions.Prediction.t(),
String.t(),
String.t(),
Signs.Realtime.t(),
integer()
) :: t() | nil
def non_terminal(prediction, station_code, zone, sign, width \\ 18)

def non_terminal(prediction, station_code, zone, sign, width) do
@spec non_terminal(Predictions.Prediction.t(), String.t(), String.t(), Signs.Realtime.t()) ::
t() | nil
def non_terminal(prediction, station_code, zone, sign) do
# e.g., North Station which is non-terminal but has trips that begin there
predicted_time = prediction.seconds_until_arrival || prediction.seconds_until_departure

Expand All @@ -89,30 +72,18 @@ defmodule Content.Message.Predictions do
destination: Content.Utilities.destination_for_prediction(prediction),
minutes: minutes,
approximate?: approximate?,
route_id: prediction.route_id,
stop_id: prediction.stop_id,
trip_id: prediction.trip_id,
direction_id: prediction.direction_id,
width: width,
prediction: prediction,
new_cars?: sign.location_engine.for_vehicle(prediction.vehicle_id) |> new_cars?(),
station_code: station_code,
zone: zone,
certainty: certainty,
crowding_data_confidence: crowding_data_confidence,
crowding_description: crowding_description
}
end

@spec terminal(
Predictions.Prediction.t(),
String.t(),
String.t(),
Signs.Realtime.t(),
integer()
) :: t() | nil
def terminal(prediction, station_code, zone, sign, width \\ 18)

def terminal(prediction, station_code, zone, sign, width) do
@spec terminal(Predictions.Prediction.t(), String.t(), String.t(), Signs.Realtime.t()) ::
t() | nil
def terminal(prediction, station_code, zone, sign) do
stopped_at? = prediction.stops_away == 0

{minutes, approximate?} =
Expand All @@ -126,16 +97,11 @@ defmodule Content.Message.Predictions do
destination: Content.Utilities.destination_for_prediction(prediction),
minutes: minutes,
approximate?: approximate?,
route_id: prediction.route_id,
stop_id: prediction.stop_id,
trip_id: prediction.trip_id,
direction_id: prediction.direction_id,
width: width,
prediction: prediction,
new_cars?: sign.location_engine.for_vehicle(prediction.vehicle_id) |> new_cars?(),
station_code: station_code,
zone: zone,
terminal?: true,
certainty: prediction.departure_certainty
terminal?: true
}
end

Expand Down Expand Up @@ -265,15 +231,15 @@ defmodule Content.Message.Predictions do
defimpl Content.Message do
require Logger

@width 18
@boarding "BRD"
@arriving "ARR"

def to_string(%{
destination: destination,
minutes: minutes,
approximate?: approximate?,
width: width,
stop_id: stop_id,
prediction: %{stop_id: stop_id},
station_code: station_code,
zone: zone
}) do
Expand Down Expand Up @@ -304,19 +270,19 @@ defmodule Content.Message.Predictions do
{Content.Utilities.width_padded_string(
headsign_message,
"#{duration_string}",
width
@width
), 6},
{headsign <> platform_message, 6}
]

track_number ->
[
{Content.Utilities.width_padded_string(headsign, duration_string, width), 6},
{Content.Utilities.width_padded_string(headsign, "Trk #{track_number}", width), 6}
{Content.Utilities.width_padded_string(headsign, duration_string, @width), 6},
{Content.Utilities.width_padded_string(headsign, "Trk #{track_number}", @width), 6}
]

true ->
Content.Utilities.width_padded_string(headsign, duration_string, width)
Content.Utilities.width_padded_string(headsign, duration_string, @width)
end
end

Expand Down
14 changes: 3 additions & 11 deletions lib/content/message/stopped_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ defmodule Content.Message.StoppedTrain do
require Logger

@enforce_keys [:destination, :stops_away]
defstruct @enforce_keys ++ [:certainty, :stop_id, :trip_id, :route_id, :direction_id]
defstruct @enforce_keys ++ [:prediction]

@type t :: %__MODULE__{
destination: PaEss.destination(),
stops_away: non_neg_integer(),
certainty: non_neg_integer() | nil,
stop_id: String.t(),
trip_id: Predictions.Prediction.trip_id(),
route_id: String.t(),
direction_id: 0 | 1
prediction: Predictions.Prediction.t()
}

@spec from_prediction(Predictions.Prediction.t()) :: t() | nil
Expand All @@ -32,11 +28,7 @@ defmodule Content.Message.StoppedTrain do
%__MODULE__{
destination: Content.Utilities.destination_for_prediction(prediction),
stops_away: stops_away,
certainty: prediction.arrival_certainty || prediction.departure_certainty,
stop_id: prediction.stop_id,
trip_id: prediction.trip_id,
route_id: prediction.route_id,
direction_id: prediction.direction_id
prediction: prediction
}
end

Expand Down
32 changes: 16 additions & 16 deletions lib/signs/utilities/audio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,56 +187,56 @@ defmodule Signs.Utilities.Audio do
# Announce boarding if configured to. Also, if we normally announce arrivals, but the
# prediction went straight to boarding, announce boarding instead.
match?(%Message.Predictions{minutes: :boarding}, message) &&
message.trip_id not in sign.announced_boardings &&
message.prediction.trip_id not in sign.announced_boardings &&
(announce_boarding?(sign, message) ||
(announce_arriving?(sign, message) &&
message.trip_id not in sign.announced_arrivals)) ->
message.prediction.trip_id not in sign.announced_arrivals)) ->
{Audio.TrainIsBoarding.from_message(message),
update_in(sign.announced_boardings, &cache_value(&1, message.trip_id))}
update_in(sign.announced_boardings, &cache_value(&1, message.prediction.trip_id))}

# Announce arriving if configured to
match?(%Message.Predictions{minutes: :arriving}, message) &&
message.trip_id not in sign.announced_arrivals &&
message.prediction.trip_id not in sign.announced_arrivals &&
announce_arriving?(sign, message) ->
include_crowding? =
message.crowding_data_confidence == :high &&
message.trip_id not in sign.announced_approachings_with_crowding
message.prediction.trip_id not in sign.announced_approachings_with_crowding

{Audio.TrainIsArriving.from_message(message, include_crowding?),
update_in(sign.announced_arrivals, &cache_value(&1, message.trip_id))}
update_in(sign.announced_arrivals, &cache_value(&1, message.prediction.trip_id))}

# Announce approaching if configured to
match?(%Message.Predictions{minutes: :approaching}, message) &&
message.trip_id not in sign.announced_approachings &&
message.prediction.trip_id not in sign.announced_approachings &&
announce_arriving?(sign, message) &&
message.route_id in @heavy_rail_routes ->
message.prediction.route_id in @heavy_rail_routes ->
include_crowding? = message.crowding_data_confidence == :high

{Audio.Approaching.from_message(message, include_crowding?),
sign
|> update_in(
[Access.key!(:announced_approachings)],
&cache_value(&1, message.trip_id)
&cache_value(&1, message.prediction.trip_id)
)
|> update_in(
[Access.key!(:announced_approachings_with_crowding)],
&if(include_crowding?, do: cache_value(&1, message.trip_id), else: &1)
&if(include_crowding?, do: cache_value(&1, message.prediction.trip_id), else: &1)
)}

# Announce stopped trains
match?(%Message.StoppedTrain{}, message) && index == 0 &&
{message.trip_id, message.stops_away} not in sign.announced_stalls ->
{message.prediction.trip_id, message.stops_away} not in sign.announced_stalls ->
{Audio.StoppedTrain.from_message(message),
update_in(
sign.announced_stalls,
&cache_value(&1, {message.trip_id, message.stops_away})
&cache_value(&1, {message.prediction.trip_id, message.stops_away})
)}

# If we didn't have any predictions for a particular route/direction last update, but
# now we do, announce the next prediction.
match?(%Message.Predictions{}, message) && is_integer(message.minutes) && index == 0 &&
sign.prev_prediction_keys &&
{message.route_id, message.direction_id} not in sign.prev_prediction_keys ->
{message.prediction.route_id, message.prediction.direction_id} not in sign.prev_prediction_keys ->
{Audio.NextTrainCountdown.from_message(message), sign}

true ->
Expand All @@ -251,7 +251,7 @@ defmodule Signs.Utilities.Audio do
sign
| prev_prediction_keys:
for {:predictions, list} <- items, message <- list, uniq: true do
{message.route_id, message.direction_id}
{message.prediction.route_id, message.prediction.direction_id}
end
}

Expand Down Expand Up @@ -355,7 +355,7 @@ defmodule Signs.Utilities.Audio do

defp announce_arriving?(
%Signs.Realtime{source_config: source_config},
%Message.Predictions{stop_id: stop_id, direction_id: direction_id}
%Message.Predictions{prediction: %{stop_id: stop_id, direction_id: direction_id}}
) do
case SourceConfig.get_source_by_stop_and_direction(source_config, stop_id, direction_id) do
nil -> false
Expand All @@ -365,7 +365,7 @@ defmodule Signs.Utilities.Audio do

defp announce_boarding?(
%Signs.Realtime{source_config: source_config},
%Message.Predictions{stop_id: stop_id, direction_id: direction_id}
%Message.Predictions{prediction: %{stop_id: stop_id, direction_id: direction_id}}
) do
case SourceConfig.get_source_by_stop_and_direction(source_config, stop_id, direction_id) do
nil -> false
Expand Down
2 changes: 1 addition & 1 deletion lib/signs/utilities/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Signs.Utilities.Messages do
%Content.Message.Predictions{
station_code: "RJFK",
zone: "m",
stop_id: stop_id,
prediction: %{stop_id: stop_id},
minutes: minutes
} = prediction
) do
Expand Down
4 changes: 2 additions & 2 deletions test/content/audio/following_train_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Content.Audio.FollowingTrainTest do
test "when its a non terminal it uses arrives" do
message = %Content.Message.Predictions{
destination: :ashmont,
route_id: "Mattapan",
prediction: %Predictions.Prediction{route_id: "Mattapan"},
minutes: 5,
terminal?: false
}
Expand All @@ -36,7 +36,7 @@ defmodule Content.Audio.FollowingTrainTest do
test "when its a terminal it uses departs" do
message = %Content.Message.Predictions{
destination: :ashmont,
route_id: "Mattapan",
prediction: %Predictions.Prediction{route_id: "Mattapan"},
minutes: 5,
terminal?: true
}
Expand Down
Loading

0 comments on commit 2b60188

Please sign in to comment.