Package gfit: Retry failing API calls.
[kraftakt.git] / gfit / gfit.go
1 package gfit
2
3 import (
4         "context"
5         "fmt"
6         "net/http"
7         "strings"
8         "time"
9
10         "github.com/octo/kraftakt/app"
11         "github.com/octo/kraftakt/fitbit"
12         "github.com/octo/retry"
13         "golang.org/x/oauth2"
14         oauth2google "golang.org/x/oauth2/google"
15         fitness "google.golang.org/api/fitness/v1"
16         "google.golang.org/api/googleapi"
17         "google.golang.org/appengine"
18         "google.golang.org/appengine/log"
19 )
20
21 const (
22         csrfToken = "@CSRFTOKEN@"
23         userID    = "me"
24
25         dataTypeNameCalories        = "com.google.calories.expended"
26         dataTypeNameDistance        = "com.google.distance.delta"
27         dataTypeNameSteps           = "com.google.step_count.delta"
28         dataTypeNameHeartrate       = "com.google.heart_rate.summary"
29         dataTypeNameActivitySegment = "com.google.activity.segment"
30 )
31
32 func oauthConfig() *oauth2.Config {
33         return &oauth2.Config{
34                 ClientID:     app.Config.GoogleClientID,
35                 ClientSecret: app.Config.GoogleClientSecret,
36                 Endpoint:     oauth2google.Endpoint,
37                 RedirectURL:  "https://kraftakt.octo.it/google/grant",
38                 Scopes: []string{
39                         fitness.FitnessActivityWriteScope,
40                         fitness.FitnessBodyWriteScope,
41                         fitness.FitnessLocationWriteScope,
42                 },
43         }
44 }
45
46 func Application(ctx context.Context) *fitness.Application {
47         return &fitness.Application{
48                 Name:       "Kraftakt",
49                 Version:    appengine.VersionID(ctx),
50                 DetailsUrl: "", // optional
51         }
52 }
53
54 func AuthURL() string {
55         return oauthConfig().AuthCodeURL(csrfToken, oauth2.AccessTypeOffline)
56 }
57
58 func ParseToken(ctx context.Context, r *http.Request, u *app.User) error {
59         if state := r.FormValue("state"); state != csrfToken {
60                 return fmt.Errorf("invalid state parameter: %q", state)
61         }
62
63         tok, err := oauthConfig().Exchange(ctx, r.FormValue("code"))
64         if err != nil {
65                 return err
66         }
67
68         return u.SetToken(ctx, "Google", tok)
69 }
70
71 type Client struct {
72         *fitness.Service
73 }
74
75 func NewClient(ctx context.Context, u *app.User) (*Client, error) {
76         c, err := u.OAuthClient(ctx, "Google", oauthConfig())
77         if err != nil {
78                 return nil, err
79         }
80
81         service, err := fitness.New(c)
82         if err != nil {
83                 return nil, err
84         }
85
86         return &Client{
87                 Service: service,
88         }, nil
89 }
90
91 func DataStreamID(dataSource *fitness.DataSource) string {
92         fields := []string{
93                 dataSource.Type,
94                 dataSource.DataType.Name,
95                 app.Config.ProjectNumber,
96         }
97
98         if dev := dataSource.Device; dev != nil {
99                 if dev.Manufacturer != "" {
100                         fields = append(fields, dev.Manufacturer)
101                 }
102                 if dev.Model != "" {
103                         fields = append(fields, dev.Model)
104                 }
105                 if dev.Uid != "" {
106                         fields = append(fields, dev.Uid)
107                 }
108         }
109
110         if dataSource.DataStreamName != "" {
111                 fields = append(fields, dataSource.DataStreamName)
112         }
113
114         return strings.Join(fields, ":")
115 }
116
117 func wrapGoogleError(err error) error {
118         if err == nil {
119                 return nil
120         }
121
122         if gerr, ok := err.(*googleapi.Error); ok && gerr.Code >= 400 && gerr.Code < 500 {
123                 return retry.Abort(err)
124         }
125
126         return err
127 }
128
129 func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataSource) (string, error) {
130         var dataStreamID string
131         err := retry.Do(ctx, func(ctx context.Context) error {
132                 res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do()
133                 if err != nil {
134                         return wrapGoogleError(err)
135                 }
136                 dataStreamID = res.DataStreamId
137                 return nil
138         })
139         if err != nil {
140                 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict {
141                         if dataSource.DataStreamId != "" {
142                                 return dataSource.DataStreamId, nil
143                         }
144                         return DataStreamID(dataSource), nil
145                 }
146                 log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = %v", DataStreamID(dataSource), err)
147                 return "", err
148         }
149         return dataStreamID, nil
150 }
151
152 func (c *Client) DatasetGet(ctx context.Context, dataStreamID string, startTime, endTime time.Time) (*fitness.Dataset, error) {
153         datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
154
155         var dataset *fitness.Dataset
156         err := retry.Do(ctx, func(ctx context.Context) error {
157                 res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do()
158                 if err != nil {
159                         return wrapGoogleError(err)
160                 }
161                 dataset = res
162                 return nil
163         })
164         if err != nil {
165                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err)
166                 return nil, err
167         }
168         return dataset, nil
169 }
170
171 func (c *Client) DatasetPatch(ctx context.Context, dataSourceID string, points []*fitness.DataPoint) error {
172         startTimeNanos, endTimeNanos := int64(-1), int64(-1)
173         for _, p := range points {
174                 if startTimeNanos == -1 || startTimeNanos > p.StartTimeNanos {
175                         startTimeNanos = p.StartTimeNanos
176                 }
177                 if endTimeNanos == -1 || endTimeNanos < p.EndTimeNanos {
178                         endTimeNanos = p.EndTimeNanos
179                 }
180         }
181         datasetID := fmt.Sprintf("%d-%d", startTimeNanos, endTimeNanos)
182
183         dataset := &fitness.Dataset{
184                 DataSourceId:   dataSourceID,
185                 MinStartTimeNs: startTimeNanos,
186                 MaxEndTimeNs:   endTimeNanos,
187                 Point:          points,
188         }
189
190         err := retry.Do(ctx, func(ctx context.Context) error {
191                 _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do()
192                 return wrapGoogleError(err)
193         })
194         if err != nil {
195                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = %v", err)
196                 return err
197         }
198         return nil
199 }
200
201 func (c *Client) SetDistance(ctx context.Context, meters float64, startOfDay time.Time) error {
202         return c.updateCumulative(ctx,
203                 &fitness.DataSource{
204                         Application: Application(ctx),
205                         DataType: &fitness.DataType{
206                                 Field: []*fitness.DataTypeField{
207                                         &fitness.DataTypeField{
208                                                 Name:   "distance",
209                                                 Format: "floatPoint",
210                                         },
211                                 },
212                                 Name: dataTypeNameDistance,
213                         },
214                         Name: "Distance covered",
215                         Type: "raw",
216                 },
217                 &fitness.Value{
218                         FpVal: meters,
219                 },
220                 startOfDay)
221 }
222
223 func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.Time) error {
224         return c.updateCumulative(ctx,
225                 &fitness.DataSource{
226                         Application: Application(ctx),
227                         DataType: &fitness.DataType{
228                                 Field: []*fitness.DataTypeField{
229                                         &fitness.DataTypeField{
230                                                 Name:   "steps",
231                                                 Format: "integer",
232                                         },
233                                 },
234                                 Name: dataTypeNameSteps,
235                         },
236                         Name: "Step Count",
237                         Type: "raw",
238                 },
239                 &fitness.Value{
240                         IntVal: int64(totalSteps),
241                 },
242                 startOfDay)
243 }
244
245 func (c *Client) SetCalories(ctx context.Context, totalCalories float64, startOfDay time.Time) error {
246         return c.updateCumulative(ctx,
247                 &fitness.DataSource{
248                         Application: Application(ctx),
249                         DataType: &fitness.DataType{
250                                 Field: []*fitness.DataTypeField{
251                                         &fitness.DataTypeField{
252                                                 Name:   "calories",
253                                                 Format: "floatPoint",
254                                         },
255                                 },
256                                 Name: dataTypeNameCalories,
257                         },
258                         Name: "Calories expended",
259                         Type: "raw",
260                 },
261                 &fitness.Value{
262                         FpVal: totalCalories,
263                 },
264                 startOfDay)
265 }
266
267 type Activity struct {
268         Start time.Time
269         End   time.Time
270         Type  int64
271 }
272
273 func (a Activity) String() string {
274         return fmt.Sprintf("%s-%s %d", a.Start.Format("15:04:05"), a.End.Format("15:04:05"), a.Type)
275 }
276
277 func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error {
278         if len(activities) == 0 {
279                 return nil
280         }
281
282         dataStreamID, err := c.DataSourceCreate(ctx, &fitness.DataSource{
283                 Application: Application(ctx),
284                 DataType: &fitness.DataType{
285                         Field: []*fitness.DataTypeField{
286                                 &fitness.DataTypeField{
287                                         Name:   "activity",
288                                         Format: "integer",
289                                 },
290                         },
291                         Name: dataTypeNameActivitySegment,
292                 },
293                 Type: "raw",
294         })
295         if err != nil {
296                 return err
297         }
298
299         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
300
301         dataset, err := c.DatasetGet(ctx, dataStreamID, startOfDay, endOfDay)
302         if err != nil {
303                 return err
304         }
305
306         var dataPoints []*fitness.DataPoint
307 Next:
308         for _, a := range activities {
309                 startTimeNanos := a.Start.UnixNano()
310                 endTimeNanos := a.End.UnixNano()
311
312                 for _, p := range dataset.Point {
313                         if p.StartTimeNanos == startTimeNanos && p.EndTimeNanos == endTimeNanos && p.Value[0].IntVal == a.Type {
314                                 log.Debugf(ctx, "activity %s already stored in Google Fit", a)
315                                 continue Next
316                         }
317                 }
318
319                 log.Debugf(ctx, "activity %s will be added to Google Fit", a)
320                 dataPoints = append(dataPoints, &fitness.DataPoint{
321                         DataTypeName:   dataTypeNameActivitySegment,
322                         StartTimeNanos: startTimeNanos,
323                         EndTimeNanos:   endTimeNanos,
324                         Value: []*fitness.Value{
325                                 &fitness.Value{IntVal: a.Type},
326                         },
327                 })
328         }
329
330         if len(dataPoints) == 0 {
331                 return nil
332         }
333
334         return c.DatasetPatch(ctx, dataStreamID, dataPoints)
335 }
336
337 func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataSource, rawValue *fitness.Value, startOfDay time.Time) error {
338         switch f := dataSource.DataType.Field[0].Format; f {
339         case "integer":
340                 if rawValue.IntVal == 0 {
341                         return nil
342                 }
343         case "floatPoint":
344                 if rawValue.FpVal == 0 {
345                         return nil
346                 }
347         default:
348                 return fmt.Errorf("unexpected data type field format %q", f)
349         }
350
351         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
352         if err != nil {
353                 return err
354         }
355         dataSource.DataStreamId = dataSourceID
356
357         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
358         currValue, startTime, err := c.readCumulative(ctx, dataSource, startOfDay, endOfDay)
359         if err != nil {
360                 return err
361         }
362
363         var diffValue fitness.Value
364         if dataSource.DataType.Field[0].Format == "integer" {
365                 if rawValue.IntVal == currValue.IntVal {
366                         return nil
367                 }
368                 diffValue.IntVal = rawValue.IntVal - currValue.IntVal
369                 if diffValue.IntVal < 0 {
370                         log.Warningf(ctx, "stored value (%d) is larger than new value (%d); assuming count was reset", currValue.IntVal, rawValue.IntVal)
371                         diffValue.IntVal = rawValue.IntVal
372                 }
373         } else { // if dataSource.DataType.Field[0].Format == "floatPoint"
374                 if rawValue.FpVal == currValue.FpVal {
375                         return nil
376                 }
377                 diffValue.FpVal = rawValue.FpVal - currValue.FpVal
378                 if diffValue.FpVal < 0 {
379                         log.Warningf(ctx, "stored value (%g) is larger than new value (%g); assuming count was reset", currValue.FpVal, rawValue.FpVal)
380                         diffValue.FpVal = rawValue.FpVal
381                 }
382         }
383
384         endTime := endOfDay
385         if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
386                 endTime = now
387         }
388         log.Debugf(ctx, "add  cumulative data %s until %v: %+v", dataSource.DataStreamId, endTime, diffValue)
389
390         return c.DatasetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
391                 &fitness.DataPoint{
392                         DataTypeName:   dataSource.DataType.Name,
393                         StartTimeNanos: startTime.UnixNano(),
394                         EndTimeNanos:   endTime.UnixNano(),
395                         Value:          []*fitness.Value{&diffValue},
396                 },
397         })
398 }
399
400 func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) {
401         dataset, err := c.DatasetGet(ctx, dataSource.DataStreamId, startTime, endTime)
402         if err != nil {
403                 return nil, time.Time{}, err
404         }
405
406         if len(dataset.Point) == 0 {
407                 log.Debugf(ctx, "read cumulative data %s until %v: []", dataSource.DataStreamId, endTime)
408                 return &fitness.Value{}, startTime, nil
409         }
410
411         var sum fitness.Value
412         maxEndTime := startTime
413         for _, p := range dataset.Point {
414                 switch f := dataSource.DataType.Field[0].Format; f {
415                 case "integer":
416                         sum.IntVal += p.Value[0].IntVal
417                 case "floatPoint":
418                         sum.FpVal += p.Value[0].FpVal
419                 default:
420                         return nil, time.Time{}, fmt.Errorf("unexpected data type field format %q", f)
421                 }
422
423                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
424                 if maxEndTime.Before(pointEndTime) {
425                         maxEndTime = pointEndTime
426                 }
427         }
428
429         log.Debugf(ctx, "read cumulative data %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum)
430         return &sum, maxEndTime, nil
431 }
432
433 type heartRateDuration struct {
434         Min      int
435         Max      int
436         Duration time.Duration
437 }
438
439 type heartRateDurations []*heartRateDuration
440
441 func (res heartRateDurations) find(min, max int) (*heartRateDuration, bool) {
442         for _, d := range res {
443                 if d.Min != min || d.Max != max {
444                         continue
445                 }
446                 return d, true
447         }
448
449         return nil, false
450 }
451
452 func (c *Client) heartRate(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (heartRateDurations, time.Time, error) {
453         dataset, err := c.DatasetGet(ctx, dataSource.DataStreamId, startTime, endTime)
454         if err != nil {
455                 return nil, time.Time{}, err
456         }
457
458         if len(dataset.Point) == 0 {
459                 return nil, startTime, nil
460         }
461
462         var results heartRateDurations
463         maxEndTime := startTime
464         for _, p := range dataset.Point {
465                 max := int(p.Value[1].FpVal)
466                 min := int(p.Value[2].FpVal)
467                 duration := time.Unix(0, p.EndTimeNanos).Sub(time.Unix(0, p.StartTimeNanos))
468
469                 if d, ok := results.find(min, max); ok {
470                         d.Duration += duration
471                 } else {
472                         results = append(results, &heartRateDuration{
473                                 Min:      min,
474                                 Max:      max,
475                                 Duration: duration,
476                         })
477                 }
478
479                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
480                 if maxEndTime.Before(pointEndTime) {
481                         maxEndTime = pointEndTime
482                 }
483         }
484
485         return results, maxEndTime, nil
486 }
487
488 func (c *Client) SetHeartRate(ctx context.Context, totalDurations []fitbit.HeartRateZone, restingHeartRate int, startOfDay time.Time) error {
489         dataSource := &fitness.DataSource{
490                 Application: Application(ctx),
491                 DataType: &fitness.DataType{
492                         Field: []*fitness.DataTypeField{
493                                 &fitness.DataTypeField{
494                                         Name:   "average",
495                                         Format: "floatPoint",
496                                 },
497                                 &fitness.DataTypeField{
498                                         Name:   "max",
499                                         Format: "floatPoint",
500                                 },
501                                 &fitness.DataTypeField{
502                                         Name:   "min",
503                                         Format: "floatPoint",
504                                 },
505                         },
506                         Name: dataTypeNameHeartrate,
507                 },
508                 Name: "Heart rate summary",
509                 Type: "raw",
510         }
511
512         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
513         if err != nil {
514                 return err
515         }
516         dataSource.DataStreamId = dataSourceID
517
518         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
519         prevDurations, startTime, err := c.heartRate(ctx, dataSource, startOfDay, endOfDay)
520
521         // calculate the difference between the durations mentioned in
522         // totalDurations and prevDurations and store it in diffDurations.
523         var diffDurations heartRateDurations
524         for _, d := range totalDurations {
525                 total := time.Duration(d.Minutes) * time.Minute
526
527                 var prev time.Duration
528                 if res, ok := prevDurations.find(d.Min, d.Max); ok {
529                         prev = res.Duration
530                 }
531
532                 diff := total - prev
533                 if diff < 0 {
534                         diff = total
535                 }
536
537                 if res, ok := diffDurations.find(d.Min, d.Max); ok {
538                         res.Duration += diff
539                 } else {
540                         diffDurations = append(diffDurations, &heartRateDuration{
541                                 Min:      d.Min,
542                                 Max:      d.Max,
543                                 Duration: diff,
544                         })
545                 }
546         }
547
548         // create a fitness.DataPoint for each non-zero duration difference.
549         var dataPoints []*fitness.DataPoint
550         for _, d := range diffDurations {
551                 if d.Duration < time.Nanosecond {
552                         continue
553                 }
554
555                 endTime := startTime.Add(d.Duration)
556                 if endTime.After(endOfDay) {
557                         log.Warningf(ctx, "heart rate durations exceed one day (current end time: %v)", endTime)
558                         break
559                 }
560
561                 average := float64(d.Min+d.Max) / 2.0
562                 if d.Min <= restingHeartRate && restingHeartRate <= d.Max {
563                         average = float64(restingHeartRate)
564                 }
565
566                 dataPoints = append(dataPoints, &fitness.DataPoint{
567                         DataTypeName:   dataSource.DataType.Name,
568                         StartTimeNanos: startTime.UnixNano(),
569                         EndTimeNanos:   endTime.UnixNano(),
570                         Value: []*fitness.Value{
571                                 &fitness.Value{
572                                         FpVal: average,
573                                 },
574                                 &fitness.Value{
575                                         FpVal: float64(d.Max),
576                                 },
577                                 &fitness.Value{
578                                         FpVal: float64(d.Min),
579                                 },
580                         },
581                 })
582
583                 startTime = endTime
584         }
585
586         if len(dataPoints) == 0 {
587                 return nil
588         }
589         return c.DatasetPatch(ctx, dataSource.DataStreamId, dataPoints)
590 }