Skip to content
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

🧠 Implement "uncertainty adapter" within Concentrate #346

Merged
merged 13 commits into from
Apr 26, 2024
27 changes: 21 additions & 6 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"), trip_update)
{departure_time, _} = time_from_event(Map.get(update, "departure"), trip_update)

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

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

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

Expand Down Expand Up @@ -284,8 +287,20 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
Date.to_erl(date)
end

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

defp time_from_event(%{"time" => time} = map, %{"update_type" => update_type}) do
case update_type do
nil -> {time, Map.get(map, "uncertainty", nil)}
update_type -> {time, calculate_uncertainty(update_type)}
end
end

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

defp calculate_uncertainty("mid_trip"), do: 60
defp calculate_uncertainty("at_terminal"), do: 120
defp calculate_uncertainty("reverse_trip"), do: 360

defp schedule_relationship(nil), do: :SCHEDULED

Expand Down
75 changes: 75 additions & 0 deletions test/concentrate/parser/gtfs_realtime_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,81 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do
[td] = decode_trip_update(map, Helpers.parse_options([]))
assert TripDescriptor.revenue(td) == true
end

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

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

test "decodes mid_trip update_type to determine uncertainty value" 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 stu.uncertainty == 60
end

test "decodes at_terminal update_type to determine uncertainty value" do
update = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route"
},
"update_type" => "at_terminal",
"stop_time_update" => [
%{
"arrival" => %{"time" => 100, "uncertainty" => 500},
"departure" => %{"time" => 200, "uncertainty" => 500}
}
]
}

[_td, stu] = decode_trip_update(update, %Options{})
assert stu.uncertainty == 120
end

test "decodes reverse_trip update_type to determine uncertainty value" do
update = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route"
},
"update_type" => "reverse_trip",
"stop_time_update" => [
%{
"arrival" => %{"time" => 100, "uncertainty" => 500},
"departure" => %{"time" => 200, "uncertainty" => 500}
}
]
}

[_td, stu] = decode_trip_update(update, %Options{})
assert stu.uncertainty == 360
end
end

describe "decode_vehicle/3" do
Expand Down
Loading