Skip to content

Commit

Permalink
fix: Make as-directed parsing not-crash and default to regular trips (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored Aug 19, 2024
1 parent 538eb29 commit 8257c6a
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/schedule/hastus/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ defmodule Schedule.Hastus.Activity do
end

as_directeds_and_schedule_trips =
if operator_is_as_directed?(activity) do
[as_directed_from_trips(trips_in_piece)]
with true <- operator_is_as_directed?(activity),
{:ok, as_directed} <- as_directed_from_trips(trips_in_piece) do
[as_directed]
else
Enum.map(trips_in_piece, &Map.fetch!(schedule_trips_by_id, &1.trip_id))
_ -> Enum.map(trips_in_piece, &Map.fetch!(schedule_trips_by_id, &1.trip_id))
end

%Piece{
Expand All @@ -175,29 +176,30 @@ defmodule Schedule.Hastus.Activity do

defp operator_activity_to_piece(activity, _, _, _), do: activity

@spec as_directed_from_trips([Hastus.Trip.t()]) :: AsDirected.t()
defp as_directed_from_trips(trips_in_piece) do
[
%Hastus.Trip{route_id: nil} = _pullout,
as_directed_trip,
%Hastus.Trip{route_id: nil} = _pull_back
] = trips_in_piece

@spec as_directed_from_trips([Hastus.Trip.t()]) :: {:ok, AsDirected.t()} | :error
defp as_directed_from_trips([
%Hastus.Trip{route_id: nil} = _pullout,
as_directed_trip,
%Hastus.Trip{route_id: nil} = _pull_back
]) do
kind =
case as_directed_trip.route_id do
"rad" -> :rad
"wad" -> :wad
end

%AsDirected{
kind: kind,
start_time: as_directed_trip.start_time,
end_time: as_directed_trip.end_time,
start_place: as_directed_trip.start_place,
end_place: as_directed_trip.end_place
}
{:ok,
%AsDirected{
kind: kind,
start_time: as_directed_trip.start_time,
end_time: as_directed_trip.end_time,
start_place: as_directed_trip.start_place,
end_place: as_directed_trip.end_place
}}
end

defp as_directed_from_trips(_trips_in_piece), do: :error

@spec as_directed_activities_to_pieces([__MODULE__.t() | Piece.t()]) :: [
__MODULE__.t() | Piece.t()
]
Expand Down
244 changes: 244 additions & 0 deletions test/schedule/data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,250 @@ defmodule Schedule.DataTest do
} = Data.run_from_hastus(run_key, activities, trips, %{}, %{}, %{})
end

test "allows as directed trips and regular trips in the same piece named like a regular block" do
run_key = {"abc20011", "123-9073"}

activities = [
%Activity{
schedule_id: "abc44011",
run_id: "123-1113",
start_time: 68_220,
end_time: 68_220,
start_place: "cabot",
end_place: "cabot",
activity_type: "Sign-on"
},
%Activity{
schedule_id: "abc44011",
run_id: "123-1113",
start_time: 68_220,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
activity_type: "Operator",
partial_block_id: "block_id"
}
]

trips = [
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 68_220,
end_time: 68_820,
start_place: "cabot",
end_place: "ctypt",
route_id: nil,
trip_id: "64466045"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 68_820,
end_time: 70_860,
start_place: "ctypt",
end_place: "copst",
route_id: "09",
trip_id: "64464840"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 71_280,
end_time: 73_020,
start_place: "copst",
end_place: "ctypt",
route_id: "09",
trip_id: "64463084"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 73_020,
end_time: 73_560,
start_place: "ctypt",
end_place: "cabot",
route_id: nil,
trip_id: "64465819"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 73_800,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
route_id: "wad",
trip_id: "64463012"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "block_id",
start_time: 84_600,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
route_id: nil,
trip_id: "64466123"
}
]

schedule_trips_by_id = %{
"64466045" => build(:trip, id: "64466045"),
"64464840" => build(:trip, id: "64464840"),
"64463084" => build(:trip, id: "64463084"),
"64465819" => build(:trip, id: "64465819"),
"64463012" => build(:trip, id: "64463012"),
"64466123" => build(:trip, id: "64466123")
}

assert %Run{
activities: [
%Piece{
block_id: "block_id",
start_time: 68_220,
trips: [
%Schedule.Trip{id: "64466045"},
%Schedule.Trip{id: "64464840"},
%Schedule.Trip{id: "64463084"},
%Schedule.Trip{id: "64465819"},
%Schedule.Trip{id: "64463012"},
%Schedule.Trip{id: "64466123"}
],
end_time: 84_600
}
]
} = Data.run_from_hastus(run_key, activities, trips, %{}, schedule_trips_by_id, %{})
end

test "allows as directed trips and regular trips in the same piece named like an as-directed block" do
run_key = {"abc20011", "123-9073"}

activities = [
%Activity{
schedule_id: "abc44011",
run_id: "123-1113",
start_time: 68_220,
end_time: 68_220,
start_place: "cabot",
end_place: "cabot",
activity_type: "Sign-on"
},
%Activity{
schedule_id: "abc44011",
run_id: "123-1113",
start_time: 68_220,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
activity_type: "Operator",
partial_block_id: "wad-294"
}
]

trips = [
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 68_220,
end_time: 68_820,
start_place: "cabot",
end_place: "ctypt",
route_id: nil,
trip_id: "64466045"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 68_820,
end_time: 70_860,
start_place: "ctypt",
end_place: "copst",
route_id: "09",
trip_id: "64464840"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 71_280,
end_time: 73_020,
start_place: "copst",
end_place: "ctypt",
route_id: "09",
trip_id: "64463084"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 73_020,
end_time: 73_560,
start_place: "ctypt",
end_place: "cabot",
route_id: nil,
trip_id: "64465819"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 73_800,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
route_id: "wad",
trip_id: "64463012"
},
%Schedule.Hastus.Trip{
schedule_id: "abc44011",
run_id: "123-1113",
block_id: "Cwad-294",
start_time: 84_600,
end_time: 84_600,
start_place: "cabot",
end_place: "cabot",
route_id: nil,
trip_id: "64466123"
}
]

schedule_trips_by_id = %{
"64466045" => build(:trip, id: "64466045"),
"64464840" => build(:trip, id: "64464840"),
"64463084" => build(:trip, id: "64463084"),
"64465819" => build(:trip, id: "64465819"),
"64463012" => build(:trip, id: "64463012"),
"64466123" => build(:trip, id: "64466123")
}

assert %Run{
activities: [
%Piece{
block_id: "Cwad-294",
start_time: 68_220,
trips: [
%Schedule.Trip{id: "64466045"},
%Schedule.Trip{id: "64464840"},
%Schedule.Trip{id: "64463084"},
%Schedule.Trip{id: "64465819"},
%Schedule.Trip{id: "64463012"},
%Schedule.Trip{id: "64466123"}
],
end_time: 84_600
}
]
} = Data.run_from_hastus(run_key, activities, trips, %{}, schedule_trips_by_id, %{})
end

test "makes breaks" do
run_key = {"schedule", "run"}

Expand Down

0 comments on commit 8257c6a

Please sign in to comment.