Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bfauble committed Apr 30, 2024
1 parent f8a7f0f commit c054aca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
33 changes: 17 additions & 16 deletions lib/concentrate/group_filter/suppress_stop_time_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,40 @@ defmodule Concentrate.GroupFilter.SuppressStopTimeUpdate do

@impl Concentrate.GroupFilter
def filter({td, vps, stus}) do
case StopPredictionStatus.flagged_stops_on_route(td) do
route_id = TripDescriptor.route_id(td)
direction_id = TripDescriptor.direction_id(td)

case StopPredictionStatus.flagged_stops_on_route(route_id, direction_id) do
nil ->
{td, vps, stus}

suppressed_stops ->
route_id = TripDescriptor.route_id(td)
direction_id = TripDescriptor.direction_id(td)

unsuppressed_stus =
Enum.reject(stus, fn stu ->
{suppressed_stus, unsuppressed_stus} =
Enum.split_with(stus, fn stu ->
stop_id_suppressed?(
suppressed_stops,
StopTimeUpdate.stop_id(stu),
route_id,
direction_id
StopTimeUpdate.stop_id(stu)
)
end)

_ = log_suppressed_stus(suppressed_stus, route_id, direction_id)

{td, vps, unsuppressed_stus}
end
end

def filter(other), do: other

defp stop_id_suppressed?(suppressed_stops, stop_id, route_id, direction_id) do
if MapSet.member?(suppressed_stops, stop_id) do
defp stop_id_suppressed?(suppressed_stops, stop_id),
do: MapSet.member?(suppressed_stops, stop_id)

defp log_suppressed_stus(stus, route_id, direction_id) do
Enum.map(stus, fn stu ->
stop_id = StopTimeUpdate.stop_id(stu)

Logger.info(fn ->
"Predictions for stop_id=#{stop_id} route_id=#{route_id} direction_id=#{direction_id} have been suppressed based on RTS feed trigger"
end)

true
else
false
end
end)
end
end
12 changes: 4 additions & 8 deletions lib/concentrate/parser/stop_prediction_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ defmodule Concentrate.Parser.StopPredictionStatus do
filter out StopTimeUpdate structs for that combination.
"""

alias Concentrate.TripDescriptor

@spec flagged_stops_on_route(TripDescriptor.t()) :: nil | MapSet.t()
def flagged_stops_on_route(%TripDescriptor{} = td) do
route_id = TripDescriptor.route_id(td)
direction_id = TripDescriptor.direction_id(td)

@spec flagged_stops_on_route(binary() | integer(), 0 | 1) :: nil | MapSet.t()
def flagged_stops_on_route(route_id, direction_id)
when not is_nil(route_id) and direction_id in [0, 1] do
if route_id != nil and direction_id != nil do
# TEMP: temporary test data
MapSet.new([123])
Expand All @@ -19,5 +15,5 @@ defmodule Concentrate.Parser.StopPredictionStatus do
end
end

def flagged_stops_on_route(_), do: nil
def flagged_stops_on_route(_, _), do: nil
end
21 changes: 4 additions & 17 deletions test/concentrate/parser/stop_prediction_status_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@ defmodule Concentrate.Parser.StopPredictionStatusTest do
@moduledoc false
use ExUnit.Case
alias Concentrate.Parser.StopPredictionStatus
alias Concentrate.TripDescriptor

describe "flagged_stops_on_route" do
describe "flagged_stops_on_route/2" do
test "retuns a MapSet of stop_ids relevant to the route_id and direction_id provided" do
assert MapSet.new([123]) ==
StopPredictionStatus.flagged_stops_on_route(%TripDescriptor{
route_id: "Red",
direction_id: 0
})
assert MapSet.new([123]) == StopPredictionStatus.flagged_stops_on_route("Red", 0)
end

test "returns nil if missing stop_id or direction_id" do
assert nil ==
StopPredictionStatus.flagged_stops_on_route(%TripDescriptor{
route_id: nil,
direction_id: 1
})
assert nil == StopPredictionStatus.flagged_stops_on_route(nil, 1)

assert nil ==
StopPredictionStatus.flagged_stops_on_route(%TripDescriptor{
route_id: "Red",
direction_id: nil
})
assert nil == StopPredictionStatus.flagged_stops_on_route("Red", nil)
end
end
end

0 comments on commit c054aca

Please sign in to comment.