Skip to content

Commit

Permalink
Add more bucket sizes and report the bucket format in the response
Browse files Browse the repository at this point in the history
Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Apr 12, 2024
1 parent 179cdc4 commit 8f1e834
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 15 additions & 5 deletions pkg/database/statistics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package database

import (
"fmt"
"time"
)

type StatConfig struct {
Since string `query:"since"`
Per string `query:"per"`
Expand All @@ -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"
}
Expand All @@ -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.
Expand All @@ -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).
Expand Down
5 changes: 3 additions & 2 deletions pkg/database/user_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8f1e834

Please sign in to comment.