package cli import ( "context" "database/sql" "flag" "fmt" "log/slog" "math" "os" "sort" "text/tabwriter" "time" "github.com/martinlehoux/biking_home/config" "github.com/martinlehoux/biking_home/mountain_pass" "github.com/martinlehoux/biking_home/osmpass" "github.com/martinlehoux/biking_home/ride" "github.com/martinlehoux/biking_home/rides" "github.com/martinlehoux/biking_home/web" "github.com/martinlehoux/kagamigo/kcore" ) var ( download = flag.Bool("download", false, "download mountain passes into the database") resume = flag.Bool("resume", false, "skip departments already cached on disk") importCache = flag.Bool("import-cached", false, "import cached department CSVs into the database") fetchOSM = flag.Bool("fetch-osm", false, "download the France OSM PBF (resumable) into france-latest.osm.pbf") extractOSM = flag.String("extract-osm", "", "extract mountain passes from an OSM PBF file into the database") enrich = flag.Bool("enrich", false, "backfill mountain pass coordinates from OSM data") backfill = flag.Bool("backfill", false, "recompute all stored ride values") backtest = flag.Bool("backtest", false, "run data benchmarks comparing computed values against stored reference values") ) func Run(db *sql.DB, configPath string, appConfig config.Config) { switch { case *download: err := mountain_pass.DownloadMountainPasses(db, 5*time.Second, *resume) kcore.Expect(err, "failed to download mountain passes") case *importCache: _, err := mountain_pass.ImportCachedDepartments(db, []string{"06", "13"}) kcore.Expect(err, "failed to import cached departments") case *fetchOSM: err := osmpass.FetchFrancePBF("france-latest.osm.pbf") kcore.Expect(err, "failed to download France OSM PBF") case *extractOSM != "": _, err := osmpass.ExtractMountainPasses(context.Background(), *extractOSM, db) kcore.Expect(err, "failed to extract mountain passes from OSM") case *enrich: _, err := osmpass.EnrichMountainPasses(db) kcore.Expect(err, "failed to enrich mountain passes") case *backfill: count, err := rides.Backfill(db) kcore.Expect(err, "failed to backfill ride values") slog.Info("Backfilled ride values", "rides", count) case *backtest: kcore.Expect(runBacktest(db), "failed to run backtest") default: runServer(db, configPath, appConfig) } } func runServer(db *sql.DB, configPath string, appConfig config.Config) { server := web.NewServer(db, configPath) slog.Info("Starting web server", "address", appConfig.Server.PublicURL) kcore.Expect(server.ListenAndServe(appConfig.Server.Address), "web server stopped") } type backtestSample struct { externalID string name string computed time.Duration reference time.Duration relErrorPct float64 } func runBacktest(db *sql.DB) error { items, err := rides.List(db) if err != nil { return err } var samples []backtestSample for i, item := range items { if i > 0 && i%50 == 0 { slog.Info("Backtest progress", "processed", i, "total", len(items)) } if item.GPXPath == "" { continue } parsed, err := ride.ParseFile(ride.GPXRideParser{}, item.GPXPath) if err != nil { slog.Warn("Failed to parse ride GPX", "ride", item.ExternalID, "file", item.GPXPath, "error", err) continue } reference := time.Duration(item.MovingTimeS) * time.Second if reference <= 0 { continue } computed := ride.MovingTime(parsed) samples = append(samples, backtestSample{ externalID: item.ExternalID, name: item.Name, computed: computed, reference: reference, relErrorPct: (float64(computed) - float64(reference)) / float64(reference) * 100, }) } if len(samples) == 0 { slog.Info("No rides with GPX to backtest", "rides", len(items)) return nil } sort.Slice(samples, func(i, j int) bool { return math.Abs(samples[i].relErrorPct) > math.Abs(samples[j].relErrorPct) }) w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) fmt.Fprintln(w, "computed\treference\terr %\tride") limit := 10 if len(samples) < limit { limit = len(samples) } for _, s := range samples[:limit] { fmt.Fprintf(w, "%s\t%s\t%+.1f\t%s (%s)\n", s.computed.Round(time.Second), s.reference.Round(time.Second), s.relErrorPct, s.name, s.externalID) } w.Flush() absErrors := make([]float64, 0, len(samples)) for _, s := range samples { absErrors = append(absErrors, math.Abs(s.relErrorPct)) } percentiles := []float64{50, 75, 90, 95, 99} distribution := errorDistribution(absErrors, percentiles) table := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) fmt.Fprintln(table, "percentile\tabs err %") for _, p := range percentiles { fmt.Fprintf(table, "p%g\t%.1f\n", p, distribution[p]) } table.Flush() slog.Info("Moving time backtest", "rides", len(samples), "mean_err_pct", math.Round(meanAbsErr(absErrors)*10)/10, "stderr_pct", math.Round(stdErrAbsErr(absErrors)*10)/10) return nil } func meanAbsErr(absErrors []float64) float64 { var sum float64 for _, err := range absErrors { sum += err } return sum / float64(len(absErrors)) } func stdErrAbsErr(absErrors []float64) float64 { mean := meanAbsErr(absErrors) var sumSquares float64 for _, err := range absErrors { sumSquares += (err - mean) * (err - mean) } variance := sumSquares / float64(len(absErrors)-1) return math.Sqrt(variance / float64(len(absErrors))) } func errorDistribution(absErrors []float64, percentiles []float64) map[float64]float64 { sorted := append([]float64(nil), absErrors...) sort.Float64s(sorted) distribution := map[float64]float64{} for _, p := range percentiles { index := int(math.Ceil(p/100*float64(len(sorted))) - 1) if index < 0 { index = 0 } if index >= len(sorted) { index = len(sorted) - 1 } distribution[p] = sorted[index] } return distribution }