From df0eaab2c46cfa5d3fb616b7ff04c3921e92600a Mon Sep 17 00:00:00 2001 From: Walton Hoops Date: Fri, 3 Nov 2023 13:45:42 -0600 Subject: [PATCH] fix: don't include non-revenue trips in feeds --- .../encoder/gtfs_realtime_helpers.ex | 7 +++ lib/concentrate/encoder/trip_updates.ex | 2 + .../encoder/trip_updates_enhanced.ex | 2 + lib/concentrate/encoder/vehicle_positions.ex | 2 + .../encoder/vehicle_positions_enhanced.ex | 2 + .../encoder/trip_updates_enhanced_test.exs | 44 ++++++------------- .../concentrate/encoder/trip_updates_test.exs | 22 ++++++++++ .../vehicle_positions_enhanced_test.exs | 4 +- .../encoder/vehicle_positions_test.exs | 15 +++++++ 9 files changed, 67 insertions(+), 33 deletions(-) diff --git a/lib/concentrate/encoder/gtfs_realtime_helpers.ex b/lib/concentrate/encoder/gtfs_realtime_helpers.ex index c3743bb6..4ead8a87 100644 --- a/lib/concentrate/encoder/gtfs_realtime_helpers.ex +++ b/lib/concentrate/encoder/gtfs_realtime_helpers.ex @@ -140,6 +140,13 @@ defmodule Concentrate.Encoder.GTFSRealtimeHelpers do def schedule_relationship(:SCHEDULED), do: nil def schedule_relationship(relationship), do: relationship + @doc """ + Returns true if the group is non-revenue + """ + def non_revenue?({td, _, _} = _group) do + td && not td.revenue + end + defp group_by_trip_id(%TripDescriptor{} = td, map) do if trip_id = TripDescriptor.trip_id(td) do Map.update(map, trip_id, {td, [], []}, &add_trip_descriptor(&1, td)) diff --git a/lib/concentrate/encoder/trip_updates.ex b/lib/concentrate/encoder/trip_updates.ex index bbca581f..3173066b 100644 --- a/lib/concentrate/encoder/trip_updates.ex +++ b/lib/concentrate/encoder/trip_updates.ex @@ -8,6 +8,8 @@ defmodule Concentrate.Encoder.TripUpdates do @impl Concentrate.Encoder def encode_groups(groups, opts \\ []) when is_list(groups) do + groups = Enum.reject(groups, &non_revenue?/1) + message = %{ header: feed_header(opts), entity: trip_update_feed_entity(groups, &build_stop_time_update/1) diff --git a/lib/concentrate/encoder/trip_updates_enhanced.ex b/lib/concentrate/encoder/trip_updates_enhanced.ex index 3cfb7db7..ad018590 100644 --- a/lib/concentrate/encoder/trip_updates_enhanced.ex +++ b/lib/concentrate/encoder/trip_updates_enhanced.ex @@ -8,6 +8,8 @@ defmodule Concentrate.Encoder.TripUpdatesEnhanced do @impl Concentrate.Encoder def encode_groups(groups, opts \\ []) when is_list(groups) do + groups = Enum.reject(groups, &non_revenue?/1) + message = %{ header: feed_header(opts), entity: trip_update_feed_entity(groups, &build_stop_time_update/1, &enhanced_data/1) diff --git a/lib/concentrate/encoder/vehicle_positions.ex b/lib/concentrate/encoder/vehicle_positions.ex index e37b27a8..248c25f3 100644 --- a/lib/concentrate/encoder/vehicle_positions.ex +++ b/lib/concentrate/encoder/vehicle_positions.ex @@ -8,6 +8,8 @@ defmodule Concentrate.Encoder.VehiclePositions do @impl Concentrate.Encoder def encode_groups(groups, opts \\ []) when is_list(groups) do + groups = Enum.reject(groups, &non_revenue?/1) + message = %{ header: feed_header(opts), entity: Enum.flat_map(groups, &build_entity/1) diff --git a/lib/concentrate/encoder/vehicle_positions_enhanced.ex b/lib/concentrate/encoder/vehicle_positions_enhanced.ex index b00173fe..02a43a41 100644 --- a/lib/concentrate/encoder/vehicle_positions_enhanced.ex +++ b/lib/concentrate/encoder/vehicle_positions_enhanced.ex @@ -10,6 +10,8 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhanced do @impl Concentrate.Encoder def encode_groups(groups, opts \\ []) when is_list(groups) do + groups = Enum.reject(groups, &non_revenue?/1) + message = %{ "header" => feed_header(opts), "entity" => Enum.flat_map(groups, &build_entity/1) diff --git a/test/concentrate/encoder/trip_updates_enhanced_test.exs b/test/concentrate/encoder/trip_updates_enhanced_test.exs index 1e73278e..1a4afb16 100644 --- a/test/concentrate/encoder/trip_updates_enhanced_test.exs +++ b/test/concentrate/encoder/trip_updates_enhanced_test.exs @@ -127,43 +127,25 @@ defmodule Concentrate.Encoder.TripUpdatesEnhancedTest do } = encoded end - test "trips encode revenue status" do + test "Non-revenue trips are dropped" do parsed = [ - TripDescriptor.new(trip_id: "trip", revenue: true), - StopTimeUpdate.new(trip_id: "trip", stop_id: "stop") - ] - - encoded = Jason.decode!(encode_groups(group(parsed))) - - assert %{ - "entity" => [ - %{ - "trip_update" => %{ - "trip" => %{ - "revenue" => true - } - } - } - ] - } = encoded - - parsed = [ - TripDescriptor.new(trip_id: "trip", revenue: false), - StopTimeUpdate.new(trip_id: "trip", stop_id: "stop") + TripDescriptor.new( + trip_id: "NONREV-trip", + route_id: "route", + direction_id: 0, + revenue: false + ), + StopTimeUpdate.new( + trip_id: "NONREV-trip", + stop_id: "stop", + schedule_relationship: :SKIPPED + ) ] encoded = Jason.decode!(encode_groups(group(parsed))) assert %{ - "entity" => [ - %{ - "trip_update" => %{ - "trip" => %{ - "revenue" => false - } - } - } - ] + "entity" => [] } = encoded end end diff --git a/test/concentrate/encoder/trip_updates_test.exs b/test/concentrate/encoder/trip_updates_test.exs index efe7d2a1..befeea83 100644 --- a/test/concentrate/encoder/trip_updates_test.exs +++ b/test/concentrate/encoder/trip_updates_test.exs @@ -156,5 +156,27 @@ defmodule Concentrate.Encoder.TripUpdatesTest do refute "route_pattern_id" in Map.keys(trip) end + + test "Non-revenue trips with are dropped" do + initial = [ + TripDescriptor.new( + trip_id: "NONREV-trip", + route_id: "route", + direction_id: 0, + revenue: false + ), + StopTimeUpdate.new( + trip_id: "NONREV-trip", + stop_id: "stop", + schedule_relationship: :SKIPPED + ) + ] + + decoded = :gtfs_realtime_proto.decode_msg(encode_groups(group(initial)), :FeedMessage, []) + + assert %{ + entity: [] + } = decoded + end end end diff --git a/test/concentrate/encoder/vehicle_positions_enhanced_test.exs b/test/concentrate/encoder/vehicle_positions_enhanced_test.exs index aa6307b9..b5d84c99 100644 --- a/test/concentrate/encoder/vehicle_positions_enhanced_test.exs +++ b/test/concentrate/encoder/vehicle_positions_enhanced_test.exs @@ -66,7 +66,7 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhancedTest do assert [] == FeedUpdate.updates(round_trip(data)) end - test "includes revenue status" do + test "filters non-revenue trips" do data = [ TripDescriptor.new(trip_id: "one", vehicle_id: "y1", revenue: false), VehiclePosition.new( @@ -78,7 +78,7 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhancedTest do ) ] - assert data == FeedUpdate.updates(round_trip(data)) + assert [] == FeedUpdate.updates(round_trip(data)) end end diff --git a/test/concentrate/encoder/vehicle_positions_test.exs b/test/concentrate/encoder/vehicle_positions_test.exs index c3034cb6..cd40dd8d 100644 --- a/test/concentrate/encoder/vehicle_positions_test.exs +++ b/test/concentrate/encoder/vehicle_positions_test.exs @@ -189,6 +189,21 @@ defmodule Concentrate.Encoder.VehiclePositionsTest do assert Enum.sort(round_trip(decoded)) == Enum.sort(decoded) end + + test "filters non-revenue trips" do + data = [ + TripDescriptor.new(trip_id: "one", vehicle_id: "y1", revenue: false), + VehiclePosition.new( + trip_id: "one", + id: "y1", + latitude: 1, + longitude: 1, + status: :IN_TRANSIT_TO + ) + ] + + assert [] == round_trip(data) + end end defp round_trip(data) do