@@ -132,12 +132,33 @@ function processRawLogs(rawLogs, solutionType) {
132132 let sortedLogs = isReversed ? _ . reverse ( origLogs ) : origLogs ;
133133 let newLogs = [ ] ;
134134
135+ const tripBoundaries = new Map ( ) ; // tripId -> { minTime, maxTime }
136+
137+ // First pass to find boundaries for each trip
138+ for ( const origLog of sortedLogs ) {
139+ const apiCall = processApiCall ( origLog ) ;
140+ if ( apiCall && apiCall . response ) {
141+ const currentTrips = apiCall . response . currenttrips ;
142+ if ( currentTrips && Array . isArray ( currentTrips ) ) {
143+ const timestampMS = new Date ( origLog . timestamp || origLog . servertime ) . getTime ( ) ;
144+ for ( const tripId of currentTrips ) {
145+ if ( ! tripBoundaries . has ( tripId ) ) {
146+ tripBoundaries . set ( tripId , { minTime : timestampMS , maxTime : timestampMS } ) ;
147+ } else {
148+ const bounds = tripBoundaries . get ( tripId ) ;
149+ bounds . minTime = Math . min ( bounds . minTime , timestampMS ) ;
150+ bounds . maxTime = Math . max ( bounds . maxTime , timestampMS ) ;
151+ }
152+ }
153+ }
154+ }
155+ }
156+
135157 const lastKnownState = {
136158 location : null ,
137159 heading : 0 ,
138160 routeSegment : null ,
139161 routeSegmentTraffic : null ,
140- currentTrips : [ ] ,
141162 responseLocation : null ,
142163 responseHeading : 0 ,
143164 } ;
@@ -157,7 +178,6 @@ function processRawLogs(rawLogs, solutionType) {
157178 newLog . request = apiCall . request ;
158179 newLog . response = apiCall . response ;
159180 newLog . error = apiCall . error ;
160- const hasApiError = ! ! newLog . error || ( origLog . jsonpayload && origLog . jsonpayload . errorresponse ) ;
161181
162182 adjustFieldFormats ( solutionType , newLog ) ;
163183
@@ -242,13 +262,16 @@ function processRawLogs(rawLogs, solutionType) {
242262 }
243263 }
244264
245- // Keep the same current trips if we had an API error
246- if ( hasApiError && lastKnownState . currentTrips . length > 0 ) {
247- log ( `Preserving current trips due to API error for log at ${ newLog . timestamp } ` ) ;
248- if ( ! newLog . response ) newLog . response = { } ;
249- newLog . response . currenttrips = [ ...lastKnownState . currentTrips ] ;
250- } else if ( _ . get ( newLog , "response.currenttrips" ) ) {
251- lastKnownState . currentTrips = [ ...newLog . response . currenttrips ] ;
265+ const newCurrentTrips = _ . get ( newLog , "response.currenttrips" ) ;
266+
267+ if ( ! newCurrentTrips || newCurrentTrips . length === 0 ) {
268+ for ( const [ tripId , bounds ] of tripBoundaries . entries ( ) ) {
269+ if ( newLog . timestampMS >= bounds . minTime && newLog . timestampMS <= bounds . maxTime ) {
270+ log ( `Backfilling trip ${ tripId } for log at ${ newLog . timestamp } ` ) ;
271+ newLog . lastlocationResponse . currenttrips = [ tripId ] ;
272+ break ;
273+ }
274+ }
252275 }
253276
254277 // Sort currentTrips array since sometimes it could contain multiple trip ids in random order
@@ -326,7 +349,7 @@ class TripLogs {
326349 }
327350
328351 // Check trip ID in vehicle rows
329- const currentTrips = _ . get ( le , "response.currenttrips" ) ;
352+ const currentTrips = _ . get ( le , "response.currenttrips" ) || _ . get ( le , "lastlocationResponse.currenttrips" ) ;
330353 if ( currentTrips && Array . isArray ( currentTrips ) ) {
331354 return currentTrips . some ( ( id ) => id . includes ( trimmedFilter ) ) ;
332355 }
@@ -506,7 +529,10 @@ class TripLogs {
506529 const stopsLeft = _ . get ( logEntry , "response.remainingvehiclejourneysegments" ) ;
507530 return stopsLeft && "Stops Left " + stopsLeft . length ;
508531 } else {
509- const currentTrips = _ . get ( logEntry , "response.currenttrips" ) ;
532+ let currentTrips = _ . get ( logEntry , "response.currenttrips" ) ;
533+ if ( ! currentTrips || currentTrips . length === 0 ) {
534+ currentTrips = _ . get ( logEntry , "lastlocationResponse.currenttrips" ) ;
535+ }
510536 if ( currentTrips && Array . isArray ( currentTrips ) && currentTrips . length > 0 ) {
511537 return currentTrips [ 0 ] ;
512538 }
0 commit comments