Skip to content

Commit bba5da8

Browse files
committed
Add parsing of biometric data
1 parent 4bfb5c8 commit bba5da8

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

gocronometer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (c *Client) ExportExercisesParsedWithLocation(ctx context.Context, startDat
619619
return nil, fmt.Errorf("retreiving raw data: %s", err)
620620
}
621621

622-
exercises, err := ParseServingsExercises(strings.NewReader(raw), location)
622+
exercises, err := ParseExerciseExport(strings.NewReader(raw), location)
623623
if err != nil {
624624
return nil, fmt.Errorf("parsing raw data: %s", err)
625625
}
@@ -638,3 +638,19 @@ func closeAndExhaustReader(r io.ReadCloser) {
638638
}
639639
return
640640
}
641+
642+
// 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
643+
// endDate. The export is parsed and dates set to the location provided.
644+
func (c *Client) ExportBiometricRecordsParsedWithLocation(ctx context.Context, startDate time.Time, endDate time.Time, location *time.Location) (BiometricRecords, error) {
645+
raw, err := c.ExportBiometrics(ctx, startDate, endDate)
646+
if err != nil {
647+
return nil, fmt.Errorf("retreiving raw data: %s", err)
648+
}
649+
650+
exercises, err := ParseBiometricRecordsExport(strings.NewReader(raw), location)
651+
if err != nil {
652+
return nil, fmt.Errorf("parsing raw data: %s", err)
653+
}
654+
655+
return exercises, nil
656+
}

gocronometer_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,25 @@ func TestClient_ExportExercisesParsed(t *testing.T) {
242242
}
243243

244244
}
245+
246+
func TestClient_ExportBiometricRecordsParsed(t *testing.T) {
247+
username, password, client, err := setup()
248+
if err != nil {
249+
t.Fatal(err)
250+
}
251+
252+
if err := client.Login(context.Background(), username, password); err != nil {
253+
t.Fatalf("failed to login: %s", err)
254+
}
255+
256+
defer client.Logout(context.Background())
257+
258+
startTime := time.Date(2021, 6, 1, 0, 0, 0, 0, time.Local)
259+
endTime := time.Date(2021, 6, 10, 0, 0, 0, 0, time.Local)
260+
261+
_, err = client.ExportBiometricRecordsParsedWithLocation(context.Background(), startTime, endTime, time.UTC)
262+
if err != nil {
263+
t.Fatalf("failed to export bio: %s", err)
264+
}
265+
266+
}

parse.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ type ExerciseRecord struct {
557557

558558
type ExerciseRecords []ExerciseRecord
559559

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

562562
r := csv.NewReader(rawCSVReader)
563563

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

633633
}
634+
635+
type BiometricRecord struct {
636+
RecordedTime time.Time
637+
Metric string
638+
Unit string
639+
Amount float64
640+
}
641+
642+
type BiometricRecords []BiometricRecord
643+
644+
func ParseBiometricRecordsExport(rawCSVReader io.Reader, location *time.Location) (BiometricRecords, error) {
645+
646+
r := csv.NewReader(rawCSVReader)
647+
648+
lineNum := 0
649+
headers := make(map[int]string)
650+
records := make(BiometricRecords, 0, 0)
651+
652+
for {
653+
record, err := r.Read()
654+
if err == io.EOF {
655+
break
656+
}
657+
if err != nil {
658+
return nil, err
659+
}
660+
661+
// Index all the headers.
662+
if lineNum == 0 {
663+
664+
for i, v := range record {
665+
headers[i] = v
666+
}
667+
lineNum++
668+
continue
669+
}
670+
lineNum++
671+
672+
var date string
673+
var timeStr string
674+
bioRecord := BiometricRecord{}
675+
for i, v := range record {
676+
columnName := headers[i]
677+
678+
switch columnName {
679+
case "Day":
680+
date = v
681+
case "Time":
682+
timeStr = v
683+
case "Metric":
684+
bioRecord.Metric = v
685+
case "Unit":
686+
bioRecord.Unit = v
687+
case "Amount":
688+
f, err := parseFloat(v, 64)
689+
if err != nil {
690+
return nil, fmt.Errorf("parsing energy: %s", err)
691+
}
692+
bioRecord.Amount = f
693+
694+
}
695+
}
696+
if timeStr == "" {
697+
timeStr = "00:00 AM"
698+
}
699+
700+
if location == nil {
701+
location = time.UTC
702+
}
703+
bioRecord.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
704+
if err != nil {
705+
return nil, fmt.Errorf("parsing record time: %s", err)
706+
}
707+
records = append(records, bioRecord)
708+
}
709+
710+
return records, nil
711+
712+
}

0 commit comments

Comments
 (0)