Skip to content

Commit 60c4ad3

Browse files
feat: Make trips more robust for errors and API calls. (#314)
* feat: Make trips more robust for errors and API calls. * Change trip color from light blue to red. Red is already used for the slowest traffic but overall still better than the blue which matches river/water. 67e6da2
1 parent 7ff755d commit 60c4ad3

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/Trip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class Trip {
118118
*/
119119
export function getColor(tripIdx) {
120120
const colors = [
121-
"#0dcaf0", // Cyan
122121
"#97cc04", // Lime Green
123122
"#198754", // Green
124123
"#6A0DAD", // Purple
@@ -127,6 +126,7 @@ export function getColor(tripIdx) {
127126
"#d63384", // Magenta
128127
"#f45d01", // Orange
129128
"#fdc500", // Gold
129+
"#e63946", // Vibrant Red
130130
];
131131
return colors[tripIdx % colors.length];
132132
}

src/TripLogs.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)