1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package web
import (
"database/sql"
"log/slog"
"github.com/martinlehoux/biking_home/config"
"github.com/martinlehoux/biking_home/mountain_pass"
"github.com/martinlehoux/biking_home/official_climb"
"github.com/martinlehoux/biking_home/ride"
"github.com/martinlehoux/biking_home/rideanalysis"
"github.com/martinlehoux/kagamigo/kcore"
"github.com/schollz/progressbar/v3"
)
// BackfillOfficialClimbAttempts parses every route-bearing ride, matches its
// official climbs, and persists the resulting attempts. An optional reporter
// is invoked with progress for CLI rendering.
func BackfillOfficialClimbAttempts(db *sql.DB, configPath string, reporters ...func(current, total int)) (int, error) {
appConfig, err := config.Load(configPath)
if err != nil {
return 0, err
}
matchPolicy := official_climb.DefaultMatchPolicy()
matchPolicy.EndpointRadiusM = appConfig.OfficialClimb.MatchRadiusM
items, err := ride.List(db)
if err != nil {
return 0, err
}
officialClimbs, err := official_climb.List(db)
if err != nil {
return 0, err
}
var bar *progressbar.ProgressBar
report := func(int, int) {}
if len(reporters) > 0 {
report = reporters[0]
}
backfilled := 0
for i, item := range items {
report(i+1, len(items))
if item.FitPath == "" && item.GPXPath == "" {
continue
}
parsed, err := ride.ParseStoredRide(item)
if err != nil {
slog.Warn("Failed to parse ride for climb backfill", "ride_id", item.ID, "error", err)
continue
}
passes, err := mountain_pass.LoadMountainPassesAroundRide(db, parsed.Coords, rideDetailMountainPassMarginM)
if err != nil {
slog.Warn("Failed to load passes for climb backfill", "ride_id", item.ID, "error", err)
}
analysis := rideanalysis.Analyze(parsed, passes, officialClimbs, matchPolicy)
attempts := attemptsFromAnalysis(item.ID, analysis, parsed)
if err := official_climb.ReplaceRideAttempts(db, item.ID, attempts); err != nil {
slog.Warn("Failed to record climb attempts", "ride_id", item.ID, "error", err)
continue
}
backfilled++
}
if bar != nil {
bar.Finish()
}
return backfilled, nil
}
// BackfillOfficialClimbAttemptsCmd runs the climb attempt backfill from the CLI.
func BackfillOfficialClimbAttemptsCmd(db *sql.DB, configPath string) {
var bar *progressbar.ProgressBar
count, err := BackfillOfficialClimbAttempts(db, configPath, func(current, total int) {
if bar == nil && mountain_pass.ProgressBarEnabled() {
bar = progressbar.Default(int64(total), "Backfilling climb attempts")
}
if bar != nil {
bar.Set(current)
}
})
kcore.Expect(err, "failed to backfill climb attempts")
slog.Info("Backfilled climb attempts", "rides", count)
}
|