fix(phase26): server trip delete persists across recompute + batch delete with checkboxes

This commit is contained in:
Mathieu Moran
2026-08-11 04:18:58 -04:00
parent e6bc488e84
commit bdc7a9afb7
6 changed files with 133 additions and 5 deletions
+44 -3
View File
@@ -398,6 +398,15 @@ def compute_trips_for_device(device_id):
"point_count": len(seg_rows),
})
# Skip trips that were manually deleted (recompute won't re-insert them).
with get_cursor() as cur:
cur.execute(
"SELECT start_time FROM deleted_trip_markers WHERE device_id = %s",
(device_id,),
)
deleted_starts = {row[0] for row in cur.fetchall()}
trips = [t for t in trips if t["start_time"] not in deleted_starts]
with get_cursor(commit=True) as cur:
for t in trips:
cur.execute(
@@ -540,13 +549,45 @@ def get_dashcam_file(device_id, file_id):
def delete_trip(device_id, trip_id):
"""Delete a computed trip record. Raw tracking_points are NOT removed."""
"""Delete a computed trip record and mark its time range as deleted
so auto-recompute doesn't re-insert it. Raw tracking_points are kept."""
with get_cursor(commit=True) as cur:
# Record the start_time before deleting so recompute skips this trip.
cur.execute(
"DELETE FROM trips WHERE id = %s AND device_id = %s RETURNING id",
"SELECT start_time FROM trips WHERE id = %s AND device_id = %s",
(trip_id, device_id),
)
return cur.fetchone() is not None
row = cur.fetchone()
if row is None:
return False
cur.execute(
"""INSERT INTO deleted_trip_markers (device_id, start_time)
VALUES (%s, %s) ON CONFLICT DO NOTHING""",
(device_id, row[0]),
)
cur.execute(
"DELETE FROM trips WHERE id = %s AND device_id = %s",
(trip_id, device_id),
)
return True
def delete_trips_batch(device_id, trip_ids):
"""Delete multiple trips in one transaction."""
deleted = 0
for tid in trip_ids:
if delete_trip(device_id, tid):
deleted += 1
return deleted
def clear_deleted_trip_markers(device_id):
"""Remove all deleted trip markers so recompute rebuilds everything."""
with get_cursor(commit=True) as cur:
cur.execute(
"DELETE FROM deleted_trip_markers WHERE device_id = %s",
(device_id,),
)
def delete_dashcam_file(device_id, file_id):