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 10, 2024
1 parent c584592 commit cf53232
Show file tree
Hide file tree
Showing 10 changed files with 1,134 additions and 846 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
45 changes: 45 additions & 0 deletions lib/screens/elevator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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

@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 ->
nil
end
end
end
37 changes: 17 additions & 20 deletions lib/screens/v2/candidate_generator/elevator/closures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
require Logger

alias Screens.Alerts.{Alert, InformedEntity}
alias Screens.Elevator
alias Screens.Routes.Route
alias Screens.Stops.Stop
alias Screens.V2.WidgetInstance
Expand All @@ -18,29 +19,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 @@ -188,15 +180,20 @@ 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
_ =
Sentry.capture_message(
"Elevator #{informed_facility_id} does not exist in redundancy data"
)
case @elevator.get(informed_facility_id) do
%Elevator{redundancy: :nearby} ->
true

%Elevator{} ->
false

nil ->
_ =
Sentry.capture_message(
"Elevator #{informed_facility_id} does not exist in redundancy data"
)

false
false
end
end
end
Loading

0 comments on commit cf53232

Please sign in to comment.