Skip to content

Commit

Permalink
fix(Store.Predictions): Only remove trips if there are no other predi…
Browse files Browse the repository at this point in the history
…ctions (#217)

* chore: cleanup prediction store trip logs

* fix(Store.Predictions): Only remove trips if there are no other predictions for that trip
  • Loading branch information
KaylaBrady authored Oct 16, 2024
1 parent df3742b commit 79abefe
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
25 changes: 19 additions & 6 deletions lib/mbta_v3_api/store/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule MBTAV3API.Store.Predictions.Impl do
Store.timed_fetch(
@trips_table_name,
trip_match_specs,
"fetch_keys=#{inspect(trip_fetch_keys_list)}"
"trip_count=#{inspect(length(trip_fetch_keys_list))}"
)

vehicles =
Expand Down Expand Up @@ -189,12 +189,25 @@ defmodule MBTAV3API.Store.Predictions.Impl do
@impl true
def process_remove(references) do
for reference <- references do
Logger.info("#{__MODULE__} process_remove #{inspect(reference)}")

case reference do
%{type: "prediction", id: id} -> :ets.delete(@predictions_table_name, id)
%{type: "trip", id: id} -> :ets.delete(@trips_table_name, id)
_ -> :ok
%{type: "prediction", id: id} ->
Logger.info("#{__MODULE__} process_remove type=prediction id=#{id}")
:ets.delete(@predictions_table_name, id)

%{type: "trip", id: id} ->
exsiting_predictions_for_trip = fetch(trip_id: id)

if Enum.empty?(exsiting_predictions_for_trip) do
Logger.info("#{__MODULE__} process_remove type=trip id=#{id}")
:ets.delete(@trips_table_name, id)
else
Logger.info(
"#{__MODULE__} process_remove skipping removal type=trip id=#{id} remaining_prediction_count=#{length(exsiting_predictions_for_trip)}"
)
end

_ ->
:ok
end
end

Expand Down
49 changes: 47 additions & 2 deletions test/mbta_v3_api/store/predictions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,58 @@ defmodule MBTAV3API.Store.PredictionsTest do
])
end)

assert msg =~ "process_remove %MBTAV3API.JsonApi.Reference{type: \"prediction\", id: \"1\"}"
assert msg =~ "process_remove %MBTAV3API.JsonApi.Reference{type: \"trip\", id: \"trip_1\"}"
assert msg =~ "process_remove type=prediction id=1"
assert msg =~ "process_remove type=trip id=trip_1"

assert JsonApi.Object.to_full_map([prediction_2, trip_2]) ==
Store.Predictions.fetch_with_associations(stop_id: "12345")
end

@tag :capture_log
test "process_remove when trip still has predictions on another route, trip not deleted", %{
prediction_1: prediction_1,
prediction_2: prediction_2,
trip_1: trip_1,
trip_2: trip_2
} do
set_log_level(:info)

trip_1_prediction_other_route = %{
prediction_1
| id: "other_prediction_trip_1",
route_id: "OTHER_ROUTE"
}

Store.Predictions.process_upsert(:add, [
prediction_1,
prediction_2,
trip_1_prediction_other_route,
trip_1,
trip_2
])

msg =
capture_log([level: :info], fn ->
Store.Predictions.process_remove([
%Reference{type: "prediction", id: prediction_1.id},
%Reference{type: "trip", id: trip_1.id}
])
end)

assert msg =~ "process_remove type=prediction id=1"

assert msg =~
"process_remove skipping removal type=trip id=trip_1 remaining_prediction_count=1"

assert JsonApi.Object.to_full_map([
prediction_2,
trip_1_prediction_other_route,
trip_1,
trip_2
]) ==
Store.Predictions.fetch_with_associations(stop_id: "12345")
end

test "process_reset" do
prediction_66 =
build(:prediction, id: "1", stop_id: "12345", route_id: "66", trip_id: "trip_1")
Expand Down

0 comments on commit 79abefe

Please sign in to comment.