Skip to content

Commit

Permalink
test message logic at the sign API level (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Aug 7, 2023
1 parent f4c5970 commit 845af14
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 571 deletions.
7 changes: 4 additions & 3 deletions lib/engine/alerts.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Engine.Alerts do
@behaviour Engine.AlertsAPI
use GenServer
require Logger
alias Engine.Alerts.Fetcher
Expand All @@ -22,8 +23,7 @@ defmodule Engine.Alerts do
GenServer.start_link(__MODULE__, opts, name: name)
end

@spec max_stop_status(ets_tables(), [Fetcher.stop_id()], [Fetcher.route_id()]) ::
Fetcher.stop_status()
@impl true
def max_stop_status(
tables \\ %{stops_table: @stops_table, routes_table: @routes_table},
stop_ids,
Expand Down Expand Up @@ -65,7 +65,7 @@ defmodule Engine.Alerts do
end
end

@spec init(Keyword.t()) :: {:ok, state()}
@impl true
def init(opts) do
fetch_ms = opts[:fetch_ms] || 30_000
fetcher = opts[:fetcher] || Engine.Alerts.ApiFetcher
Expand Down Expand Up @@ -93,6 +93,7 @@ defmodule Engine.Alerts do
{:ok, state}
end

@impl true
def handle_info(:fetch, state) do
schedule_fetch(self(), state.fetch_ms)

Expand Down
4 changes: 4 additions & 0 deletions lib/engine/alerts_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Engine.AlertsAPI do
alias Engine.Alerts.Fetcher
@callback max_stop_status([Fetcher.stop_id()], [Fetcher.route_id()]) :: Fetcher.stop_status()
end
9 changes: 5 additions & 4 deletions lib/engine/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Engine.Config do
@moduledoc """
Manages the dynamic configurable pieces of the signs such as if they are on
"""
@behaviour Engine.ConfigAPI

use GenServer
require Logger
Expand All @@ -28,15 +29,15 @@ defmodule Engine.Config do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end

@spec sign_config(:ets.tab(), String.t()) :: sign_config()
@impl true
def sign_config(table_name \\ @table_signs, sign_id) do
case :ets.lookup(table_name, sign_id) do
[{^sign_id, config}] -> config
_ -> :off
end
end

@spec headway_config(:ets.tab(), String.t(), DateTime.t()) :: Headway.t() | nil
@impl true
def headway_config(table_name \\ @table_headways, headway_group, current_time) do
time_period = Headway.current_time_period(current_time)
Headways.get_headway(table_name, {headway_group, time_period})
Expand All @@ -54,7 +55,7 @@ defmodule Engine.Config do
schedule_update(pid, 0)
end

@spec init(map()) :: {:ok, state}
@impl true
def init(opts) do
state = %{
table_name_signs: @table_signs,
Expand Down Expand Up @@ -84,7 +85,7 @@ defmodule Engine.Config do
])
end

@spec handle_info(:update, state) :: {:noreply, state}
@impl true
def handle_info(:update, state) do
schedule_update(self())
updater = Application.get_env(:realtime_signs, :external_config_getter)
Expand Down
4 changes: 4 additions & 0 deletions lib/engine/config_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Engine.ConfigAPI do
@callback sign_config(String.t()) :: Engine.Config.sign_config()
@callback headway_config(String.t(), DateTime.t()) :: Engine.Config.Headway.t() | nil
end
5 changes: 4 additions & 1 deletion lib/engine/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Engine.Predictions do
Offers a `for_stop/1` public interface to get a list of Predictions.Prediction's
for a given GTFS stop.
"""
@behaviour Engine.PredictionsAPI

use GenServer
require Logger
Expand All @@ -25,7 +26,7 @@ defmodule Engine.Predictions do
end

@doc "The upcoming predicted times a vehicle will be at this stop"
@spec for_stop(String.t(), 0 | 1) :: [Predictions.Prediction.t()]
@impl true
def for_stop(predictions_table_id \\ @trip_updates_table, gtfs_stop_id, direction_id) do
case :ets.lookup(predictions_table_id, {gtfs_stop_id, direction_id}) do
[{_, :none}] -> []
Expand All @@ -34,6 +35,7 @@ defmodule Engine.Predictions do
end
end

@impl true
def init(_) do
schedule_update(self())

Expand All @@ -48,6 +50,7 @@ defmodule Engine.Predictions do
}}
end

@impl true
def handle_info(:update, state) do
schedule_update(self())
current_time = Timex.now()
Expand Down
3 changes: 3 additions & 0 deletions lib/engine/predictions_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Engine.PredictionsAPI do
@callback for_stop(String.t(), 0 | 1) :: [Predictions.Prediction.t()]
end
11 changes: 9 additions & 2 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Signs.Realtime do
:last_departure_engine,
:config_engine,
:alerts_engine,
:current_time_fn,
:sign_updater,
:last_update,
:tick_audit,
Expand Down Expand Up @@ -58,6 +59,7 @@ defmodule Signs.Realtime do
last_departure_engine: module(),
config_engine: module(),
alerts_engine: module(),
current_time_fn: fun(),
sign_updater: module(),
last_update: DateTime.t(),
tick_audit: non_neg_integer(),
Expand Down Expand Up @@ -91,6 +93,12 @@ defmodule Signs.Realtime do
last_departure_engine: last_departure_engine,
config_engine: config_engine,
alerts_engine: alerts_engine,
current_time_fn:
opts[:current_time_fn] ||
fn ->
time_zone = Application.get_env(:realtime_signs, :time_zone)
DateTime.utc_now() |> DateTime.shift_zone!(time_zone)
end,
sign_updater: sign_updater,
last_update: nil,
tick_audit: 60,
Expand All @@ -113,8 +121,7 @@ defmodule Signs.Realtime do
sign_routes = SourceConfig.sign_routes(sign.source_config)
alert_status = sign.alerts_engine.max_stop_status(sign_stop_ids, sign_routes)
sign_config = sign.config_engine.sign_config(sign.id)
time_zone = Application.get_env(:realtime_signs, :time_zone)
{:ok, current_time} = DateTime.utc_now() |> DateTime.shift_zone(time_zone)
current_time = sign.current_time_fn.()

predictions =
case sign.source_config do
Expand Down
Loading

0 comments on commit 845af14

Please sign in to comment.