aboutsummaryrefslogtreecommitdiff
path: root/cli/cli.go
diff options
context:
space:
mode:
Diffstat (limited to 'cli/cli.go')
-rw-r--r--cli/cli.go48
1 files changed, 43 insertions, 5 deletions
diff --git a/cli/cli.go b/cli/cli.go
index e2138cd..1fdb866 100644
--- a/cli/cli.go
+++ b/cli/cli.go
@@ -18,6 +18,7 @@ import (
"github.com/martinlehoux/biking_home/ride"
"github.com/martinlehoux/biking_home/web"
"github.com/martinlehoux/kagamigo/kcore"
+ "github.com/schollz/progressbar/v3"
)
var (
@@ -34,10 +35,34 @@ func Run(db *sql.DB, configPath string, appConfig config.Config) {
err := mountain_pass.FetchFrancePBF("france-latest.osm.pbf")
kcore.Expect(err, "failed to download France OSM PBF")
case *extractOSM != "":
- _, err := mountain_pass.ExtractMountainPasses(context.Background(), *extractOSM, db)
+ enabled := progressBarEnabled()
+ var bar *progressbar.ProgressBar
+ _, err := mountain_pass.ExtractMountainPasses(context.Background(), *extractOSM, db, func(current, total int64) {
+ if bar == nil && enabled {
+ bar = progressbar.DefaultBytes(total, "Extracting mountain passes")
+ }
+ if bar != nil {
+ bar.Set64(current)
+ }
+ })
+ if bar != nil {
+ bar.Finish()
+ }
kcore.Expect(err, "failed to extract mountain passes from OSM")
case *backfill:
- count, err := ride.Backfill(db)
+ enabled := progressBarEnabled()
+ var bar *progressbar.ProgressBar
+ count, err := ride.Backfill(db, func(current, total int) {
+ if bar == nil && enabled {
+ bar = progressbar.Default(int64(total), "Backfilling ride values")
+ }
+ if bar != nil {
+ bar.Set(current)
+ }
+ })
+ if bar != nil {
+ bar.Finish()
+ }
kcore.Expect(err, "failed to backfill ride values")
slog.Info("Backfilled ride values", "rides", count)
case *backtest:
@@ -47,6 +72,14 @@ func Run(db *sql.DB, configPath string, appConfig config.Config) {
}
}
+// progressBarEnabled reports whether stderr is attached to a terminal, so the
+// progress bar only renders for interactive runs and stays out of piped or
+// redirected output.
+func progressBarEnabled() bool {
+ fi, err := os.Stderr.Stat()
+ return err == nil && fi.Mode()&os.ModeCharDevice != 0
+}
+
func runServer(db *sql.DB, configPath string, appConfig config.Config) {
server := web.NewServer(db, configPath).WithPprof(*enablePprof)
if *enablePprof {
@@ -70,9 +103,14 @@ func runBacktest(db *sql.DB) error {
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))
+ var bar *progressbar.ProgressBar
+ if progressBarEnabled() {
+ bar = progressbar.Default(int64(len(items)), "Backtesting rides")
+ defer bar.Finish()
+ }
+ for _, item := range items {
+ if bar != nil {
+ bar.Add(1)
}
if !isBacktestEligible(item) {
continue