Skip to content

Commit

Permalink
Add parsing of biometric data
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmycanady committed Mar 30, 2022
1 parent 4bfb5c8 commit bba5da8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
18 changes: 17 additions & 1 deletion gocronometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (c *Client) ExportExercisesParsedWithLocation(ctx context.Context, startDat
return nil, fmt.Errorf("retreiving raw data: %s", err)
}

exercises, err := ParseServingsExercises(strings.NewReader(raw), location)
exercises, err := ParseExerciseExport(strings.NewReader(raw), location)
if err != nil {
return nil, fmt.Errorf("parsing raw data: %s", err)
}
Expand All @@ -638,3 +638,19 @@ func closeAndExhaustReader(r io.ReadCloser) {
}
return
}

// ExportBiometricRecordsParsedWithLocation exports the biometric records within the date range and parses them into a go struct. Only the YYYY-mm-dd is utilized of startDate and
// endDate. The export is parsed and dates set to the location provided.
func (c *Client) ExportBiometricRecordsParsedWithLocation(ctx context.Context, startDate time.Time, endDate time.Time, location *time.Location) (BiometricRecords, error) {
raw, err := c.ExportBiometrics(ctx, startDate, endDate)
if err != nil {
return nil, fmt.Errorf("retreiving raw data: %s", err)
}

exercises, err := ParseBiometricRecordsExport(strings.NewReader(raw), location)
if err != nil {
return nil, fmt.Errorf("parsing raw data: %s", err)
}

return exercises, nil
}
22 changes: 22 additions & 0 deletions gocronometer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,25 @@ func TestClient_ExportExercisesParsed(t *testing.T) {
}

}

func TestClient_ExportBiometricRecordsParsed(t *testing.T) {
username, password, client, err := setup()
if err != nil {
t.Fatal(err)
}

if err := client.Login(context.Background(), username, password); err != nil {
t.Fatalf("failed to login: %s", err)
}

defer client.Logout(context.Background())

startTime := time.Date(2021, 6, 1, 0, 0, 0, 0, time.Local)
endTime := time.Date(2021, 6, 10, 0, 0, 0, 0, time.Local)

_, err = client.ExportBiometricRecordsParsedWithLocation(context.Background(), startTime, endTime, time.UTC)
if err != nil {
t.Fatalf("failed to export bio: %s", err)
}

}
81 changes: 80 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ type ExerciseRecord struct {

type ExerciseRecords []ExerciseRecord

func ParseServingsExercises(rawCSVReader io.Reader, location *time.Location) (ExerciseRecords, error) {
func ParseExerciseExport(rawCSVReader io.Reader, location *time.Location) (ExerciseRecords, error) {

r := csv.NewReader(rawCSVReader)

Expand Down Expand Up @@ -631,3 +631,82 @@ func ParseServingsExercises(rawCSVReader io.Reader, location *time.Location) (Ex
return exercises, nil

}

type BiometricRecord struct {
RecordedTime time.Time
Metric string
Unit string
Amount float64
}

type BiometricRecords []BiometricRecord

func ParseBiometricRecordsExport(rawCSVReader io.Reader, location *time.Location) (BiometricRecords, error) {

r := csv.NewReader(rawCSVReader)

lineNum := 0
headers := make(map[int]string)
records := make(BiometricRecords, 0, 0)

for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

// Index all the headers.
if lineNum == 0 {

for i, v := range record {
headers[i] = v
}
lineNum++
continue
}
lineNum++

var date string
var timeStr string
bioRecord := BiometricRecord{}
for i, v := range record {
columnName := headers[i]

switch columnName {
case "Day":
date = v
case "Time":
timeStr = v
case "Metric":
bioRecord.Metric = v
case "Unit":
bioRecord.Unit = v
case "Amount":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing energy: %s", err)
}
bioRecord.Amount = f

}
}
if timeStr == "" {
timeStr = "00:00 AM"
}

if location == nil {
location = time.UTC
}
bioRecord.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
if err != nil {
return nil, fmt.Errorf("parsing record time: %s", err)
}
records = append(records, bioRecord)
}

return records, nil

}

0 comments on commit bba5da8

Please sign in to comment.