Initial commit: smartrun MVP
Garmin run classification and progression tracker. Go backend (MCP client to mcp-garmin, SQLite store, deterministic rule engine, REST API) and React/TS frontend (Dashboard, Review Queue, Workout Kinds). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
85
backend/internal/store/laps.go
Normal file
85
backend/internal/store/laps.go
Normal file
@@ -0,0 +1,85 @@
|
||||
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.
|
||||
type Lap struct {
|
||||
ID int64
|
||||
ActivityID int64
|
||||
LapIndex int
|
||||
StartTimeUTC string
|
||||
DurationSeconds float64
|
||||
DistanceMeters float64
|
||||
AvgHR *float64
|
||||
MaxHR *float64
|
||||
AvgSpeedMps *float64
|
||||
MaxSpeedMps *float64
|
||||
ElevationGainM *float64
|
||||
ElevationLossM *float64
|
||||
IntensityType string
|
||||
HRDriftBpmPerMin *float64
|
||||
HRRecoveryBpmPerMin *float64
|
||||
RawJSON string
|
||||
}
|
||||
|
||||
// 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 (
|
||||
activity_id, lap_index, start_time_utc, duration_seconds, distance_meters,
|
||||
avg_hr, max_hr, avg_speed_mps, max_speed_mps, elevation_gain_m, elevation_loss_m,
|
||||
intensity_type, hr_drift_bpm_per_min, hr_recovery_bpm_per_min, raw_json
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
||||
activityID, l.LapIndex, l.StartTimeUTC, l.DurationSeconds, l.DistanceMeters,
|
||||
l.AvgHR, l.MaxHR, l.AvgSpeedMps, l.MaxSpeedMps, l.ElevationGainM, l.ElevationLossM,
|
||||
l.IntensityType, l.HRDriftBpmPerMin, l.HRRecoveryBpmPerMin, l.RawJSON,
|
||||
)
|
||||
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, `
|
||||
SELECT id, activity_id, lap_index, start_time_utc, duration_seconds, distance_meters,
|
||||
avg_hr, max_hr, avg_speed_mps, max_speed_mps, elevation_gain_m, elevation_loss_m,
|
||||
intensity_type, hr_drift_bpm_per_min, hr_recovery_bpm_per_min, raw_json
|
||||
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(
|
||||
&l.ID, &l.ActivityID, &l.LapIndex, &l.StartTimeUTC, &l.DurationSeconds, &l.DistanceMeters,
|
||||
&l.AvgHR, &l.MaxHR, &l.AvgSpeedMps, &l.MaxSpeedMps, &l.ElevationGainM, &l.ElevationLossM,
|
||||
&l.IntensityType, &l.HRDriftBpmPerMin, &l.HRRecoveryBpmPerMin, &l.RawJSON,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan lap row: %w", err)
|
||||
}
|
||||
laps = append(laps, l)
|
||||
}
|
||||
return laps, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user