-
Notifications
You must be signed in to change notification settings - Fork 2
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
Migrate from RTR to GTFS-rt #810
Merged
Merged
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
d9c11dc
Migrate to GTFS-rt predictions and locations feeds
PaulJKim 5419995
PR feedback
PaulJKim f03d8a7
simplify abstraction
PaulJKim 948020e
Update .envrc.template
PaulJKim cae438f
Merge branch 'main' into pk/migrate-from-rtr-to-gtfs-rt
PaulJKim 8251ca9
Filter out past departures and adjust last trip filtering
PaulJKim 90f4da1
Merge branch 'pk/migrate-from-rtr-to-gtfs-rt' of https://github.com/m…
PaulJKim 099efbe
Remove some references to RTR
PaulJKim a22e752
Move route_id check to a shared helper
PaulJKim 8852743
Log prediction details for terminal predictions
PaulJKim 5903d3f
add inspects
PaulJKim 1726597
add inspect
PaulJKim 41f3b10
fix typo
PaulJKim 28a000b
only log when seconds til boarding is under terminal brd seconds
PaulJKim d76733e
Add some logging to stops after terminals
PaulJKim f7e550f
Try adding a buffer to account for potential latency between RTR and …
PaulJKim 25f59bd
Log more details
PaulJKim c3e0837
Log stopped_at_predicted_stop
PaulJKim 0f11d6b
Merge branch 'main' into pk/migrate-from-rtr-to-gtfs-rt
PaulJKim a67c8c3
increase buffer to account for negative departures
PaulJKim 7fe2370
Account for skipped predictions when calculating seconds_until_passth…
PaulJKim 135c50a
Add filter for determining if prediction has passed
PaulJKim b005b04
Remove extra logging
PaulJKim 429bb18
read passthrough_time field directly
PaulJKim 18ee9ff
oops forgot the actual variable
PaulJKim a4db738
don't fiter out passthroughs
PaulJKim 9ca909b
use the right field
PaulJKim ef91775
Merge branch 'main' into pk/migrate-from-rtr-to-gtfs-rt
PaulJKim 442d955
remove stops_away
PaulJKim 79d976d
Remove required concentrate urls from envrc template
PaulJKim 1974248
Merge branch 'main' into pk/migrate-from-rtr-to-gtfs-rt
PaulJKim 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
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
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 |
---|---|---|
|
@@ -9,23 +9,28 @@ defmodule Predictions.Predictions do | |
def get_all(feed_message, current_time) do | ||
predictions = | ||
feed_message["entity"] | ||
|> Enum.map(& &1["trip_update"]) | ||
|> Enum.reject(&(&1["trip"]["schedule_relationship"] == "CANCELED")) | ||
|> Enum.flat_map(&transform_stop_time_updates/1) | ||
|> Enum.filter(fn {update, _, _, _, _, _, _} -> | ||
((update["arrival"] || update["departure"]) && | ||
not is_nil(update["stops_away"])) || update["passthrough_time"] | ||
|> Stream.map(& &1["trip_update"]) | ||
|> Stream.filter( | ||
&(relevant_rail_route?(&1["trip"]["route_id"]) and | ||
&1["trip"]["schedule_relationship"] != "CANCELED") | ||
) | ||
|> Stream.flat_map(&transform_stop_time_updates/1) | ||
|> Stream.filter(fn {update, _, _, _, _, _, _} -> | ||
(update["arrival"] && update["arrival"]["uncertainty"]) || | ||
(update["departure"] && update["departure"]["uncertainty"]) || | ||
update["passthrough_time"] | ||
end) | ||
|> Enum.map(&prediction_from_update(&1, current_time)) | ||
|> Stream.map(&prediction_from_update(&1, current_time)) | ||
|> Enum.reject( | ||
&(is_nil(&1.seconds_until_arrival) and is_nil(&1.seconds_until_departure) and | ||
is_nil(&1.seconds_until_passthrough)) | ||
&((is_nil(&1.seconds_until_arrival) and is_nil(&1.seconds_until_departure) and | ||
is_nil(&1.seconds_until_passthrough)) or | ||
has_departed?(&1)) | ||
) | ||
|
||
vehicles_running_revenue_trips = | ||
predictions | ||
|> Enum.filter(& &1.revenue_trip?) | ||
|> Enum.map(& &1.vehicle_id) | ||
|> Stream.filter(& &1.revenue_trip?) | ||
|> Stream.map(& &1.vehicle_id) | ||
|> MapSet.new() | ||
|
||
{Enum.group_by(predictions, fn prediction -> | ||
|
@@ -43,15 +48,12 @@ defmodule Predictions.Predictions do | |
end) | ||
|> Map.get("stop_id") | ||
|
||
revenue_trip? = | ||
Enum.any?(trip_update["stop_time_update"], &(&1["schedule_relationship"] != "SKIPPED")) | ||
|
||
vehicle_id = get_in(trip_update, ["vehicle", "id"]) | ||
|
||
Enum.map( | ||
trip_update["stop_time_update"], | ||
&{&1, last_stop_id, trip_update["trip"]["route_id"], trip_update["trip"]["direction_id"], | ||
trip_update["trip"]["trip_id"], revenue_trip?, vehicle_id} | ||
trip_update["trip"]["trip_id"], trip_update["trip"]["revenue"], vehicle_id} | ||
) | ||
end | ||
|
||
|
@@ -67,6 +69,9 @@ defmodule Predictions.Predictions do | |
) do | ||
current_time_seconds = DateTime.to_unix(current_time) | ||
|
||
schedule_relationship = | ||
translate_schedule_relationship(stop_time_update["schedule_relationship"]) | ||
|
||
seconds_until_arrival = | ||
if stop_time_update["arrival"] && | ||
sufficient_certainty?(stop_time_update["arrival"], route_id), | ||
|
@@ -84,21 +89,23 @@ defmodule Predictions.Predictions do | |
do: stop_time_update["passthrough_time"] - current_time_seconds, | ||
else: nil | ||
|
||
vehicle_location = Engine.Locations.for_vehicle(vehicle_id) | ||
|
||
%Prediction{ | ||
stop_id: stop_time_update["stop_id"], | ||
direction_id: direction_id, | ||
seconds_until_arrival: max(0, seconds_until_arrival), | ||
arrival_certainty: stop_time_update["arrival"]["uncertainty"], | ||
seconds_until_departure: max(0, seconds_until_departure), | ||
seconds_until_departure: seconds_until_departure, | ||
departure_certainty: stop_time_update["departure"]["uncertainty"], | ||
seconds_until_passthrough: max(0, seconds_until_passthrough), | ||
schedule_relationship: | ||
translate_schedule_relationship(stop_time_update["schedule_relationship"]), | ||
schedule_relationship: schedule_relationship, | ||
route_id: route_id, | ||
trip_id: trip_id, | ||
destination_stop_id: last_stop_id, | ||
stopped?: stop_time_update["stopped?"], | ||
stops_away: stop_time_update["stops_away"], | ||
stopped_at_predicted_stop?: | ||
not is_nil(vehicle_location) and vehicle_location.status == :stopped_at and | ||
stop_time_update["stop_id"] == vehicle_location.stop_id, | ||
boarding_status: stop_time_update["boarding_status"], | ||
revenue_trip?: revenue_trip?, | ||
vehicle_id: vehicle_id | ||
|
@@ -113,6 +120,19 @@ defmodule Predictions.Predictions do | |
Jason.decode!(body) | ||
end | ||
|
||
def relevant_rail_route?(route_id) do | ||
route_id in [ | ||
"Red", | ||
"Blue", | ||
"Orange", | ||
"Green-B", | ||
"Green-C", | ||
"Green-D", | ||
"Green-E", | ||
"Mattapan" | ||
] | ||
end | ||
|
||
@spec translate_schedule_relationship(String.t()) :: :skipped | :scheduled | ||
defp translate_schedule_relationship("SKIPPED") do | ||
:skipped | ||
|
@@ -135,4 +155,10 @@ defmodule Predictions.Predictions do | |
true | ||
end | ||
end | ||
|
||
@spec has_departed?(Predictions.Prediction.t()) :: boolean() | ||
defp has_departed?(prediction) do | ||
prediction.seconds_until_departure && prediction.seconds_until_departure < 0 && | ||
not prediction.stopped_at_predicted_stop? | ||
end | ||
Comment on lines
+160
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flagging for review: This is new logic since the first attempt at migrating for more accurately filtering out departed trains. |
||
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
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.
Just a drive-by note, these have "official" public-facing URLs which are behind a CDN. We ended up using these in Screens LTOTD tracking rather than these internal S3 bucket URLs.
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.
Sounds good to me, thanks for calling that out. Does Screens LTOTD also use dev/dev-blue versions of the CDN URLs as well?
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.
Only the production feeds have CDN URLs as far as I know — if we're fetching a non-prod feed those will probably have to still use S3 bucket URLs.