Skip to content

feat(itinerary): add ability to sort itineraries by fare #1411

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

Open
wants to merge 6 commits into
base: dev
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
1 change: 1 addition & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ components:
selectCost: Cost
selectDepartureTime: Departure time
selectDuration: Duration
selectFare: Transit fare
selectWalkTime: Walk time
sortResults: Sort results
viewAll: View all options
Expand Down
9 changes: 8 additions & 1 deletion lib/components/util/sortOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const sortOptions = (
'ARRIVALTIME',
'WALKTIME',
'COST',
'DEPARTURETIME'
'DEPARTURETIME',
'FARE'
]
): SortOptionEntry[] => {
const sortOptionsArray: SortOptionEntry[] = [
Expand Down Expand Up @@ -51,6 +52,12 @@ export const sortOptions = (
id: 'components.NarrativeItinerariesHeader.selectCost'
}),
value: 'COST'
},
{
text: intl.formatMessage({
id: 'components.NarrativeItinerariesHeader.selectFare'
}),
value: 'FARE'
}
]

Expand Down
1 change: 1 addition & 0 deletions lib/util/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export type ItinerarySortOption =
| 'WALKTIME'
| 'COST'
| 'DEPARTURETIME'
| 'FARE'

export interface ItineraryCostWeights {
driveReluctance: number
Expand Down
5 changes: 4 additions & 1 deletion lib/util/itinerary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface ItineraryWithCO2Info extends Itinerary {
export interface ItineraryWithSortingCosts extends Itinerary {
rank: number
totalFare: number
transitFare?: number
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed this is an optional, which leads me to my next question. I think either you should remove the optional here or remove the || 0 below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the optional, as both values of zero and undefined do not change the sort order

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe if we get an undefined fare we return .01 or something? So that the confirmed free trips are sorted above fares where we don't actually know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if a fare is undefined would that be a data issue? Should we try to handle data issues?

Copy link
Contributor

Choose a reason for hiding this comment

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

If agencies don't provide us fares we display "no fare available" and i think that's fine to do, I just don't know how it fits into this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I swapped back to optional to better handle undefined fare values.

}

export interface ItineraryFareSummary {
Expand Down Expand Up @@ -453,10 +454,12 @@ export function addSortingCosts<T extends Itinerary>(
totalFareResult === null ? Number.MAX_VALUE : totalFareResult

const rank = calculateItineraryCost(itinerary, config)
const transitFare = getFare(itinerary).transitFare
return {
...itinerary,
rank,
totalFare
totalFare,
transitFare
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/util/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ export function getActiveSearch(state) {
return state.otp.searches[state.otp.activeSearchId]
}

function compareItineraryFare(a, b) {
// If both fares are not defined, they are equal.
if (a === undefined && b === undefined) return 0
// If one fare is not defined, the other is greater.
if (a === undefined) return 1
if (b === undefined) return -1
// If both fares are defined, compare them.
return a - b
}

/**
* Array sort function for itineraries (in batch routing context) that attempts
* to sort based on the type/direction specified.
Expand All @@ -50,6 +60,8 @@ export function sortItineraries(type, direction, a, b, config) {
return dirFactor * (a.duration - b.duration)
case 'COST':
return dirFactor * (a.totalFare - b.totalFare)
case 'FARE':
return dirFactor * compareItineraryFare(a.transitFare, b.transitFare)
default:
if (type !== 'BEST')
console.warn(`Sort (${type}) not supported. Defaulting to BEST.`)
Expand Down
Loading