Skip to content
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
29 changes: 22 additions & 7 deletions internal/restapi/stops_for_route_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net/http"
"sort"
"sync"
"time"

"github.com/twpayne/go-polyline"
Expand Down Expand Up @@ -215,14 +216,28 @@ func buildStopsList(ctx context.Context, api *RestAPI, calc *GTFS.AdvancedDirect
stopIDs = append(stopIDs, stopID)
}

stops, err := api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, stopIDs)
if err != nil {
return nil, err
// Fetch stops and routes in parallel for improved performance
var stops []gtfsdb.Stop
var routeRows []gtfsdb.GetRouteIDsForStopsRow
var stopsErr, routesErr error
var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()
stops, stopsErr = api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, stopIDs)
}()
go func() {
defer wg.Done()
routeRows, routesErr = api.GtfsManager.GtfsDB.Queries.GetRouteIDsForStops(ctx, stopIDs)
}()
wg.Wait()

if stopsErr != nil {
return nil, stopsErr
}

routeRows, err := api.GtfsManager.GtfsDB.Queries.GetRouteIDsForStops(ctx, stopIDs)
if err != nil {
return nil, err
if routesErr != nil {
return nil, routesErr
}

// Organize Routes in Memory
Expand Down
31 changes: 23 additions & 8 deletions internal/restapi/trip_details_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"strconv"
"sync"
"time"

"maglev.onebusaway.org/gtfsdb"
Expand Down Expand Up @@ -309,21 +310,35 @@ func (api *RestAPI) buildStopReferences(ctx context.Context, calc *GTFS.Advanced
return []models.Stop{}, nil
}

stops, err := api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, originalStopIDs)
if err != nil {
return nil, err
// Fetch stops and routes in parallel for improved performance
var stops []gtfsdb.Stop
var allRoutes []gtfsdb.GetRoutesForStopsRow
var stopsErr, routesErr error
var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()
stops, stopsErr = api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, originalStopIDs)
}()
go func() {
defer wg.Done()
allRoutes, routesErr = api.GtfsManager.GtfsDB.Queries.GetRoutesForStops(ctx, originalStopIDs)
}()
wg.Wait()

if stopsErr != nil {
return nil, stopsErr
}
if routesErr != nil {
return nil, routesErr
}

stopMap := make(map[string]gtfsdb.Stop)
for _, stop := range stops {
stopMap[stop.ID] = stop
}

allRoutes, err := api.GtfsManager.GtfsDB.Queries.GetRoutesForStops(ctx, originalStopIDs)
if err != nil {
return nil, err
}

routesByStop := make(map[string][]gtfsdb.Route)
for _, routeRow := range allRoutes {
if ctx.Err() != nil {
Expand Down
32 changes: 24 additions & 8 deletions internal/restapi/trip_for_vehicle_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"net/http"
"strconv"
"sync"
"time"

"maglev.onebusaway.org/gtfsdb"
Expand Down Expand Up @@ -301,20 +302,35 @@ func BuildStopReferencesAndRouteIDsForStops(api *RestAPI, ctx context.Context, a
}
}

stopsDB, err := api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, uniqueStopIDs)
if err != nil {
return nil, nil, err
// Fetch stops and routes in parallel for improved performance
var stopsDB []gtfsdb.Stop
var allRoutes []gtfsdb.GetRoutesForStopsRow
var stopsErr, routesErr error
var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()
stopsDB, stopsErr = api.GtfsManager.GtfsDB.Queries.GetStopsByIDs(ctx, uniqueStopIDs)
}()
go func() {
defer wg.Done()
allRoutes, routesErr = api.GtfsManager.GtfsDB.Queries.GetRoutesForStops(ctx, uniqueStopIDs)
}()
wg.Wait()

if stopsErr != nil {
return nil, nil, stopsErr
}
if routesErr != nil {
return nil, nil, routesErr
}

stopMap := make(map[string]gtfsdb.Stop)
for _, stop := range stopsDB {
stopMap[stop.ID] = stop
}

allRoutes, err := api.GtfsManager.GtfsDB.Queries.GetRoutesForStops(ctx, uniqueStopIDs)
if err != nil {
return nil, nil, err
}

routesByStop := make(map[string][]gtfsdb.Route)
uniqueRouteMap := make(map[string]gtfsdb.GetRoutesForStopsRow)
for _, routeRow := range allRoutes {
Expand Down
Loading