Skip to content

Commit

Permalink
refactor(RoutePattern): get stop_ids from trip ++ related improveme…
Browse files Browse the repository at this point in the history
…nts (#1755)

* feat(RoutePattern): get stop_ids from trip

- also enable this for `RoutePatterns.Repo.by_stop_id/1`

* refactor(RoutePatterns.Repo): remove hard include

let other functions request the shape

* refactor(TimetableController): get stop_ids directly from route pattern

* refactor(Line.Helpers): use canonical route patterns

- also parse stops for route patterns

* postfix(Line.Helpers): base logic on mode

ferries and buses don't have canonical routes

* fix: add sort_order to RoutePattern type

* fix: add headsign to RoutePattern type
  • Loading branch information
thecristen authored Sep 26, 2023
1 parent 958c3ff commit 11d4f72
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 167 deletions.
3 changes: 1 addition & 2 deletions apps/route_patterns/lib/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ defmodule RoutePatterns.Repo do
opts
|> Keyword.put(:route, route_id)
|> Keyword.put(:sort, "typicality,sort_order")
|> Keyword.put(:include, "representative_trip.shape")
|> cache(&api_all/1)
|> Enum.sort(&reorder_mrts(&1, &2, route_id))
end

def by_stop_id(stop_id) do
[stop: stop_id]
|> Keyword.put(:include, "representative_trip.shape")
|> Keyword.put(:include, "representative_trip.shape,representative_trip.stops")
|> cache(&api_all/1)
end

Expand Down
71 changes: 47 additions & 24 deletions apps/route_patterns/lib/route_pattern.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ defmodule RoutePatterns.RoutePattern do
:stop_ids,
:route_id,
:time_desc,
:typicality
:typicality,
sort_order: 0
]

@type id_t :: String.t()
Expand All @@ -53,7 +54,8 @@ defmodule RoutePatterns.RoutePattern do
stop_ids: [Stop.id_t()],
route_id: Route.id_t(),
time_desc: String.t(),
typicality: typicality_t()
typicality: typicality_t(),
sort_order: integer()
}

def new(%Item{
Expand All @@ -62,7 +64,8 @@ defmodule RoutePatterns.RoutePattern do
"direction_id" => direction_id,
"name" => name,
"time_desc" => time_desc,
"typicality" => typicality
"typicality" => typicality,
"sort_order" => sort_order
},
relationships: %{
"representative_trip" => [
Expand All @@ -71,20 +74,7 @@ defmodule RoutePatterns.RoutePattern do
"headsign" => headsign
},
id: representative_trip_id,
relationships: %{
"shape" => [
%Item{
attributes: %{
"polyline" => representative_trip_polyline,
"priority" => shape_priority
},
id: shape_id,
relationships: %{
"stops" => stops
}
}
]
}
relationships: trip_relationships
}
],
"route" => [%Item{id: route_id}]
Expand All @@ -95,14 +85,14 @@ defmodule RoutePatterns.RoutePattern do
id: id,
name: name,
representative_trip_id: representative_trip_id,
representative_trip_polyline: representative_trip_polyline,
shape_id: shape_id,
shape_priority: shape_priority,
representative_trip_polyline: polyline(trip_relationships),
shape_id: shape_id(trip_relationships),
headsign: headsign,
stop_ids: Enum.map(stops, fn %JsonApi.Item{id: id} -> id end),
stop_ids: stop_ids(trip_relationships),
route_id: route_id,
time_desc: time_desc,
typicality: typicality
typicality: typicality,
sort_order: sort_order
}
end

Expand All @@ -112,7 +102,8 @@ defmodule RoutePatterns.RoutePattern do
"direction_id" => direction_id,
"name" => name,
"time_desc" => time_desc,
"typicality" => typicality
"typicality" => typicality,
"sort_order" => sort_order
},
relationships: %{
"representative_trip" => [%Item{id: representative_trip_id}],
Expand All @@ -126,7 +117,39 @@ defmodule RoutePatterns.RoutePattern do
representative_trip_id: representative_trip_id,
route_id: route_id,
time_desc: time_desc,
typicality: typicality
typicality: typicality,
sort_order: sort_order
}
end

defp polyline(%{
"shape" => [
%Item{
attributes: %{
"polyline" => polyline
}
}
]
}),
do: polyline

defp polyline(_), do: nil

defp shape_id(%{
"shape" => [
%Item{
id: shape_id
}
]
}),
do: shape_id

defp shape_id(_), do: nil

# Note: these are child stop IDs
defp stop_ids(%{"stops" => stops}) when is_list(stops) do
Enum.map(stops, & &1.id)
end

defp stop_ids(_), do: nil
end
8 changes: 6 additions & 2 deletions apps/route_patterns/test/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ defmodule RoutePatterns.RepoTest do
end

describe "by_stop_id/1" do
test "returns route patterns for a stop" do
assert [%RoutePattern{} | _] = Repo.by_stop_id("place-sstat")
test "returns route patterns for a stop, with shape and stops" do
assert [%RoutePattern{representative_trip_polyline: polyline, stop_ids: stop_ids} | _] =
Repo.by_stop_id("place-sstat")

assert stop_ids
assert polyline
end
end
end
3 changes: 2 additions & 1 deletion apps/routes/test/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ defmodule Routes.ParserTest do
"direction_id" => 1,
"name" => "rp",
"time_desc" => "td",
"typicality" => 1
"typicality" => 1,
"sort_order" => 12_132_123
},
relationships: %{
"representative_trip" => [%Item{id: "id"}],
Expand Down
2 changes: 2 additions & 0 deletions apps/site/assets/ts/__v3api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,11 @@ export interface RoutePattern {
shape_id: string;
shape_priority: number;
stop_ids: string[];
headsign: string;
name: string;
id: string;
direction_id: DirectionId;
sort_order: number;
}

export interface StopHours {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ const routePatternsByDirection = {
name: "North Station - Wachusett",
headsign: "Wachusett",
id: "CR-Fitchburg-0-0",
direction_id: 0
direction_id: 0,
sort_order: 3
}
],
"1": [
Expand All @@ -163,7 +164,8 @@ const routePatternsByDirection = {
name: "Wachusett - North Station",
headsign: "North Station",
id: "CR-Fitchburg-0-1",
direction_id: 1
direction_id: 1,
sort_order: 4
}
]
} as RoutePatternsByDirection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
"route_id": "route-1",
"representative_trip_id": "trip-1",
"representative_trip_polyline": "trip-1-polyline",
"stop_ids": ["1", "stop-place-alfcl", "2", "3"],
"stop_ids": [
"1",
"stop-place-alfcl",
"2",
"3"
],
"name": "Pattern 1 - Dest",
"headsign": "Pattern 1",
"id": "pattern-1",
"direction_id": 0
"direction_id": 0,
"sort_order": 123
},
{
"typicality": 1,
Expand All @@ -22,11 +28,17 @@
"route_id": "route-1",
"representative_trip_id": "trip-3",
"representative_trip_polyline": "trip-3-polyline",
"stop_ids": ["1", "stop-place-alfcl", "2", "4"],
"stop_ids": [
"1",
"stop-place-alfcl",
"2",
"4"
],
"name": "Pattern 3 - Dest",
"headsign": "Pattern 3",
"id": "pattern-3",
"direction_id": 0
"direction_id": 0,
"sort_order": 123
},
{
"typicality": 3,
Expand All @@ -36,11 +48,17 @@
"route_id": "route-1",
"representative_trip_id": "trip-4",
"representative_trip_polyline": "trip-4-polyline",
"stop_ids": ["1", "stop-place-alfcl", "3", "4"],
"stop_ids": [
"1",
"stop-place-alfcl",
"3",
"4"
],
"name": "Pattern 4 - Dest",
"headsign": "Pattern 3",
"id": "pattern-4",
"direction_id": 0
"direction_id": 0,
"sort_order": 123
}
],
"1": [
Expand All @@ -52,11 +70,17 @@
"route_id": "route-1",
"representative_trip_id": "trip-1",
"representative_trip_polyline": "trip-2-polyline",
"stop_ids": ["2", "stop-place-alfcl", "3", "4"],
"stop_ids": [
"2",
"stop-place-alfcl",
"3",
"4"
],
"name": "Pattern 2 - Dest",
"headsign": "Pattern 2",
"id": "pattern-2",
"direction_id": 1
"direction_id": 1,
"sort_order": 123
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const routePatterns: EnhancedRoutePattern[] = [
shape_id: "660140",
shape_priority: 1,
time_desc: null,
typicality: 1
typicality: 1,
sort_order: 5
},
{
typicality: 3,
Expand All @@ -37,7 +38,8 @@ const routePatterns: EnhancedRoutePattern[] = [
name: "Dudley Station - Union Square, Boston",
id: "66-B-0",
headsign: "Watertown Yard via Union Square Allston",
direction_id: 0
direction_id: 0,
sort_order: 7
}
];
const singleRoutePattern = routePatterns.slice(0, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const routePatternsForInbound: EnhancedRoutePattern[] = [
shape_id: "1110177",
shape_priority: 1,
time_desc: null,
typicality: 1
typicality: 1,
sort_order: 1
},
{
direction_id: 1,
Expand All @@ -29,7 +30,8 @@ const routePatternsForInbound: EnhancedRoutePattern[] = [
shape_id: "1110157",
shape_priority: 1,
time_desc: "Weekdays only",
typicality: 2
typicality: 2,
sort_order: 2
}
];

Expand All @@ -55,7 +57,8 @@ const initialState: State = {
shape_priority: 1,
shape_id: "1110180",
time_desc: null,
typicality: 1
typicality: 1,
sort_order: 3
},
directionId: 0,
routePatternsByDirection: {
Expand Down Expand Up @@ -86,7 +89,8 @@ it("menuReducer handles 'toggleDirection'", () => {
shape_id: "1110177",
shape_priority: 1,
time_desc: null,
typicality: 1
typicality: 1,
sort_order: 1
}
};

Expand Down
2 changes: 1 addition & 1 deletion apps/site/lib/site_web/controllers/route_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule SiteWeb.RouteController do

defp route_polylines(route, stop_id) do
route.id
|> RoutePatterns.Repo.by_route_id(stop: stop_id)
|> RoutePatterns.Repo.by_route_id(stop: stop_id, include: "representative_trip.shape")
|> Enum.filter(&(!is_nil(&1.representative_trip_polyline)))
|> Enum.map(&Polyline.new(&1, color: "#" <> route.color, weight: 4))
end
Expand Down
Loading

0 comments on commit 11d4f72

Please sign in to comment.