-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Make trip channel pull from ETS predictions #235
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
82cc9b7
feat: Create new trip v2 channel
EmmaSimon 3a104b4
test: Add tests for trip v2 predictions channel
EmmaSimon 0cc1e5d
refactor: Replace existing channel
EmmaSimon 8f90d4b
fix: Address PR feedback
EmmaSimon 55fb549
fix: Add missing field to typespec
EmmaSimon 5822b47
Merge branch 'main' into es-trip-v2
EmmaSimon 80556ac
fix: Fix type for dialyzer
EmmaSimon f61303b
fix: Broken test
EmmaSimon d632fb3
Merge branch 'main' into es-trip-v2
EmmaSimon bb38ec5
Merge branch 'main' into es-trip-v2
EmmaSimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 31 additions & 57 deletions
88
lib/mobile_app_backend_web/channels/predictions_for_trip_channel.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,48 @@ | ||
defmodule MobileAppBackendWeb.PredictionsForTripChannel do | ||
use MobileAppBackendWeb, :channel | ||
|
||
alias MBTAV3API.JsonApi | ||
alias MBTAV3API.Prediction | ||
|
||
@throttle_ms 500 | ||
require Logger | ||
|
||
@impl true | ||
def join("predictions:trip:" <> trip_id, _payload, socket) do | ||
{:ok, throttler} = | ||
MobileAppBackend.Throttler.start_link( | ||
target: self(), | ||
cast: :send_data, | ||
ms: @throttle_ms | ||
) | ||
|
||
{:ok, %{data: [trip]}} = MBTAV3API.Repository.trips(filter: [id: trip_id]) | ||
|
||
route_id = trip.route_id | ||
|
||
{:ok, data} = MBTAV3API.Stream.StaticInstance.subscribe("predictions:route:#{route_id}") | ||
if trip_id == "" do | ||
{:error, %{code: :no_trip_id}} | ||
else | ||
subscribe(trip_id, socket) | ||
end | ||
end | ||
|
||
data = filter_data(data, trip_id) | ||
defp subscribe(trip_id, socket) do | ||
pubsub_module = | ||
Application.get_env( | ||
:mobile_app_backend, | ||
MobileAppBackend.Predictions.PubSub, | ||
MobileAppBackend.Predictions.PubSub | ||
) | ||
|
||
{:ok, data, assign(socket, data: data, trip_id: trip_id, throttler: throttler)} | ||
end | ||
case :timer.tc(fn -> pubsub_module.subscribe_for_trip(trip_id) end) do | ||
{time_micros, :error} -> | ||
Logger.warning("#{__MODULE__} failed join duration=#{time_micros / 1000}") | ||
{:error, %{code: :subscribe_failed}} | ||
|
||
@impl true | ||
def handle_info({:stream_data, "predictions:route:" <> _route_id, data}, socket) do | ||
old_data = socket.assigns.data | ||
new_data = filter_data(data, socket.assigns.trip_id) | ||
{time_micros, initial_data} -> | ||
Logger.info("#{__MODULE__} join duration=#{time_micros / 1000}") | ||
|
||
if old_data != new_data do | ||
MobileAppBackend.Throttler.request(socket.assigns.throttler) | ||
{:ok, initial_data, socket} | ||
end | ||
|
||
socket = assign(socket, data: new_data) | ||
{:noreply, socket} | ||
end | ||
|
||
@impl true | ||
def handle_cast(:send_data, socket) do | ||
:ok = push(socket, "stream_data", socket.assigns.data) | ||
{:noreply, socket} | ||
end | ||
@spec handle_info({:new_predictions, any()}, Phoenix.Socket.t()) :: | ||
{:noreply, Phoenix.Socket.t()} | ||
def handle_info({:new_predictions, new_predictions_for_trip}, socket) do | ||
{time_micros, _result} = | ||
:timer.tc(fn -> | ||
:ok = push(socket, "stream_data", new_predictions_for_trip) | ||
end) | ||
|
||
@doc """ | ||
Filters the given data to predictions that are at one of the listed stops and the associated trips and vehicles. | ||
""" | ||
@spec filter_data(JsonApi.Object.full_map(), String.t()) :: JsonApi.Object.full_map() | ||
def filter_data(route_data, trip_id) do | ||
%{predictions: predictions, vehicle_ids: vehicle_ids} = | ||
for {_, %Prediction{} = prediction} <- route_data.predictions, | ||
reduce: %{predictions: %{}, vehicle_ids: []} do | ||
%{predictions: predictions, vehicle_ids: vehicle_ids} -> | ||
if prediction.trip_id == trip_id do | ||
%{ | ||
predictions: Map.put(predictions, prediction.id, prediction), | ||
vehicle_ids: [prediction.vehicle_id | vehicle_ids] | ||
} | ||
else | ||
%{predictions: predictions, vehicle_ids: vehicle_ids} | ||
end | ||
end | ||
Logger.info("#{__MODULE__} push duration=#{time_micros / 1000}") | ||
|
||
%{ | ||
JsonApi.Object.to_full_map([]) | ||
| predictions: predictions, | ||
trips: Map.take(route_data.trips, [trip_id]), | ||
vehicles: Map.take(route_data.vehicles, vehicle_ids) | ||
} | ||
require Logger | ||
EmmaSimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{:noreply, socket} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion(non-blocking): add the new
trip_id
field hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand what you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a trip_id field added to the map here, I think it would be worth adding to the typespec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, okay yes, updated