Skip to content

Commit

Permalink
refactor: remove unused parts of Signs UI config
Browse files Browse the repository at this point in the history
PA/ESS sign modes configured in Signs UI were only used by "v1" screen
logic, so we no longer need to parse or store this data.
  • Loading branch information
digitalcora committed Nov 4, 2024
1 parent 0e3664e commit 4e433ed
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 86 deletions.
49 changes: 9 additions & 40 deletions lib/screens/signs_ui_config/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 4 additions & 19 deletions lib/screens/signs_ui_config/cache/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
28 changes: 5 additions & 23 deletions lib/screens/signs_ui_config/parse.ex
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/screens/v2/candidate_generator/dup/departures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/signs_ui_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,5 @@
"range_low": 9
}
}
},
"signs": {}
}
}

0 comments on commit 4e433ed

Please sign in to comment.