Skip to content

Commit

Permalink
refactor(Stops.Repo): create a behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed May 2, 2024
1 parent cf38c51 commit a4c4749
Show file tree
Hide file tree
Showing 42 changed files with 180 additions and 78 deletions.
5 changes: 4 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ config :elixir, ansi_enabled: true
config :dotcom, :httpoison, HTTPoison

config :dotcom, :mbta_api_module, MBTA.Api
config :dotcom, :repo_modules, route_patterns: RoutePatterns.Repo

config :dotcom, :repo_modules,
route_patterns: RoutePatterns.Repo,
stops: Stops.Repo

config :dotcom, :redis, Dotcom.Cache.Multilevel.Redis
config :dotcom, :redix, Redix
Expand Down
5 changes: 4 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ config :dotcom, :cache, Dotcom.Cache.TestCache
config :dotcom, :httpoison, HTTPoison.Mock

config :dotcom, :mbta_api_module, MBTA.Api.Mock
config :dotcom, :repo_modules, route_patterns: RoutePatterns.Repo.Mock

config :dotcom, :repo_modules,
route_patterns: RoutePatterns.Repo.Mock,
stops: Stops.Repo.Mock

config :dotcom, :redis, Dotcom.Redis.Mock
config :dotcom, :redix, Dotcom.Redix.Mock
Expand Down
4 changes: 3 additions & 1 deletion lib/alerts/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ defmodule Alerts.Alert do

use Timex

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@ongoing_effects [
:cancellation,
:detour,
Expand Down Expand Up @@ -239,7 +241,7 @@ defmodule Alerts.Alert do
|> get_entity(:stop)
|> MapSet.delete(nil)
|> Enum.find_value(fn stop_id ->
with %Stops.Stop{} = stop <- Stops.Repo.get(stop_id) do
with %Stops.Stop{} = stop <- @stops_repo.get(stop_id) do
stop.municipality
end
end)
Expand Down
4 changes: 3 additions & 1 deletion lib/alerts/historical_alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Alerts.HistoricalAlert do

@type entity_key :: :route | :stop

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@spec from_alert(Alert.t()) :: t()
def from_alert(alert) when not is_nil(alert) do
%__MODULE__{
Expand All @@ -44,7 +46,7 @@ defmodule Alerts.HistoricalAlert do
module =
case key do
:route -> Routes.Repo
:stop -> Stops.Repo
:stop -> @stops_repo
end

case apply(module, :get, [id]) do
Expand Down
4 changes: 3 additions & 1 deletion lib/algolia/object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defprotocol Algolia.Object do
end

defimpl Algolia.Object, for: Stops.Stop do
@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

def object_id(stop), do: "stop-" <> stop.id
def url(stop), do: Util.site_path(:stop_path, [:show, stop])

Expand All @@ -19,7 +21,7 @@ defimpl Algolia.Object, for: Stops.Stop do
stop: stop,
zone: stop.zone,
routes: Algolia.Stop.Routes.for_stop(routes_for_stop),
features: Stops.Repo.stop_features(stop),
features: @stops_repo.stop_features(stop),
green_line_branches: Algolia.Stop.Routes.green_line_branches(routes_for_stop)
}
end
Expand Down
6 changes: 4 additions & 2 deletions lib/detailed_stop_group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule DetailedStopGroup do
alias Routes.Route
alias Stops.Stop

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@type t :: {Route.t(), [DetailedStop.t()]}
@type grouped_stops :: {Route.t(), [Stop.t()]}

Expand Down Expand Up @@ -35,7 +37,7 @@ defmodule DetailedStopGroup do
mode
|> Route.types_for_mode()
|> Routes.Repo.by_type()
|> Task.async_stream(&{&1, Stops.Repo.by_route(&1.id, 0)})
|> Task.async_stream(&{&1, @stops_repo.by_route(&1.id, 0)})
|> Enum.map(fn {:ok, stops} -> stops end)
end

Expand Down Expand Up @@ -64,7 +66,7 @@ defmodule DetailedStopGroup do
green_line? = route.id == "Green"

features =
Stops.Repo.stop_features(
@stops_repo.stop_features(
stop,
expand_branches?: green_line?
)
Expand Down
5 changes: 3 additions & 2 deletions lib/dotcom/realtime_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ defmodule Dotcom.RealtimeSchedule do
alias Routes.Route
alias Schedules.RepoCondensed, as: SchedulesRepo
alias Schedules.ScheduleCondensed
alias Stops.Repo, as: StopsRepo
alias Stops.Stop

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

# the long timeout is to address a worst-case scenario of cold schedule cache
@long_timeout 15_000

@predicted_schedules_per_stop 2

@default_opts [
stops_fn: &StopsRepo.get/1,
stops_fn: {@stops_repo, :get, 1},
routes_fn: &RoutesRepo.by_stop_with_route_pattern/1,
predictions_fn: &PredictionsRepo.all_no_cache/1,
schedules_fn: &SchedulesRepo.by_route_ids/2,
Expand Down
3 changes: 2 additions & 1 deletion lib/dotcom/transit_near_me.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ defmodule Dotcom.TransitNearMe do
required(:schedule_relationship) => Prediction.schedule_relationship()
}

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]
@default_opts [
stops_nearby_fn: &Nearby.nearby_with_varying_radius_by_mode/1,
schedules_fn: &Schedules.Repo.schedules_for_stop/2
Expand Down Expand Up @@ -245,7 +246,7 @@ defmodule Dotcom.TransitNearMe do
stop_id =
ps
|> PredictedSchedule.stop()
|> Stops.Repo.get_parent()
|> @stops_repo.get_parent()
|> Map.fetch!(:id)

{closest_time, headsigns} =
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom/trip_plan/itinerary_row.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ defmodule Dotcom.TripPlan.ItineraryRow do
defmodule Dependencies do
@moduledoc false

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@type stop_mapper :: (Stops.Stop.id_t() -> Stops.Stop.t() | nil)
@type route_mapper :: (Routes.Route.id_t() -> Routes.Route.t() | nil)
@type trip_mapper :: (Schedules.Trip.id_t() -> Schedules.Trip.t() | nil)
@type alerts_repo :: (DateTime.t() -> [Alerts.Alert.t()] | nil)

defstruct stop_mapper: &Stops.Repo.get_parent/1,
defstruct stop_mapper: {@stops_repo, :get_parent, 1},
route_mapper: &Routes.Repo.get/1,
trip_mapper: &Schedules.Repo.trip/1,
alerts_repo: &Alerts.Repo.all/1
Expand Down
4 changes: 2 additions & 2 deletions lib/dotcom/trip_plan/itinerary_row_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Dotcom.TripPlan.ItineraryRowList do
}

@type opts :: [to: String.t() | nil, from: String.t() | nil]

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]
@doc """
Builds a ItineraryRowList from the given itinerary
"""
Expand Down Expand Up @@ -74,7 +74,7 @@ defmodule Dotcom.TripPlan.ItineraryRowList do
last_leg = List.last(legs)

{name, stop_id} =
last_leg |> Map.get(:to) |> ItineraryRow.name_from_position(&Stops.Repo.get_parent/1)
last_leg |> Map.get(:to) |> ItineraryRow.name_from_position(&@stops_repo.get_parent/1)

alerts = Alerts.Stop.match(alerts, stop_id)
{destination_name(name, opts[:to]), stop_id, last_leg.stop, alerts}
Expand Down
3 changes: 2 additions & 1 deletion lib/dotcom/trip_plan/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ defmodule Dotcom.TripPlan.Map do
@type route_mapper :: (String.t() -> Route.t() | nil)
@type stop_mapper :: (String.t() -> Stops.Stop.t() | nil)

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]
@default_opts [
route_mapper: &Routes.Repo.get/1,
stop_mapper: &Stops.Repo.get_parent/1
stop_mapper: {@stops_repo, :get_parent, 1}
]

@moduledoc """
Expand Down
3 changes: 2 additions & 1 deletion lib/dotcom/trip_plan/related_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Dotcom.TripPlan.RelatedLink do
url: "",
icon_name: nil

@default_opts [route_by_id: &Routes.Repo.get/1, stop_by_id: &Stops.Repo.get_parent/1]
@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]
@default_opts [route_by_id: &Routes.Repo.get/1, stop_by_id: {@stops_repo, :get_parent, 1}]

import Phoenix.HTML.Link, only: [link: 2]
# Need a view in order to use the components. Ideally we'd have a separate
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/ambiguous_alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defprotocol DotcomWeb.AmbiguousAlert do
end

defimpl DotcomWeb.AmbiguousAlert, for: Alerts.Alert do
@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

def alert_start_date(%{active_period: [{start_date, _} | _]}) do
start_date
end
Expand All @@ -42,7 +44,7 @@ defimpl DotcomWeb.AmbiguousAlert, for: Alerts.Alert do
|> Alerts.Alert.get_entity(:stop)
|> MapSet.delete(nil)
|> Enum.map(fn id ->
case Stops.Repo.get_parent(id) do
case @stops_repo.get_parent(id) do
%Stops.Stop{} = stop -> stop
_ -> id
end
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/channels/vehicle_map_marker_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule DotcomWeb.VehicleMapMarkerChannel do
alias Leaflet.MapData.Marker
alias Vehicles.Vehicle

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

intercept(["reset", "add", "update", "remove"])

@impl Phoenix.Channel
Expand Down Expand Up @@ -82,7 +84,7 @@ defmodule DotcomWeb.VehicleMapMarkerChannel do
end

defp get_stop_name(stop_id) do
case Stops.Repo.get_parent(stop_id) do
case @stops_repo.get_parent(stop_id) do
nil -> ""
%Stops.Stop{name: name} -> name
end
Expand Down
6 changes: 4 additions & 2 deletions lib/dotcom_web/controllers/alert_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule DotcomWeb.AlertController do
plug(DotcomWeb.Plugs.AlertsByTimeframe)
plug(DotcomWeb.Plug.Mticket)

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@valid_ids ~w(subway commuter-rail bus ferry access)s

def index(conn, _params) do
Expand Down Expand Up @@ -82,7 +84,7 @@ defmodule DotcomWeb.AlertController do
|> Enum.filter(&MapSet.member?(access_effects, &1.effect))
|> Enum.reduce(%{}, &group_access_alerts_by_stop/2)
|> Enum.map(fn {stop_id, alerts} ->
stop = Stops.Repo.get_parent(stop_id)
stop = @stops_repo.get_parent(stop_id)
{stop, alerts}
end)
|> Enum.sort_by(fn {stop, _} -> stop.name end)
Expand All @@ -97,7 +99,7 @@ defmodule DotcomWeb.AlertController do
defp do_group_access_alerts_by_stop(stop_id, alert, acc) do
# stop_ids are sometimes child stops.
# Fetch the stop_id from the repo to get the parent id.
case Stops.Repo.get_parent(stop_id) do
case @stops_repo.get_parent(stop_id) do
%Stop{id: parent_stop_id} ->
Map.update(acc, parent_stop_id, MapSet.new([alert]), &MapSet.put(&1, alert))

Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/old_site_redirect_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule DotcomWeb.OldSiteRedirectController do
import DotcomWeb.Router.Helpers
import DotcomWeb.ViewHelpers, only: [cms_static_page_path: 2]

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

def schedules_and_maps(conn, %{"route" => route}) do
case old_route_to_route_id(route) do
nil -> permanent_redirect(conn, mode_path(conn, :index))
Expand All @@ -15,7 +17,7 @@ defmodule DotcomWeb.OldSiteRedirectController do
visitors to old links for stops. This will redirect them to the right page.
"""
def schedules_and_maps(conn, %{"path" => [_mode, "lines", "stations" | _], "stopId" => stop_id}) do
case Stops.Repo.old_id_to_gtfs_id(stop_id) do
case @stops_repo.old_id_to_gtfs_id(stop_id) do
nil -> permanent_redirect(conn, mode_path(conn, :index))
gtfs_id -> permanent_redirect(conn, stop_path(conn, :show, gtfs_id))
end
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/schedule/all_stops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule DotcomWeb.ScheduleController.AllStops do

require Logger

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@impl true
def init([]), do: []

Expand All @@ -14,7 +16,7 @@ defmodule DotcomWeb.ScheduleController.AllStops do
end

def do_call(conn, opts) do
repo_fn = Keyword.get(opts, :repo_fn, &Stops.Repo.by_route/3)
repo_fn = Keyword.get(opts, :repo_fn, &@stops_repo.by_route/3)
stops = get_all_stops(conn, repo_fn)
assign_all_stops(conn, stops)
end
Expand Down
8 changes: 5 additions & 3 deletions lib/dotcom_web/controllers/schedule/line.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ defmodule DotcomWeb.ScheduleController.Line do
alias DotcomWeb.ScheduleController.Line.Dependencies, as: Dependencies
alias DotcomWeb.ScheduleController.Line.Helpers, as: LineHelpers
alias DotcomWeb.ScheduleController.Line.Maps
alias Stops.Repo, as: StopsRepo
alias Stops.{RouteStops, RouteStop}

defmodule Dependencies do
@moduledoc """
Actions pulled in from elsewhere
"""
defstruct stops_by_route_fn: &StopsRepo.by_route/3
defstruct [:stops_by_route_fn]

@type t :: %__MODULE__{stops_by_route_fn: StopsRepo.stop_by_route()}
@type t :: %__MODULE__{
stops_by_route_fn:
{Application.compile_env!(:dotcom, :repo_modules)[:stops], :by_route, 3}
}
end

@type query_param :: String.t() | nil
Expand Down
8 changes: 4 additions & 4 deletions lib/dotcom_web/controllers/schedule/line/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do
alias RoutePatterns.RoutePattern
alias Routes.Repo, as: RoutesRepo
alias Routes.{Route, Shape}
alias Stops.Repo, as: StopsRepo
alias Stops.{RouteStop, RouteStops, Stop}

@route_patterns_repo Application.compile_env!(:dotcom, :repo_modules)[:route_patterns]
@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@type query_param :: String.t() | nil
@type direction_id :: 0 | 1
Expand Down Expand Up @@ -165,7 +165,7 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do
RoutesRepo.get_shapes(route_id, direction_id: direction_id)
end

@spec get_route_stops(Route.id_t(), direction_id, StopsRepo.stop_by_route()) ::
@spec get_route_stops(Route.id_t(), direction_id, Stops.Repo.stop_by_route()) ::
stops_by_route()
def get_route_stops("Green", direction_id, stops_by_route_fn) do
GreenLine.branch_ids()
Expand All @@ -177,7 +177,7 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do
do_get_route_stops(route_id, direction_id, stops_by_route_fn)
end

@spec do_get_route_stops(Route.id_t(), direction_id, StopsRepo.stop_by_route()) ::
@spec do_get_route_stops(Route.id_t(), direction_id, Stops.Repo.stop_by_route()) ::
stops_by_route()
defp do_get_route_stops(route_id, direction_id, stops_by_route_fn) do
case stops_by_route_fn.(route_id, direction_id, []) do
Expand Down Expand Up @@ -249,7 +249,7 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do

@spec stops_for_route_pattern(RoutePattern.t()) :: {RoutePattern.t(), [Stop.t()]}
defp stops_for_route_pattern(%RoutePattern{stop_ids: stop_ids} = route_pattern) do
stops = Enum.map(stop_ids, &StopsRepo.get_parent/1)
stops = Enum.map(stop_ids, &@stops_repo.get_parent/1)
{route_pattern, stops}
end

Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/schedule/line_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule DotcomWeb.ScheduleController.LineApi do
alias Stops.Stop
alias Vehicles.Vehicle

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@typep simple_vehicle :: %{
id: String.t(),
headsign: String.t() | nil,
Expand Down Expand Up @@ -126,7 +128,7 @@ defmodule DotcomWeb.ScheduleController.LineApi do
end

defp group_tooltips_by_stop(tooltip) do
case Stops.Repo.get_parent(tooltip.vehicle.stop_id) do
case @stops_repo.get_parent(tooltip.vehicle.stop_id) do
%Stop{id: id} -> id
_ -> nil
end
Expand Down
Loading

0 comments on commit a4c4749

Please sign in to comment.