Skip to content

Commit

Permalink
fix: don't include non-revenue trips in feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Whoops committed Dec 4, 2023
1 parent 28a1788 commit df0eaab
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 33 deletions.
7 changes: 7 additions & 0 deletions lib/concentrate/encoder/gtfs_realtime_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions lib/concentrate/encoder/trip_updates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/concentrate/encoder/trip_updates_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/concentrate/encoder/vehicle_positions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/concentrate/encoder/vehicle_positions_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 13 additions & 31 deletions test/concentrate/encoder/trip_updates_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/concentrate/encoder/trip_updates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/concentrate/encoder/vehicle_positions_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -78,7 +78,7 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhancedTest do
)
]

assert data == FeedUpdate.updates(round_trip(data))
assert [] == FeedUpdate.updates(round_trip(data))
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/concentrate/encoder/vehicle_positions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df0eaab

Please sign in to comment.