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

fix: Filter out predictions w/ arrival_time but no departure_time. #2211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/screens/v2/departure/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Screens.V2.Departure.Builder do
def get_relevant_departures(predictions_or_schedules, now \\ DateTime.utc_now()) do
predictions_or_schedules
|> Stream.reject(&in_past_or_nil_time?(&1, now))
|> Stream.reject(&prediction_has_no_departure_time?/1)
|> Stream.reject(&multi_route_duplicate?/1)
|> Stream.reject(&vehicle_already_departed?/1)
|> choose_earliest_arrival_per_trip()
Expand Down Expand Up @@ -58,6 +59,12 @@ defmodule Screens.V2.Departure.Builder do

defp vehicle_already_departed?(_), do: false

defp prediction_has_no_departure_time?(%Schedule{}), do: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also do this for scheduled departures, I think (filtering them out if they have no departure time). We don't want to display an "arrival-only" stop that won't be accepting passengers, even if it's not a live prediction.


defp prediction_has_no_departure_time?(prediction) do
prediction.arrival_time != nil and prediction.departure_time == nil
end

defp choose_earliest_arrival_per_trip(predictions_or_schedules) do
{departures_without_trip, departures_with_trip} =
Enum.split_with(predictions_or_schedules, fn
Expand Down
27 changes: 24 additions & 3 deletions test/screens/v2/departure/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ defmodule Screens.V2.Departure.BuilderTest do

describe "get_relevant_departures/1" do
test "filters out departures with both arrival_time and departure_time nil" do
d1 = %Prediction{
id: "departure",
arrival_time: nil,
departure_time: ~U[2020-02-01T01:00:00Z]
}

d2 = %Prediction{
id: "both",
arrival_time: ~U[2020-02-01T01:00:00Z],
departure_time: ~U[2020-02-01T01:00:00Z]
}

d3 = %Prediction{id: "neither", arrival_time: nil, departure_time: nil}
departures = [d1, d2, d3]

now = ~U[2020-01-01T01:00:00Z]

assert MapSet.new([d1, d2]) ==
MapSet.new(Builder.get_relevant_departures(departures, now))
end

test "filters out departures with an arrival_time but no departure_time" do
d1 = %Prediction{id: "arrival", arrival_time: ~U[2020-02-01T01:00:00Z], departure_time: nil}

d2 = %Prediction{
Expand All @@ -26,12 +48,11 @@ defmodule Screens.V2.Departure.BuilderTest do
departure_time: ~U[2020-02-01T01:00:00Z]
}

d4 = %Prediction{id: "neither", arrival_time: nil, departure_time: nil}
departures = [d1, d2, d3, d4]
departures = [d1, d2, d3]

now = ~U[2020-01-01T01:00:00Z]

assert MapSet.new([d1, d2, d3]) ==
assert MapSet.new([d2, d3]) ==
MapSet.new(Builder.get_relevant_departures(departures, now))
end

Expand Down
Loading