Skip to content

Commit 88cba66

Browse files
committed
refactor(gtfs): optimize situation IDs fetch by eliminating duplicate DB queries
1 parent 0ac83b2 commit 88cba66

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

internal/gtfs/realtime.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,36 @@ func (manager *Manager) GetAlertsForRoute(routeID string) []gtfs.Alert {
105105
return alerts
106106
}
107107

108-
// GetAlertsForTrip returns alerts matching the trip, its route, or agency.
109108
// IMPORTANT: Caller must hold manager.RLock() before calling this method.
109+
func (manager *Manager) GetAlertsByIDs(tripID, routeID, agencyID string) []gtfs.Alert {
110+
manager.realTimeMutex.RLock()
111+
defer manager.realTimeMutex.RUnlock()
112+
113+
var alerts []gtfs.Alert
114+
115+
for _, alert := range manager.realTimeAlerts {
116+
if alert.InformedEntities == nil {
117+
continue
118+
}
119+
for _, entity := range alert.InformedEntities {
120+
if entity.TripID != nil && entity.TripID.ID == tripID {
121+
alerts = append(alerts, alert)
122+
break
123+
}
124+
if entity.RouteID != nil && routeID != "" && *entity.RouteID == routeID {
125+
alerts = append(alerts, alert)
126+
break
127+
}
128+
if entity.AgencyID != nil && agencyID != "" && *entity.AgencyID == agencyID {
129+
alerts = append(alerts, alert)
130+
break
131+
}
132+
}
133+
}
134+
return alerts
135+
}
136+
137+
// GetAlertsForTrip returns alerts matching the trip, its route, or agency.
110138
func (manager *Manager) GetAlertsForTrip(ctx context.Context, tripID string) []gtfs.Alert {
111139
var routeID string
112140
var agencyID string
@@ -122,38 +150,7 @@ func (manager *Manager) GetAlertsForTrip(ctx context.Context, tripID string) []g
122150
}
123151
}
124152

125-
manager.realTimeMutex.RLock()
126-
defer manager.realTimeMutex.RUnlock()
127-
128-
alertMap := make(map[string]gtfs.Alert)
129-
130-
for _, alert := range manager.realTimeAlerts {
131-
if alert.InformedEntities != nil {
132-
for _, entity := range alert.InformedEntities {
133-
if entity.TripID != nil && entity.TripID.ID == tripID {
134-
alertMap[alert.ID] = alert
135-
break
136-
}
137-
138-
if entity.RouteID != nil && routeID != "" && *entity.RouteID == routeID {
139-
alertMap[alert.ID] = alert
140-
break
141-
}
142-
143-
if entity.AgencyID != nil && agencyID != "" && *entity.AgencyID == agencyID {
144-
alertMap[alert.ID] = alert
145-
break
146-
}
147-
}
148-
}
149-
}
150-
151-
alerts := make([]gtfs.Alert, 0, len(alertMap))
152-
for _, alert := range alertMap {
153-
alerts = append(alerts, alert)
154-
}
155-
156-
return alerts
153+
return manager.GetAlertsByIDs(tripID, routeID, agencyID)
157154
}
158155

159156
func (manager *Manager) GetAlertsForStop(stopID string) []gtfs.Alert {

internal/restapi/trips_helper.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -768,27 +768,26 @@ func distanceToLineSegment(px, py, x1, y1, x2, y2 float64) (distance, ratio floa
768768

769769
// IMPORTANT: Caller must hold manager.RLock() before calling this method.
770770
func (api *RestAPI) GetSituationIDsForTrip(ctx context.Context, tripID string) []string {
771-
alerts := api.GtfsManager.GetAlertsForTrip(ctx, tripID)
772-
771+
var routeID string
773772
var agencyID string
774-
trip, err := api.GtfsManager.GtfsDB.Queries.GetTrip(ctx, tripID)
775-
if err != nil {
776-
return nil
777-
}
778-
route, err := api.GtfsManager.GtfsDB.Queries.GetRoute(ctx, trip.RouteID)
779-
if err == nil {
780-
agencyID = route.AgencyID
781-
}
782773

783-
situationIDs := make([]string, 0, len(alerts))
784-
for _, alert := range alerts {
785-
if alert.ID != "" {
786-
if agencyID != "" {
787-
situationIDs = append(situationIDs, utils.FormCombinedID(agencyID, alert.ID))
788-
} else {
789-
situationIDs = append(situationIDs, alert.ID)
774+
if api.GtfsManager.GtfsDB != nil {
775+
trip, err := api.GtfsManager.GtfsDB.Queries.GetTrip(ctx, tripID)
776+
if err == nil {
777+
routeID = trip.RouteID
778+
route, err := api.GtfsManager.GtfsDB.Queries.GetRoute(ctx, routeID)
779+
if err == nil {
780+
agencyID = route.AgencyID
790781
}
791782
}
792783
}
784+
785+
alerts := api.GtfsManager.GetAlertsByIDs(tripID, routeID, agencyID)
786+
787+
situationIDs := []string{}
788+
for _, alert := range alerts {
789+
situationIDs = append(situationIDs, alert.ID)
790+
}
791+
793792
return situationIDs
794793
}

0 commit comments

Comments
 (0)