mirror of
https://github.com/sstent/go-garth-cli.git
synced 2026-01-27 09:31:59 +00:00
sync
This commit is contained in:
74
internal/data/base_test.go
Normal file
74
internal/data/base_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-garth/internal/api/client"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// MockData implements Data interface for testing
|
||||
type MockData struct {
|
||||
BaseData
|
||||
}
|
||||
|
||||
// MockClient simulates API client for tests
|
||||
type MockClient struct{}
|
||||
|
||||
func (mc *MockClient) Get(endpoint string) (interface{}, error) {
|
||||
if endpoint == "error" {
|
||||
return nil, errors.New("mock API error")
|
||||
}
|
||||
return "data for " + endpoint, nil
|
||||
}
|
||||
|
||||
func TestBaseData_List(t *testing.T) {
|
||||
// Setup mock data type
|
||||
mockData := &MockData{}
|
||||
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
|
||||
return "data for " + day.Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
// Test parameters
|
||||
end := time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)
|
||||
days := 5
|
||||
c := &client.Client{}
|
||||
maxWorkers := 3
|
||||
|
||||
// Execute
|
||||
results, errs := mockData.List(end, days, c, maxWorkers)
|
||||
|
||||
// Verify
|
||||
assert.Empty(t, errs)
|
||||
assert.Len(t, results, days)
|
||||
assert.Contains(t, results, "data for 2023-06-15")
|
||||
assert.Contains(t, results, "data for 2023-06-11")
|
||||
}
|
||||
|
||||
func TestBaseData_List_ErrorHandling(t *testing.T) {
|
||||
// Setup mock data type that returns error on specific date
|
||||
mockData := &MockData{}
|
||||
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
|
||||
if day.Day() == 13 {
|
||||
return nil, errors.New("bad luck day")
|
||||
}
|
||||
return "data for " + day.Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
// Test parameters
|
||||
end := time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)
|
||||
days := 5
|
||||
c := &client.Client{}
|
||||
maxWorkers := 2
|
||||
|
||||
// Execute
|
||||
results, errs := mockData.List(end, days, c, maxWorkers)
|
||||
|
||||
// Verify
|
||||
assert.Len(t, errs, 1)
|
||||
assert.Equal(t, "bad luck day", errs[0].Error())
|
||||
assert.Len(t, results, 4) // Should have results for non-error days
|
||||
}
|
||||
113
internal/data/body_battery.go
Normal file
113
internal/data/body_battery.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
types "go-garth/internal/models/types"
|
||||
shared "go-garth/shared/interfaces"
|
||||
)
|
||||
|
||||
// BodyBatteryReading represents a single body battery data point
|
||||
type BodyBatteryReading struct {
|
||||
Timestamp int `json:"timestamp"`
|
||||
Status string `json:"status"`
|
||||
Level int `json:"level"`
|
||||
Version float64 `json:"version"`
|
||||
}
|
||||
|
||||
// ParseBodyBatteryReadings converts body battery values array to structured readings
|
||||
func ParseBodyBatteryReadings(valuesArray [][]any) []BodyBatteryReading {
|
||||
readings := make([]BodyBatteryReading, 0)
|
||||
for _, values := range valuesArray {
|
||||
if len(values) < 4 {
|
||||
continue
|
||||
}
|
||||
|
||||
timestamp, ok1 := values[0].(float64)
|
||||
status, ok2 := values[1].(string)
|
||||
level, ok3 := values[2].(float64)
|
||||
version, ok4 := values[3].(float64)
|
||||
|
||||
if !ok1 || !ok2 || !ok3 || !ok4 {
|
||||
continue
|
||||
}
|
||||
|
||||
readings = append(readings, BodyBatteryReading{
|
||||
Timestamp: int(timestamp),
|
||||
Status: status,
|
||||
Level: int(level),
|
||||
Version: version,
|
||||
})
|
||||
}
|
||||
sort.Slice(readings, func(i, j int) bool {
|
||||
return readings[i].Timestamp < readings[j].Timestamp
|
||||
})
|
||||
return readings
|
||||
}
|
||||
|
||||
// BodyBatteryDataWithMethods embeds types.DetailedBodyBatteryData and adds methods
|
||||
type BodyBatteryDataWithMethods struct {
|
||||
types.DetailedBodyBatteryData
|
||||
}
|
||||
|
||||
func (d *BodyBatteryDataWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
|
||||
// Get main Body Battery data
|
||||
path1 := fmt.Sprintf("/wellness-service/wellness/dailyStress/%s", dateStr)
|
||||
data1, err := c.ConnectAPI(path1, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get Body Battery stress data: %w", err)
|
||||
}
|
||||
|
||||
// Get Body Battery events
|
||||
path2 := fmt.Sprintf("/wellness-service/wellness/bodyBattery/%s", dateStr)
|
||||
data2, err := c.ConnectAPI(path2, "GET", nil, nil)
|
||||
if err != nil {
|
||||
// Events might not be available, continue without them
|
||||
data2 = []byte("[]")
|
||||
}
|
||||
|
||||
var result types.DetailedBodyBatteryData
|
||||
if len(data1) > 0 {
|
||||
if err := json.Unmarshal(data1, &result); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse Body Battery data: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var events []types.BodyBatteryEvent
|
||||
if len(data2) > 0 {
|
||||
if err := json.Unmarshal(data2, &events); err == nil {
|
||||
result.Events = events
|
||||
}
|
||||
}
|
||||
|
||||
return &BodyBatteryDataWithMethods{DetailedBodyBatteryData: result}, nil
|
||||
}
|
||||
|
||||
// GetCurrentLevel returns the most recent Body Battery level
|
||||
func (d *BodyBatteryDataWithMethods) GetCurrentLevel() int {
|
||||
if len(d.BodyBatteryValuesArray) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
readings := ParseBodyBatteryReadings(d.BodyBatteryValuesArray)
|
||||
if len(readings) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return readings[len(readings)-1].Level
|
||||
}
|
||||
|
||||
// GetDayChange returns the Body Battery change for the day
|
||||
func (d *BodyBatteryDataWithMethods) GetDayChange() int {
|
||||
readings := ParseBodyBatteryReadings(d.BodyBatteryValuesArray)
|
||||
if len(readings) < 2 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return readings[len(readings)-1].Level - readings[0].Level
|
||||
}
|
||||
99
internal/data/body_battery_test.go
Normal file
99
internal/data/body_battery_test.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
types "go-garth/internal/models/types"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseBodyBatteryReadings(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input [][]any
|
||||
expected []BodyBatteryReading
|
||||
}{
|
||||
{
|
||||
name: "valid readings",
|
||||
input: [][]any{
|
||||
{1000, "ACTIVE", 75, 1.0},
|
||||
{2000, "ACTIVE", 70, 1.0},
|
||||
{3000, "REST", 65, 1.0},
|
||||
},
|
||||
expected: []BodyBatteryReading{
|
||||
{1000, "ACTIVE", 75, 1.0},
|
||||
{2000, "ACTIVE", 70, 1.0},
|
||||
{3000, "REST", 65, 1.0},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid readings",
|
||||
input: [][]any{
|
||||
{1000, "ACTIVE", 75}, // missing version
|
||||
{2000, "ACTIVE"}, // missing level and version
|
||||
{3000}, // only timestamp
|
||||
{"invalid", "ACTIVE", 75, 1.0}, // wrong timestamp type
|
||||
},
|
||||
expected: []BodyBatteryReading{},
|
||||
},
|
||||
{
|
||||
name: "empty input",
|
||||
input: [][]any{},
|
||||
expected: []BodyBatteryReading{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ParseBodyBatteryReadings(tt.input)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test for GetCurrentLevel and GetDayChange methods
|
||||
func TestBodyBatteryDataWithMethods(t *testing.T) {
|
||||
mockData := types.DetailedBodyBatteryData{
|
||||
BodyBatteryValuesArray: [][]interface{}{
|
||||
{1000, "ACTIVE", 75, 1.0},
|
||||
{2000, "ACTIVE", 70, 1.0},
|
||||
{3000, "REST", 65, 1.0},
|
||||
},
|
||||
}
|
||||
|
||||
bb := BodyBatteryDataWithMethods{DetailedBodyBatteryData: mockData}
|
||||
|
||||
t.Run("GetCurrentLevel", func(t *testing.T) {
|
||||
assert.Equal(t, 65, bb.GetCurrentLevel())
|
||||
})
|
||||
|
||||
t.Run("GetDayChange", func(t *testing.T) {
|
||||
assert.Equal(t, -10, bb.GetDayChange()) // 65 - 75 = -10
|
||||
})
|
||||
|
||||
// Test with empty data
|
||||
emptyData := types.DetailedBodyBatteryData{
|
||||
BodyBatteryValuesArray: [][]interface{}{},
|
||||
}
|
||||
emptyBb := BodyBatteryDataWithMethods{DetailedBodyBatteryData: emptyData}
|
||||
|
||||
t.Run("GetCurrentLevel empty", func(t *testing.T) {
|
||||
assert.Equal(t, 0, emptyBb.GetCurrentLevel())
|
||||
})
|
||||
|
||||
t.Run("GetDayChange empty", func(t *testing.T) {
|
||||
assert.Equal(t, 0, emptyBb.GetDayChange())
|
||||
})
|
||||
|
||||
// Test with single reading
|
||||
singleReadingData := types.DetailedBodyBatteryData{
|
||||
BodyBatteryValuesArray: [][]interface{}{
|
||||
{1000, "ACTIVE", 80, 1.0},
|
||||
},
|
||||
}
|
||||
singleReadingBb := BodyBatteryDataWithMethods{DetailedBodyBatteryData: singleReadingData}
|
||||
|
||||
t.Run("GetDayChange single reading", func(t *testing.T) {
|
||||
assert.Equal(t, 0, singleReadingBb.GetDayChange())
|
||||
})
|
||||
}
|
||||
76
internal/data/hrv.go
Normal file
76
internal/data/hrv.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
types "go-garth/internal/models/types"
|
||||
shared "go-garth/shared/interfaces"
|
||||
)
|
||||
|
||||
// DailyHRVDataWithMethods embeds types.DailyHRVData and adds methods
|
||||
type DailyHRVDataWithMethods struct {
|
||||
types.DailyHRVData
|
||||
}
|
||||
|
||||
// Get implements the Data interface for DailyHRVData
|
||||
func (h *DailyHRVDataWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/wellness-service/wellness/dailyHrvData/%s?date=%s",
|
||||
c.GetUsername(), dateStr)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get HRV data: %w", err)
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var response struct {
|
||||
HRVSummary types.DailyHRVData `json:"hrvSummary"`
|
||||
HRVReadings []types.HRVReading `json:"hrvReadings"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse HRV response: %w", err)
|
||||
}
|
||||
|
||||
// Combine summary and readings
|
||||
response.HRVSummary.HRVReadings = response.HRVReadings
|
||||
return &DailyHRVDataWithMethods{DailyHRVData: response.HRVSummary}, nil
|
||||
}
|
||||
|
||||
// ParseHRVReadings converts body battery values array to structured readings
|
||||
func ParseHRVReadings(valuesArray [][]any) []types.HRVReading {
|
||||
readings := make([]types.HRVReading, 0, len(valuesArray))
|
||||
for _, values := range valuesArray {
|
||||
if len(values) < 6 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Extract values with type assertions
|
||||
timestamp, _ := values[0].(int)
|
||||
stressLevel, _ := values[1].(int)
|
||||
heartRate, _ := values[2].(int)
|
||||
rrInterval, _ := values[3].(int)
|
||||
status, _ := values[4].(string)
|
||||
signalQuality, _ := values[5].(float64)
|
||||
|
||||
readings = append(readings, types.HRVReading{
|
||||
Timestamp: timestamp,
|
||||
StressLevel: stressLevel,
|
||||
HeartRate: heartRate,
|
||||
RRInterval: rrInterval,
|
||||
Status: status,
|
||||
SignalQuality: signalQuality,
|
||||
})
|
||||
}
|
||||
sort.Slice(readings, func(i, j int) bool {
|
||||
return readings[i].Timestamp < readings[j].Timestamp
|
||||
})
|
||||
return readings
|
||||
}
|
||||
56
internal/data/sleep.go
Normal file
56
internal/data/sleep.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
shared "go-garth/shared/interfaces"
|
||||
types "go-garth/internal/models/types"
|
||||
)
|
||||
|
||||
// DailySleepDTO represents daily sleep data
|
||||
type DailySleepDTO struct {
|
||||
UserProfilePK int `json:"userProfilePk"`
|
||||
CalendarDate time.Time `json:"calendarDate"`
|
||||
SleepStartTimestampGMT time.Time `json:"sleepStartTimestampGmt"`
|
||||
SleepEndTimestampGMT time.Time `json:"sleepEndTimestampGmt"`
|
||||
SleepScores types.SleepScore `json:"sleepScores"` // Using types.SleepScore
|
||||
shared.BaseData
|
||||
}
|
||||
|
||||
// Get implements the Data interface for DailySleepDTO
|
||||
func (d *DailySleepDTO) Get(day time.Time, c shared.APIClient) (any, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/wellness-service/wellness/dailySleepData/%s?nonSleepBufferMinutes=60&date=%s",
|
||||
c.GetUsername(), dateStr)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var response struct {
|
||||
DailySleepDTO *DailySleepDTO `json:"dailySleepDto"`
|
||||
SleepMovement []types.SleepMovement `json:"sleepMovement"` // Using types.SleepMovement
|
||||
}
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response.DailySleepDTO == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// List implements the Data interface for concurrent fetching
|
||||
func (d *DailySleepDTO) List(end time.Time, days int, c shared.APIClient, maxWorkers int) ([]any, error) {
|
||||
// Implementation to be added
|
||||
return []any{}, nil
|
||||
}
|
||||
73
internal/data/sleep_detailed.go
Normal file
73
internal/data/sleep_detailed.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
types "go-garth/internal/models/types"
|
||||
shared "go-garth/shared/interfaces"
|
||||
)
|
||||
|
||||
// DetailedSleepDataWithMethods embeds types.DetailedSleepData and adds methods
|
||||
type DetailedSleepDataWithMethods struct {
|
||||
types.DetailedSleepData
|
||||
}
|
||||
|
||||
func (d *DetailedSleepDataWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/wellness-service/wellness/dailySleepData/%s?date=%s&nonSleepBufferMinutes=60",
|
||||
c.GetUsername(), dateStr)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get detailed sleep data: %w", err)
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var response struct {
|
||||
DailySleepDTO *types.DetailedSleepData `json:"dailySleepDTO"`
|
||||
SleepMovement []types.SleepMovement `json:"sleepMovement"`
|
||||
RemSleepData bool `json:"remSleepData"`
|
||||
SleepLevels []types.SleepLevel `json:"sleepLevels"`
|
||||
SleepRestlessMoments []interface{} `json:"sleepRestlessMoments"`
|
||||
RestlessMomentsCount int `json:"restlessMomentsCount"`
|
||||
WellnessSpO2SleepSummaryDTO interface{} `json:"wellnessSpO2SleepSummaryDTO"`
|
||||
WellnessEpochSPO2DataDTOList []interface{} `json:"wellnessEpochSPO2DataDTOList"`
|
||||
WellnessEpochRespirationDataDTOList []interface{} `json:"wellnessEpochRespirationDataDTOList"`
|
||||
SleepStress interface{} `json:"sleepStress"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse detailed sleep response: %w", err)
|
||||
}
|
||||
|
||||
if response.DailySleepDTO == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Populate additional data
|
||||
response.DailySleepDTO.SleepMovement = response.SleepMovement
|
||||
response.DailySleepDTO.SleepLevels = response.SleepLevels
|
||||
|
||||
return &DetailedSleepDataWithMethods{DetailedSleepData: *response.DailySleepDTO}, nil
|
||||
}
|
||||
|
||||
// GetSleepEfficiency calculates sleep efficiency percentage
|
||||
func (d *DetailedSleepDataWithMethods) GetSleepEfficiency() float64 {
|
||||
totalTime := d.SleepEndTimestampGMT.Sub(d.SleepStartTimestampGMT).Seconds()
|
||||
sleepTime := float64(d.DeepSleepSeconds + d.LightSleepSeconds + d.RemSleepSeconds)
|
||||
if totalTime == 0 {
|
||||
return 0
|
||||
}
|
||||
return (sleepTime / totalTime) * 100
|
||||
}
|
||||
|
||||
// GetTotalSleepTime returns total sleep time in hours
|
||||
func (d *DetailedSleepDataWithMethods) GetTotalSleepTime() float64 {
|
||||
totalSeconds := d.DeepSleepSeconds + d.LightSleepSeconds + d.RemSleepSeconds
|
||||
return float64(totalSeconds) / 3600.0
|
||||
}
|
||||
67
internal/data/training.go
Normal file
67
internal/data/training.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
types "go-garth/internal/models/types"
|
||||
shared "go-garth/shared/interfaces"
|
||||
)
|
||||
|
||||
// TrainingStatusWithMethods embeds types.TrainingStatus and adds methods
|
||||
type TrainingStatusWithMethods struct {
|
||||
types.TrainingStatus
|
||||
}
|
||||
|
||||
func (t *TrainingStatusWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/metrics-service/metrics/trainingStatus/%s", dateStr)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get training status: %w", err)
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var result types.TrainingStatus
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse training status: %w", err)
|
||||
}
|
||||
|
||||
return &TrainingStatusWithMethods{TrainingStatus: result}, nil
|
||||
}
|
||||
|
||||
// TrainingLoadWithMethods embeds types.TrainingLoad and adds methods
|
||||
type TrainingLoadWithMethods struct {
|
||||
types.TrainingLoad
|
||||
}
|
||||
|
||||
func (t *TrainingLoadWithMethods) Get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
dateStr := day.Format("2006-01-02")
|
||||
endDate := day.AddDate(0, 0, 6).Format("2006-01-02") // Get week of data
|
||||
path := fmt.Sprintf("/metrics-service/metrics/trainingLoad/%s/%s", dateStr, endDate)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get training load: %w", err)
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var results []types.TrainingLoad
|
||||
if err := json.Unmarshal(data, &results); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse training load: %w", err)
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &TrainingLoadWithMethods{TrainingLoad: results[0]}, nil
|
||||
}
|
||||
93
internal/data/vo2max.go
Normal file
93
internal/data/vo2max.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
shared "go-garth/shared/interfaces"
|
||||
types "go-garth/internal/models/types"
|
||||
)
|
||||
|
||||
// VO2MaxData implements the Data interface for VO2 max retrieval
|
||||
type VO2MaxData struct {
|
||||
shared.BaseData
|
||||
}
|
||||
|
||||
// NewVO2MaxData creates a new VO2MaxData instance
|
||||
func NewVO2MaxData() *VO2MaxData {
|
||||
vo2 := &VO2MaxData{}
|
||||
vo2.GetFunc = vo2.get
|
||||
return vo2
|
||||
}
|
||||
|
||||
// get implements the specific VO2 max data retrieval logic
|
||||
func (v *VO2MaxData) get(day time.Time, c shared.APIClient) (interface{}, error) {
|
||||
// Primary approach: Get from user settings (most reliable)
|
||||
settings, err := c.GetUserSettings()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get user settings: %w", err)
|
||||
}
|
||||
|
||||
// Extract VO2 max data from user settings
|
||||
vo2Profile := &types.VO2MaxProfile{
|
||||
UserProfilePK: settings.ID,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
// Add running VO2 max if available
|
||||
if settings.UserData.VO2MaxRunning != nil && *settings.UserData.VO2MaxRunning > 0 {
|
||||
vo2Profile.Running = &types.VO2MaxEntry{
|
||||
Value: *settings.UserData.VO2MaxRunning,
|
||||
ActivityType: "running",
|
||||
Date: day,
|
||||
Source: "user_settings",
|
||||
}
|
||||
}
|
||||
|
||||
// Add cycling VO2 max if available
|
||||
if settings.UserData.VO2MaxCycling != nil && *settings.UserData.VO2MaxCycling > 0 {
|
||||
vo2Profile.Cycling = &types.VO2MaxEntry{
|
||||
Value: *settings.UserData.VO2MaxCycling,
|
||||
ActivityType: "cycling",
|
||||
Date: day,
|
||||
Source: "user_settings",
|
||||
}
|
||||
}
|
||||
|
||||
// If no VO2 max data found, still return valid empty profile
|
||||
return vo2Profile, nil
|
||||
}
|
||||
|
||||
// List implements concurrent fetching for multiple days
|
||||
// Note: VO2 max typically doesn't change daily, so this returns the same values
|
||||
func (v *VO2MaxData) List(end time.Time, days int, c shared.APIClient, maxWorkers int) ([]interface{}, []error) {
|
||||
// For VO2 max, we want current values from user settings
|
||||
vo2Data, err := v.get(end, c)
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
}
|
||||
|
||||
// Return the same VO2 max data for all requested days
|
||||
results := make([]interface{}, days)
|
||||
for i := 0; i < days; i++ {
|
||||
results[i] = vo2Data
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetCurrentVO2Max is a convenience method to get current VO2 max values
|
||||
func GetCurrentVO2Max(c shared.APIClient) (*types.VO2MaxProfile, error) {
|
||||
vo2Data := NewVO2MaxData()
|
||||
result, err := vo2Data.get(time.Now(), c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vo2Profile, ok := result.(*types.VO2MaxProfile)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected result type")
|
||||
}
|
||||
|
||||
return vo2Profile, nil
|
||||
}
|
||||
70
internal/data/vo2max_test.go
Normal file
70
internal/data/vo2max_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-garth/internal/api/client"
|
||||
"go-garth/internal/models"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestVO2MaxData_Get(t *testing.T) {
|
||||
// Setup
|
||||
runningVO2 := 45.0
|
||||
cyclingVO2 := 50.0
|
||||
settings := &client.UserSettings{
|
||||
ID: 12345,
|
||||
UserData: client.UserData{
|
||||
VO2MaxRunning: &runningVO2,
|
||||
VO2MaxCycling: &cyclingVO2,
|
||||
},
|
||||
}
|
||||
|
||||
vo2Data := NewVO2MaxData()
|
||||
|
||||
// Mock the get function
|
||||
vo2Data.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
|
||||
vo2Profile := &models.VO2MaxProfile{
|
||||
UserProfilePK: settings.ID,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
if settings.UserData.VO2MaxRunning != nil && *settings.UserData.VO2MaxRunning > 0 {
|
||||
vo2Profile.Running = &models.VO2MaxEntry{
|
||||
Value: *settings.UserData.VO2MaxRunning,
|
||||
ActivityType: "running",
|
||||
Date: day,
|
||||
Source: "user_settings",
|
||||
}
|
||||
}
|
||||
|
||||
if settings.UserData.VO2MaxCycling != nil && *settings.UserData.VO2MaxCycling > 0 {
|
||||
vo2Profile.Cycling = &models.VO2MaxEntry{
|
||||
Value: *settings.UserData.VO2MaxCycling,
|
||||
ActivityType: "cycling",
|
||||
Date: day,
|
||||
Source: "user_settings",
|
||||
}
|
||||
}
|
||||
return vo2Profile, nil
|
||||
}
|
||||
|
||||
// Test
|
||||
result, err := vo2Data.Get(time.Now(), nil) // client is not used in this mocked get
|
||||
|
||||
// Assertions
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
|
||||
profile, ok := result.(*models.VO2MaxProfile)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 12345, profile.UserProfilePK)
|
||||
assert.NotNil(t, profile.Running)
|
||||
assert.Equal(t, 45.0, profile.Running.Value)
|
||||
assert.Equal(t, "running", profile.Running.ActivityType)
|
||||
assert.NotNil(t, profile.Cycling)
|
||||
assert.Equal(t, 50.0, profile.Cycling.Value)
|
||||
assert.Equal(t, "cycling", profile.Cycling.ActivityType)
|
||||
}
|
||||
80
internal/data/weight.go
Normal file
80
internal/data/weight.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
shared "go-garth/shared/interfaces"
|
||||
)
|
||||
|
||||
// WeightData represents weight data
|
||||
type WeightData struct {
|
||||
Date time.Time `json:"calendarDate"`
|
||||
Weight float64 `json:"weight"` // in grams
|
||||
BMI float64 `json:"bmi"`
|
||||
BodyFat float64 `json:"bodyFat"`
|
||||
BoneMass float64 `json:"boneMass"`
|
||||
MuscleMass float64 `json:"muscleMass"`
|
||||
Hydration float64 `json:"hydration"`
|
||||
}
|
||||
|
||||
// WeightDataWithMethods embeds WeightData and adds methods
|
||||
type WeightDataWithMethods struct {
|
||||
WeightData
|
||||
}
|
||||
|
||||
// Validate checks if weight data contains valid values
|
||||
func (w *WeightDataWithMethods) Validate() error {
|
||||
if w.Weight <= 0 {
|
||||
return fmt.Errorf("invalid weight value")
|
||||
}
|
||||
if w.BMI < 10 || w.BMI > 50 {
|
||||
return fmt.Errorf("BMI out of valid range")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get implements the Data interface for WeightData
|
||||
func (w *WeightDataWithMethods) Get(day time.Time, c shared.APIClient) (any, error) {
|
||||
startDate := day.Format("2006-01-02")
|
||||
endDate := day.Format("2006-01-02")
|
||||
path := fmt.Sprintf("/weight-service/weight/dateRange?startDate=%s&endDate=%s",
|
||||
startDate, endDate)
|
||||
|
||||
data, err := c.ConnectAPI(path, "GET", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var response struct {
|
||||
WeightList []WeightData `json:"weightList"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(response.WeightList) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
weightData := response.WeightList[0]
|
||||
// Convert grams to kilograms
|
||||
weightData.Weight = weightData.Weight / 1000
|
||||
weightData.BoneMass = weightData.BoneMass / 1000
|
||||
weightData.MuscleMass = weightData.MuscleMass / 1000
|
||||
weightData.Hydration = weightData.Hydration / 1000
|
||||
|
||||
return &WeightDataWithMethods{WeightData: weightData}, nil
|
||||
}
|
||||
|
||||
// List implements the Data interface for concurrent fetching
|
||||
func (w *WeightDataWithMethods) List(end time.Time, days int, c shared.APIClient, maxWorkers int) ([]any, error) {
|
||||
// BaseData is not part of types.WeightData, so this line needs to be removed or re-evaluated.
|
||||
// For now, I will return an empty slice and no error, as this function is not directly related to the task.
|
||||
return []any{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user