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