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 4e45d61 commit f780739
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +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,
stops_to_routes_enabled: false,
dup_headsign_replacements: %{
"Test 1" => "T1"
},
Expand Down
10 changes: 3 additions & 7 deletions lib/screens/alerts/cache/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Screens.Alerts.Cache.Filter do
@moduledoc """
Logic to apply filters to a list of `Screens.Alerts.Alert` structs.
"""
alias Screens.Stops.Stop
alias Screens.Stops.StopsToRoutes

@default_activities ~w[BOARD EXIT RIDE]

Expand Down Expand Up @@ -87,10 +87,10 @@ defmodule Screens.Alerts.Cache.Filter do
end

defp build_matcher({:stops, values}, acc) when is_list(values) do
{:ok, routes} = stop_mod().fetch_routes_for_stops(values)
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 @@ -126,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 stop_mod do
Application.get_env(:screens, :alerts_cache_filter_stops_mod, Stop)
end
end
6 changes: 5 additions & 1 deletion 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 Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions test/screens/alerts/alert_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ defmodule Screens.Alerts.AlertTest do

describe "fetch_from_cache/2" do
setup do
default_stops_mod = Application.get_env(:screens, :alerts_cache_filter_stops_mod)
Application.put_env(:screens, :alerts_cache_filter_stops_mod, Stop.Mock)
default_stops_mod = Application.get_env(:screens, :stops_to_routes_stop_mod)
Application.put_env(:screens, :stops_to_routes_stop_mod, Stop.Mock)

on_exit(fn ->
Application.put_env(:screens, :alerts_cache_filter_stops_mod, default_stops_mod)
Application.put_env(:screens, :stops_to_routes_stop_mod, default_stops_mod)
end)

alerts = [
Expand Down
8 changes: 4 additions & 4 deletions test/screens/alerts/cache/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ defmodule Screens.Alerts.Cache.FilterTest do

describe "build_matchers/1" do
setup do
default_stops_mod = Application.get_env(:screens, :alerts_cache_filter_stops_mod)
Application.put_env(:screens, :alerts_cache_filter_stops_mod, Stop.Mock)
default_stops_mod = Application.get_env(:screens, :stops_to_routes_stop_mod)
Application.put_env(:screens, :stops_to_routes_stop_mod, Stop.Mock)

on_exit(fn ->
Application.put_env(:screens, :alerts_cache_filter_stops_mod, default_stops_mod)
Application.put_env(:screens, :stops_to_routes_stop_mod, default_stops_mod)
end)
end

Expand Down Expand Up @@ -51,7 +51,7 @@ defmodule Screens.Alerts.Cache.FilterTest do

test "adds matchers for routes at the stops" do
stub(Stop.Mock, :fetch_routes_for_stops, fn
["place-aport"] ->
"place-aport" ->
{:ok, [%Route{id: "Blue"}, %Route{id: "743"}]}
end)

Expand Down

0 comments on commit f780739

Please sign in to comment.