From 34b99eb438a14ea7e8320e0870c96f677e1f2ba3 Mon Sep 17 00:00:00 2001 From: sloane Date: Thu, 15 Aug 2024 10:32:49 -0400 Subject: [PATCH] feat: use stop to route cache in alerts filter expansion --- config/config.exs | 2 ++ config/test.exs | 4 +++- lib/screens/alerts/cache/filter.ex | 16 +++------------- lib/screens/stops/stops_to_routes.ex | 11 ++++------- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/config/config.exs b/config/config.exs index 5644301b5..762e36f1f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -485,6 +485,8 @@ config :screens, Screens.ScreenApiResponseCache, gc_interval: :timer.hours(1), allocated_memory: 250_000_000 +config :screens, Screens.Stops.StopsToRoutes, adapter: Nebulex.Adapters.Local + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/config/test.exs b/config/test.exs index edd76db44..8223d7557 100644 --- a/config/test.exs +++ b/config/test.exs @@ -26,7 +26,7 @@ config :screens, blue_bikes_station_information_url: [:no_api_requests_allowed_during_testing], blue_bikes_station_status_url: [:no_api_requests_allowed_during_testing], blue_bikes_api_client: Screens.BlueBikes.FakeClient, - alerts_cache_filter_route_mod: Screens.Routes.Route.Mock, + stops_to_routes_route_mod: Screens.Routes.Route.Mock, dup_headsign_replacements: %{ "Test 1" => "T1" }, @@ -161,3 +161,5 @@ config :screens, :screens_by_alert, screens_by_alert_ttl_seconds: 2, screens_last_updated_ttl_seconds: 2, screens_ttl_seconds: 1 + +config :screens, Screens.Stops.StopsToRoutes, adapter: Nebulex.Adapters.Nil diff --git a/lib/screens/alerts/cache/filter.ex b/lib/screens/alerts/cache/filter.ex index 8f8ddff73..1d8df5685 100644 --- a/lib/screens/alerts/cache/filter.ex +++ b/lib/screens/alerts/cache/filter.ex @@ -2,12 +2,7 @@ defmodule Screens.Alerts.Cache.Filter do @moduledoc """ Logic to apply filters to a list of `Screens.Alerts.Alert` structs. """ - - @route_mod Application.compile_env( - :screens, - :alerts_cache_filter_route_mod, - Screens.Routes.Route - ) + alias Screens.Stops.StopsToRoutes @default_activities ~w[BOARD EXIT RIDE] @@ -92,15 +87,10 @@ defmodule Screens.Alerts.Cache.Filter do end defp build_matcher({:stops, values}, acc) when is_list(values) do - routes = - values - |> Enum.flat_map(fn stop_id -> - {:ok, routes} = @route_mod.serving_stop(stop_id) - routes - end) + route_ids = StopsToRoutes.stops_to_routes(values) route_matchers = - for %{id: route_id} <- routes, + for route_id <- route_ids, stop_id <- [nil | values] do %{route: route_id, stop: stop_id} end diff --git a/lib/screens/stops/stops_to_routes.ex b/lib/screens/stops/stops_to_routes.ex index 73d4f5f51..f89bd3783 100644 --- a/lib/screens/stops/stops_to_routes.ex +++ b/lib/screens/stops/stops_to_routes.ex @@ -5,7 +5,9 @@ defmodule Screens.Stops.StopsToRoutes do """ use Nebulex.Cache, otp_app: :screens, - adapter: Nebulex.Adapters.Local + adapter: Application.compile_env(:screens, [__MODULE__, :adapter]) + + @route_mod Application.compile_env(:screens, :stops_to_routes_route_mod, Screens.Routes.Route) @base_ttl :timer.hours(1) @@ -20,7 +22,7 @@ defmodule Screens.Stops.StopsToRoutes do else from_api = for stop_id <- missing_stop_ids, into: %{} do - {:ok, routes} = fetch_routes_for_stops(stop_id) + {:ok, routes} = @route_mod.serving_stop(stop_id) route_ids = Enum.map(routes, & &1.id) {stop_id, route_ids} @@ -37,11 +39,6 @@ defmodule Screens.Stops.StopsToRoutes do |> Enum.uniq() end - defp fetch_routes_for_stops(stop_ids) do - stop_impl = Application.get_env(:screens, :stops_to_routes_stop_mod, Screens.Stops.Stop) - stop_impl.fetch_routes_for_stops(stop_ids) - end - defp ungroup_values(map) do map |> Map.values()