Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: parse all elevator redundancy categories #2340

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Category 2 should also be returning summary text as well shouldn't it? At least according to the task that this is a prerequisite for mentioned in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm conceiving of this module as purely an interface to the data in the spreadsheet — "rules" apart from the raw data, like "when the redundancy category is 2, always use this specific text" would be implemented in the widget generator. I decided to draw the line here because of the rule where the summary text for all categories changes if any of the "backup elevators" for a given elevator are down, which depends on data from outside the spreadsheet (active alerts).

%__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
Loading