2026-07-17 18:33:06 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-24 21:08:07 +02:00
|
|
|
// Activity is geniusrun's persisted view of a Garmin activity. Field mapping
|
2026-07-17 18:33:06 +02:00
|
|
|
// from the Garmin/MCP response happens in internal/sync, not here, so this
|
|
|
|
|
// package stays independent of internal/garmin.
|
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 (though Garmin's response includes them):
|
|
|
|
|
// ActivityName/ActivityType, plus every field removed in the 2026-07
|
|
|
|
|
// dedup pass (BeginTimestampMs, MaxSpeedMps, ElevationLossM, Calories,
|
|
|
|
|
// LapCount, TrainingEffectLabel, HrTimeInZone1-5). None of them are read by
|
|
|
|
|
// any SQL query, the classify rule engine, or any other Go code -- they'd be
|
|
|
|
|
// pure duplicates of RawJSON with no purpose beyond being slightly more
|
|
|
|
|
// convenient to read than parsing JSON. ActivityName/ActivityType do have a
|
|
|
|
|
// real frontend display need, so the API layer (internal/api) decodes them
|
|
|
|
|
// from RawJSON at response time instead of storing a redundant copy -- see
|
|
|
|
|
// decodeActivityDisplayFields.
|
2026-07-17 18:33:06 +02:00
|
|
|
type Activity struct {
|
|
|
|
|
ID int64
|
|
|
|
|
GarminActivityID int64
|
2026-07-19 10:52:09 +02:00
|
|
|
EventTypeKey string
|
2026-07-19 11:55:13 +02:00
|
|
|
WorkoutID *int64
|
2026-07-17 18:33:06 +02:00
|
|
|
StartTimeUTC string
|
|
|
|
|
DurationSeconds float64
|
|
|
|
|
DistanceMeters float64
|
|
|
|
|
AvgHR *float64
|
|
|
|
|
MaxHR *float64
|
|
|
|
|
AvgSpeedMps *float64
|
|
|
|
|
ElevationGainM *float64
|
|
|
|
|
AerobicTrainingEffect *float64
|
|
|
|
|
AnaerobicTrainingEffect *float64
|
|
|
|
|
VO2MaxValue *float64
|
|
|
|
|
RawJSON string
|
|
|
|
|
DetailsFetchedAt *string
|
|
|
|
|
DetailsRawJSON *string
|
|
|
|
|
SplitsFetchedAt *string
|
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
|
|
|
// WorkoutRawJSON is the genuine raw get_workout_by_id() response for this
|
|
|
|
|
// activity's structured workout -- the source used to compute each lap's
|
|
|
|
|
// TargetPaceLowMps/HighMps and TargetHRLowBpm/HighBpm (see
|
|
|
|
|
// internal/sync/mapping.go's alignWorkoutTargets). Nil when the activity
|
|
|
|
|
// has no WorkoutID, or was synced before this column existed.
|
|
|
|
|
WorkoutRawJSON *string
|
|
|
|
|
CreatedAt string
|
|
|
|
|
UpdatedAt string
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// UpsertActivity inserts a new activity for userID or updates the existing
|
|
|
|
|
// row for the same (userID, garmin_activity_id) pair (idempotent, safe to
|
|
|
|
|
// call on every sync pass), and returns its internal id.
|
2026-07-25 13:17:17 +02:00
|
|
|
func (db *DB) UpsertActivity(ctx context.Context, userID int64, a Activity) (int64, error) {
|
2026-07-17 18:33:06 +02:00
|
|
|
_, err := db.ExecContext(ctx, `
|
|
|
|
|
INSERT INTO activities (
|
2026-07-25 13:17:17 +02:00
|
|
|
user_id, garmin_activity_id, event_type_key, workout_id, start_time_utc,
|
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
|
|
|
duration_seconds, distance_meters, avg_hr, max_hr,
|
|
|
|
|
avg_speed_mps, elevation_gain_m,
|
|
|
|
|
aerobic_training_effect, anaerobic_training_effect, vo2max_value,
|
2026-07-17 18:33:06 +02:00
|
|
|
raw_json, updated_at
|
2026-07-25 13:17:17 +02:00
|
|
|
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, datetime('now'))
|
2026-07-25 14:11:51 +02:00
|
|
|
ON CONFLICT(user_id, garmin_activity_id) DO UPDATE SET
|
2026-07-19 10:52:09 +02:00
|
|
|
event_type_key=excluded.event_type_key,
|
2026-07-19 11:55:13 +02:00
|
|
|
workout_id=excluded.workout_id,
|
2026-07-17 18:33:06 +02:00
|
|
|
start_time_utc=excluded.start_time_utc,
|
|
|
|
|
duration_seconds=excluded.duration_seconds,
|
|
|
|
|
distance_meters=excluded.distance_meters,
|
|
|
|
|
avg_hr=excluded.avg_hr,
|
|
|
|
|
max_hr=excluded.max_hr,
|
|
|
|
|
avg_speed_mps=excluded.avg_speed_mps,
|
|
|
|
|
elevation_gain_m=excluded.elevation_gain_m,
|
|
|
|
|
aerobic_training_effect=excluded.aerobic_training_effect,
|
|
|
|
|
anaerobic_training_effect=excluded.anaerobic_training_effect,
|
|
|
|
|
vo2max_value=excluded.vo2max_value,
|
|
|
|
|
raw_json=excluded.raw_json,
|
|
|
|
|
updated_at=datetime('now')
|
|
|
|
|
`,
|
2026-07-25 13:17:17 +02:00
|
|
|
userID, a.GarminActivityID, a.EventTypeKey, a.WorkoutID, a.StartTimeUTC,
|
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
|
|
|
a.DurationSeconds, a.DistanceMeters, a.AvgHR, a.MaxHR,
|
|
|
|
|
a.AvgSpeedMps, a.ElevationGainM,
|
|
|
|
|
a.AerobicTrainingEffect, a.AnaerobicTrainingEffect, a.VO2MaxValue,
|
2026-07-17 18:33:06 +02:00
|
|
|
a.RawJSON,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-07-25 13:17:17 +02:00
|
|
|
return 0, fmt.Errorf("upsert activity %d for user %d: %w", a.GarminActivityID, userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var id int64
|
2026-07-25 14:11:51 +02:00
|
|
|
if err := db.QueryRowContext(ctx, `SELECT id FROM activities WHERE user_id = ? AND garmin_activity_id = ?`, userID, a.GarminActivityID).Scan(&id); err != nil {
|
|
|
|
|
return 0, fmt.Errorf("fetch id for activity %d (user %d): %w", a.GarminActivityID, userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func scanActivity(row interface{ Scan(...any) error }) (Activity, error) {
|
|
|
|
|
var a Activity
|
|
|
|
|
err := row.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
|
|
|
&a.ID, &a.GarminActivityID, &a.EventTypeKey, &a.WorkoutID, &a.StartTimeUTC,
|
|
|
|
|
&a.DurationSeconds, &a.DistanceMeters, &a.AvgHR, &a.MaxHR,
|
|
|
|
|
&a.AvgSpeedMps, &a.ElevationGainM,
|
|
|
|
|
&a.AerobicTrainingEffect, &a.AnaerobicTrainingEffect, &a.VO2MaxValue,
|
|
|
|
|
&a.RawJSON, &a.DetailsFetchedAt, &a.DetailsRawJSON, &a.SplitsFetchedAt, &a.WorkoutRawJSON,
|
2026-07-17 18:33:06 +02:00
|
|
|
&a.CreatedAt, &a.UpdatedAt,
|
|
|
|
|
)
|
|
|
|
|
return a, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const activityColumns = `
|
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
|
|
|
id, garmin_activity_id, event_type_key, workout_id, start_time_utc,
|
|
|
|
|
duration_seconds, distance_meters, avg_hr, max_hr,
|
|
|
|
|
avg_speed_mps, elevation_gain_m,
|
|
|
|
|
aerobic_training_effect, anaerobic_training_effect, vo2max_value,
|
|
|
|
|
raw_json, details_fetched_at, details_raw_json, splits_fetched_at, workout_raw_json,
|
2026-07-17 18:33:06 +02:00
|
|
|
created_at, updated_at
|
|
|
|
|
`
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// GetActivity fetches one activity by its internal id, scoped to userID.
|
|
|
|
|
func (db *DB) GetActivity(ctx context.Context, userID, id int64) (Activity, bool, error) {
|
|
|
|
|
row := db.QueryRowContext(ctx, `SELECT `+activityColumns+` FROM activities WHERE id = ? AND user_id = ?`, id, userID)
|
2026-07-17 18:33:06 +02:00
|
|
|
a, err := scanActivity(row)
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return Activity{}, false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return Activity{}, false, fmt.Errorf("get activity %d for user %d: %w", id, userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return a, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ActivityFilter narrows ListActivities results. Zero values mean "no filter".
|
|
|
|
|
type ActivityFilter struct {
|
|
|
|
|
FromDate string // inclusive, "YYYY-MM-DD"
|
|
|
|
|
ToDate string // inclusive, "YYYY-MM-DD"
|
|
|
|
|
Limit int
|
|
|
|
|
Offset int
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// ListActivities returns userID's activities newest-first, optionally
|
|
|
|
|
// filtered by date range.
|
2026-07-25 13:17:17 +02:00
|
|
|
func (db *DB) ListActivities(ctx context.Context, userID int64, f ActivityFilter) ([]Activity, error) {
|
|
|
|
|
query := `SELECT ` + activityColumns + ` FROM activities WHERE user_id = ?`
|
|
|
|
|
args := []any{userID}
|
2026-07-17 18:33:06 +02:00
|
|
|
if f.FromDate != "" {
|
|
|
|
|
query += ` AND start_time_utc >= ?`
|
|
|
|
|
args = append(args, f.FromDate)
|
|
|
|
|
}
|
|
|
|
|
if f.ToDate != "" {
|
|
|
|
|
query += ` AND start_time_utc <= ?`
|
|
|
|
|
args = append(args, f.ToDate+" 23:59:59")
|
|
|
|
|
}
|
|
|
|
|
query += ` ORDER BY start_time_utc DESC`
|
|
|
|
|
if f.Limit > 0 {
|
|
|
|
|
query += ` LIMIT ? OFFSET ?`
|
|
|
|
|
args = append(args, f.Limit, f.Offset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, args...)
|
|
|
|
|
if err != nil {
|
2026-07-25 13:17:17 +02:00
|
|
|
return nil, fmt.Errorf("list activities for user %d: %w", userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
activities := []Activity{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
a, err := scanActivity(rows)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("scan activity row: %w", err)
|
|
|
|
|
}
|
|
|
|
|
activities = append(activities, a)
|
|
|
|
|
}
|
|
|
|
|
return activities, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// ActivityExists reports whether userID already has an activity with this
|
|
|
|
|
// garmin_activity_id stored.
|
|
|
|
|
func (db *DB) ActivityExists(ctx context.Context, userID, garminActivityID int64) (bool, error) {
|
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
|
|
|
var id int64
|
2026-07-25 14:11:51 +02:00
|
|
|
err := db.QueryRowContext(ctx, `SELECT id FROM activities WHERE user_id = ? AND garmin_activity_id = ?`, userID, garminActivityID).Scan(&id)
|
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
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return false, fmt.Errorf("check activity %d exists for user %d: %w", garminActivityID, userID, err)
|
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
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// LatestActivityStartTime returns userID's most recently started activity's
|
|
|
|
|
// start_time_utc, used to compute the incremental sync window.
|
|
|
|
|
func (db *DB) LatestActivityStartTime(ctx context.Context, userID int64) (string, bool, error) {
|
2026-07-17 18:33:06 +02:00
|
|
|
var t string
|
2026-07-25 14:11:51 +02:00
|
|
|
err := db.QueryRowContext(ctx, `SELECT start_time_utc FROM activities WHERE user_id = ? ORDER BY start_time_utc DESC LIMIT 1`, userID).Scan(&t)
|
2026-07-17 18:33:06 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return "", false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return "", false, fmt.Errorf("latest activity start time for user %d: %w", userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return t, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetActivityDetails records that get_activity_details has been fetched for
|
|
|
|
|
// this activity, storing the raw response for future reprocessing.
|
2026-07-25 14:11:51 +02:00
|
|
|
func (db *DB) SetActivityDetails(ctx context.Context, userID, activityID int64, rawJSON string) error {
|
2026-07-17 18:33:06 +02:00
|
|
|
_, err := db.ExecContext(ctx, `
|
|
|
|
|
UPDATE activities SET details_fetched_at = datetime('now'), details_raw_json = ?, updated_at = datetime('now')
|
2026-07-25 14:11:51 +02:00
|
|
|
WHERE id = ? AND user_id = ?`, rawJSON, activityID, userID)
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return fmt.Errorf("set activity %d details for user %d: %w", activityID, userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// SetActivityWorkout stores the raw get_workout_by_id() response used to
|
|
|
|
|
// compute this activity's laps' target pace/HR bands.
|
2026-07-25 14:11:51 +02:00
|
|
|
func (db *DB) SetActivityWorkout(ctx context.Context, userID, activityID int64, rawJSON string) error {
|
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
|
|
|
_, err := db.ExecContext(ctx, `
|
|
|
|
|
UPDATE activities SET workout_raw_json = ?, updated_at = datetime('now')
|
2026-07-25 14:11:51 +02:00
|
|
|
WHERE id = ? AND user_id = ?`, rawJSON, activityID, userID)
|
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
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return fmt.Errorf("set activity %d workout for user %d: %w", activityID, userID, err)
|
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
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 18:33:06 +02:00
|
|
|
// SetActivitySplitsFetched records that get_activity_splits has been fetched
|
|
|
|
|
// for this activity.
|
2026-07-25 14:11:51 +02:00
|
|
|
func (db *DB) SetActivitySplitsFetched(ctx context.Context, userID, activityID int64) error {
|
2026-07-17 18:33:06 +02:00
|
|
|
_, err := db.ExecContext(ctx, `
|
|
|
|
|
UPDATE activities SET splits_fetched_at = datetime('now'), updated_at = datetime('now')
|
2026-07-25 14:11:51 +02:00
|
|
|
WHERE id = ? AND user_id = ?`, activityID, userID)
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return fmt.Errorf("set activity %d splits fetched for user %d: %w", activityID, userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// ActivitiesMissingDetails returns userID's activities that haven't had
|
2026-07-17 18:33:06 +02:00
|
|
|
// get_activity_details/get_activity_splits fetched yet, for the lazy
|
|
|
|
|
// background detail-fill pass.
|
2026-07-25 14:11:51 +02:00
|
|
|
func (db *DB) ActivitiesMissingDetails(ctx context.Context, userID int64, limit int) ([]Activity, error) {
|
2026-07-17 18:33:06 +02:00
|
|
|
rows, err := db.QueryContext(ctx, `SELECT `+activityColumns+` FROM activities
|
2026-07-25 14:11:51 +02:00
|
|
|
WHERE user_id = ? AND (details_fetched_at IS NULL OR splits_fetched_at IS NULL)
|
|
|
|
|
ORDER BY start_time_utc DESC LIMIT ?`, userID, limit)
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return nil, fmt.Errorf("list activities missing details for user %d: %w", userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
activities := []Activity{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
a, err := scanActivity(rows)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("scan activity row: %w", err)
|
|
|
|
|
}
|
|
|
|
|
activities = append(activities, a)
|
|
|
|
|
}
|
|
|
|
|
return activities, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 14:11:51 +02:00
|
|
|
// CountActivitiesMissingDetails returns how many of userID's activities
|
|
|
|
|
// still need get_activity_details/get_activity_splits fetched, regardless
|
|
|
|
|
// of any per-call batch limit -- used to report overall remaining work.
|
|
|
|
|
func (db *DB) CountActivitiesMissingDetails(ctx context.Context, userID int64) (int, error) {
|
2026-07-17 18:33:06 +02:00
|
|
|
var n int
|
|
|
|
|
err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM activities
|
2026-07-25 14:11:51 +02:00
|
|
|
WHERE user_id = ? AND (details_fetched_at IS NULL OR splits_fetched_at IS NULL)`, userID).Scan(&n)
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
2026-07-25 14:11:51 +02:00
|
|
|
return 0, fmt.Errorf("count activities missing details for user %d: %w", userID, err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
return n, nil
|
|
|
|
|
}
|
2026-07-27 06:49:44 +02:00
|
|
|
|
|
|
|
|
// ActivitiesMissingWorkout returns userID's activities that have a
|
|
|
|
|
// structured workout (WorkoutID set at initial upsert time, straight from
|
|
|
|
|
// Garmin's activity summary) but haven't had get_workout_by_id fetched yet.
|
|
|
|
|
// Independently queryable from ActivitiesMissingDetails: workout_id is
|
|
|
|
|
// known well before any detail fetch, and workout_raw_json is only ever
|
|
|
|
|
// set by SetActivityWorkout, so this also picks up an activity whose
|
|
|
|
|
// details were fetched successfully in some prior run but whose workout
|
|
|
|
|
// fetch failed back then -- ActivitiesMissingDetails would never surface
|
|
|
|
|
// that activity again (details_fetched_at/splits_fetched_at are already
|
|
|
|
|
// set), silently losing its target pace/HR bands forever without this.
|
|
|
|
|
func (db *DB) ActivitiesMissingWorkout(ctx context.Context, userID int64, limit int) ([]Activity, error) {
|
|
|
|
|
rows, err := db.QueryContext(ctx, `SELECT `+activityColumns+` FROM activities
|
|
|
|
|
WHERE user_id = ? AND workout_id IS NOT NULL AND workout_raw_json IS NULL
|
|
|
|
|
ORDER BY start_time_utc DESC LIMIT ?`, userID, limit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("list activities missing workout for user %d: %w", userID, err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
activities := []Activity{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
a, err := scanActivity(rows)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("scan activity row: %w", err)
|
|
|
|
|
}
|
|
|
|
|
activities = append(activities, a)
|
|
|
|
|
}
|
|
|
|
|
return activities, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountActivitiesMissingWorkout returns how many of userID's activities
|
|
|
|
|
// still need get_workout_by_id fetched, regardless of any per-call batch
|
|
|
|
|
// limit -- used to report overall remaining work, mirroring
|
|
|
|
|
// CountActivitiesMissingDetails.
|
|
|
|
|
func (db *DB) CountActivitiesMissingWorkout(ctx context.Context, userID int64) (int, error) {
|
|
|
|
|
var n int
|
|
|
|
|
err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM activities
|
|
|
|
|
WHERE user_id = ? AND workout_id IS NOT NULL AND workout_raw_json IS NULL`, userID).Scan(&n)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("count activities missing workout for user %d: %w", userID, err)
|
|
|
|
|
}
|
|
|
|
|
return n, nil
|
|
|
|
|
}
|