Skip to content

Commit

Permalink
fix(RoutePattern): amend struct values
Browse files Browse the repository at this point in the history
- add new typicality value
- better type for sort_order
- add a few reasonable defaults
- sort properties alphabetically
- refactor: remove unpopulated shape_priority
  • Loading branch information
thecristen committed May 1, 2024
1 parent b671eba commit 1ac1088
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 62 deletions.
19 changes: 9 additions & 10 deletions assets/ts/__v3api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,19 @@ export interface Shape {
}

export interface RoutePattern {
typicality: 1 | 2 | 3 | 4;
time_desc: string | null;
route_id: string;
canonical: boolean;
direction_id: DirectionId;
headsign: string;
id: string;
name: string;
representative_trip_id: string;
representative_trip_polyline: string;
route_id: string;
shape_id: string;
shape_priority: number;
stop_ids: string[];
headsign: string;
name: string;
id: string;
direction_id: DirectionId;
sort_order: number;
canonical: boolean;
stop_ids: string[];
time_desc: string | null;
typicality: 0 | 1 | 2 | 3 | 4 | 5;
}

export interface StopHours {
Expand Down
4 changes: 0 additions & 4 deletions assets/ts/schedule/components/ScheduleDirection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ const ScheduleDirection = ({
if (result.length === 0 || current.typicality < result[0].typicality)
return [current];
if (current.typicality === result[0].typicality) {
if (result[0].shape_priority < 0 && current.shape_priority > 0)
return [current];
if (current.shape_priority < 0 && result[0].shape_priority > 0)
return result;
return result.concat(current);
}
return result;
Expand Down
1 change: 0 additions & 1 deletion assets/ts/schedule/components/__schedule.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

export interface EnhancedRoutePattern extends RoutePattern {
shape_id: string;
shape_priority: number;
headsign: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ const routePatternsByDirection = {
typicality: 1,
time_desc: "School Trip",
shape_id: "9840004",
shape_priority: 1,
route_id: "CR-Fitchburg",
representative_trip_id: "CR-Weekday-Spring-19-401",
representative_trip_polyline: "qwerty123@777njhgb",
Expand All @@ -159,7 +158,6 @@ const routePatternsByDirection = {
typicality: 1,
time_desc: "School Trip",
shape_id: "9840003",
shape_priority: 1,
route_id: "CR-Fitchburg",
representative_trip_id: "CR-Weekday-Spring-19-400",
representative_trip_polyline: "lkjhg987bvcxz88!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"typicality": 1,
"time_desc": null,
"shape_id": "shape-1",
"shape_priority": 1,
"route_id": "route-1",
"representative_trip_id": "trip-1",
"representative_trip_polyline": "trip-1-polyline",
Expand All @@ -25,7 +24,6 @@
"typicality": 1,
"time_desc": null,
"shape_id": "shape-3",
"shape_priority": 1,
"route_id": "route-1",
"representative_trip_id": "trip-3",
"representative_trip_polyline": "trip-3-polyline",
Expand All @@ -46,7 +44,6 @@
"typicality": 3,
"time_desc": null,
"shape_id": "shape-4",
"shape_priority": 1,
"route_id": "route-1",
"representative_trip_id": "trip-4",
"representative_trip_polyline": "trip-4-polyline",
Expand All @@ -69,7 +66,6 @@
"typicality": 2,
"time_desc": null,
"shape_id": "shape-2",
"shape_priority": 1,
"route_id": "route-1",
"representative_trip_id": "trip-1",
"representative_trip_polyline": "trip-2-polyline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const routePatterns: EnhancedRoutePattern[] = [
stop_ids: ["123", "456", "789"],
route_id: "66",
shape_id: "660140",
shape_priority: 1,
time_desc: null,
typicality: 1,
sort_order: 5,
Expand All @@ -30,7 +29,6 @@ const routePatterns: EnhancedRoutePattern[] = [
{
typicality: 3,
time_desc: "School days only",
shape_priority: 1,
shape_id: "660141-2",
route_id: "66",
representative_trip_id: "43773700_2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const routePatternsForInbound: EnhancedRoutePattern[] = [
stop_ids: ["123", "456", "789"],
route_id: "111",
shape_id: "1110177",
shape_priority: 1,
time_desc: null,
typicality: 1,
sort_order: 1,
Expand All @@ -29,7 +28,6 @@ const routePatternsForInbound: EnhancedRoutePattern[] = [
stop_ids: ["123", "555", "789"],
route_id: "111",
shape_id: "1110157",
shape_priority: 1,
time_desc: "Weekdays only",
typicality: 2,
sort_order: 2,
Expand All @@ -56,7 +54,6 @@ const initialState: State = {
representative_trip_polyline: "asdf444$hhhhmnb",
stop_ids: ["123", "555", "777"],
route_id: "111",
shape_priority: 1,
shape_id: "1110180",
time_desc: null,
typicality: 1,
Expand Down Expand Up @@ -90,7 +87,6 @@ it("menuReducer handles 'toggleDirection'", () => {
stop_ids: ["123", "456", "789"],
route_id: "111",
shape_id: "1110177",
shape_priority: 1,
time_desc: null,
typicality: 1,
sort_order: 1,
Expand Down
9 changes: 1 addition & 8 deletions lib/dotcom_web/controllers/schedule/line/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,12 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do

@doc """
Filters a list of route patterns down to the route patterns sharing the lowest
number for the "typicality" property, additionally removing route patterns
associated with a negative shape_priority value.
number for the "typicality" property.
"""
@spec filtered_by_typicality([RoutePattern.t()]) :: [RoutePattern.t()]
def filtered_by_typicality(route_patterns) do
route_patterns
|> filter_by_min_typicality()
|> Enum.filter(fn x ->
# TODO: Deprecate our use of shape priority entirely,
# because it's no longer supported in the V3 API
# For now, be less strict if using the most typical route pattern
if x.typicality == 1, do: true, else: x.shape_priority > 0
end)
end

# Filters route patterns by the smallest typicality found in the array
Expand Down
30 changes: 14 additions & 16 deletions lib/route_patterns/route_pattern.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,38 @@ defmodule RoutePatterns.RoutePattern do

defstruct [
:direction_id,
:headsign,
:id,
:name,
:representative_trip_id,
:representative_trip_polyline,
:shape_id,
:shape_priority,
:headsign,
:stop_ids,
:route_id,
:time_desc,
:typicality,
:service_id,
:canonical,
sort_order: 0
:shape_id,
:time_desc,
canonical: false,
sort_order: 0,
stop_ids: [],
typicality: 5
]

@type id_t :: String.t()
@type typicality_t :: 0 | 1 | 2 | 3 | 4
@type typicality_t :: 0 | 1 | 2 | 3 | 4 | 5
@type t :: %__MODULE__{
canonical: boolean(),
direction_id: 0 | 1,
id: id_t(),
headsign: String.t(),
name: String.t(),
representative_trip_id: Trip.id_t(),
representative_trip_polyline: String.t(),
route_id: Route.id_t(),
service_id: String.t(),
shape_id: String.t(),
shape_priority: number,
headsign: String.t(),
sort_order: non_neg_integer(),
stop_ids: [Stop.id_t()],
route_id: Route.id_t(),
time_desc: String.t(),
typicality: typicality_t(),
sort_order: integer(),
canonical: boolean(),
service_id: String.t()
typicality: typicality_t()
}

def new(%Item{
Expand Down
11 changes: 0 additions & 11 deletions test/dotcom_web/controllers/schedule/line/helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -866,17 +866,6 @@ defmodule DotcomWeb.ScheduleController.Line.HelpersTest do
%RoutePatterns.RoutePattern{typicality: 5}
])
end

test "excludes route patterns with negative shape_priority" do
assert [%RoutePatterns.RoutePattern{typicality: 2, shape_priority: 1}, _] =
Helpers.filtered_by_typicality([
%RoutePatterns.RoutePattern{typicality: 2, shape_priority: -2},
%RoutePatterns.RoutePattern{typicality: 2, shape_priority: 1},
%RoutePatterns.RoutePattern{typicality: 2, shape_priority: 2},
%RoutePatterns.RoutePattern{typicality: 4, shape_priority: 1},
%RoutePatterns.RoutePattern{typicality: 5, shape_priority: 1}
])
end
end

describe "get_stop_tree_or_lists/2" do
Expand Down

0 comments on commit 1ac1088

Please sign in to comment.