Open databases in the logbook package so the user does not have to pass in a

boltDB pointer.
This commit is contained in:
gmp
2025-05-15 13:08:28 -04:00
parent b39deb1986
commit a6604728d9
6 changed files with 456 additions and 610 deletions
+43 -16
View File
@@ -12,7 +12,6 @@ import (
"git.wow.st/gmp/logbook"
"git.wow.st/gmp/logbook/parser"
"git.wow.st/gmp/logbook/review"
bolt "go.etcd.io/bbolt"
)
func expandPath(path string) string {
@@ -71,6 +70,7 @@ func getPerformances(wr *logbook.WorkoutRepository, mr *logbook.MeasurementRepos
ps, err := pr.FindPerformances(filter)
return ps, err
}
func main() {
defaultDB := "~/.config/logbook/logbook.db"
var dbPath string
@@ -85,22 +85,13 @@ func main() {
dbPath = expandPath(dbPath)
db, err := bolt.Open(dbPath, 0600, nil)
wr, mr, err := logbook.NewRepositories(dbPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening database: %v\n", err)
os.Exit(1)
}
defer db.Close()
wr, err := logbook.NewWorkoutRepository(db)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening workout repository: %v\n", err)
os.Exit(1)
}
mr, err := logbook.NewMeasurementRepository(db)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening measurement repository: %v\n", err)
fmt.Fprintf(os.Stderr, "Error opening repositories: %v\n", err)
os.Exit(1)
}
defer wr.Close()
defer mr.Close()
cmd := flag.Arg(0)
switch cmd {
@@ -232,7 +223,6 @@ func main() {
}
}
}
//a, b := review.FitPowerLaw(weights, reps, date, 14.0)
est := review.NewEstimator(review.WithHalfLife(14.0))
err = est.Fit(weights, reps, dates)
if err != nil {
@@ -244,7 +234,44 @@ func main() {
if isbw {
adj = ps[len(ps)-1].Bodyweight
}
fmt.Printf("%d: %0.0f\n", i, est.EstimateMaxWeight(time.Now(), float64(i)) - adj)
fmt.Printf("%d: %0.0f\n", i, est.EstimateMaxWeight(float64(i)) - adj)
}
case "weekly-estimate":
ps, err := getPerformances(wr, mr)
if err != nil {
fmt.Printf("Error finding performance: %v\n", err)
os.Exit(1)
}
var weights, reps []float64
var dates []time.Time
isbw := review.IsBodyweight(ps[0].Exercise, ps[0].Type)
for _, p := range (ps) {
for i, w := range (p.Weights) {
if isbw {
w = w + p.Bodyweight
}
if p.RIR[i] == 0 {
weights = append(weights, w)
reps = append(reps, float64(p.Reps[i]))
dates = append(dates, p.Times[i].UTC())
fmt.Printf("Set: weight %0.0f, reps %0.0f\n", w, float64(p.Reps[i]))
}
}
}
var adj float64
if isbw {
adj = ps[len(ps)-1].Bodyweight
}
est := review.NewEstimator(review.WithHalfLife(14.0))
err = est.Fit(weights, reps, dates)
if err != nil {
fmt.Printf("Error estimating performance: %v\n", err)
os.Exit(1)
}
times, vals := est.TimelineEstimate1RM(7*24*time.Hour)
for i, t := range times {
fmt.Printf("%s: %0.0f\n", t.Format("2006-01-02"), vals[i] - adj)
}
case "predict":
if flag.NArg() != 3 {