Package gfit: Unify debug messages for easier reading.
[kraftakt.git] / gfit / gfit.go
index eca961c..b6e9bc0 100644 (file)
@@ -120,7 +120,7 @@ func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataS
                        }
                        return DataStreamID(dataSource), nil
                }
-               log.Errorf(ctx, "c.Service.Users.DataSources.Create() = (%+v, %v)", res, err)
+               log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = (%+v, %v)", dataSource, res, err)
                return "", err
        }
        return res.DataStreamId, nil
@@ -225,13 +225,31 @@ type Activity struct {
        Type  int64
 }
 
+func (a Activity) String() string {
+       return fmt.Sprintf("%s-%s %d", a.Start.Format("15:04:05"), a.End.Format("15:04:05"), a.Type)
+}
+
 func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error {
-       dataStreamID := DataStreamID(&fitness.DataSource{
+       if len(activities) == 0 {
+               return nil
+       }
+
+       dataStreamID, err := c.DataSourceCreate(ctx, &fitness.DataSource{
+               Application: Application(ctx),
                DataType: &fitness.DataType{
+                       Field: []*fitness.DataTypeField{
+                               &fitness.DataTypeField{
+                                       Name:   "activity",
+                                       Format: "integer",
+                               },
+                       },
                        Name: dataTypeNameActivitySegment,
                },
                Type: "raw",
        })
+       if err != nil {
+               return err
+       }
 
        endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
 
@@ -249,13 +267,13 @@ Next:
                endTimeNanos := a.End.UnixNano()
 
                for _, p := range res.Point {
-                       if p.StartTimeNanos == startTimeNanos &&
-                               p.EndTimeNanos == endTimeNanos &&
-                               p.Value[0].IntVal == a.Type {
+                       if p.StartTimeNanos == startTimeNanos && p.EndTimeNanos == endTimeNanos && p.Value[0].IntVal == a.Type {
+                               log.Debugf(ctx, "activity %s already stored in Google Fit", a)
                                continue Next
                        }
                }
 
+               log.Debugf(ctx, "activity %s will be added to Google Fit", a)
                dataPoints = append(dataPoints, &fitness.DataPoint{
                        DataTypeName:   dataTypeNameActivitySegment,
                        StartTimeNanos: startTimeNanos,
@@ -324,7 +342,7 @@ func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataS
        if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
                endTime = now
        }
-       log.Debugf(ctx, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue)
+       log.Debugf(ctx, "add  cumulative data %s until %v: %+v", dataSource.DataStreamId, endTime, diffValue)
 
        return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
                &fitness.DataPoint{
@@ -346,6 +364,7 @@ func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSou
        }
 
        if len(res.Point) == 0 {
+               log.Debugf(ctx, "read cumulative data %s until %v: []", dataSource.DataStreamId, endTime)
                return &fitness.Value{}, startTime, nil
        }