Skip to content

Commit

Permalink
Merge pull request #91 from jovandeginste/fixups
Browse files Browse the repository at this point in the history
Several fixups in one PR
  • Loading branch information
jovandeginste authored Apr 12, 2024
2 parents 3b7f172 + 8f1e834 commit e0a914e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
7 changes: 0 additions & 7 deletions pkg/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ func (a *App) redirectWithError(c echo.Context, target string, err error) error

func (a *App) statisticsHandler(c echo.Context) error {
data := a.defaultData(c)
u := a.getCurrentUser(c)

if err := a.addWorkouts(u, data); err != nil {
return a.redirectWithError(c, a.echo.Reverse("user-signout"), err)
}

data["user"] = u

return c.Render(http.StatusOK, "user_statistics.html", data)
}
Expand Down
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
11 changes: 11 additions & 0 deletions pkg/database/workouts_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ func creatorNeedsCorrection(creator string) bool {
return creator != "Garmin Connect" && creator != "Apple Watch" && creator != "StravaGPX iPhone" && creator != "StravaGPX"
}

func normalizeDegrees(val float64) float64 {
if val < 0 {
return val + 360
}

return val
}

func correctAltitude(creator string, lat, long, alt float64) float64 {
if !creatorNeedsCorrection(creator) {
return alt
}

lat = normalizeDegrees(lat)
long = normalizeDegrees(long)

loc := egm96.NewLocationGeodetic(lat, long, alt)

h, err := loc.HeightAboveMSL()
Expand Down

0 comments on commit e0a914e

Please sign in to comment.