Package gfit: Improve Create error message.
[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(%q) = (%+v, %v)", dataSource, 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         if len(activities) == 0 {
234                 return nil
235         }
236
237         dataStreamID := DataStreamID(&fitness.DataSource{
238                 DataType: &fitness.DataType{
239                         Name: dataTypeNameActivitySegment,
240                 },
241                 Type: "raw",
242         })
243
244         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
245
246         datasetID := fmt.Sprintf("%d-%d", startOfDay.UnixNano(), endOfDay.UnixNano())
247         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do()
248         if err != nil {
249                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err)
250                 return err
251         }
252
253         var dataPoints []*fitness.DataPoint
254 Next:
255         for _, a := range activities {
256                 startTimeNanos := a.Start.UnixNano()
257                 endTimeNanos := a.End.UnixNano()
258
259                 for _, p := range res.Point {
260                         if p.StartTimeNanos == startTimeNanos && p.EndTimeNanos == endTimeNanos && p.Value[0].IntVal == a.Type {
261                                 log.Debugf(ctx, "activity %s already stored in Google Fit", a)
262                                 continue Next
263                         }
264                 }
265
266                 log.Debugf(ctx, "activity %s will be added to Google Fit", a)
267                 dataPoints = append(dataPoints, &fitness.DataPoint{
268                         DataTypeName:   dataTypeNameActivitySegment,
269                         StartTimeNanos: startTimeNanos,
270                         EndTimeNanos:   endTimeNanos,
271                         Value: []*fitness.Value{
272                                 &fitness.Value{IntVal: a.Type},
273                         },
274                 })
275         }
276
277         if len(dataPoints) == 0 {
278                 return nil
279         }
280
281         return c.DataSetPatch(ctx, dataStreamID, dataPoints)
282 }
283
284 func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataSource, rawValue *fitness.Value, startOfDay time.Time) error {
285         switch f := dataSource.DataType.Field[0].Format; f {
286         case "integer":
287                 if rawValue.IntVal == 0 {
288                         return nil
289                 }
290         case "floatPoint":
291                 if rawValue.FpVal == 0 {
292                         return nil
293                 }
294         default:
295                 return fmt.Errorf("unexpected data type field format %q", f)
296         }
297
298         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
299         if err != nil {
300                 return err
301         }
302         dataSource.DataStreamId = dataSourceID
303
304         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
305         currValue, startTime, err := c.readCumulative(ctx, dataSource, startOfDay, endOfDay)
306         if err != nil {
307                 return err
308         }
309
310         var diffValue fitness.Value
311         if dataSource.DataType.Field[0].Format == "integer" {
312                 if rawValue.IntVal == currValue.IntVal {
313                         return nil
314                 }
315                 diffValue.IntVal = rawValue.IntVal - currValue.IntVal
316                 if diffValue.IntVal < 0 {
317                         log.Warningf(ctx, "stored value (%d) is larger than new value (%d); assuming count was reset", currValue.IntVal, rawValue.IntVal)
318                         diffValue.IntVal = rawValue.IntVal
319                 }
320         } else { // if dataSource.DataType.Field[0].Format == "floatPoint"
321                 if rawValue.FpVal == currValue.FpVal {
322                         return nil
323                 }
324                 diffValue.FpVal = rawValue.FpVal - currValue.FpVal
325                 if diffValue.FpVal < 0 {
326                         log.Warningf(ctx, "stored value (%g) is larger than new value (%g); assuming count was reset", currValue.FpVal, rawValue.FpVal)
327                         diffValue.FpVal = rawValue.FpVal
328                 }
329         }
330
331         endTime := endOfDay
332         if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
333                 endTime = now
334         }
335         log.Debugf(ctx, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue)
336
337         return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
338                 &fitness.DataPoint{
339                         DataTypeName:   dataSource.DataType.Name,
340                         StartTimeNanos: startTime.UnixNano(),
341                         EndTimeNanos:   endTime.UnixNano(),
342                         Value:          []*fitness.Value{&diffValue},
343                 },
344         })
345 }
346
347 func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) {
348         datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
349
350         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do()
351         if err != nil {
352                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err)
353                 return nil, time.Time{}, err
354         }
355
356         if len(res.Point) == 0 {
357                 return &fitness.Value{}, startTime, nil
358         }
359
360         var sum fitness.Value
361         maxEndTime := startTime
362         for _, p := range res.Point {
363                 switch f := dataSource.DataType.Field[0].Format; f {
364                 case "integer":
365                         sum.IntVal += p.Value[0].IntVal
366                 case "floatPoint":
367                         sum.FpVal += p.Value[0].FpVal
368                 default:
369                         return nil, time.Time{}, fmt.Errorf("unexpected data type field format %q", f)
370                 }
371
372                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
373                 if maxEndTime.Before(pointEndTime) {
374                         maxEndTime = pointEndTime
375                 }
376         }
377
378         log.Debugf(ctx, "read cumulative data %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum)
379         return &sum, maxEndTime, nil
380 }
381
382 type heartRateDuration struct {
383         Min      int
384         Max      int
385         Duration time.Duration
386 }
387
388 type heartRateDurations []*heartRateDuration
389
390 func (res heartRateDurations) find(min, max int) (*heartRateDuration, bool) {
391         for _, d := range res {
392                 if d.Min != min || d.Max != max {
393                         continue
394                 }
395                 return d, true
396         }
397
398         return nil, false
399 }
400
401 func (c *Client) heartRate(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (heartRateDurations, time.Time, error) {
402         datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
403
404         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do()
405         if err != nil {
406                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err)
407                 return nil, time.Time{}, err
408         }
409
410         if len(res.Point) == 0 {
411                 return nil, startTime, nil
412         }
413
414         var results heartRateDurations
415         maxEndTime := startTime
416         for _, p := range res.Point {
417                 max := int(p.Value[1].FpVal)
418                 min := int(p.Value[2].FpVal)
419                 duration := time.Unix(0, p.EndTimeNanos).Sub(time.Unix(0, p.StartTimeNanos))
420
421                 if d, ok := results.find(min, max); ok {
422                         d.Duration += duration
423                 } else {
424                         results = append(results, &heartRateDuration{
425                                 Min:      min,
426                                 Max:      max,
427                                 Duration: duration,
428                         })
429                 }
430
431                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
432                 if maxEndTime.Before(pointEndTime) {
433                         maxEndTime = pointEndTime
434                 }
435         }
436
437         return results, maxEndTime, nil
438 }
439
440 func (c *Client) SetHeartRate(ctx context.Context, totalDurations []fitbit.HeartRateZone, restingHeartRate int, startOfDay time.Time) error {
441         dataSource := &fitness.DataSource{
442                 Application: Application(ctx),
443                 DataType: &fitness.DataType{
444                         Field: []*fitness.DataTypeField{
445                                 &fitness.DataTypeField{
446                                         Name:   "average",
447                                         Format: "floatPoint",
448                                 },
449                                 &fitness.DataTypeField{
450                                         Name:   "max",
451                                         Format: "floatPoint",
452                                 },
453                                 &fitness.DataTypeField{
454                                         Name:   "min",
455                                         Format: "floatPoint",
456                                 },
457                         },
458                         Name: dataTypeNameHeartrate,
459                 },
460                 Name: "Heart rate summary",
461                 Type: "raw",
462         }
463
464         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
465         if err != nil {
466                 return err
467         }
468         dataSource.DataStreamId = dataSourceID
469
470         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
471         prevDurations, startTime, err := c.heartRate(ctx, dataSource, startOfDay, endOfDay)
472
473         // calculate the difference between the durations mentioned in
474         // totalDurations and prevDurations and store it in diffDurations.
475         var diffDurations heartRateDurations
476         for _, d := range totalDurations {
477                 total := time.Duration(d.Minutes) * time.Minute
478
479                 var prev time.Duration
480                 if res, ok := prevDurations.find(d.Min, d.Max); ok {
481                         prev = res.Duration
482                 }
483
484                 diff := total - prev
485                 if diff < 0 {
486                         diff = total
487                 }
488
489                 if res, ok := diffDurations.find(d.Min, d.Max); ok {
490                         res.Duration += diff
491                 } else {
492                         diffDurations = append(diffDurations, &heartRateDuration{
493                                 Min:      d.Min,
494                                 Max:      d.Max,
495                                 Duration: diff,
496                         })
497                 }
498         }
499
500         // create a fitness.DataPoint for each non-zero duration difference.
501         var dataPoints []*fitness.DataPoint
502         for _, d := range diffDurations {
503                 if d.Duration < time.Nanosecond {
504                         continue
505                 }
506
507                 endTime := startTime.Add(d.Duration)
508                 if endTime.After(endOfDay) {
509                         log.Warningf(ctx, "heart rate durations exceed one day (current end time: %v)", endTime)
510                         break
511                 }
512
513                 average := float64(d.Min+d.Max) / 2.0
514                 if d.Min <= restingHeartRate && restingHeartRate <= d.Max {
515                         average = float64(restingHeartRate)
516                 }
517
518                 dataPoints = append(dataPoints, &fitness.DataPoint{
519                         DataTypeName:   dataSource.DataType.Name,
520                         StartTimeNanos: startTime.UnixNano(),
521                         EndTimeNanos:   endTime.UnixNano(),
522                         Value: []*fitness.Value{
523                                 &fitness.Value{
524                                         FpVal: average,
525                                 },
526                                 &fitness.Value{
527                                         FpVal: float64(d.Max),
528                                 },
529                                 &fitness.Value{
530                                         FpVal: float64(d.Min),
531                                 },
532                         },
533                 })
534
535                 startTime = endTime
536         }
537
538         if len(dataPoints) == 0 {
539                 return nil
540         }
541         return c.DataSetPatch(ctx, dataSource.DataStreamId, dataPoints)
542 }