Skip to content

Commit

Permalink
Log warning for prediction with nil trip
Browse files Browse the repository at this point in the history
Although all predictions should have a trip ID, some are getting fetched
by the realtime schedule endpoint without one. To help the data team
debug this, we've added a bit of logging for when that happens.
  • Loading branch information
phildarnowsky committed Mar 23, 2020
1 parent ade947a commit 5a3a2ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions apps/site/lib/site/realtime_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule Site.RealtimeSchedule do
alias Stops.Repo, as: StopsRepo
alias Stops.Stop

require Logger

# the long timeout is to address a worst-case scenario of cold schedule cache
@long_timeout 15_000

Expand Down Expand Up @@ -298,6 +300,8 @@ defmodule Site.RealtimeSchedule do

@spec shrink_predicted_schedule(PredictedSchedule.t(), DateTime.t()) :: map
defp shrink_predicted_schedule(%{schedule: schedule, prediction: prediction}, now) do
_ = log_warning_if_missing_trip(prediction)

%{
prediction:
prediction
Expand All @@ -308,6 +312,13 @@ defmodule Site.RealtimeSchedule do
}
end

def log_warning_if_missing_trip(prediction) do
_ =
if prediction && prediction.trip == nil do
Logger.warn("prediction_without_trip prediction=#{inspect(prediction)}")
end
end

@spec add_trip_headsign(map) :: map | nil
defp add_trip_headsign(nil), do: nil

Expand Down
9 changes: 9 additions & 0 deletions apps/site/test/site/realtime_schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,13 @@ defmodule Site.RealtimeScheduleTest do

assert actual == expected
end

test "log_warning_if_missing_trip/1 logs warning if prediction has no trip" do
log_messages =
ExUnit.CaptureLog.capture_log(fn ->
RealtimeSchedule.log_warning_if_missing_trip(%Prediction{trip: nil})
end)

assert log_messages =~ "prediction_without_trip"
end
end

0 comments on commit 5a3a2ee

Please sign in to comment.