diff --git a/lib/screens/signs_ui_config/cache.ex b/lib/screens/signs_ui_config/cache.ex index 0cd1577f2..83ce25fa7 100644 --- a/lib/screens/signs_ui_config/cache.ex +++ b/lib/screens/signs_ui_config/cache.ex @@ -5,50 +5,19 @@ defmodule Screens.SignsUiConfig.Cache do use Screens.Cache.Client, table: :signs_ui_config - @type table_contents :: list(table_entry) - - @type table_entry :: - {{:sign_mode, sign_id :: String.t()}, atom()} - | {{:time_ranges, zone_id :: String.t()}, %{optional(atom()) => time_range}} + @type entry :: {{:headways, headway_key()}, headway_values()} + @type headway_key :: String.t() + @type headway_values :: %{optional(atom()) => time_range()} @type time_range :: {low :: integer(), high :: integer()} - # Implementation notes: - # Table entries use 2-part tuples as keys, to distinguish sign mode entries from time range entries. - # They look like: - # - Sign mode entry: {{:sign_mode, sign_id}, mode} - # - Time ranges entry: {{:time_ranges, line_or_trunk}, %{off_peak: {low, high}, peak: {low, high}, saturday: {low, high}, sunday: {low, high}}} - # - # To look up the mode that a given sign is in for example, use: - # [[mode]] = :ets.match(@table, {{:sign_mode, sign_id}, :"$1}) - - def all_signs_in_headway_mode?(sign_ids) do - all_signs_in_modes?(sign_ids, [:headway]) - end - - def all_signs_inactive?(sign_ids) do - all_signs_in_modes?(sign_ids, [:off, :static_text]) - end - - def time_ranges(line_or_trunk) do - with_table default: nil do - case :ets.match(@table, {{:time_ranges, line_or_trunk}, :"$1"}) do - [[ranges]] -> ranges - [] -> nil + @spec headways(headway_key()) :: headway_values() + def headways(key) do + with_table default: %{} do + case :ets.match(@table, {{:headways, key}, :"$1"}) do + [[headways]] -> headways + [] -> %{} end end end - - defp all_signs_in_modes?([], _modes), do: false - - defp all_signs_in_modes?(sign_ids, modes) do - with_table default: false do - Enum.all?(sign_ids, fn sign_id -> - case :ets.match(@table, {{:sign_mode, sign_id}, :"$1"}) do - [[mode]] -> mode in modes - [] -> false - end - end) - end - end end diff --git a/lib/screens/signs_ui_config/cache/engine.ex b/lib/screens/signs_ui_config/cache/engine.ex index 4ce359ff5..d1aac5578 100644 --- a/lib/screens/signs_ui_config/cache/engine.ex +++ b/lib/screens/signs_ui_config/cache/engine.ex @@ -3,9 +3,7 @@ defmodule Screens.SignsUiConfig.Cache.Engine do Engine for the Signs UI config cache. """ - alias Screens.SignsUiConfig.Cache - alias Screens.SignsUiConfig.Fetch - alias Screens.SignsUiConfig.Parse + alias Screens.SignsUiConfig.{Fetch, Parse} @behaviour Screens.Cache.Engine @@ -14,13 +12,9 @@ defmodule Screens.SignsUiConfig.Cache.Engine do @impl true def update_table(current_version) do - with {:ok, body, new_version} <- Fetch.fetch_config(current_version), - {:ok, deserialized} <- Jason.decode(body) do - config = Parse.parse_config(deserialized) - - table_entries = config_to_table_entries(config) - - {:replace, table_entries, new_version} + with {:ok, file_contents, new_version} <- Fetch.fetch_config(current_version), + {:ok, decoded_config} <- Jason.decode(file_contents) do + {:replace, Parse.parse_config(decoded_config), new_version} else :unchanged -> :unchanged _ -> :error @@ -32,13 +26,4 @@ defmodule Screens.SignsUiConfig.Cache.Engine do @impl true def update_failure_error_log_threshold_minutes, do: 2 - - @spec config_to_table_entries(config :: tuple()) :: Cache.table_contents() - defp config_to_table_entries({sign_modes, time_ranges}) do - sign_modes = Enum.map(sign_modes, fn {id, mode} -> {{:sign_mode, id}, mode} end) - - time_ranges = Enum.map(time_ranges, fn {id, ranges} -> {{:time_ranges, id}, ranges} end) - - sign_modes ++ time_ranges - end end diff --git a/lib/screens/signs_ui_config/parse.ex b/lib/screens/signs_ui_config/parse.ex index 7c428a41b..7e0c90528 100644 --- a/lib/screens/signs_ui_config/parse.ex +++ b/lib/screens/signs_ui_config/parse.ex @@ -1,18 +1,14 @@ defmodule Screens.SignsUiConfig.Parse do @moduledoc false - def parse_config(%{"configured_headways" => headways, "signs" => signs}) do - time_ranges = - headways - |> Enum.map(fn {id, data} -> {id, parse_time_ranges(data)} end) - |> Enum.into(%{}) + alias Screens.SignsUiConfig.Cache - sign_modes = parse_sign_modes(signs) - - {sign_modes, time_ranges} + @spec parse_config(map()) :: list(Cache.entry()) + def parse_config(%{"configured_headways" => headways}) do + Enum.map(headways, fn {key, value} -> {{:headways, key}, parse_headways(value)} end) end - defp parse_time_ranges(map) do + defp parse_headways(map) do for {key, field} <- [ off_peak: "off_peak", peak: "peak", @@ -27,18 +23,4 @@ defmodule Screens.SignsUiConfig.Parse do defp parse_time_range(%{"range_low" => low, "range_high" => high}), do: {low, high} defp parse_time_range(_), do: nil - - defp parse_sign_modes(signs) do - signs - |> Enum.map(fn {_, %{"id" => id, "mode" => mode}} -> {id, parse_sign_mode(mode)} end) - |> Enum.into(%{}) - end - - for mode <- ~w[auto headway off static_text temporary_terminal]a do - mode_string = Atom.to_string(mode) - - defp parse_sign_mode(unquote(mode_string)), do: unquote(mode) - end - - defp parse_sign_mode(_), do: :unknown end diff --git a/lib/screens/v2/candidate_generator/dup/departures.ex b/lib/screens/v2/candidate_generator/dup/departures.ex index 27ad5fe5c..4ec159874 100644 --- a/lib/screens/v2/candidate_generator/dup/departures.ex +++ b/lib/screens/v2/candidate_generator/dup/departures.ex @@ -363,10 +363,10 @@ defmodule Screens.V2.CandidateGenerator.Dup.Departures do not (branch_station?(stop_ids) and branch_alert?(interpreted_alert)) if headway_mode? do - time_ranges = SignsUiConfig.Cache.time_ranges(headway_id) + headways = SignsUiConfig.Cache.headways(headway_id) current_time_period = Screens.Util.time_period(current_time) - case time_ranges do + case headways do %{^current_time_period => {lo, hi}} -> {:active, {lo, hi}, interpreted_alert.headsign} diff --git a/test/fixtures/signs_ui_config.json b/test/fixtures/signs_ui_config.json index e3884f04d..3ac6a903f 100644 --- a/test/fixtures/signs_ui_config.json +++ b/test/fixtures/signs_ui_config.json @@ -90,6 +90,5 @@ "range_low": 9 } } - }, - "signs": {} + } }