From 9d5d60873954091569af8e5d22b9f8af3ad6eb98 Mon Sep 17 00:00:00 2001 From: Paul Kim Date: Mon, 25 Nov 2024 13:21:05 -0500 Subject: [PATCH] Migrate from RTR to GTFS-rt (#810) * Migrate to GTFS-rt predictions and locations feeds * PR feedback * simplify abstraction * Update .envrc.template Co-authored-by: Brett Heath-Wlaz * Filter out past departures and adjust last trip filtering * Remove some references to RTR * Move route_id check to a shared helper * Log prediction details for terminal predictions * add inspects * add inspect * fix typo * only log when seconds til boarding is under terminal brd seconds * Add some logging to stops after terminals * Try adding a buffer to account for potential latency between RTR and concentrate * Log more details * Log stopped_at_predicted_stop * increase buffer to account for negative departures * Account for skipped predictions when calculating seconds_until_passthrough * Add filter for determining if prediction has passed * Remove extra logging * read passthrough_time field directly * oops forgot the actual variable * don't fiter out passthroughs * use the right field * remove stops_away * Remove required concentrate urls from envrc template --------- Co-authored-by: Brett Heath-Wlaz --- .envrc.template | 7 +- config/runtime.exs | 4 +- lib/content/message/predictions.ex | 3 +- lib/fake/httpoison.ex | 21 +-- lib/predictions/last_trip.ex | 7 +- lib/predictions/prediction.ex | 6 +- lib/predictions/predictions.ex | 66 +++++-- lib/signs/utilities/predictions.ex | 7 +- test/content/messages/predictions_test.exs | 47 +---- test/predictions/predictions_test.exs | 201 ++++----------------- test/signs/realtime_test.exs | 29 +-- 11 files changed, 125 insertions(+), 273 deletions(-) diff --git a/.envrc.template b/.envrc.template index 4cb6df709..9414c3d7a 100644 --- a/.envrc.template +++ b/.envrc.template @@ -17,7 +17,6 @@ export API_V3_URL=https://api-dev-green.mbtace.com #export CHELSEA_BRIDGE_URL= #export CHELSEA_BRIDGE_AUTH= -# URLs of the enhanced trip-update and vehicle-position feeds. Default to the real feed URLs if -# not set here. -#export TRIP_UPDATE_URL= -#export VEHICLE_POSITIONS_URL= +# URLs of the enhanced trip-update and vehicle-position feeds. +#export TRIP_UPDATE_URL="https://s3.amazonaws.com/mbta-gtfs-s3/concentrate/TripUpdates_enhanced.json" +#export VEHICLE_POSITIONS_URL="https://s3.amazonaws.com/mbta-gtfs-s3/concentrate/VehiclePositions_enhanced.json" diff --git a/config/runtime.exs b/config/runtime.exs index 27778360f..4d1fc5b5b 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -12,12 +12,12 @@ if config_env() != :test do trip_update_url: System.get_env( "TRIP_UPDATE_URL", - "https://s3.amazonaws.com/mbta-gtfs-s3/rtr/TripUpdates_enhanced.json" + "https://s3.amazonaws.com/mbta-gtfs-s3/concentrate/TripUpdates_enhanced.json" ), vehicle_positions_url: System.get_env( "VEHICLE_POSITIONS_URL", - "https://s3.amazonaws.com/mbta-gtfs-s3/rtr/VehiclePositions_enhanced.json" + "https://s3.amazonaws.com/mbta-gtfs-s3/concentrate/VehiclePositions_enhanced.json" ), s3_bucket: System.get_env("SIGNS_S3_BUCKET"), s3_path: System.get_env("SIGNS_S3_PATH"), diff --git a/lib/content/message/predictions.ex b/lib/content/message/predictions.ex index 0630edfed..9a2e22840 100644 --- a/lib/content/message/predictions.ex +++ b/lib/content/message/predictions.ex @@ -35,12 +35,11 @@ defmodule Content.Message.Predictions do end min = round(sec / 60) - stopped_at? = prediction.stops_away == 0 reverse_prediction? = Signs.Utilities.Predictions.reverse_prediction?(prediction, terminal?) {minutes, approximate?} = cond do - stopped_at? and (!terminal? or sec <= 30) -> {:boarding, false} + prediction.stopped_at_predicted_stop? and (!terminal? or sec <= 30) -> {:boarding, false} !terminal? and sec <= 30 -> {:arriving, false} !terminal? and sec <= 60 -> {:approaching, false} min > 60 -> {60, true} diff --git a/lib/fake/httpoison.ex b/lib/fake/httpoison.ex index be1c10d2e..b16bad2cc 100644 --- a/lib/fake/httpoison.ex +++ b/lib/fake/httpoison.ex @@ -65,15 +65,11 @@ defmodule Fake.HTTPoison do %{ "entity" => [ %{ - "alert" => nil, "id" => "1490783458_32568935", - "is_deleted" => false, "trip_update" => %{ - "delay" => nil, "stop_time_update" => [ %{ "arrival" => %{ - "delay" => nil, "time" => 1_491_570_120, "uncertainty" => nil }, @@ -84,7 +80,6 @@ defmodule Fake.HTTPoison do }, %{ "arrival" => %{ - "delay" => nil, "time" => 1_491_570_180, "uncertainty" => nil }, @@ -243,23 +238,17 @@ defmodule Fake.HTTPoison do %{ "entity" => [ %{ - "alert" => nil, "id" => "1490783458_32568935", - "is_deleted" => false, "trip_update" => %{ - "delay" => nil, "stop_time_update" => [ %{ "arrival" => %{ - "delay" => nil, "time" => 1_491_570_180, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "stop_to_update", - "stop_sequence" => 1, - "stops_away" => 0 + "stop_sequence" => 1 } ], "timestamp" => nil, @@ -269,15 +258,15 @@ defmodule Fake.HTTPoison do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "G-10040", "label" => "3260", "license_plate" => nil } - }, - "vehicle" => nil + } } ], "header" => %{ diff --git a/lib/predictions/last_trip.ex b/lib/predictions/last_trip.ex index 23d5d1987..d9adf529f 100644 --- a/lib/predictions/last_trip.ex +++ b/lib/predictions/last_trip.ex @@ -1,8 +1,13 @@ defmodule Predictions.LastTrip do + alias Predictions.Predictions + defp get_running_trips(predictions_feed) do predictions_feed["entity"] |> Stream.map(& &1["trip_update"]) - |> Enum.reject(&(&1["trip"]["schedule_relationship"] == "CANCELED")) + |> Stream.filter( + &(Predictions.relevant_rail_route?(&1["trip"]["route_id"]) and + &1["trip"]["schedule_relationship"] != "CANCELED") + ) end def get_last_trips(predictions_feed) do diff --git a/lib/predictions/prediction.ex b/lib/predictions/prediction.ex index 70b5b693a..ee9fe08f3 100644 --- a/lib/predictions/prediction.ex +++ b/lib/predictions/prediction.ex @@ -10,8 +10,7 @@ defmodule Predictions.Prediction do route_id: nil, trip_id: nil, destination_stop_id: nil, - stopped?: false, - stops_away: 0, + stopped_at_predicted_stop?: false, boarding_status: nil, revenue_trip?: true, vehicle_id: nil @@ -30,8 +29,7 @@ defmodule Predictions.Prediction do route_id: String.t(), trip_id: trip_id() | nil, destination_stop_id: String.t(), - stopped?: boolean(), - stops_away: integer(), + stopped_at_predicted_stop?: boolean(), boarding_status: String.t() | nil, revenue_trip?: boolean(), vehicle_id: String.t() | nil diff --git a/lib/predictions/predictions.ex b/lib/predictions/predictions.ex index db856514a..c0560d15d 100644 --- a/lib/predictions/predictions.ex +++ b/lib/predictions/predictions.ex @@ -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 end diff --git a/lib/signs/utilities/predictions.ex b/lib/signs/utilities/predictions.ex index 1a4ac3964..3fba621d4 100644 --- a/lib/signs/utilities/predictions.ex +++ b/lib/signs/utilities/predictions.ex @@ -34,10 +34,9 @@ defmodule Signs.Utilities.Predictions do {if terminal? do 0 else - case prediction.stops_away do - 0 -> 0 - _ -> 1 - end + if prediction.stopped_at_predicted_stop?, + do: 0, + else: 1 end, prediction.seconds_until_departure, prediction.seconds_until_arrival} end) |> filter_large_red_line_gaps() diff --git a/test/content/messages/predictions_test.exs b/test/content/messages/predictions_test.exs index 3b692f9a3..a2d22ac45 100644 --- a/test/content/messages/predictions_test.exs +++ b/test/content/messages/predictions_test.exs @@ -7,8 +7,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 0, direction_id: 1, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70261" } @@ -23,8 +21,7 @@ defmodule Content.Message.PredictionsTest do direction_id: 1, route_id: "Mattapan", destination_stop_id: "70261", - stopped?: false, - stops_away: 0, + stopped_at_predicted_stop?: true, boarding_status: "Boarding" } @@ -38,8 +35,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 30, direction_id: 0, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70275" } @@ -53,8 +48,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 31, direction_id: 0, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70275" } @@ -68,8 +61,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 61, direction_id: 0, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70275" } @@ -84,8 +75,6 @@ defmodule Content.Message.PredictionsTest do arrival_certainty: 360, direction_id: 0, route_id: "Mattapan", - stopped?: false, - stops_away: 10, destination_stop_id: "70275" } @@ -99,8 +88,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 65, direction_id: 1, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70261" } @@ -114,8 +101,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 125, direction_id: 1, route_id: "Mattapan", - stopped?: false, - stops_away: 2, destination_stop_id: "70261" } @@ -129,8 +114,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: -5, route_id: "Mattapan", direction_id: 1, - stopped?: false, - stops_away: 1, destination_stop_id: "70261" } @@ -145,8 +128,7 @@ defmodule Content.Message.PredictionsTest do route_id: "Mattapan", direction_id: 1, destination_stop_id: "70261", - stopped?: false, - stops_away: 0, + stopped_at_predicted_stop?: true, boarding_status: "Boarding" } @@ -160,8 +142,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_arrival: 91, route_id: "Mattapan", direction_id: 1, - stopped?: false, - stops_away: 2, destination_stop_id: "70261" } @@ -175,9 +155,7 @@ defmodule Content.Message.PredictionsTest do stop_id: "70086", seconds_until_arrival: 300, direction_id: 1, - route_id: "Red", - stopped?: false, - stops_away: 2 + route_id: "Red" } msg = Content.Message.Predictions.new(prediction, false, :jfk_mezzanine) @@ -193,9 +171,7 @@ defmodule Content.Message.PredictionsTest do stop_id: "70096", seconds_until_arrival: 300, direction_id: 1, - route_id: "Red", - stopped?: false, - stops_away: 2 + route_id: "Red" } msg = Content.Message.Predictions.new(prediction, false, :jfk_mezzanine) @@ -211,9 +187,7 @@ defmodule Content.Message.PredictionsTest do stop_id: "70096", seconds_until_arrival: 360, direction_id: 1, - route_id: "Red", - stopped?: false, - stops_away: 2 + route_id: "Red" } msg = Content.Message.Predictions.new(prediction, false, :jfk_mezzanine) @@ -232,8 +206,7 @@ defmodule Content.Message.PredictionsTest do direction_id: 1, route_id: "Mattapan", destination_stop_id: "70261", - stopped?: false, - stops_away: 0, + stopped_at_predicted_stop?: true, boarding_status: "Stopped at station" } @@ -248,8 +221,7 @@ defmodule Content.Message.PredictionsTest do direction_id: 1, route_id: "Mattapan", destination_stop_id: "70261", - stopped?: false, - stops_away: 0, + stopped_at_predicted_stop?: true, boarding_status: "Stopped at station" } @@ -263,8 +235,6 @@ defmodule Content.Message.PredictionsTest do seconds_until_departure: 70, direction_id: 1, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70261" } @@ -279,8 +249,7 @@ defmodule Content.Message.PredictionsTest do seconds_until_departure: 120, direction_id: 1, route_id: "Orange", - stopped?: false, - stops_away: 0 + stopped_at_predicted_stop?: true } msg = Content.Message.Predictions.new(prediction, true, nil) diff --git a/test/predictions/predictions_test.exs b/test/predictions/predictions_test.exs index 71d94aac8..63eb514ee 100644 --- a/test/predictions/predictions_test.exs +++ b/test/predictions/predictions_test.exs @@ -7,47 +7,32 @@ defmodule Predictions.PredictionsTest do @feed_message %{ "entity" => [ %{ - "alert" => nil, "id" => "1490783458_32568935", - "is_deleted" => false, "trip_update" => %{ - "delay" => nil, "stop_time_update" => [ %{ "arrival" => nil, "departure" => nil, - "schedule_relationship" => "SKIPPED", "stop_id" => "70265", - "stop_sequence" => 1, - "stops_away" => nil, - "stopped?" => false, - "passthrough_time" => 1_491_570_110 + "stop_sequence" => 1 }, %{ "arrival" => nil, "departure" => %{ - "delay" => nil, "time" => 1_491_570_120, - "uncertainty" => nil + "uncertainty" => 60 }, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70263", "stop_sequence" => 1, - "stops_away" => 1, - "stopped?" => true, "boarding_status" => "Stopped 1 stop away" }, %{ "arrival" => %{ - "delay" => nil, "time" => 1_491_570_180, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70261", - "stops_away" => 1, - "stopped?" => false, "stop_sequence" => 1 } ], @@ -58,7 +43,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "G-10040", @@ -83,11 +69,10 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70261", seconds_until_arrival: 180, + arrival_certainty: 60, direction_id: 0, schedule_relationship: :scheduled, route_id: "Mattapan", - stops_away: 1, - stopped?: false, destination_stop_id: "70261", trip_id: "32568935", revenue_trip?: true, @@ -98,32 +83,16 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70263", seconds_until_departure: 120, + departure_certainty: 60, direction_id: 0, schedule_relationship: :scheduled, route_id: "Mattapan", destination_stop_id: "70261", - stops_away: 1, - stopped?: true, boarding_status: "Stopped 1 stop away", trip_id: "32568935", revenue_trip?: true, vehicle_id: "G-10040" } - ], - {"70265", 0} => [ - %Predictions.Prediction{ - stop_id: "70265", - seconds_until_passthrough: 110, - direction_id: 0, - schedule_relationship: :skipped, - route_id: "Mattapan", - destination_stop_id: "70261", - stops_away: nil, - stopped?: false, - trip_id: "32568935", - revenue_trip?: true, - vehicle_id: "G-10040" - } ] } @@ -144,10 +113,9 @@ defmodule Predictions.PredictionsTest do "arrival" => %{ "delay" => nil, "time" => 1_491_570_120, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70263", "stopped?" => false, "stops_away" => 1, @@ -157,10 +125,9 @@ defmodule Predictions.PredictionsTest do "arrival" => %{ "delay" => nil, "time" => 1_491_570_180, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70261", "stopped?" => false, "stops_away" => 1, @@ -169,7 +136,6 @@ defmodule Predictions.PredictionsTest do %{ "arrival" => nil, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70261", "stopped?" => false, "stops_away" => 1, @@ -183,7 +149,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "G-10040", @@ -204,10 +171,9 @@ defmodule Predictions.PredictionsTest do "arrival" => %{ "delay" => nil, "time" => 1_491_570_200, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70038", "stopped?" => false, "stops_away" => 1, @@ -217,10 +183,9 @@ defmodule Predictions.PredictionsTest do "arrival" => %{ "delay" => nil, "time" => 1_491_570_400, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, - "schedule_relationship" => "SCHEDULED", "stop_id" => "70060", "stopped?" => false, "stops_away" => 1, @@ -234,7 +199,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "trip_2" + "trip_id" => "trip_2", + "revenue" => true }, "vehicle" => %{ "id" => "vehicle_2", @@ -258,7 +224,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "CANCELED", "start_date" => "20190821", "start_time" => nil, - "trip_id" => "40826503" + "trip_id" => "40826503", + "revenue" => true }, "vehicle" => %{ "id" => nil, @@ -281,11 +248,10 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70261", seconds_until_arrival: 180, + arrival_certainty: 60, schedule_relationship: :scheduled, direction_id: 0, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70261", trip_id: "32568935", revenue_trip?: true, @@ -296,11 +262,10 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70263", seconds_until_arrival: 120, + arrival_certainty: 60, direction_id: 0, schedule_relationship: :scheduled, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70261", trip_id: "32568935", revenue_trip?: true, @@ -311,11 +276,10 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70038", seconds_until_arrival: 200, + arrival_certainty: 60, direction_id: 1, schedule_relationship: :scheduled, route_id: "Blue", - stopped?: false, - stops_away: 1, destination_stop_id: "70060", trip_id: "trip_2", revenue_trip?: true, @@ -326,11 +290,10 @@ defmodule Predictions.PredictionsTest do %Predictions.Prediction{ stop_id: "70060", seconds_until_arrival: 400, + arrival_certainty: 60, direction_id: 1, schedule_relationship: :scheduled, route_id: "Blue", - stopped?: false, - stops_away: 1, destination_stop_id: "70060", trip_id: "trip_2", revenue_trip?: true, @@ -356,7 +319,7 @@ defmodule Predictions.PredictionsTest do "arrival" => %{ "delay" => nil, "time" => Timex.to_unix(@current_time) - 100, - "uncertainty" => nil + "uncertainty" => 60 }, "departure" => nil, "schedule_relationship" => "SCHEDULED", @@ -373,7 +336,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "G-10040", @@ -399,8 +363,6 @@ defmodule Predictions.PredictionsTest do direction_id: 0, schedule_relationship: :scheduled, route_id: "Mattapan", - stopped?: false, - stops_away: 1, destination_stop_id: "70263", trip_id: "32568935", revenue_trip?: true, @@ -410,109 +372,6 @@ defmodule Predictions.PredictionsTest do }, _} = get_all(feed_message, @current_time) end - test "filters stop_time_updates where stops_away == nil" do - feed_message = %{ - "entity" => [ - %{ - "alert" => nil, - "id" => "1490783458_32568935", - "is_deleted" => false, - "trip_update" => %{ - "delay" => nil, - "stop_time_update" => [ - %{ - "arrival" => %{ - "delay" => nil, - "time" => Timex.to_unix(@current_time) - 100, - "uncertainty" => nil - }, - "departure" => %{ - "delay" => nil, - "time" => Timex.to_unix(@current_time) - 100, - "uncertainty" => nil - }, - "schedule_relationship" => "SCHEDULED", - "stop_id" => "70262", - "stopped?" => false, - "stops_away" => nil, - "stop_sequence" => 1 - }, - %{ - "arrival" => nil, - "departure" => %{ - "delay" => nil, - "time" => Timex.to_unix(@current_time) - 100, - "uncertainty" => nil - }, - "schedule_relationship" => "SCHEDULED", - "stop_id" => "70263", - "stopped?" => false, - "stops_away" => nil, - "stop_sequence" => 1 - }, - %{ - "arrival" => nil, - "departure" => %{ - "delay" => nil, - "time" => Timex.to_unix(@current_time) + 100, - "uncertainty" => nil - }, - "schedule_relationship" => "SCHEDULED", - "stop_id" => "70264", - "stopped?" => false, - "stops_away" => 1, - "stop_sequence" => 2 - } - ], - "timestamp" => nil, - "trip" => %{ - "direction_id" => 0, - "route_id" => "Mattapan", - "schedule_relationship" => "SCHEDULED", - "start_date" => "20170329", - "start_time" => nil, - "trip_id" => "32568935" - }, - "vehicle" => %{ - "id" => "G-10040", - "label" => "3260", - "license_plate" => nil - } - }, - "vehicle" => nil - } - ], - "header" => %{ - "gtfs_realtime_version" => "1.0", - "incrementality" => "FULL_DATASET", - "timestamp" => 1_490_783_458 - } - } - - {predictions_map, _} = get_all(feed_message, @current_time) - - assert predictions_map == %{ - {"70264", 0} => [ - %Predictions.Prediction{ - boarding_status: nil, - destination_stop_id: "70262", - direction_id: 0, - revenue_trip?: true, - route_id: "Mattapan", - schedule_relationship: :scheduled, - seconds_until_arrival: nil, - seconds_until_departure: 100, - seconds_until_passthrough: nil, - stop_id: "70264", - stopped?: false, - stops_away: 1, - trip_id: "32568935", - vehicle_id: "G-10040" - } - ] - } - end - test "include predictions with low uncertainty" do reassign_env(:filter_uncertain_predictions?, true) @@ -547,7 +406,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "R-54639F6C", @@ -612,7 +472,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "R-54639F6C", @@ -673,7 +534,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "R-54639F6C", @@ -739,7 +601,8 @@ defmodule Predictions.PredictionsTest do "schedule_relationship" => "SCHEDULED", "start_date" => "20170329", "start_time" => nil, - "trip_id" => "32568935" + "trip_id" => "32568935", + "revenue" => true }, "vehicle" => %{ "id" => "G-10040", diff --git a/test/signs/realtime_test.exs b/test/signs/realtime_test.exs index 80cd89635..8c5dcab76 100644 --- a/test/signs/realtime_test.exs +++ b/test/signs/realtime_test.exs @@ -525,8 +525,8 @@ defmodule Signs.RealtimeTest do test "only the first prediction in a source list can be BRD" do expect(Engine.Predictions.Mock, :for_stop, fn _, _ -> [ - prediction(destination: :mattapan, arrival: 0, stops_away: 0, trip_id: "1"), - prediction(destination: :mattapan, arrival: 100, stops_away: 1) + prediction(destination: :mattapan, arrival: 0, stopped: 0, trip_id: "1"), + prediction(destination: :mattapan, arrival: 100) ] end) @@ -546,7 +546,7 @@ defmodule Signs.RealtimeTest do expect(Engine.Predictions.Mock, :for_stop, fn _, _ -> [ prediction(destination: :boston_college, arrival: 200), - prediction(destination: :cleveland_circle, arrival: 250, stops_away: 0) + prediction(destination: :cleveland_circle, arrival: 250, stopped: 0) ] end) @@ -599,21 +599,21 @@ defmodule Signs.RealtimeTest do [ prediction( destination: :riverside, - stops_away: 0, + stopped: 0, seconds_until_arrival: -30, seconds_until_departure: 60, trip_id: "1" ), prediction( destination: :riverside, - stops_away: 0, + stopped: 0, seconds_until_arrival: -15, seconds_until_departure: 75, trip_id: "2" ), prediction( destination: :boston_college, - stops_away: 0, + stopped: 0, seconds_until_arrival: nil, seconds_until_departure: 60, trip_id: "3" @@ -656,7 +656,7 @@ defmodule Signs.RealtimeTest do test "reads special boarding button announcement at Bowdoin" do expect(Engine.Predictions.Mock, :for_stop, fn _, _ -> - [prediction(arrival: 0, destination: :wonderland, stops_away: 0)] + [prediction(arrival: 0, destination: :wonderland, stopped: 0)] end) expect_audios( @@ -1012,7 +1012,7 @@ defmodule Signs.RealtimeTest do # special case. expect(Engine.Predictions.Mock, :for_stop, fn _, _ -> [ - prediction(destination: :ashmont, arrival: 0, stops_away: 0, trip_id: "1"), + prediction(destination: :ashmont, arrival: 0, stopped: 0, trip_id: "1"), prediction(destination: :braintree, arrival: 45, trip_id: "2") ] end) @@ -1903,8 +1903,14 @@ defmodule Signs.RealtimeTest do opts = opts ++ case Keyword.get(opts, :stopped) do - nil -> [] - stops -> [stops_away: stops, boarding_status: "Stopped #{stops} stop away"] + nil -> + [] + + 0 -> + [stopped_at_predicted_stop: true] + + stops -> + [stopped_at_predicted_stop: false, boarding_status: "Stopped #{stops} stop away"] end %Predictions.Prediction{ @@ -1919,8 +1925,7 @@ defmodule Signs.RealtimeTest do route_id: Keyword.get(opts, :route_id), trip_id: Keyword.get(opts, :trip_id, "123"), destination_stop_id: Keyword.get(opts, :destination_stop_id), - stopped?: false, - stops_away: Keyword.get(opts, :stops_away, 1), + stopped_at_predicted_stop?: Keyword.get(opts, :stopped_at_predicted_stop, false), boarding_status: Keyword.get(opts, :boarding_status), revenue_trip?: true, vehicle_id: "v1"