From cbf1d8a97edbb0c091e2d98e9f7c585266d662e9 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/test.exs | 3 ++- lib/screens/alerts/cache/filter.ex | 16 +++------------- lib/screens/stops/stops_to_routes.ex | 14 +++++++++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/config/test.exs b/config/test.exs index 5c16c6153..1ab890c74 100644 --- a/config/test.exs +++ b/config/test.exs @@ -26,7 +26,8 @@ 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_routes_mod: Screens.Routes.Route.Mock, + stops_to_routes_enabled: false, + stops_to_routes_route_mod: Screens.Routes.Route.Mock, dup_headsign_replacements: %{ "Test 1" => "T1" }, diff --git a/lib/screens/alerts/cache/filter.ex b/lib/screens/alerts/cache/filter.ex index 2d516eb66..1d8df5685 100644 --- a/lib/screens/alerts/cache/filter.ex +++ b/lib/screens/alerts/cache/filter.ex @@ -2,6 +2,7 @@ defmodule Screens.Alerts.Cache.Filter do @moduledoc """ Logic to apply filters to a list of `Screens.Alerts.Alert` structs. """ + alias Screens.Stops.StopsToRoutes @default_activities ~w[BOARD EXIT RIDE] @@ -86,17 +87,10 @@ defmodule Screens.Alerts.Cache.Filter do end defp build_matcher({:stops, values}, acc) when is_list(values) do - route_mod = route_mod() - - 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 @@ -132,8 +126,4 @@ defmodule Screens.Alerts.Cache.Filter do defp matches?(alert, {key, value}) do Enum.any?(alert.informed_entities, &(Map.get(&1, key) == value)) end - - defp route_mod do - Application.get_env(:screens, :alerts_cache_filter_routes_mod, Route) - end end diff --git a/lib/screens/stops/stops_to_routes.ex b/lib/screens/stops/stops_to_routes.ex index 73d4f5f51..363071737 100644 --- a/lib/screens/stops/stops_to_routes.ex +++ b/lib/screens/stops/stops_to_routes.ex @@ -11,7 +11,7 @@ defmodule Screens.Stops.StopsToRoutes do @spec stops_to_routes([stop_id :: String.t()]) :: [route_id :: String.t()] def stops_to_routes(stop_ids) do - from_cache = get_all(stop_ids) + from_cache = if enabled?(), do: get_all(stop_ids), else: %{} missing_stop_ids = stop_ids -- Map.keys(from_cache) from_api = @@ -20,7 +20,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} = fetch_routes_for_stop(stop_id) route_ids = Enum.map(routes, & &1.id) {stop_id, route_ids} @@ -37,9 +37,9 @@ 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) + defp fetch_routes_for_stop(stop_id) do + route_impl = Application.get_env(:screens, :stops_to_routes_route_mod, Screens.Routes.Route) + route_impl.serving_stop(stop_id) end defp ungroup_values(map) do @@ -54,4 +54,8 @@ defmodule Screens.Stops.StopsToRoutes do additional_minutes = :rand.uniform(30) @base_ttl + :timer.minutes(additional_minutes) end + + defp enabled? do + Application.get_env(:screens, :stops_to_routes_enabled, true) + end end