Skip to content
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

fix: rides history page #867

Merged
merged 4 commits into from
Aug 30, 2024
Merged
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
58 changes: 51 additions & 7 deletions src/api/gtfsService.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { GtfsApi, GtfsRideWithRelatedPydanticModel } from 'open-bus-stride-client'
import axios from 'axios'
import {
GtfsApi,
GtfsRideStopPydanticModel,
GtfsRideWithRelatedPydanticModel,
} from 'open-bus-stride-client'
import moment, { Moment } from 'moment'
import { BusRoute, fromGtfsRoute } from 'src/model/busRoute'
import { BusStop, fromGtfsStop } from 'src/model/busStop'
import { API_CONFIG, MAX_HITS_COUNT } from 'src/api/apiConfig'
import { API_CONFIG, MAX_HITS_COUNT, BASE_PATH } from 'src/api/apiConfig'
// import { Route } from 'react-router'

const GTFS_API = new GtfsApi(API_CONFIG)
//const USER_CASES_API = new UserCasesApi(API_CONFIG)
const JOIN_SEPARATOR = ','
const SEARCH_MARGIN_HOURS = 4

type StopHitsPayLoadType = {
gtfsRideIds: string
gtfsStopIds: string
arrival_time_to: number
arrival_time_from: number
}

export async function getRoutesAsync(
fromTimestamp: moment.Moment,
toTimestamp: moment.Moment,
Expand Down Expand Up @@ -118,9 +130,41 @@ export async function getGtfsStopHitTimesAsync(stop: BusStop, timestamp: Moment)
.slice(0, MAX_HITS_COUNT)

const rideIds = closestInTimeRides.map((ride) => ride.id).join(JOIN_SEPARATOR)
const stopHits = await GTFS_API.gtfsRideStopsListGet({
gtfsRideIds: rideIds,
gtfsStopIds: stop.stopId.toString(),
})
return stopHits.sort((hit1, hit2) => +hit1.arrivalTime! - +hit2.arrivalTime!)

const minStartTime = Math.min(...rides.map((ride) => Number(ride.startTime)))
const maxEndTime = Math.max(...rides.map((ride) => Number(ride.endTime)))

/* Fix StopHits bugs next steps TODO:
1. Add a test to ensure this feature is working correctly.
2. Optimize the axios request to minimize latency. - Currently takes forever.
3. Fix any on this- define the hit type
*/

try {
const stopHitsRequestPayLoad: StopHitsPayLoadType = {
gtfsRideIds: rideIds,
gtfsStopIds: stop.stopId.toString(),
arrival_time_from: minStartTime,
arrival_time_to: maxEndTime,
}

const stopHitsRes = await axios.get(`${BASE_PATH}/gtfs_ride_stops/list`, {
params: stopHitsRequestPayLoad,
})

if (stopHitsRes.status !== 200) {
throw new Error(`Error fetching stop hits: ${stopHitsRes.statusText}`)
}

if (stopHitsRes.data.length === 0) {
throw new Error(`No stop hits found`)
}

const stopHits: GtfsRideStopPydanticModel[] = stopHitsRes.data

return stopHits.sort((hit1, hit2) => +hit1.arrivalTime! - +hit2.arrivalTime!)
} catch (error) {
console.error(`Error fetching stop hits:`, error)
return []
Copy link
Member

Choose a reason for hiding this comment

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

nice. I guess we can use some error boundary component to render a meaning full error message to the user

}
}
Loading