fix: trip builder handles NULL-GPS speed-only rows (BUG-16 follow-up)

Trips starting/ending on speed-only rows violated the trips.*GPSX NOT NULL
constraint. Use the first/last GPS-bearing row for start/end coordinates, and
skip a trip that never got any GPS fix (no location to record).
This commit is contained in:
Mathieu Moran
2026-08-13 15:52:07 -04:00
parent e5ac14e38b
commit f5f20c315b
2 changed files with 36 additions and 9 deletions
+16 -4
View File
@@ -198,16 +198,28 @@ def build_trip_summaries(raw_rows):
if not any(event["obd"] for event in events) and duration_seconds < 10:
continue
# Speed-only rows (logged before the first GPS fix) have NULL coords.
# Use the first/last row that actually has a position for start/end GPS,
# and skip a trip that never got any fix at all (no location to record).
gps_rows = [
r for r in trip_rows
if r["latitude"] is not None and r["longitude"] is not None
]
if not gps_rows:
continue
start_gps = gps_rows[0]
end_gps = gps_rows[-1]
trips.append({
"id": start["id"],
"start_tracking_id": start["id"],
"end_tracking_id": end["id"],
"start_datetime": start["datetime"].isoformat(sep=" "),
"end_datetime": end["datetime"].isoformat(sep=" "),
"start_gpsx": start["latitude"],
"start_gpsy": start["longitude"],
"end_gpsx": end["latitude"],
"end_gpsy": end["longitude"],
"start_gpsx": start_gps["latitude"],
"start_gpsy": start_gps["longitude"],
"end_gpsx": end_gps["latitude"],
"end_gpsy": end_gps["longitude"],
"distance_km": round(distance_km, 3),
"duration_seconds": duration_seconds,
"point_count": len(trip_rows),