Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into bhw/prediction-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos committed Aug 14, 2023
2 parents dbbfa96 + 68fc4b4 commit e476028
Show file tree
Hide file tree
Showing 13 changed files with 939 additions and 18 deletions.
17 changes: 13 additions & 4 deletions lib/content/message/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ defmodule Content.Message.Predictions do
width: 18,
platform: nil,
new_cars?: false,
terminal?: false
terminal?: false,
certainty: nil
]

@type t :: %__MODULE__{
Expand All @@ -45,7 +46,8 @@ defmodule Content.Message.Predictions do
station_code: String.t() | nil,
zone: String.t() | nil,
platform: Content.platform() | nil,
terminal?: boolean()
terminal?: boolean(),
certainty: non_neg_integer() | nil
}

@spec non_terminal(
Expand All @@ -61,6 +63,11 @@ defmodule Content.Message.Predictions 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

certainty =
if prediction.seconds_until_arrival,
do: prediction.arrival_certainty,
else: prediction.departure_certainty

minutes =
cond do
prediction.stops_away == 0 -> :boarding
Expand All @@ -87,7 +94,8 @@ defmodule Content.Message.Predictions do
new_cars?: prediction.new_cars?,
station_code: station_code,
zone: zone,
platform: platform
platform: platform,
certainty: certainty
}

{:error, _} ->
Expand Down Expand Up @@ -125,7 +133,8 @@ defmodule Content.Message.Predictions do
direction_id: prediction.direction_id,
width: width,
new_cars?: prediction.new_cars?,
terminal?: true
terminal?: true,
certainty: prediction.departure_certainty
}

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

@enforce_keys [:destination, :stops_away]
defstruct @enforce_keys
defstruct @enforce_keys ++ [:certainty]

@type t :: %__MODULE__{
destination: PaEss.destination(),
stops_away: non_neg_integer()
stops_away: non_neg_integer(),
certainty: non_neg_integer() | nil
}

@spec from_prediction(Predictions.Prediction.t()) :: t() | nil
Expand All @@ -32,7 +33,8 @@ defmodule Content.Message.StoppedTrain do

%__MODULE__{
destination: destination,
stops_away: stops_away
stops_away: stops_away,
certainty: prediction.arrival_certainty || prediction.departure_certainty
}

{:error, _} ->
Expand Down
7 changes: 7 additions & 0 deletions lib/engine/scheduled_headways.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ defmodule Engine.ScheduledHeadways do
:ets.select(table_name, pattern)
end

@impl true
def get_first_scheduled_departure(stop_ids) do
get_first_last_departures(stop_ids)
|> Enum.map(&elem(&1, 0))
|> min_time()
end

@doc "Checks if the given time is after the first scheduled stop and before the last.
A buffer of minutes (positive) is subtracted from the first time. so that headways are
shown for a short time before the first train."
Expand Down
1 change: 1 addition & 0 deletions lib/engine/scheduled_headways_api.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule Engine.ScheduledHeadwaysAPI do
@callback display_headways?([String.t()], DateTime.t(), non_neg_integer()) :: boolean()
@callback get_first_scheduled_departure([binary]) :: nil | DateTime.t()
end
4 changes: 4 additions & 0 deletions lib/predictions/prediction.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Predictions.Prediction do
defstruct stop_id: nil,
seconds_until_arrival: nil,
arrival_certainty: nil,
seconds_until_departure: nil,
departure_certainty: nil,
seconds_until_passthrough: nil,
direction_id: nil,
schedule_relationship: nil,
Expand All @@ -20,7 +22,9 @@ defmodule Predictions.Prediction do
@type t :: %__MODULE__{
stop_id: String.t(),
seconds_until_arrival: non_neg_integer() | nil,
arrival_certainty: non_neg_integer() | nil,
seconds_until_departure: non_neg_integer() | nil,
departure_certainty: non_neg_integer() | nil,
seconds_until_passthrough: non_neg_integer() | nil,
direction_id: 0 | 1,
schedule_relationship: :scheduled | :skipped | nil,
Expand Down
2 changes: 2 additions & 0 deletions lib/predictions/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ defmodule Predictions.Predictions do
stop_id: stop_time_update["stop_id"],
direction_id: direction_id,
seconds_until_arrival: max(0, seconds_until_arrival),
arrival_certainty: stop_time_update["arrival"]["uncertainty"],
seconds_until_departure: max(0, seconds_until_departure),
departure_certainty: stop_time_update["departure"]["uncertainty"],
seconds_until_passthrough: max(0, seconds_until_passthrough),
schedule_relationship:
translate_schedule_relationship(stop_time_update["schedule_relationship"]),
Expand Down
21 changes: 19 additions & 2 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ defmodule Signs.Realtime do
:last_departure_engine,
:config_engine,
:alerts_engine,
:current_time_fn,
:sign_updater,
:last_update,
:tick_audit,
Expand All @@ -35,6 +34,7 @@ defmodule Signs.Realtime do
defstruct @enforce_keys ++
[
:headway_stop_id,
:current_time_fn,
announced_arrivals: [],
announced_approachings: [],
announced_passthroughs: [],
Expand Down Expand Up @@ -123,6 +123,22 @@ defmodule Signs.Realtime do
sign_config = sign.config_engine.sign_config(sign.id)
current_time = sign.current_time_fn.()

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(sign_stop_ids),
source.headway_destination}
end

predictions =
case sign.source_config do
{top, bottom} -> {fetch_predictions(top, sign), fetch_predictions(bottom, sign)}
Expand All @@ -135,7 +151,8 @@ defmodule Signs.Realtime do
sign,
sign_config,
current_time,
alert_status
alert_status,
first_scheduled_departures
)

sign =
Expand Down
Loading

0 comments on commit e476028

Please sign in to comment.