Skip to content

Commit

Permalink
feat: use stop to route cache in alerts filter expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
sloanelybutsurely committed Aug 15, 2024
1 parent ddf1f66 commit cbf1d8a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
16 changes: 3 additions & 13 deletions lib/screens/alerts/cache/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 9 additions & 5 deletions lib/screens/stops/stops_to_routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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}
Expand All @@ -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
Expand All @@ -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

0 comments on commit cbf1d8a

Please sign in to comment.