Skip to content
Open
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: 19 additions & 10 deletions internal/restapi/trips_for_route_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"database/sql"
"net/http"
"sort"
"time"

"github.com/OneBusAway/go-gtfs"
Expand Down Expand Up @@ -57,12 +58,11 @@
}

// Calculate nanoseconds since midnight of the service day
serviceDayMidnight := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location())
nanosSinceMidnight := currentTime.Sub(serviceDayMidnight).Nanoseconds()
if nanosSinceMidnight < 0 {
nanosSinceMidnight = 0
serviceDayMidnight := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentLocation())

Check failure on line 61 in internal/restapi/trips_for_route_handler.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: cannot call non-function currentLocation (variable of type *time.Location) (typecheck)

Check failure on line 61 in internal/restapi/trips_for_route_handler.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: cannot call non-function currentLocation (variable of type *time.Location)) (typecheck)

Check failure on line 61 in internal/restapi/trips_for_route_handler.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

invalid operation: cannot call non-function currentLocation (variable of type *time.Location)

Check failure on line 61 in internal/restapi/trips_for_route_handler.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

invalid operation: cannot call non-function currentLocation (variable of type *time.Location)
currentNanosSinceMidnight := currentTime.Sub(serviceDayMidnight).Nanoseconds()
if currentNanosSinceMidnight < 0 {
currentNanosSinceMidnight = 0
}
currentNanosSinceMidnight := nanosSinceMidnight

indexIDs, err := api.GtfsManager.GtfsDB.Queries.GetBlockTripIndexIDsForRoute(ctx, gtfsdb.GetBlockTripIndexIDsForRouteParams{
RouteID: routeID,
Expand Down Expand Up @@ -126,8 +126,13 @@
HasVehicle bool
}
var activeTrips []ActiveTripEntry

// Extract block IDs from map and sort them to enable deterministic iteration.
deterministicBlockIDs := make([]string, 0, len(allLinkedBlocks))
for blockID := range allLinkedBlocks {
deterministicBlockIDs = append(deterministicBlockIDs, blockID)
}
sort.Strings(deterministicBlockIDs)
for _, blockID := range deterministicBlockIDs {
blockIDNullStr := sql.NullString{String: blockID, Valid: true}

tripsInBlock, err := api.GtfsManager.GtfsDB.Queries.GetTripsInBlock(ctx, gtfsdb.GetTripsInBlockParams{
Expand Down Expand Up @@ -172,14 +177,16 @@
}

tripIDsSet := make(map[string]bool)

for _, entry := range activeTrips {
tripIDsSet[entry.TripID] = true
}
var tripIDs []string

for id := range tripIDsSet {
tripIDs = append(tripIDs, id)
}

sort.Strings(tripIDs)
var fetchedTrips []gtfsdb.Trip
if len(tripIDs) > 0 {
fetchedTrips, err = api.GtfsManager.GtfsDB.Queries.GetTripsByIDs(ctx, tripIDs)
Expand All @@ -199,6 +206,7 @@
for id := range routeIDsSet {
routeIDs = append(routeIDs, id)
}
sort.Strings(routeIDs)

var fetchedRoutes []gtfsdb.Route
if len(routeIDs) > 0 {
Expand All @@ -220,7 +228,6 @@
}
}

todayMidnight := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentLocation)
stopIDsMap := make(map[string]bool)

var result []models.TripsForRouteListEntry
Expand Down Expand Up @@ -261,7 +268,7 @@
Frequency: nil,
Schedule: schedule,
Status: status,
ServiceDate: todayMidnight.UnixMilli(),
ServiceDate: serviceDayMidnight.UnixMilli(),
SituationIds: api.GetSituationIDsForTrip(r.Context(), tripID),
TripId: utils.FormCombinedID(agencyID, tripID),
}
Expand Down Expand Up @@ -343,6 +350,7 @@
for id := range presentTrips {
tripIDsToFetch = append(tripIDsToFetch, id)
}
sort.Strings(tripIDsToFetch)

if len(tripIDsToFetch) > 0 {
fetchedTrips, err := api.GtfsManager.GtfsDB.Queries.GetTripsByIDs(ctx, tripIDsToFetch)
Expand Down Expand Up @@ -371,7 +379,8 @@
for id := range presentRoutes {
routeIDsToFetch = append(routeIDsToFetch, id)
}


sort.Strings(routeIDsToFetch)
presentAgencies := make(map[string]models.AgencyReference)

if len(routeIDsToFetch) > 0 {
Expand Down
Loading