Tolerate exactly one extra trailing lap when aligning workout targets

Confirmed via Garmin Connect against a real activity ("Auriol - W3-3-Double
Barrel"): the workout's step count didn't match its 18 recorded laps
because the athlete kept running 6:46 past the prescribed 5-minute
cool-down, logged as an 18th lap the workout never defined. The strict
equality check meant this one extra lap discarded every other lap's real
target too, not just its own.

alignWorkoutTargets now tolerates exactly one extra recorded lap beyond the
step count: the steps that do exist still zip to their laps normally, and
only the trailing extra lap is left without a target. Any larger mismatch
still falls back to nil for every lap, since that can't be trusted at all.
This commit is contained in:
2026-07-19 18:01:06 +02:00
parent 513418123f
commit 9818d35910
2 changed files with 61 additions and 17 deletions

View File

@@ -118,19 +118,30 @@ func toLapRows(laps []garmin.Lap, samples []garmin.Sample, targets []*garmin.Wor
// alignWorkoutTargets zips an activity's recorded laps against its
// structured workout's flattened steps, returning one *garmin.WorkoutStep
// per lap (nil where unavailable). Zipping only happens when the counts
// match exactly -- a mismatch (extra manual laps, auto-lap-by-distance also
// firing, etc.) means we can't trust the alignment, so every entry comes
// back nil rather than risk showing a target against the wrong lap.
// per lap (nil where unavailable).
//
// Confirmed against a real activity (via Garmin Connect's own workout view)
// that recording sometimes continues one lap past the end of the workout's
// last step -- e.g. a 5-minute prescribed cool-down followed by another
// 6:46 the athlete just kept running, logged as a further lap Garmin never
// defined a target for. That shows up here as exactly one more recorded lap
// than the workout has steps, so that specific case zips the steps that do
// exist and leaves the trailing extra lap unmapped, rather than discarding
// every other lap's real target along with it.
//
// Any other mismatch (extra manual laps, auto-lap-by-distance also firing,
// etc.) can't be trusted at all, so every entry comes back nil rather than
// risk showing a target against the wrong lap.
func alignWorkoutTargets(laps []garmin.Lap, workout garmin.Workout) []*garmin.WorkoutStep {
steps := workout.FlattenSteps()
if len(steps) != len(laps) {
return make([]*garmin.WorkoutStep, len(laps))
}
out := make([]*garmin.WorkoutStep, len(steps))
for i := range steps {
s := steps[i]
out[i] = &s
out := make([]*garmin.WorkoutStep, len(laps))
switch len(laps) - len(steps) {
case 0, 1:
for i := range steps {
s := steps[i]
out[i] = &s
}
}
return out
}