diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-24 08:37:33 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-24 08:37:33 +0200 |
| commit | 90614736f2a0c556f31eca5361b3c032f5f309f6 (patch) | |
| tree | 81fda954dc2bff8ae8b0023b502343c455995272 /rideanalysis | |
| parent | e3d0beee5be3dd2854b0f4fc10905a4fcdc08f47 (diff) | |
Match official climbs against their recorded sample
An official climb matched on endpoint coordinates alone. On a loop ridden
the other way the route meets the climb's top before its bottom, so pairing
each start with the first end inside its lap window credited a 5.9 km
passage as the 850 m Chemin de la Tour Pey Blanc, and a 49 km one on
another ride.
Climbs now keep the sample of the segment they were created from: the
recorded length and elevation gain of the selected route points, captured
by the ride detail and preview handlers and stored NOT NULL. Matching takes
the whole route and drops any candidate outside ±50% of that sample on
either axis, and attempts are refreshed by upsert and prune so re-analyzing
a ride keeps the recorded-at stamp of the passages that survive.
The backfill leaves the web package: backfill.Run recomputes ride values
then official climb attempts as a single `go run . backfill` command
(climbs-backfill is gone), rideanalysis owns the attempt snapshot, and the
mountain-pass loading margin is shared. Applied to the stored rides it
stamps 78 samples, drops the four bogus attempts and leaves ride 800 with
its three Pont Rout passages.
Diffstat (limited to 'rideanalysis')
| -rw-r--r-- | rideanalysis/analysis.go | 10 | ||||
| -rw-r--r-- | rideanalysis/attempts.go | 29 | ||||
| -rw-r--r-- | rideanalysis/attempts_test.go | 46 |
3 files changed, 83 insertions, 2 deletions
diff --git a/rideanalysis/analysis.go b/rideanalysis/analysis.go index bf3c2bb..81c9a98 100644 --- a/rideanalysis/analysis.go +++ b/rideanalysis/analysis.go @@ -17,6 +17,9 @@ const ( passCrossingElevationToleranceM = 25 ) +// PassMarginM is how far around a ride's track mountain passes are loaded for matching and display. +const PassMarginM = 10_000 + type Result struct { Climbs []AnalyzedClimb Crossings []mountain_pass.Crossing @@ -27,6 +30,7 @@ type AnalyzedClimb struct { Name string Category string DistanceM float64 + ElevationGainM float64 SlopePercent float64 Cotacol float64 OfficialClimbID int64 @@ -42,7 +46,7 @@ func Analyze(parsed ride.Ride, passes []mountain_pass.MountainPass, officialClim claimedClimbs := make([]AnalyzedClimb, 0) claimedRanges := make([]geo.Interval, 0) for _, official := range officialClimbs { - for _, matched := range official_climb.MatchClimbs(parsed.Coords, official, matchPolicy.EndpointRadiusM) { + for _, matched := range official_climb.MatchClimbs(parsed.Route, official, matchPolicy.EndpointRadiusM) { interval := geo.NewInterval(matched.StartIndex, matched.EndIndex) claimedRanges = append(claimedRanges, interval) claimedClimbs = append(claimedClimbs, analyzedClimb(routeClimb(parsed.Route, interval), interval, official.Name, official.ID)) @@ -81,11 +85,13 @@ func routeClimb(route climb.Route, interval geo.Interval) climb.Climb { func analyzedClimb(segment climb.Climb, interval geo.Interval, name string, officialID int64) AnalyzedClimb { cotacol := segment.DifficultyScore() + sample := segment.Sample(0, len(segment.Distances)-1) return AnalyzedClimb{ Segment: segment, Name: name, Category: climb.CategoryOf(cotacol).String(), - DistanceM: segment.Distances[len(segment.Distances)-1] - segment.Distances[0], + DistanceM: sample.DistanceM, + ElevationGainM: sample.ElevationGainM, SlopePercent: climb.Slope(segment.Distances, segment.Elevations) * 100, Cotacol: cotacol, OfficialClimbID: officialID, diff --git a/rideanalysis/attempts.go b/rideanalysis/attempts.go new file mode 100644 index 0000000..faf02e9 --- /dev/null +++ b/rideanalysis/attempts.go @@ -0,0 +1,29 @@ +package rideanalysis + +import ( + "github.com/martinlehoux/biking_home/official_climb" + "github.com/martinlehoux/biking_home/ride" +) + +// Attempts extracts one attempt record per official-climb occurrence: numbered per climb in route order, timestamped from the ride's recorded points. +func Attempts(rideID int64, analysis Result, parsed ride.Ride) []official_climb.Attempt { + occurrences := make(map[int64]int) + attempts := make([]official_climb.Attempt, 0) + for _, analyzed := range analysis.Climbs { + if analyzed.OfficialClimbID == 0 { + continue + } + occurrence := occurrences[analyzed.OfficialClimbID] + occurrences[analyzed.OfficialClimbID] = occurrence + 1 + attempts = append(attempts, official_climb.AttemptFromStats(rideID, occurrence, official_climb.AttemptStats{ + OfficialClimbID: analyzed.OfficialClimbID, + DistanceM: analyzed.DistanceM, + ElevationGainM: analyzed.ElevationGainM, + SlopePercent: analyzed.SlopePercent, + Cotacol: analyzed.Cotacol, + StartIndex: analyzed.Interval.Start, + EndIndex: analyzed.Interval.End, + }, parsed.Timestamps())) + } + return attempts +} diff --git a/rideanalysis/attempts_test.go b/rideanalysis/attempts_test.go new file mode 100644 index 0000000..6fab8e1 --- /dev/null +++ b/rideanalysis/attempts_test.go @@ -0,0 +1,46 @@ +package rideanalysis_test + +import ( + "testing" + "time" + + "github.com/martinlehoux/biking_home/internal/ridetest" + "github.com/martinlehoux/biking_home/official_climb" + "github.com/martinlehoux/biking_home/ride" + "github.com/martinlehoux/biking_home/rideanalysis" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var attemptRideStart = time.Date(2026, 9, 1, 8, 0, 0, 0, time.UTC) + +func attemptRideForClimb(t *testing.T) (ride.Ride, official_climb.OfficialClimb) { + t.Helper() + parsed := ridetest.MustRide("1km at 14.7% | 1km at -6.7%", ridetest.StartCoord(43.61, 5.42), ridetest.StartElevation(300), ridetest.Sampling(1000), ridetest.StartTime(attemptRideStart)) + return parsed, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: parsed.Coords[0], EndCoord: parsed.Coords[1], Sample: parsed.Sample(0, 1)} +} + +func TestAttemptsSnapshotsOfficialClimbs(t *testing.T) { + parsed, climb := attemptRideForClimb(t) + analysis := rideanalysis.Analyze(parsed, nil, []official_climb.OfficialClimb{{ID: 42, Name: climb.Name, StartCoord: climb.StartCoord, EndCoord: climb.EndCoord, Sample: climb.Sample}}, official_climb.DefaultMatchPolicy()) + + attempts := rideanalysis.Attempts(7, analysis, parsed) + require.Len(t, attempts, 1) + assert.Equal(t, int64(7), attempts[0].RideID) + assert.Equal(t, int64(42), attempts[0].ClimbID) + assert.Equal(t, 0, attempts[0].Occurrence) + assert.InDelta(t, 1000, attempts[0].DistanceM, 1) + assert.InDelta(t, 147, attempts[0].ElevationGainM, 1) + assert.InDelta(t, 14.7, attempts[0].SlopePercent, 0.1) + assert.Positive(t, attempts[0].Cotacol) + assert.Equal(t, "v1", attempts[0].CotacolAlgoVersion) + assert.Equal(t, attemptRideStart, attempts[0].StartedAt.UTC()) + assert.False(t, attempts[0].EndedAt.IsZero(), "attempt end must come from ride timestamps") +} + +func TestAttemptsSkipsUnmatchedClimbs(t *testing.T) { + parsed, _ := attemptRideForClimb(t) + analysis := rideanalysis.Analyze(parsed, nil, nil, official_climb.DefaultMatchPolicy()) + + assert.Empty(t, rideanalysis.Attempts(7, analysis, parsed)) +} |
