2026-07-17 18:33:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
|
2026-07-24 21:08:07 +02:00
|
|
|
"geniusrun/backend/internal/store"
|
2026-07-17 18:33:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type progressionPoint struct {
|
|
|
|
|
Date string `json:"date"`
|
|
|
|
|
ActivityID int64 `json:"activity_id"`
|
|
|
|
|
Value float64 `json:"value"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func metricValue(metric string, a store.Activity) (float64, bool) {
|
|
|
|
|
switch metric {
|
|
|
|
|
case "pace":
|
|
|
|
|
if a.AvgSpeedMps == nil || *a.AvgSpeedMps <= 0 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return 1000 / *a.AvgSpeedMps, true
|
|
|
|
|
case "hr":
|
|
|
|
|
if a.AvgHR == nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return *a.AvgHR, true
|
|
|
|
|
case "vo2max":
|
|
|
|
|
if a.VO2MaxValue == nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return *a.VO2MaxValue, true
|
|
|
|
|
case "aerobic_te":
|
|
|
|
|
if a.AerobicTrainingEffect == nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return *a.AerobicTrainingEffect, true
|
|
|
|
|
case "anaerobic_te":
|
|
|
|
|
if a.AnaerobicTrainingEffect == nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return *a.AnaerobicTrainingEffect, true
|
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
|
|
|
case "efficiency_factor":
|
|
|
|
|
// Speed per heartbeat: how much ground each beat covers, on average.
|
|
|
|
|
// A rising trend at similar effort/HR over time is a classic sign of
|
|
|
|
|
// improving aerobic fitness, independent of pace/HR shown alone.
|
|
|
|
|
// Scaled by 1000 (m/s per bpm is otherwise a tiny fraction like
|
|
|
|
|
// 0.02, hard to read/compare at a glance).
|
|
|
|
|
if a.AvgSpeedMps == nil || a.AvgHR == nil || *a.AvgHR <= 0 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return (*a.AvgSpeedMps / *a.AvgHR) * 1000, true
|
2026-07-17 18:33:06 +02:00
|
|
|
default:
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleProgression returns a time series of the requested metric for every
|
|
|
|
|
// activity currently assigned to a workout kind, for progression charts.
|
|
|
|
|
func (s *Server) handleProgression(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
kindID, err := strconv.ParseInt(chi.URLParam(r, "kindID"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "invalid workout kind id")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
metric := r.URL.Query().Get("metric")
|
|
|
|
|
if metric == "" {
|
|
|
|
|
metric = "pace"
|
|
|
|
|
}
|
|
|
|
|
from, to := r.URL.Query().Get("from"), r.URL.Query().Get("to")
|
|
|
|
|
|
|
|
|
|
assignments, err := s.DB.AssignmentsForKind(r.Context(), kindID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
points := []progressionPoint{}
|
|
|
|
|
for _, a := range assignments {
|
|
|
|
|
activity, ok, err := s.DB.GetActivity(r.Context(), a.ActivityID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if from != "" && activity.StartTimeUTC < from {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if to != "" && activity.StartTimeUTC > to+" 23:59:59" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
value, ok := metricValue(metric, activity)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
points = append(points, progressionPoint{Date: activity.StartTimeUTC, ActivityID: activity.ID, Value: value})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(points, func(i, j int) bool { return points[i].Date < points[j].Date })
|
|
|
|
|
writeJSON(w, http.StatusOK, points)
|
|
|
|
|
}
|