From d941e307d9f8c3175e2ceb9f29395e56759d757c Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 22 Jan 2018 22:10:28 +0100 Subject: [PATCH] Call ActivitySummary() and Profile() concurrently. --- fitbit/fitbit.go | 4 ++-- gfitsync.go | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index 414ea37..4ba42e4 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -145,9 +145,9 @@ func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client, }, nil } -func (c *Client) ActivitySummary(ctx context.Context, t time.Time) (*ActivitySummary, error) { +func (c *Client) ActivitySummary(ctx context.Context, date string) (*ActivitySummary, error) { url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/activities/date/%s.json", - c.fitbitUserID, t.Format("2006-01-02")) + c.fitbitUserID, date) res, err := c.client.Get(url) if err != nil { diff --git a/gfitsync.go b/gfitsync.go index 5bb80fc..3bd416f 100644 --- a/gfitsync.go +++ b/gfitsync.go @@ -229,32 +229,51 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error { return err } - profile, err := fitbitClient.Profile(ctx) - if err != nil { - return err + var ( + wg = &sync.WaitGroup{} + errs appengine.MultiError + summary *fitbit.ActivitySummary + profile *fitbit.Profile + ) + + wg.Add(1) + go func() { + var err error + summary, err = fitbitClient.ActivitySummary(ctx, s.Date) + if err != nil { + errs = append(errs, fmt.Errorf("fitbitClient.ActivitySummary(%q) = %v", s.Date, err)) + } + wg.Done() + }() + + wg.Add(1) + go func() { + var err error + profile, err = fitbitClient.Profile(ctx) + if err != nil { + errs = append(errs, fmt.Errorf("fitbitClient.Profile(%q) = %v", s.Date, err)) + } + wg.Done() + }() + + wg.Wait() + if len(errs) != 0 { + return errs } - log.Debugf(ctx, "profile = %+v", profile) tm, err := time.ParseInLocation("2006-01-02", s.Date, profile.Timezone) if err != nil { return err } - summary, err := fitbitClient.ActivitySummary(ctx, tm) - if err != nil { - return err - } log.Debugf(ctx, "%s (%s) took %d steps on %s", - profile.Name, u.Email, summary.Summary.Steps, s.Date) + profile.Name, u.Email, summary.Summary.Steps, tm) gfitClient, err := gfit.NewClient(ctx, u) if err != nil { return err } - var errs appengine.MultiError - wg := &sync.WaitGroup{} - wg.Add(1) go func() { if err := gfitClient.SetSteps(ctx, summary.Summary.Steps, tm); err != nil { -- 2.11.0