Skip to content

Commit

Permalink
fix update_type not being passed along to feed and don't overwrite un…
Browse files Browse the repository at this point in the history
…certainty value if update_type is not present
  • Loading branch information
bfauble committed Apr 24, 2024
1 parent 64339d8 commit 75e5c67
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
9 changes: 6 additions & 3 deletions lib/concentrate/group_filter/uncertainty_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ defmodule Concentrate.GroupFilter.UncertaintyValue do

@impl Concentrate.GroupFilter
def filter({%TripDescriptor{update_type: update_type} = td, vps, stus}) do
stus = set_uncertainty(update_type, stus)
stus =
update_type
|> calculate_uncertainty()
|> set_uncertainty(stus)

{td, vps, stus}
end
Expand All @@ -16,9 +19,9 @@ defmodule Concentrate.GroupFilter.UncertaintyValue do

defp set_uncertainty(nil, stus), do: stus

defp set_uncertainty(update_type, stus) do
defp set_uncertainty(uncertainty, stus) do
Enum.map(stus, fn stu ->
StopTimeUpdate.update_uncertainty(stu, calculate_uncertainty(update_type))
StopTimeUpdate.update_uncertainty(stu, uncertainty)
end)
end

Expand Down
17 changes: 9 additions & 8 deletions lib/concentrate/parser/gtfs_realtime_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
) do
max_time = options.max_time

arrival_time = time_from_event(Map.get(update, "arrival"))
departure_time = time_from_event(Map.get(update, "departure"))
{arrival_time, _} = time_from_event(Map.get(update, "arrival"))
{departure_time, _} = time_from_event(Map.get(update, "departure"))

cond do
td != [] and not Helpers.valid_route_id?(options, TripDescriptor.route_id(List.first(td))) ->
Expand All @@ -144,9 +144,9 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
true ->
stop_updates =
for stu <- updates do
arrival_time = time_from_event(Map.get(stu, "arrival"))
{arrival_time, arrival_uncertainty} = time_from_event(Map.get(stu, "arrival"))

departure_time = time_from_event(Map.get(stu, "departure"))
{departure_time, departure_uncertainty} = time_from_event(Map.get(stu, "departure"))

boarding_status = decode_boarding_status(Map.get(stu, "boarding_status"))

Expand All @@ -158,6 +158,7 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
schedule_relationship: schedule_relationship(Map.get(stu, "schedule_relationship")),
arrival_time: arrival_time,
departure_time: departure_time,
uncertainty: arrival_uncertainty || departure_uncertainty,
status: boarding_status,
platform_id: Map.get(stu, "platform_id")
)
Expand Down Expand Up @@ -249,7 +250,8 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
timestamp: Map.get(descriptor, "timestamp"),
revenue: Map.get(trip, "revenue", true),
vehicle_id: vehicle_id,
last_trip: Map.get(trip, "last_trip", false)
last_trip: Map.get(trip, "last_trip", false),
update_type: Map.get(descriptor, "update_type")
)
]
end
Expand Down Expand Up @@ -285,9 +287,8 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
Date.to_erl(date)
end

defp time_from_event(nil), do: nil

defp time_from_event(%{"time" => time}), do: time
defp time_from_event(nil), do: {nil, nil}
defp time_from_event(%{"time" => time} = map), do: {time, Map.get(map, "uncertainty", nil)}

defp schedule_relationship(nil), do: :SCHEDULED

Expand Down
16 changes: 8 additions & 8 deletions test/concentrate/group_filter/unceratinty_value_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Concentrate.GroupFilter.UncertaintyValueTest do
alias Concentrate.TripDescriptor

describe "filter/1" do
test "populates uncertainty in TripDescriptor based on update_type value of mid_trip" do
test "populates uncertainty in StopTimeUpdate based on update_type value of mid_trip" do
td = TripDescriptor.new(update_type: "mid_trip")

stus = [
Expand All @@ -22,7 +22,7 @@ defmodule Concentrate.GroupFilter.UncertaintyValueTest do
end)
end

test "populates uncertainty in TripDescriptor based on update_type value of at_terminal" do
test "populates uncertainty in StopTimeUpdate based on update_type value of at_terminal" do
td = TripDescriptor.new(update_type: "at_terminal")

stus = [
Expand All @@ -38,7 +38,7 @@ defmodule Concentrate.GroupFilter.UncertaintyValueTest do
end)
end

test "populates uncertainty in TripDescriptor based on update_type value of reverse_trip" do
test "populates uncertainty in StopTimeUpdate based on update_type value of reverse_trip" do
td = TripDescriptor.new(update_type: "reverse_trip")

stus = [
Expand All @@ -54,19 +54,19 @@ defmodule Concentrate.GroupFilter.UncertaintyValueTest do
end)
end

test "populates uncertainty in TripDescriptor based on update_type value of other" do
test "maintains uncertainty in StopTimeUpdate if update_type is not valid" do
td = TripDescriptor.new(update_type: nil)

stus = [
StopTimeUpdate.new(uncertainty: nil),
StopTimeUpdate.new(uncertainty: nil),
StopTimeUpdate.new(uncertainty: nil)
StopTimeUpdate.new(uncertainty: 60),
StopTimeUpdate.new(uncertainty: 60),
StopTimeUpdate.new(uncertainty: 60)
]

{^td, [], processed_stus} = filter({td, [], stus})

assert Enum.all?(processed_stus, fn procced_stu ->
StopTimeUpdate.uncertainty(procced_stu) == nil
StopTimeUpdate.uncertainty(procced_stu) == 60
end)
end
end
Expand Down
27 changes: 24 additions & 3 deletions test/concentrate/parser/gtfs_realtime_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,27 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do
assert TripDescriptor.revenue(td) == true
end

test "update_type is nil if update_type is not present" do
test "update_type and uncertainty values are set if present" do
update = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route",
"update_type" => "mid_trip"
},
"stop_time_update" => [
%{
"arrival" => %{"time" => 100, "uncertainty" => 500},
"departure" => %{"time" => 200, "uncertainty" => 500}
}
]
}

[td, stu] = decode_trip_update(update, %Options{})
assert td.update_type == "mid_trip"
assert stu.uncertainty == 500
end

test "uncertainty uses uncertainty value if update_type is not present" do
update = %{
"trip" => %{
"trip_id" => "trip",
Expand All @@ -386,8 +406,9 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do
]
}

[_td, stu] = decode_trip_update(update, %Options{})
assert stu.uncertainty == nil
[td, stu] = decode_trip_update(update, %Options{})
refute td.update_type
assert stu.uncertainty == 500
end

test "decodes last_trip" do
Expand Down

0 comments on commit 75e5c67

Please sign in to comment.