Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically use most popular workout in totals #130

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pkg/database/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,38 @@ func (u *User) GetStatistics(statConfig StatConfig) (*Statistics, error) {
return r, nil
}

func (u *User) GetHighestWorkoutType() (*WorkoutType, error) {
r := ""

err := u.db.
Table("workouts").
Select("type").
Where("user_id = ?", u.ID).
Group("type").
Order("count(*) DESC").
Limit(1).
Pluck("type", &r).Error
if err != nil {
return nil, err
}

wt := AsWorkoutType(r)

return &wt, nil
}

func (u *User) GetDefaultTotals() (*Bucket, error) {
return u.GetTotals(u.Profile.TotalsShow)
t := u.Profile.TotalsShow
if t == WorkoutTypeAutoDetect {
ht, err := u.GetHighestWorkoutType()
if err != nil {
return nil, err
}

t = *ht
}

return u.GetTotals(t)
}

func (u *User) GetTotals(t WorkoutType) (*Bucket, error) {
Expand Down
1 change: 1 addition & 0 deletions views/partials/messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{{ i18n "The user '%s' has been deleted." .Name }}
{{ i18n "Added %d new workout(s): %s" (len .msg) .msg }}
{{ i18n "API key updated" }}
{{ i18n "workouts" }}

All workout types:

Expand Down
17 changes: 9 additions & 8 deletions views/partials/stats_records_totals.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{{ define "stats_records_total" }} {{ with .GetDefaultTotals }}
<div class="md:flex md:flex-wrap">
{{ $di := i18n "distance" }} {{ $w := i18n "workouts"}} {{ $u := i18n "up" }}
{{ $du := i18n "duration" }} {{ template "statistic_tile"
(BuildDecoratedAttribute "workout" $w .Workouts "") }} {{ template
"statistic_tile" (BuildDecoratedAttribute "distance" $di (.Distance |
HumanDistance ) CurrentUser.PreferredUnits.Distance) }} {{ template
"statistic_tile" (BuildDecoratedAttribute "up" $u (.Up | HumanElevation)
CurrentUser.PreferredUnits.Elevation) }} {{ template "statistic_tile"
(BuildDecoratedAttribute "duration" $du (.Duration | HumanDuration) "") }}
{{ $di := i18n "distance" }} {{ $w := i18n (print .WorkoutType.String " "
"workouts") }} {{ $u := i18n "up" }} {{ $du := i18n "duration" }} {{ template
"statistic_tile" (BuildDecoratedAttribute .WorkoutType.String $w .Workouts "")
}} {{ template "statistic_tile" (BuildDecoratedAttribute "distance" $di
(.Distance | HumanDistance ) CurrentUser.PreferredUnits.Distance) }} {{
template "statistic_tile" (BuildDecoratedAttribute "up" $u (.Up |
HumanElevation) CurrentUser.PreferredUnits.Elevation) }} {{ template
"statistic_tile" (BuildDecoratedAttribute "duration" $du (.Duration |
HumanDuration) "") }}
</div>
{{ end }} {{ end }}
1 change: 1 addition & 0 deletions views/partials/user_profile_totals_show.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{ define "user_profile_totals_show" }}{{ $userTotalsShow := . }}
<select id="totals_show" name="totals_show">
<option value="auto">{{ i18n "Auto-detect" }}</option>
{{ range workoutTypes }} {{ if .IsDistance }} {{ $selected := "" }} {{ if eq
.String $userTotalsShow }} {{ $selected = "selected" }} {{ end }}
<option value="{{ .String }}" {{ $selected }}>{{ i18n .String }}</option>
Expand Down
Loading