all time calculations done within database
All checks were successful
pedestrian-simulator / build (push) Successful in 1m0s

This commit is contained in:
2026-01-11 21:07:37 -07:00
parent 6e5fe90d29
commit 89816a5a8c
2 changed files with 30 additions and 28 deletions

View File

@@ -93,10 +93,6 @@ func (sm *StepManager) SaveTripState() {
}
func (sm *StepManager) saveTripStateLocked() {
fmt.Printf("[StepManager] Saving trip state for %s: StartTime=%v (%s), LastSync=%v (%s)\n",
sm.userID, sm.tripState.StartTime, sm.tripState.StartTime.Location(),
sm.lastSyncTime, sm.lastSyncTime.Location())
_, err := db.Exec(`
INSERT INTO trips (user_id, start_date, start_time, start_day_initial_steps, previous_total_steps, target_total_steps, last_sync_time, next_sync_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
@@ -248,6 +244,20 @@ func (sm *StepManager) performSync(interval time.Duration) {
sm.lastSyncTime = time.Now()
sm.nextSyncTime = time.Now().Add(interval)
// Save final state with explicit NOW() for last_sync_time to ensure DB consistency
// next_sync_time is also calculated in SQL relative to the same NOW()
_, err = db.Exec(`
UPDATE trips SET
last_sync_time = NOW(),
next_sync_time = DATE_ADD(NOW(), INTERVAL ? SECOND),
previous_total_steps = ?,
target_total_steps = ?
WHERE user_id = ?
`, interval.Seconds(), sm.previousTotalSteps, sm.targetTotalSteps, sm.userID)
if err != nil {
fmt.Printf("Error saving sync completion: %v\n", err)
}
fmt.Printf("Sync Complete. Total Trip Steps: %d\n", sm.targetTotalSteps)
}