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 16, 2024
1 parent 99cc65e commit 34b99eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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
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,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]

Expand Down Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions lib/screens/stops/stops_to_routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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}
Expand All @@ -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()
Expand Down

0 comments on commit 34b99eb

Please sign in to comment.