Skip to content

Commit

Permalink
fix: rides history page (#867)
Browse files Browse the repository at this point in the history
Fixes issue #856 by adding the required "arrival_time_from" and "arrival_time_to" fields to the gtfs_ride_stops api call.
Changed the previous api call which utilized the gtfsAPI to an axios call directly to the server with the required data since the gtfsAPI whitelisted the required fields mentioned above which caused the api call to fail.

Still requires futher optimizations since the request is very slow and tests need to be written in future issues.
  • Loading branch information
TomRytt authored Aug 30, 2024
1 parent f9b69fe commit 2c9611e
Showing 1 changed file with 51 additions and 7 deletions.
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 []
}
}

0 comments on commit 2c9611e

Please sign in to comment.