2026-07-17 18:33:06 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Lap is one lap/split of an activity, from get_activity_splits, plus
|
|
|
|
|
// derived HR drift/recovery metrics computed from activity_samples.
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
//
|
|
|
|
|
// Deliberately NOT modeled here: StartTimeUTC, DistanceMeters, MaxHR,
|
|
|
|
|
// MaxSpeedMps, ElevationGainM, ElevationLossM (removed in the 2026-07
|
|
|
|
|
// dedup pass -- zero consumers anywhere, pure duplicates of RawJSON), plus
|
|
|
|
|
// DurationSeconds and AvgHR, which DO have a real frontend chart need but no
|
|
|
|
|
// backend one, so the API layer decodes them from RawJSON at response time
|
|
|
|
|
// instead of storing a redundant copy -- see decodeLapDisplayFields.
|
2026-07-17 18:33:06 +02:00
|
|
|
type Lap struct {
|
|
|
|
|
ID int64
|
|
|
|
|
ActivityID int64
|
|
|
|
|
LapIndex int
|
|
|
|
|
AvgSpeedMps *float64
|
|
|
|
|
IntensityType string
|
|
|
|
|
HRDriftBpmPerMin *float64
|
|
|
|
|
HRRecoveryBpmPerMin *float64
|
2026-07-19 11:55:13 +02:00
|
|
|
// TargetPaceLowMps/High and TargetHRLowBpm/High hold the expected band
|
|
|
|
|
// for this lap, resolved from the activity's structured Garmin workout
|
|
|
|
|
// (see internal/sync's alignWorkoutTargets) when its steps line up 1:1
|
|
|
|
|
// with the recorded laps. Nil when the activity has no structured
|
|
|
|
|
// workout, the step counts don't match, or the step targets neither
|
|
|
|
|
// pace nor heart rate.
|
|
|
|
|
TargetPaceLowMps *float64
|
|
|
|
|
TargetPaceHighMps *float64
|
|
|
|
|
TargetHRLowBpm *float64
|
|
|
|
|
TargetHRHighBpm *float64
|
|
|
|
|
RawJSON string
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReplaceLaps deletes any existing laps for activityID and inserts the given
|
|
|
|
|
// set, so re-syncing an activity's splits is idempotent.
|
|
|
|
|
func (db *DB) ReplaceLaps(ctx context.Context, activityID int64, laps []Lap) error {
|
|
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("begin replace laps tx: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM laps WHERE activity_id = ?`, activityID); err != nil {
|
|
|
|
|
return fmt.Errorf("delete existing laps for activity %d: %w", activityID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, l := range laps {
|
|
|
|
|
_, err := tx.ExecContext(ctx, `
|
|
|
|
|
INSERT INTO laps (
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
activity_id, lap_index, avg_speed_mps,
|
2026-07-19 11:55:13 +02:00
|
|
|
intensity_type, hr_drift_bpm_per_min, hr_recovery_bpm_per_min,
|
|
|
|
|
target_pace_low_mps, target_pace_high_mps, target_hr_low_bpm, target_hr_high_bpm, raw_json
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
) VALUES (?,?,?,?,?,?,?,?,?,?,?)`,
|
|
|
|
|
activityID, l.LapIndex, l.AvgSpeedMps,
|
2026-07-19 11:55:13 +02:00
|
|
|
l.IntensityType, l.HRDriftBpmPerMin, l.HRRecoveryBpmPerMin,
|
|
|
|
|
l.TargetPaceLowMps, l.TargetPaceHighMps, l.TargetHRLowBpm, l.TargetHRHighBpm, l.RawJSON,
|
2026-07-17 18:33:06 +02:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("insert lap %d for activity %d: %w", l.LapIndex, activityID, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tx.Commit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LapsForActivity returns all laps for an activity, ordered by lap_index.
|
|
|
|
|
func (db *DB) LapsForActivity(ctx context.Context, activityID int64) ([]Lap, error) {
|
|
|
|
|
rows, err := db.QueryContext(ctx, `
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
SELECT id, activity_id, lap_index, avg_speed_mps,
|
2026-07-19 11:55:13 +02:00
|
|
|
intensity_type, hr_drift_bpm_per_min, hr_recovery_bpm_per_min,
|
|
|
|
|
target_pace_low_mps, target_pace_high_mps, target_hr_low_bpm, target_hr_high_bpm, raw_json
|
2026-07-17 18:33:06 +02:00
|
|
|
FROM laps WHERE activity_id = ? ORDER BY lap_index`, activityID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("laps for activity %d: %w", activityID, err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
laps := []Lap{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var l Lap
|
|
|
|
|
if err := rows.Scan(
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
&l.ID, &l.ActivityID, &l.LapIndex, &l.AvgSpeedMps,
|
2026-07-19 11:55:13 +02:00
|
|
|
&l.IntensityType, &l.HRDriftBpmPerMin, &l.HRRecoveryBpmPerMin,
|
|
|
|
|
&l.TargetPaceLowMps, &l.TargetPaceHighMps, &l.TargetHRLowBpm, &l.TargetHRHighBpm, &l.RawJSON,
|
2026-07-17 18:33:06 +02:00
|
|
|
); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("scan lap row: %w", err)
|
|
|
|
|
}
|
|
|
|
|
laps = append(laps, l)
|
|
|
|
|
}
|
|
|
|
|
return laps, rows.Err()
|
|
|
|
|
}
|