Skip to content

Commit

Permalink
refactor: parse all elevator redundancy categories
Browse files Browse the repository at this point in the history
Rather than only storing whether an elevator's redundancy category
is "nearby", we now store all categories, including any associated
summary text. This is a prerequisite for displaying redundancy
summaries on the system-wide outages view.

Also extracts this logic into a dedicated module which can be mocked for
testing, removing the need for a separate fixture file.
  • Loading branch information
digitalcora committed Dec 12, 2024
1 parent 5af3f61 commit ef87088
Show file tree
Hide file tree
Showing 10 changed files with 1,130 additions and 843 deletions.
4 changes: 1 addition & 3 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ config :screens,
headway_headsign: "Test"
}
]
},
elevator_redundancy_data_path:
Path.join(~w[#{File.cwd!()} test fixtures elevator_redundancy_data.json])
}

config :screens, ScreensWeb.AuthManager, secret_key: "test key"

Expand Down
47 changes: 47 additions & 0 deletions lib/screens/elevator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Screens.Elevator do
@moduledoc """
Exposes hand-authored data about elevator accessibility that is currently owned by the Screens
team and not (yet?) available in GTFS.
"""

alias Screens.Facilities.Facility
alias Screens.Log

@enforce_keys ~w[id redundancy]a
defstruct @enforce_keys

@type t :: %__MODULE__{id: Facility.id(), redundancy: redundancy()}

@type redundancy ::
:nearby
| :in_station
| {:different_station, summary :: String.t()}
| {:contact, summary :: String.t()}

@data :screens
|> :code.priv_dir()
|> Path.join("elevators.json")
|> File.read!()
|> Jason.decode!()

@callback get(Facility.id()) :: t() | nil
def get(id) do
case Map.get(@data, id) do
%{"redundancy" => 1} ->
%__MODULE__{id: id, redundancy: :nearby}

%{"redundancy" => 2} ->
%__MODULE__{id: id, redundancy: :in_station}

%{"redundancy" => 3, "summary" => summary} ->
%__MODULE__{id: id, redundancy: {:different_station, summary}}

%{"redundancy" => 4, "summary" => summary} ->
%__MODULE__{id: id, redundancy: {:contact, summary}}

_other ->
Log.warning("elevator_redundancy_not_found", id: id)
nil
end
end
end
24 changes: 7 additions & 17 deletions lib/screens/v2/candidate_generator/elevator/closures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
@moduledoc false

alias Screens.Alerts.{Alert, InformedEntity}
alias Screens.Elevator
alias Screens.Log
alias Screens.Routes.Route
alias Screens.Stops.Stop
Expand All @@ -17,29 +18,20 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
alias Screens.V2.WidgetInstance.Elevator.Closure
alias Screens.V2.WidgetInstance.Serializer.RoutePill
alias ScreensConfig.Screen
alias ScreensConfig.V2.Elevator
alias ScreensConfig.V2.Elevator, as: ElevatorConfig

import Screens.Inject

@alert injected(Alert)
@elevator injected(Elevator)
@facility injected(Screens.Facilities.Facility)
@route injected(Route)
@stop injected(Stop)

@elevator_redundancy_data :screens
|> Application.compile_env(
:elevator_redundancy_data_path,
:screens
|> :code.priv_dir()
|> Path.join("elevators/elevator_redundancy_data.json")
)
|> File.read!()
|> Jason.decode!()

@spec elevator_status_instances(Screen.t(), NormalHeader.t(), Footer.t()) ::
list(WidgetInstance.t())
def elevator_status_instances(
%Screen{app_params: %Elevator{elevator_id: elevator_id} = config},
%Screen{app_params: %ElevatorConfig{elevator_id: elevator_id} = config},
header_instance,
footer_instance
) do
Expand Down Expand Up @@ -187,11 +179,9 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
defp has_nearby_redundancy?(%Alert{
informed_entities: [%{facility: %{id: informed_facility_id}} | _]
}) do
if data = @elevator_redundancy_data[informed_facility_id] do
data["nearby_redundancy?"]
else
Log.warning("elevator_redundancy_not_found", facility_id: informed_facility_id)
false
case @elevator.get(informed_facility_id) do
%Elevator{redundancy: :nearby} -> true
_ -> false
end
end
end
Loading

0 comments on commit ef87088

Please sign in to comment.