Skip to content

Commit

Permalink
feat(database): Improve statistics query
Browse files Browse the repository at this point in the history
This commit improves the statistics query to handle the `since` parameter more
gracefully.

The query only adds a date filter if the `since` parameter is not
"forever". This change ensures that the statistics are calculated correctly for
all time periods. Additionally, the translation for "forever" has been added to
support this new functionality. Finally, the `since` options in the UI have been
updated to allow selection of "forever".

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Jan 19, 2025
1 parent 803a063 commit 9e3b2e6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
26 changes: 18 additions & 8 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
[build]
bin = "./tmp/workout-tracker"
cmd = "make build-server"
delay = 1000
exclude_dir = ["docs", "testdata", "tmp", "vendor"]
exclude_file = ["assets/output.css", "screenshots.js"]
exclude_regex = ["_templ.go", "_test.go"]
bin = "./tmp/workout-tracker"
cmd = "make build-server"
delay = 1000
exclude_dir = ["docs", "testdata", "tmp", "vendor"]
exclude_file = ["assets/output.css", "screenshots.js"]
exclude_regex = ["_templ.go", "_test.go"]
exclude_unchanged = false
include_ext = ["css", "go", "html", "js", "json", "templ", "tmpl", "tpl"]
stop_on_error = true
include_ext = [
"css",
"go",
"html",
"js",
"json",
"templ",
"tmpl",
"tpl",
"yaml",
]
stop_on_error = true

[screen]
clear_on_rebuild = false
12 changes: 8 additions & 4 deletions pkg/database/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (u *User) GetStatistics(statConfig StatConfig) (*Statistics, error) {
Buckets: map[WorkoutType]Buckets{},
}

rows, err := u.db.
q := u.db.
Table("workouts").
Select(
"count(*) as workouts",
Expand All @@ -102,9 +102,13 @@ func (u *User) GetStatistics(statConfig StatConfig) (*Statistics, error) {
statConfig.GetBucketFormatExpression(sqlDialect),
).
Joins("join map_data on workouts.id = map_data.workout_id").
Where("user_id = ?", u.ID).
Where(GetDateLimitExpression(sqlDialect), "-"+statConfig.GetSince()).
Group("bucket, workout_type").Rows()
Where("user_id = ?", u.ID)

if statConfig.Since != "" && statConfig.Since != "forever" {
q = q.Where(GetDateLimitExpression(sqlDialect), "-"+statConfig.GetSince())
}

rows, err := q.Group("bucket, workout_type").Rows()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion translations/en.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
en:
forever: forever
Heatmap: Heatmap
"(Re)generate publicly shareable link": "(Re)generate publicly shareable link"
1 day: 1 day
1 month: 1 month
1 year: 1 year
10 years: 10 years
Expand Down
2 changes: 1 addition & 1 deletion views/helpers/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package helpers

func StatisticSinceOptions() []string {
return []string{
"1 day",
"7 days",
"1 month", "3 months", "6 months",
"1 year", "2 years", "5 years", "10 years",
"forever",
}
}

Expand Down

0 comments on commit 9e3b2e6

Please sign in to comment.