From 8f1e834be2c23ce98610e232e6e118630b57fa44 Mon Sep 17 00:00:00 2001 From: Jo Vandeginste Date: Fri, 12 Apr 2024 21:16:23 +0200 Subject: [PATCH] Add more bucket sizes and report the bucket format in the response Signed-off-by: Jo Vandeginste --- pkg/database/statistics.go | 20 +++++++++++++++----- pkg/database/user_statistics.go | 5 +++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/database/statistics.go b/pkg/database/statistics.go index 5329cc51..6ab0aebe 100644 --- a/pkg/database/statistics.go +++ b/pkg/database/statistics.go @@ -1,5 +1,10 @@ package database +import ( + "fmt" + "time" +) + type StatConfig struct { Since string `query:"since"` Per string `query:"per"` @@ -9,6 +14,10 @@ func (sc *StatConfig) GetBucketString() string { switch sc.Per { case "year": return "%Y" + case "week": + return "%Y-%W" + case "day": + return "%Y-%m-%d" default: return "%Y-%m" } @@ -31,8 +40,9 @@ func (u *User) GetDefaultStatistics() (*Statistics, error) { func (u *User) GetStatistics(statConfig StatConfig) (*Statistics, error) { r := &Statistics{ - UserID: u.ID, - Buckets: map[WorkoutType]map[string]Bucket{}, + UserID: u.ID, + BucketFormat: statConfig.GetBucketString(), + Buckets: map[WorkoutType]map[string]Bucket{}, } rows, err := u.db. @@ -44,9 +54,9 @@ func (u *User) GetStatistics(statConfig StatConfig) (*Statistics, error) { "sum(total_distance) as distance", "sum(total_up) as up", "max(max_speed) as max_speed", - "max(total_distance / (total_duration / 1000000000)) as average_speed", - "max(total_distance / ((total_duration - pause_duration) / 1000000000)) as average_speed_no_pause", - "strftime('"+statConfig.GetBucketString()+"', workouts.date) as bucket", + fmt.Sprintf("max(total_distance / (total_duration / %d)) as average_speed", time.Second), + fmt.Sprintf("max(total_distance / ((total_duration - pause_duration) / %d)) as average_speed_no_pause", time.Second), + "strftime('"+r.BucketFormat+"', workouts.date) as bucket", ). Joins("join map_data on workouts.id = map_data.workout_id"). Where("user_id = ?", u.ID). diff --git a/pkg/database/user_statistics.go b/pkg/database/user_statistics.go index 9b24332c..ca2424a0 100644 --- a/pkg/database/user_statistics.go +++ b/pkg/database/user_statistics.go @@ -6,8 +6,9 @@ import ( type ( Statistics struct { - UserID uint - Buckets map[WorkoutType]map[string]Bucket + UserID uint + BucketFormat string + Buckets map[WorkoutType]map[string]Bucket } Bucket struct {