porting - part2 wk2 done

This commit is contained in:
2025-09-07 19:01:42 -07:00
parent 5c44f01bc3
commit 84c5c2ba6a
10 changed files with 347 additions and 32 deletions

90
garth/stats/base.go Normal file
View File

@@ -0,0 +1,90 @@
package stats
import (
"fmt"
"strings"
"time"
"garmin-connect/garth/client"
"garmin-connect/garth/utils"
)
type Stats interface {
List(end time.Time, period int, client *client.Client) ([]interface{}, error)
}
type BaseStats struct {
Path string
PageSize int
}
func (b *BaseStats) List(end time.Time, period int, client *client.Client) ([]interface{}, error) {
endDate := utils.FormatEndDate(end)
if period > b.PageSize {
// Handle pagination - get first page
page, err := b.fetchPage(endDate, b.PageSize, client)
if err != nil || len(page) == 0 {
return page, err
}
// Get remaining pages recursively
remainingStart := endDate.AddDate(0, 0, -b.PageSize)
remainingPeriod := period - b.PageSize
remainingData, err := b.List(remainingStart, remainingPeriod, client)
if err != nil {
return page, err
}
return append(remainingData, page...), nil
}
return b.fetchPage(endDate, period, client)
}
func (b *BaseStats) fetchPage(end time.Time, period int, client *client.Client) ([]interface{}, error) {
var start time.Time
var path string
if strings.Contains(b.Path, "daily") {
start = end.AddDate(0, 0, -(period - 1))
path = strings.Replace(b.Path, "{start}", start.Format("2006-01-02"), 1)
path = strings.Replace(path, "{end}", end.Format("2006-01-02"), 1)
} else {
path = strings.Replace(b.Path, "{end}", end.Format("2006-01-02"), 1)
path = strings.Replace(path, "{period}", fmt.Sprintf("%d", period), 1)
}
response, err := client.ConnectAPI(path, "GET", nil)
if err != nil {
return nil, err
}
if response == nil {
return []interface{}{}, nil
}
responseSlice, ok := response.([]interface{})
if !ok || len(responseSlice) == 0 {
return []interface{}{}, nil
}
var results []interface{}
for _, item := range responseSlice {
itemMap := item.(map[string]interface{})
// Handle nested "values" structure
if values, exists := itemMap["values"]; exists {
valuesMap := values.(map[string]interface{})
for k, v := range valuesMap {
itemMap[k] = v
}
delete(itemMap, "values")
}
snakeItem := utils.CamelToSnakeDict(itemMap)
results = append(results, snakeItem)
}
return results, nil
}

21
garth/stats/hrv.go Normal file
View File

@@ -0,0 +1,21 @@
package stats
import "time"
const BASE_HRV_PATH = "/usersummary-service/stats/hrv"
type DailyHRV struct {
CalendarDate time.Time `json:"calendar_date"`
RestingHR *int `json:"resting_hr"`
HRV *int `json:"hrv"`
BaseStats
}
func NewDailyHRV() *DailyHRV {
return &DailyHRV{
BaseStats: BaseStats{
Path: BASE_HRV_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}

20
garth/stats/hydration.go Normal file
View File

@@ -0,0 +1,20 @@
package stats
import "time"
const BASE_HYDRATION_PATH = "/usersummary-service/stats/hydration"
type DailyHydration struct {
CalendarDate time.Time `json:"calendar_date"`
TotalWaterML *int `json:"total_water_ml"`
BaseStats
}
func NewDailyHydration() *DailyHydration {
return &DailyHydration{
BaseStats: BaseStats{
Path: BASE_HYDRATION_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}

View File

@@ -0,0 +1,21 @@
package stats
import "time"
const BASE_INTENSITY_PATH = "/usersummary-service/stats/intensity_minutes"
type DailyIntensityMinutes struct {
CalendarDate time.Time `json:"calendar_date"`
ModerateIntensity *int `json:"moderate_intensity"`
VigorousIntensity *int `json:"vigorous_intensity"`
BaseStats
}
func NewDailyIntensityMinutes() *DailyIntensityMinutes {
return &DailyIntensityMinutes{
BaseStats: BaseStats{
Path: BASE_INTENSITY_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}

27
garth/stats/sleep.go Normal file
View File

@@ -0,0 +1,27 @@
package stats
import "time"
const BASE_SLEEP_PATH = "/usersummary-service/stats/sleep"
type DailySleep struct {
CalendarDate time.Time `json:"calendar_date"`
TotalSleepTime *int `json:"total_sleep_time"`
RemSleepTime *int `json:"rem_sleep_time"`
DeepSleepTime *int `json:"deep_sleep_time"`
LightSleepTime *int `json:"light_sleep_time"`
AwakeTime *int `json:"awake_time"`
SleepScore *int `json:"sleep_score"`
SleepStartTimestamp *int64 `json:"sleep_start_timestamp"`
SleepEndTimestamp *int64 `json:"sleep_end_timestamp"`
BaseStats
}
func NewDailySleep() *DailySleep {
return &DailySleep{
BaseStats: BaseStats{
Path: BASE_SLEEP_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}

41
garth/stats/steps.go Normal file
View File

@@ -0,0 +1,41 @@
package stats
import "time"
const BASE_STEPS_PATH = "/usersummary-service/stats/steps"
type DailySteps struct {
CalendarDate time.Time `json:"calendar_date"`
TotalSteps *int `json:"total_steps"`
TotalDistance *int `json:"total_distance"`
StepGoal int `json:"step_goal"`
BaseStats
}
func NewDailySteps() *DailySteps {
return &DailySteps{
BaseStats: BaseStats{
Path: BASE_STEPS_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}
type WeeklySteps struct {
CalendarDate time.Time `json:"calendar_date"`
TotalSteps int `json:"total_steps"`
AverageSteps float64 `json:"average_steps"`
AverageDistance float64 `json:"average_distance"`
TotalDistance float64 `json:"total_distance"`
WellnessDataDaysCount int `json:"wellness_data_days_count"`
BaseStats
}
func NewWeeklySteps() *WeeklySteps {
return &WeeklySteps{
BaseStats: BaseStats{
Path: BASE_STEPS_PATH + "/weekly/{end}/{period}",
PageSize: 52,
},
}
}

24
garth/stats/stress.go Normal file
View File

@@ -0,0 +1,24 @@
package stats
import "time"
const BASE_STRESS_PATH = "/usersummary-service/stats/stress"
type DailyStress struct {
CalendarDate time.Time `json:"calendar_date"`
OverallStressLevel int `json:"overall_stress_level"`
RestStressDuration *int `json:"rest_stress_duration"`
LowStressDuration *int `json:"low_stress_duration"`
MediumStressDuration *int `json:"medium_stress_duration"`
HighStressDuration *int `json:"high_stress_duration"`
BaseStats
}
func NewDailyStress() *DailyStress {
return &DailyStress{
BaseStats: BaseStats{
Path: BASE_STRESS_PATH + "/daily/{start}/{end}",
PageSize: 28,
},
}
}