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