From a4c4749c0119164b5d61acbfdb6fda3e59db7e64 Mon Sep 17 00:00:00 2001 From: Cristen Jones Date: Thu, 2 May 2024 16:54:13 -0400 Subject: [PATCH] refactor(Stops.Repo): create a behaviour --- config/config.exs | 5 ++- config/test.exs | 5 ++- lib/alerts/alert.ex | 4 ++- lib/alerts/historical_alert.ex | 4 ++- lib/algolia/object.ex | 4 ++- lib/detailed_stop_group.ex | 6 ++-- lib/dotcom/realtime_schedule.ex | 5 +-- lib/dotcom/transit_near_me.ex | 3 +- lib/dotcom/trip_plan/itinerary_row.ex | 4 ++- lib/dotcom/trip_plan/itinerary_row_list.ex | 4 +-- lib/dotcom/trip_plan/map.ex | 3 +- lib/dotcom/trip_plan/related_link.ex | 3 +- lib/dotcom_web/ambiguous_alert.ex | 4 ++- .../channels/vehicle_map_marker_channel.ex | 4 ++- .../controllers/alert_controller.ex | 6 ++-- .../old_site_redirect_controller.ex | 4 ++- .../controllers/schedule/all_stops.ex | 4 ++- lib/dotcom_web/controllers/schedule/line.ex | 8 +++-- .../controllers/schedule/line/helpers.ex | 8 ++--- .../controllers/schedule/line_api.ex | 4 ++- .../schedule/timetable_controller.ex | 5 +-- .../controllers/schedule/vehicle_locations.ex | 4 ++- lib/dotcom_web/controllers/stop_controller.ex | 13 ++++---- lib/dotcom_web/views/helpers.ex | 4 ++- lib/dotcom_web/views/page_view.ex | 4 +-- lib/dotcom_web/views/stop_view.ex | 3 +- lib/fares/fares.ex | 4 ++- lib/green_line.ex | 6 ++-- lib/predictions/repo.ex | 4 ++- lib/predictions/stream_parser.ex | 6 ++-- lib/schedules/hours_of_operation.ex | 4 ++- lib/schedules/repo.ex | 4 ++- lib/schedules/repo_condensed.ex | 4 +-- lib/stops/nearby.ex | 3 +- lib/stops/repo.ex | 30 ++++++++++------- lib/stops/repo/behaviour.ex | 33 +++++++++++++++++++ lib/stops/route_stop.ex | 16 +++++---- lib/stops/stop.ex | 4 ++- lib/trip_info.ex | 4 ++- lib/trip_plan/transfer.ex | 6 ++-- lib/vehicle_helpers.ex | 4 ++- test/support/mocks.ex | 1 + 42 files changed, 180 insertions(+), 78 deletions(-) create mode 100644 lib/stops/repo/behaviour.ex diff --git a/config/config.exs b/config/config.exs index e25619c672..1c0ee29b86 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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 diff --git a/config/test.exs b/config/test.exs index 5344e26b3b..e32c0fd827 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/lib/alerts/alert.ex b/lib/alerts/alert.ex index 6077a00314..3d622eb742 100644 --- a/lib/alerts/alert.ex +++ b/lib/alerts/alert.ex @@ -73,6 +73,8 @@ defmodule Alerts.Alert do use Timex + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @ongoing_effects [ :cancellation, :detour, @@ -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) diff --git a/lib/alerts/historical_alert.ex b/lib/alerts/historical_alert.ex index a1d5981b0f..0637646fae 100644 --- a/lib/alerts/historical_alert.ex +++ b/lib/alerts/historical_alert.ex @@ -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__{ @@ -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 diff --git a/lib/algolia/object.ex b/lib/algolia/object.ex index 4405a8e01e..1ac93563dc 100644 --- a/lib/algolia/object.ex +++ b/lib/algolia/object.ex @@ -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]) @@ -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 diff --git a/lib/detailed_stop_group.ex b/lib/detailed_stop_group.ex index d4826e7b97..a4fa93ba05 100644 --- a/lib/detailed_stop_group.ex +++ b/lib/detailed_stop_group.ex @@ -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()]} @@ -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 @@ -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? ) diff --git a/lib/dotcom/realtime_schedule.ex b/lib/dotcom/realtime_schedule.ex index fbf6548e39..440b18c58e 100644 --- a/lib/dotcom/realtime_schedule.ex +++ b/lib/dotcom/realtime_schedule.ex @@ -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, diff --git a/lib/dotcom/transit_near_me.ex b/lib/dotcom/transit_near_me.ex index a2c452049e..308bd8ebac 100644 --- a/lib/dotcom/transit_near_me.ex +++ b/lib/dotcom/transit_near_me.ex @@ -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 @@ -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} = diff --git a/lib/dotcom/trip_plan/itinerary_row.ex b/lib/dotcom/trip_plan/itinerary_row.ex index a7348bf9ba..d3c3e9022b 100644 --- a/lib/dotcom/trip_plan/itinerary_row.ex +++ b/lib/dotcom/trip_plan/itinerary_row.ex @@ -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 diff --git a/lib/dotcom/trip_plan/itinerary_row_list.ex b/lib/dotcom/trip_plan/itinerary_row_list.ex index ea82ffef4e..b3aed85c36 100644 --- a/lib/dotcom/trip_plan/itinerary_row_list.ex +++ b/lib/dotcom/trip_plan/itinerary_row_list.ex @@ -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 """ @@ -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} diff --git a/lib/dotcom/trip_plan/map.ex b/lib/dotcom/trip_plan/map.ex index effe0ad01a..bb3d65783a 100644 --- a/lib/dotcom/trip_plan/map.ex +++ b/lib/dotcom/trip_plan/map.ex @@ -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 """ diff --git a/lib/dotcom/trip_plan/related_link.ex b/lib/dotcom/trip_plan/related_link.ex index 3ba2ea78c7..a6c19036ba 100644 --- a/lib/dotcom/trip_plan/related_link.ex +++ b/lib/dotcom/trip_plan/related_link.ex @@ -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 diff --git a/lib/dotcom_web/ambiguous_alert.ex b/lib/dotcom_web/ambiguous_alert.ex index c5a0cff608..1d6b64ea89 100644 --- a/lib/dotcom_web/ambiguous_alert.ex +++ b/lib/dotcom_web/ambiguous_alert.ex @@ -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 @@ -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 diff --git a/lib/dotcom_web/channels/vehicle_map_marker_channel.ex b/lib/dotcom_web/channels/vehicle_map_marker_channel.ex index 5f3ba183a9..91d5d3d2b9 100644 --- a/lib/dotcom_web/channels/vehicle_map_marker_channel.ex +++ b/lib/dotcom_web/channels/vehicle_map_marker_channel.ex @@ -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 @@ -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 diff --git a/lib/dotcom_web/controllers/alert_controller.ex b/lib/dotcom_web/controllers/alert_controller.ex index 762b925ae0..4d87157a55 100644 --- a/lib/dotcom_web/controllers/alert_controller.ex +++ b/lib/dotcom_web/controllers/alert_controller.ex @@ -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 @@ -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) @@ -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)) diff --git a/lib/dotcom_web/controllers/old_site_redirect_controller.ex b/lib/dotcom_web/controllers/old_site_redirect_controller.ex index 546f0b310b..6490784922 100644 --- a/lib/dotcom_web/controllers/old_site_redirect_controller.ex +++ b/lib/dotcom_web/controllers/old_site_redirect_controller.ex @@ -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)) @@ -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 diff --git a/lib/dotcom_web/controllers/schedule/all_stops.ex b/lib/dotcom_web/controllers/schedule/all_stops.ex index 2c38d1b785..c1bfb667fd 100644 --- a/lib/dotcom_web/controllers/schedule/all_stops.ex +++ b/lib/dotcom_web/controllers/schedule/all_stops.ex @@ -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: [] @@ -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 diff --git a/lib/dotcom_web/controllers/schedule/line.ex b/lib/dotcom_web/controllers/schedule/line.ex index d8cf6ad082..d53abf3bd2 100644 --- a/lib/dotcom_web/controllers/schedule/line.ex +++ b/lib/dotcom_web/controllers/schedule/line.ex @@ -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 diff --git a/lib/dotcom_web/controllers/schedule/line/helpers.ex b/lib/dotcom_web/controllers/schedule/line/helpers.ex index 331f7d9987..8f3c99f2ab 100644 --- a/lib/dotcom_web/controllers/schedule/line/helpers.ex +++ b/lib/dotcom_web/controllers/schedule/line/helpers.ex @@ -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 @@ -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() @@ -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 @@ -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 diff --git a/lib/dotcom_web/controllers/schedule/line_api.ex b/lib/dotcom_web/controllers/schedule/line_api.ex index 16a2fe2224..da2481d65c 100644 --- a/lib/dotcom_web/controllers/schedule/line_api.ex +++ b/lib/dotcom_web/controllers/schedule/line_api.ex @@ -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, @@ -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 diff --git a/lib/dotcom_web/controllers/schedule/timetable_controller.ex b/lib/dotcom_web/controllers/schedule/timetable_controller.ex index 85cbd858cc..c1ca541bd8 100644 --- a/lib/dotcom_web/controllers/schedule/timetable_controller.ex +++ b/lib/dotcom_web/controllers/schedule/timetable_controller.ex @@ -8,6 +8,7 @@ defmodule DotcomWeb.ScheduleController.TimetableController do require Logger @route_patterns_repo Application.compile_env!(:dotcom, :repo_modules)[:route_patterns] + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] plug(DotcomWeb.Plugs.Route) plug(DotcomWeb.Plugs.DateInRating) @@ -141,7 +142,7 @@ defmodule DotcomWeb.ScheduleController.TimetableController do @doc """ If the scheduled platform stop is not canonical, then return the stop of that track change. """ - def track_change_for_schedule(schedule, canonical_stop_ids, stop_get_fn \\ &Stops.Repo.get/1) do + def track_change_for_schedule(schedule, canonical_stop_ids, stop_get_fn \\ &@stops_repo.get/1) do if has_scheduled_track_change(schedule, canonical_stop_ids) do case stop_get_fn.(schedule.platform_stop_id) do nil -> nil @@ -234,7 +235,7 @@ defmodule DotcomWeb.ScheduleController.TimetableController do defp all_stops(conn, _) do all_stops = - Stops.Repo.by_route(conn.assigns.route.id, conn.assigns.direction_id, + @stops_repo.by_route(conn.assigns.route.id, conn.assigns.direction_id, date: conn.assigns.date ) diff --git a/lib/dotcom_web/controllers/schedule/vehicle_locations.ex b/lib/dotcom_web/controllers/schedule/vehicle_locations.ex index bf6d64e29d..b8ab04ca95 100644 --- a/lib/dotcom_web/controllers/schedule/vehicle_locations.ex +++ b/lib/dotcom_web/controllers/schedule/vehicle_locations.ex @@ -8,6 +8,8 @@ defmodule DotcomWeb.ScheduleController.VehicleLocations do alias Stops.Stop + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @default_opts [ location_fn: &Vehicles.Repo.route/2, schedule_for_trip_fn: &Schedules.Repo.schedule_for_trip/2 @@ -49,7 +51,7 @@ defmodule DotcomWeb.ScheduleController.VehicleLocations do @spec stop_name(String.t()) :: String.t() defp stop_name(<>) do - stop = Stops.Repo.get_parent(stop_id) + stop = @stops_repo.get_parent(stop_id) if stop do stop.name diff --git a/lib/dotcom_web/controllers/stop_controller.ex b/lib/dotcom_web/controllers/stop_controller.ex index 96060edf6c..679dfeb811 100644 --- a/lib/dotcom_web/controllers/stop_controller.ex +++ b/lib/dotcom_web/controllers/stop_controller.ex @@ -14,10 +14,11 @@ defmodule DotcomWeb.StopController do alias RoutePatterns.RoutePattern alias Services.Service alias Dotcom.TransitNearMe - alias Stops.{Repo, Stop} + alias Stops.Stop alias Util.AndOr @route_patterns_repo Application.compile_env!(:dotcom, :repo_modules)[:route_patterns] + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] plug(:alerts) plug(DotcomWeb.Plugs.AlertsByTimeframe) @@ -51,12 +52,12 @@ defmodule DotcomWeb.StopController do stop = stop_id |> URI.decode_www_form() - |> Repo.get() + |> @stops_repo.get() if stop do - if Repo.has_parent?(stop) do + if @stops_repo.has_parent?(stop) do conn - |> redirect(to: stop_path(conn, :show, Repo.get_parent(stop))) + |> redirect(to: stop_path(conn, :show, @stops_repo.get_parent(stop))) |> halt() else routes_by_stop = Routes.Repo.by_stop(stop_id, include: "stop.connecting_stops") @@ -76,7 +77,7 @@ defmodule DotcomWeb.StopController do @spec get(Conn.t(), map) :: Conn.t() def get(conn, %{"id" => stop_id}) do - json(conn, Repo.get(stop_id)) + json(conn, @stops_repo.get(stop_id)) end @spec grouped_route_patterns(Conn.t(), map) :: Conn.t() @@ -148,7 +149,7 @@ defmodule DotcomWeb.StopController do defp ends_at?(%RoutePattern{stop_ids: stop_ids}, stop_id) when is_list(stop_ids) do with last_stop_id <- List.last(stop_ids), - %Stop{child_ids: child_ids} <- Stops.Repo.get(stop_id) do + %Stop{child_ids: child_ids} <- @stops_repo.get(stop_id) do last_stop_id == stop_id || last_stop_id in child_ids else _ -> diff --git a/lib/dotcom_web/views/helpers.ex b/lib/dotcom_web/views/helpers.ex index bc6fb9425c..ffbf2b44fb 100644 --- a/lib/dotcom_web/views/helpers.ex +++ b/lib/dotcom_web/views/helpers.ex @@ -16,6 +16,8 @@ defmodule DotcomWeb.ViewHelpers do alias Plug.Conn alias Phoenix.HTML.Safe + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @subway_lines [ :red_line, :blue_line, @@ -360,7 +362,7 @@ defmodule DotcomWeb.ViewHelpers do def stop_link(stop_id) do stop_id - |> Stops.Repo.get_parent() + |> @stops_repo.get_parent() |> stop_link end diff --git a/lib/dotcom_web/views/page_view.ex b/lib/dotcom_web/views/page_view.ex index 88389d716e..54c16d4322 100644 --- a/lib/dotcom_web/views/page_view.ex +++ b/lib/dotcom_web/views/page_view.ex @@ -9,7 +9,7 @@ defmodule DotcomWeb.PageView do alias DotcomWeb.PartialView use DotcomWeb, :view - + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] @spec get_route(Routes.Route.id_t()) :: Routes.Route.t() | nil def get_route(id) do case DotcomWeb.ScheduleController.Line.Helpers.get_route(id) do @@ -66,7 +66,7 @@ defmodule DotcomWeb.PageView do ) |> Enum.map(fn {type, stops} -> {type, - Enum.map(stops, &Stops.Repo.get_parent/1) + Enum.map(stops, &@stops_repo.get_parent/1) |> Enum.filter(& &1) |> Enum.uniq_by(& &1.id) |> Enum.sort_by(& &1.name)} diff --git a/lib/dotcom_web/views/stop_view.ex b/lib/dotcom_web/views/stop_view.ex index 09b823b8f6..7ddb02a7ae 100644 --- a/lib/dotcom_web/views/stop_view.ex +++ b/lib/dotcom_web/views/stop_view.ex @@ -5,14 +5,13 @@ defmodule DotcomWeb.StopView do use DotcomWeb, :view alias Phoenix.HTML.Safe - alias Stops.Repo alias DotcomWeb.PartialView.SvgIconWithCircle alias Routes.Route @doc """ Returns correct svg Icon for the given feature """ - @spec stop_feature_icon(Repo.stop_feature(), :small | :default) :: Safe.t() + @spec stop_feature_icon(Stops.Repo.stop_feature(), :small | :default) :: Safe.t() def stop_feature_icon(feature, size \\ :default) def stop_feature_icon(feature, size) when is_atom(size) do diff --git a/lib/fares/fares.ex b/lib/fares/fares.ex index e69642715f..d843b272fc 100644 --- a/lib/fares/fares.ex +++ b/lib/fares/fares.ex @@ -9,6 +9,8 @@ defmodule Fares do alias Schedules.Trip alias Stops.Stop + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @silver_line_rapid_transit ~w(741 742 743 746) @silver_line_rapid_transit_set MapSet.new(@silver_line_rapid_transit) @@ -77,7 +79,7 @@ defmodule Fares do end defp zone_for_stop(stop_id) do - case Stops.Repo.get(stop_id) do + case @stops_repo.get(stop_id) do %{zone: zone} -> zone _ -> nil end diff --git a/lib/green_line.ex b/lib/green_line.ex index 074412972e..e7d3da681a 100644 --- a/lib/green_line.ex +++ b/lib/green_line.ex @@ -7,6 +7,8 @@ defmodule GreenLine do alias Routes.Route alias Stops.Stop + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @type route_id_stop_id_map :: %{Route.id_t() => MapSet.t()} @type stop_routes_pair :: {[Stop.t()] | {:error, any}, route_id_stop_id_map} @type branch_name :: String.t() @@ -27,7 +29,7 @@ defmodule GreenLine do """ @spec calculate_stops_on_routes(0 | 1, Date.t() | nil, stops_by_routes_fn | nil) :: stop_routes_pair - def calculate_stops_on_routes(direction_id, date \\ nil, stops_fn \\ &Stops.Repo.by_route/3) do + def calculate_stops_on_routes(direction_id, date \\ nil, stops_fn \\ &@stops_repo.by_route/3) do branch_ids() |> Task.async_stream(&green_line_stops(&1, direction_id, date, stops_fn)) |> Enum.reduce({[], %{}}, &merge_green_line_stops/2) @@ -39,7 +41,7 @@ defmodule GreenLine do """ def termini_stops() do for direction_id <- [0, 1], branch_id <- GreenLine.branch_ids(), into: %{} do - stop = Stops.Repo.by_route(branch_id, direction_id) |> List.last() + stop = @stops_repo.by_route(branch_id, direction_id) |> List.last() {{branch_id, direction_id}, stop} end end diff --git a/lib/predictions/repo.ex b/lib/predictions/repo.ex index 1d66bb2f22..5928562b05 100644 --- a/lib/predictions/repo.ex +++ b/lib/predictions/repo.ex @@ -10,6 +10,8 @@ defmodule Predictions.Repo do alias Routes.Route alias Stops.Stop + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @default_params [ "fields[prediction]": "status,departure_time,arrival_time,direction_id,schedule_relationship,stop_sequence", @@ -173,7 +175,7 @@ defmodule Predictions.Repo do defp record_to_structs({_, _, <>, _, _, _, _, _, _, _, _, _, _, _} = record) do stop_id - |> Stops.Repo.get_parent() + |> @stops_repo.get_parent() |> do_record_to_structs(record) |> discard_if_subway_past_prediction() end diff --git a/lib/predictions/stream_parser.ex b/lib/predictions/stream_parser.ex index 05eb9208ac..c60b145edf 100644 --- a/lib/predictions/stream_parser.ex +++ b/lib/predictions/stream_parser.ex @@ -13,6 +13,8 @@ defmodule Predictions.StreamParser do alias Schedules.Trip alias Stops.Stop + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @spec parse(Item.t()) :: Prediction.t() def parse(%Item{} = item) do route = included_route(item) @@ -37,7 +39,7 @@ defmodule Predictions.StreamParser do stop_sequence: Parser.stop_sequence(item), time: Schedules.Parser.display_time(arrival, departure, route), route: route, - stop: Stops.Repo.get_parent(stop), + stop: @stops_repo.get_parent(stop), platform_stop_id: stop_id(item), trip: trip, schedule_relationship: Parser.schedule_relationship(item), @@ -89,7 +91,7 @@ defmodule Predictions.StreamParser do # note: likely to be a child stop @spec included_stop(Item.t()) :: Stops.Stop.t() | nil - defp included_stop(%Item{relationships: %{"stop" => [%Item{id: id}]}}), do: Stops.Repo.get(id) + defp included_stop(%Item{relationships: %{"stop" => [%Item{id: id}]}}), do: @stops_repo.get(id) defp included_stop(_), do: nil end diff --git a/lib/schedules/hours_of_operation.ex b/lib/schedules/hours_of_operation.ex index dbb0861f3e..add2da6aed 100644 --- a/lib/schedules/hours_of_operation.ex +++ b/lib/schedules/hours_of_operation.ex @@ -8,6 +8,8 @@ defmodule Schedules.HoursOfOperation do alias Schedules.Departures alias Services.Service + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @cache Application.compile_env!(:dotcom, :cache) @ttl :timer.hours(1) @@ -460,7 +462,7 @@ defmodule Schedules.HoursOfOperation do end) Enum.map(times_by_stop, fn {id, x} -> - stop = Stops.Repo.get!(id) + stop = @stops_repo.get!(id) {min, max} = x diff --git a/lib/schedules/repo.ex b/lib/schedules/repo.ex index 52e312f595..fa6c5900c3 100644 --- a/lib/schedules/repo.ex +++ b/lib/schedules/repo.ex @@ -15,6 +15,8 @@ defmodule Schedules.Repo do alias Schedules.{Parser, Schedule} alias Util + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @cache Application.compile_env!(:dotcom, :cache) @ttl :timer.hours(1) @@ -248,7 +250,7 @@ defmodule Schedules.Repo do route: Routes.Repo.get(route_id), trip: trip(trip_id), platform_stop_id: stop_id, - stop: Stops.Repo.get_parent(stop_id), + stop: @stops_repo.get_parent(stop_id), arrival_time: arrival_time, departure_time: departure_time, time: time, diff --git a/lib/schedules/repo_condensed.ex b/lib/schedules/repo_condensed.ex index 2f7c62d1e9..ff8f3183bc 100644 --- a/lib/schedules/repo_condensed.ex +++ b/lib/schedules/repo_condensed.ex @@ -13,8 +13,8 @@ defmodule Schedules.RepoCondensed do alias MBTA.Api.Schedules, as: SchedulesApi alias Routes.Route alias Schedules.{Parser, Repo, ScheduleCondensed} - alias Stops.Repo, as: StopsRepo + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] @cache Application.compile_env!(:dotcom, :cache) @ttl :timer.hours(1) @@ -122,7 +122,7 @@ defmodule Schedules.RepoCondensed do |> Enum.map(fn {_, trip_id, stop_id, _, _, time, _, _, _, stop_sequence, _} -> Task.async(fn -> trip = Repo.trip(trip_id) - stop = StopsRepo.get!(stop_id) + stop = @stops_repo.get!(stop_id) %ScheduleCondensed{ time: time, diff --git a/lib/stops/nearby.ex b/lib/stops/nearby.ex index 2c49820800..31991bf715 100644 --- a/lib/stops/nearby.ex +++ b/lib/stops/nearby.ex @@ -16,7 +16,8 @@ defmodule Stops.Nearby do @moduledoc "Defines shared options and defaults for this module's functions." defstruct api_fn: &Stops.Nearby.api_around/2, keys_fn: &Stops.Nearby.keys/1, - fetch_fn: &Stops.Repo.get_parent/1, + fetch_fn: + {Application.compile_env!(:dotcom, :repo_modules)[:stops], :get_parent, 1}, routes_fn: &Routes.Repo.by_stop_and_direction/2, limit: nil end diff --git a/lib/stops/repo.ex b/lib/stops/repo.ex index 0475ec02ca..6fff36b6a2 100644 --- a/lib/stops/repo.ex +++ b/lib/stops/repo.ex @@ -9,6 +9,8 @@ defmodule Stops.Repo do alias Stops.{Api, Stop} alias Routes.Route + @behaviour Stops.Repo.Behaviour + @cache Application.compile_env!(:dotcom, :cache) @ttl :timer.hours(1) @@ -30,6 +32,7 @@ defmodule Stops.Repo do |> CSV.decode!(headers: true) |> Enum.map(&{&1 |> Map.get("atisId") |> String.split(","), Map.get(&1, "stopID")}) |> Enum.flat_map(fn {ids, gtfs_id} -> Enum.map(ids, &{&1, gtfs_id}) end) do + @impl Stops.Repo.Behaviour def old_id_to_gtfs_id(unquote(old_id)) do unquote(gtfs_id) end @@ -39,7 +42,7 @@ defmodule Stops.Repo do nil end - @spec get(Stop.id_t()) :: Stop.t() | nil + @impl Stops.Repo.Behaviour def get(id) when is_binary(id) do case stop(id) do {:ok, s} -> s @@ -47,7 +50,7 @@ defmodule Stops.Repo do end end - @spec get!(Stop.id_t()) :: Stop.t() + @impl Stops.Repo.Behaviour def get!(id) do case stop(id) do {:ok, %Stop{} = s} -> s @@ -55,12 +58,12 @@ defmodule Stops.Repo do end end - @spec has_parent?(Stop.t() | Stop.id_t() | nil) :: boolean + @impl Stops.Repo.Behaviour def has_parent?(nil), do: false def has_parent?(%Stop{parent_id: nil}), do: false def has_parent?(%Stop{parent_id: _}), do: true - @spec get_parent(Stop.t() | Stop.id_t() | nil) :: Stop.t() | nil + @impl Stops.Repo.Behaviour def get_parent(nil), do: nil def get_parent(%Stop{parent_id: nil} = stop) do @@ -85,7 +88,7 @@ defmodule Stops.Repo do Api.by_gtfs_id(id) end - @spec by_route(Route.id_t(), 0 | 1, Keyword.t()) :: stops_response + @impl Stops.Repo.Behaviour @decorate cacheable(cache: @cache, on_error: :nothing, opts: [ttl: @ttl]) def by_route(route_id, direction_id, opts \\ []) do with stops when is_list(stops) <- Api.by_route({route_id, direction_id, opts}) do @@ -99,7 +102,7 @@ defmodule Stops.Repo do end end - @spec by_routes([Route.id_t()], 0 | 1, Keyword.t()) :: stops_response + @impl Stops.Repo.Behaviour def by_routes(route_ids, direction_id, opts \\ []) when is_list(route_ids) do # once the V3 API supports multiple route_ids in this field, we can do it # as a single lookup -ps @@ -112,6 +115,7 @@ defmodule Stops.Repo do |> Enum.uniq() end + @impl Stops.Repo.Behaviour @decorate cacheable(cache: @cache, on_error: :nothing, opts: [ttl: @ttl]) def by_route_type(route_type, opts \\ []) do {route_type, opts} @@ -120,11 +124,13 @@ defmodule Stops.Repo do |> Enum.uniq_by(& &1.id) end + @impl Stops.Repo.Behaviour @decorate cacheable(cache: @cache, on_error: :nothing, opts: [ttl: @ttl]) def by_trip(trip_id) do Api.by_trip(trip_id) end + @impl Stops.Repo.Behaviour def stop_exists_on_route?(stop_id, route, direction_id) do route |> by_route(direction_id) @@ -134,7 +140,7 @@ defmodule Stops.Repo do @doc """ Returns a list of the features associated with the given stop """ - @spec stop_features(Stop.t(), Keyword.t()) :: [stop_feature] + @impl Stops.Repo.Behaviour def stop_features(%Stop{} = stop, opts \\ []) do excluded = Keyword.get(opts, :exclude, []) @@ -177,11 +183,11 @@ defmodule Stops.Repo do Routes.Repo.by_stop(stop_id) end - def branch_feature(%Route{id: "Green-B"}), do: :"Green-B" - def branch_feature(%Route{id: "Green-C"}), do: :"Green-C" - def branch_feature(%Route{id: "Green-D"}), do: :"Green-D" - def branch_feature(%Route{id: "Green-E"}), do: :"Green-E" - def branch_feature(route), do: Route.icon_atom(route) + defp branch_feature(%Route{id: "Green-B"}), do: :"Green-B" + defp branch_feature(%Route{id: "Green-C"}), do: :"Green-C" + defp branch_feature(%Route{id: "Green-D"}), do: :"Green-D" + defp branch_feature(%Route{id: "Green-E"}), do: :"Green-E" + defp branch_feature(route), do: Route.icon_atom(route) @spec accessibility_features([String.t()]) :: [:access] defp accessibility_features(["accessible" | _]), do: [:access] diff --git a/lib/stops/repo/behaviour.ex b/lib/stops/repo/behaviour.ex new file mode 100644 index 0000000000..b2f42fd98a --- /dev/null +++ b/lib/stops/repo/behaviour.ex @@ -0,0 +1,33 @@ +defmodule Stops.Repo.Behaviour do + @moduledoc """ + Behavior for an API client for fetching stop data + """ + alias Routes.Route + alias Schedules.Trip + alias Stops.Stop + + @callback old_id_to_gtfs_id(Stop.id_t()) :: Stop.id_t() | nil + + @callback get(Stop.id_t()) :: Stop.t() | nil + @callback get!(Stop.id_t()) :: Stop.t() + + @callback has_parent?(Stop.t() | Stop.id_t() | nil) :: boolean + + @callback get_parent(Stop.t() | Stop.id_t() | nil) :: Stop.t() | nil + + @callback by_route(Route.id_t(), 0 | 1, Keyword.t()) :: Stop.stops_response() + @callback by_route(Route.id_t(), 0 | 1) :: Stop.stops_response() + + @callback by_routes([Route.id_t()], 0 | 1) :: Stop.stops_response() + @callback by_routes([Route.id_t()], 0 | 1, Keyword.t()) :: Stop.stops_response() + + @callback by_route_type(Route.type_int()) :: Stop.stops_response() + @callback by_route_type(Route.type_int(), Keyword.t()) :: Stop.stops_response() + + @callback by_trip(Trip.id_t()) :: Stop.stops_response() + + @callback stop_exists_on_route?(Stop.id_t(), Route.t(), 0 | 1) :: boolean() + + @callback stop_features(Stop.t()) :: [Stop.stop_feature()] + @callback stop_features(Stop.t(), Keyword.t()) :: [Stop.stop_feature()] +end diff --git a/lib/stops/route_stop.ex b/lib/stops/route_stop.ex index 3ca94e9f62..e55407b4aa 100644 --- a/lib/stops/route_stop.ex +++ b/lib/stops/route_stop.ex @@ -20,7 +20,9 @@ defmodule Stops.RouteStop do """ alias Routes.{Route, Shape} alias RoutePatterns.RoutePattern - alias Stops.{Repo, Stop} + alias Stops.Stop + + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] @type branch_name_t :: String.t() | nil @type direction_id_t :: 0 | 1 @@ -33,7 +35,7 @@ defmodule Stops.RouteStop do station_info: Stop.t(), route: Route.t() | nil, connections: [Route.t()] | {:error, :not_fetched}, - stop_features: [Repo.stop_feature()] | {:error, :not_fetched}, + stop_features: [Stops.Repo.stop_feature()] | {:error, :not_fetched}, is_terminus?: boolean, is_beginning?: boolean, closed_stop_info: Stop.ClosedStopInfo.t() | nil @@ -206,7 +208,7 @@ defmodule Stops.RouteStop do |> Enum.flat_map(fn stop_id -> parent_stop_id = stop_id - |> Repo.get_parent() + |> @stops_repo.get_parent() |> Map.fetch!(:id) case Map.fetch(stops, parent_stop_id) do @@ -249,7 +251,7 @@ defmodule Stops.RouteStop do @spec fetch_zone(t) :: t def fetch_zone(%__MODULE__{zone: {:error, :not_fetched}} = route_stop) do - case Repo.get(route_stop.id) do + case @stops_repo.get(route_stop.id) do %Stop{zone: zone} -> %{route_stop | zone: zone} @@ -269,7 +271,7 @@ defmodule Stops.RouteStop do ) do connections = route_stop.id - |> Stops.Repo.get_parent() + |> @stops_repo.get_parent() |> Map.get(:id) |> Routes.Repo.by_stop(include: "stop.connecting_stops") |> Enum.reject(fn route -> @@ -280,9 +282,9 @@ defmodule Stops.RouteStop do %{route_stop | connections: connections} end - @spec route_stop_features(t) :: [Repo.stop_feature()] + @spec route_stop_features(t) :: [Stops.Repo.stop_feature()] defp route_stop_features(%__MODULE__{station_info: %Stop{}} = route_stop) do - Repo.stop_features(route_stop.station_info, connections: route_stop.connections) + @stops_repo.stop_features(route_stop.station_info, connections: route_stop.connections) end defp route_stop_features(%__MODULE__{}) do diff --git a/lib/stops/stop.ex b/lib/stops/stop.ex index 7c32ea5af1..ced2e223b5 100644 --- a/lib/stops/stop.ex +++ b/lib/stops/stop.ex @@ -60,6 +60,8 @@ defmodule Stops.Stop do zone: String.t() | nil } + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + defimpl Util.Position do def latitude(stop), do: stop.latitude def longitude(stop), do: stop.longitude @@ -86,7 +88,7 @@ defmodule Stops.Stop do """ @spec has_zone?(t | id_t) :: boolean def has_zone?(<>) do - case Stops.Repo.get(id) do + case @stops_repo.get(id) do nil -> false stop -> has_zone?(stop) end diff --git a/lib/trip_info.ex b/lib/trip_info.ex index fafb76766c..dbf780d1ab 100644 --- a/lib/trip_info.ex +++ b/lib/trip_info.ex @@ -2,6 +2,8 @@ defmodule TripInfo do require Routes.Route alias Fares.OneWay + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @moduledoc """ Wraps the important information about a trip. @@ -56,7 +58,7 @@ defmodule TripInfo do starting_stop_ids = if opts[:vehicle] do - vehicle_stop = Stops.Repo.get_parent(opts[:vehicle].stop_id) + vehicle_stop = @stops_repo.get_parent(opts[:vehicle].stop_id) [origin_id, if(vehicle_stop, do: Map.get(vehicle_stop, :id))] else [origin_id] diff --git a/lib/trip_plan/transfer.ex b/lib/trip_plan/transfer.ex index c705dbe872..78b07d8ebb 100644 --- a/lib/trip_plan/transfer.ex +++ b/lib/trip_plan/transfer.ex @@ -8,6 +8,8 @@ defmodule TripPlan.Transfer do """ alias TripPlan.{Leg, NamedPosition, TransitDetail} + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + # Paying a single-ride fare for the first may get you a transfer to the second # (can't be certain, as it depends on media used)! @single_ride_transfers %{ @@ -98,8 +100,8 @@ defmodule TripPlan.Transfer do def bus_to_subway_transfer?(_), do: false defp same_station?(from_stop, to_stop) do - to_parent_stop = Stops.Repo.get_parent(to_stop) - from_parent_stop = Stops.Repo.get_parent(from_stop) + to_parent_stop = @stops_repo.get_parent(to_stop) + from_parent_stop = @stops_repo.get_parent(from_stop) cond do is_nil(to_parent_stop) or is_nil(from_parent_stop) -> diff --git a/lib/vehicle_helpers.ex b/lib/vehicle_helpers.ex index 12904dcc02..3ff2fdabbb 100644 --- a/lib/vehicle_helpers.ex +++ b/lib/vehicle_helpers.ex @@ -11,6 +11,8 @@ defmodule VehicleHelpers do import Routes.Route, only: [vehicle_name: 1] + @stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops] + @type tooltip_index_key :: {Trip.id_t() | nil, Stop.id_t()} | Stop.id_t() @type tooltip_index :: %{ optional({Trip.id_t() | nil, Stop.id_t()}) => VehicleTooltip.t(), @@ -41,7 +43,7 @@ defmodule VehicleHelpers do {nil, nil} end - stop_name = Stops.Repo.get(vehicle.stop_id) |> stop_name() + stop_name = @stops_repo.get(vehicle.stop_id) |> stop_name() tooltip = %VehicleTooltip{ vehicle: vehicle, diff --git a/test/support/mocks.ex b/test/support/mocks.ex index e1e001f7f6..9a7a668e10 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -11,4 +11,5 @@ Mox.defmock(Dotcom.Redix.PubSub.Mock, for: Dotcom.Redix.PubSub.Behaviour) Mox.defmock(MBTA.Api.Mock, for: MBTA.Api.Behaviour) Mox.defmock(RoutePatterns.Repo.Mock, for: RoutePatterns.Repo.Behaviour) +Mox.defmock(Stops.Repo.Mock, for: Stops.Repo.Behaviour) Mox.defmock(OpenTripPlannerClient.Mock, for: OpenTripPlannerClient.Behaviour)