SynaptitudeBrainHealth / synap-epics

For ZenHub this serves as the default repository containing all issues. This is the "default repository".
1 stars 0 forks source link

Fitbit Integration: Create a Endpoint for Fitbit activity scope #28

Closed russellwjoel closed 5 years ago

russellwjoel commented 5 years ago

Is your feature request related to a problem? Please describe. There are multiple scope categories for the fitbit API, The activity scope is used to track user activity data. https://dev.fitbit.com/build/reference/web-api/oauth2/#scope

This feature is similar to previous scope fitbit api implementations (e.g. sleep)

Describe the solution you'd like An endpoint in the application backend that accepts incoming API requests from fitbit for the activity scope.

Describe alternatives you've considered Combine all fitbit api calls into one singular call. that returns all scopes, as described in the API documentation.

When does it need to happen?

GanttStart: 02-19-2019

GanttDue: 02-26-2019

russellwjoel commented 5 years ago

This fits within my work for the week, I'm happy to be assigned this.

russellwjoel commented 5 years ago

Might be good to pull my Fitbit sub folder structure within public folder: 3b71471b57fb8eee3c7c6f630341174a11466512

aanchan commented 5 years ago

References: [1] https://nvie.com/posts/modifying-deeply-nested-structures/ [2] https://hackersandslackers.com/extract-data-from-complex-json-python/

aanchan commented 5 years ago

As an additional side note:

  1. Activities list endpoint : https://dev.fitbit.com/build/reference/web-api/activity/#get-activity-logs-list gives a list of activities as a JSON.
  2. This i.e. the activities list (unlike the sleep list) not include data from a regular daily summary endpoint (https://dev.fitbit.com/build/reference/web-api/activity/#get-daily-activity-summary). Daily summary includes "goals" and a "summary" structure. Daily summary also includes "heartRateZones" summary.
  3. Instead of making 120 API calls/h as a queue to get summary data per day, the same data that is immediately relevant is available through the activity time-series endpoint : https://dev.fitbit.com/build/reference/web-api/activity/#activity-time-series. So in order to populate the activitySummary table a list-like call must be made to the different resources given the date range. These are the list of resources:
    activities/calories
    activities/caloriesBMR
    activities/steps
    activities/distance
    activities/floors
    activities/elevation
    activities/minutesSedentary
    activities/minutesLightlyActive
    activities/minutesFairlyActive
    activities/minutesVeryActive
    activities/activityCalories

An example API call using the time_series method from the Fitbit Python API helper library like so :

resource = 'activities/activityCalories'
act_stats=fitbit_client.time_series(resource, base_date='2019-01-01',end_date='2019-02-28')

Also here is a distinction between calories and activity calories. Looks like activity calories and BMR calories should add up to total calories...that doesn't always seem to be the case for example travel.hamster:

26-2-2019

Here is a similar question asked by someone : https://community.fitbit.com/t5/Blaze/quot-Calories-quot-vs-quot-Activity-Calories-quot/td-p/1913357

The reason for making these notes is that the data available through the daily summary is going to be obtained in different/more efficient ways. Some of the data that is not available through this efficient method is :

  1. Summary heart rate zones
  2. Goals -- The daily goals instead are going to be obtained through the goals endpoint : https://dev.fitbit.com/build/reference/web-api/activity/#get-activity-goals

Reference summary data JSON for 27-02-2019 for travel.hamster



    "activities": [
        {
            "activeDuration": 1024000,
            "activityLevel": [
                {
                    "minutes": 0,
                    "name": "sedentary"
                },
                {
                    "minutes": 1,
                    "name": "lightly"
                },
                {
                    "minutes": 12,
                    "name": "fairly"
                },
                {
                    "minutes": 4,
                    "name": "very"
                }
            ],
            "activityName": "Outdoor Bike",
            "activityTypeId": 1071,
            "averageHeartRate": 97,
            "calories": 72,
            "duration": 1024000,
            "hasGps": false,
            "heartRateLink": "https://api.fitbit.com/1.2/user/-/activities/heart/date/2019-02-27/2019-02-27/1sec/time/09:39:21/09:56:25.json",
            "heartRateZones": [
                {
                    "max": 78,
                    "min": 30,
                    "minutes": 1,
                    "name": "Out of Range"
                },
                {
                    "max": 109,
                    "min": 78,
                    "minutes": 13,
                    "name": "Fat Burn"
                },
                {
                    "max": 132,
                    "min": 109,
                    "minutes": 3,
                    "name": "Cardio"
                },
                {
                    "max": 220,
                    "min": 132,
                    "minutes": 0,
                    "name": "Peak"
                }
            ],
            "lastModified": "2019-02-26T23:10:58.000Z",
            "logId": 20167544269,
            "logType": "auto_detected",
            "manualValuesSpecified": {
                "calories": false,
                "distance": false,
                "steps": false
            },
            "originalDuration": 1024000,
            "originalStartTime": "2019-02-27T09:39:21.000+11:00",
            "shouldFetchDetails": false,
            "startTime": "2019-02-27T09:39:21.000+11:00"
        },
        {
            "activeDuration": 1076000,
            "activityLevel": [
                {
                    "minutes": 0,
                    "name": "sedentary"
                },
                {
                    "minutes": 12,
                    "name": "lightly"
                },
                {
                    "minutes": 6,
                    "name": "fairly"
                },
                {
                    "minutes": 0,
                    "name": "very"
                }
            ],
            "activityName": "Outdoor Bike",
            "activityTypeId": 1071,
            "averageHeartRate": 77,
            "calories": 61,
            "duration": 1076000,
            "hasGps": false,
            "heartRateLink": "https://api.fitbit.com/1.2/user/-/activities/heart/date/2019-02-27/2019-02-27/1sec/time/15:47:08/16:05:04.json",
            "heartRateZones": [
                {
                    "max": 78,
                    "min": 30,
                    "minutes": 10,
                    "name": "Out of Range"
                },
                {
                    "max": 109,
                    "min": 78,
                    "minutes": 8,
                    "name": "Fat Burn"
                },
                {
                    "max": 132,
                    "min": 109,
                    "minutes": 0,
                    "name": "Cardio"
                },
                {
                    "max": 220,
                    "min": 132,
                    "minutes": 0,
                    "name": "Peak"
                }
            ],
            "lastModified": "2019-02-27T06:19:49.000Z",
            "logId": 20172904989,
            "logType": "auto_detected",
            "manualValuesSpecified": {
                "calories": false,
                "distance": false,
                "steps": false
            },
            "originalDuration": 1076000,
            "originalStartTime": "2019-02-27T15:47:08.000+11:00",
            "shouldFetchDetails": false,
            "startTime": "2019-02-27T15:47:08.000+11:00"
        },
        {
            "activeDuration": 1384000,
            "activityLevel": [
                {
                    "minutes": 1,
                    "name": "sedentary"
                },
                {
                    "minutes": 14,
                    "name": "lightly"
                },
                {
                    "minutes": 7,
                    "name": "fairly"
                },
                {
                    "minutes": 1,
                    "name": "very"
                }
            ],
            "activityName": "Outdoor Bike",
            "activityTypeId": 1071,
            "averageHeartRate": 93,
            "calories": 81,
            "duration": 1384000,
            "hasGps": false,
            "heartRateLink": "https://api.fitbit.com/1.2/user/-/activities/heart/date/2019-02-27/2019-02-27/1sec/time/17:03:05/17:26:09.json",
            "heartRateZones": [
                {
                    "max": 78,
                    "min": 30,
                    "minutes": 1,
                    "name": "Out of Range"
                },
                {
                    "max": 109,
                    "min": 78,
                    "minutes": 20,
                    "name": "Fat Burn"
                },
                {
                    "max": 132,
                    "min": 109,
                    "minutes": 2,
                    "name": "Cardio"
                },
                {
                    "max": 220,
                    "min": 132,
                    "minutes": 0,
                    "name": "Peak"
                }
            ],
            "lastModified": "2019-02-27T06:47:55.000Z",
            "logId": 20173266173,
            "logType": "auto_detected",
            "manualValuesSpecified": {
                "calories": false,
                "distance": false,
                "steps": false
            },
            "originalDuration": 1384000,
            "originalStartTime": "2019-02-27T17:03:05.000+11:00",
            "shouldFetchDetails": false,
            "startTime": "2019-02-27T17:03:05.000+11:00"
        },
        {
            "activeDuration": 1023000,
            "activityLevel": [
                {
                    "minutes": 0,
                    "name": "sedentary"
                },
                {
                    "minutes": 3,
                    "name": "lightly"
                },
                {
                    "minutes": 11,
                    "name": "fairly"
                },
                {
                    "minutes": 3,
                    "name": "very"
                }
            ],
            "activityName": "Outdoor Bike",
            "activityTypeId": 1071,
            "averageHeartRate": 95,
            "calories": 68,
            "duration": 1023000,
            "hasGps": false,
            "heartRateLink": "https://api.fitbit.com/1.2/user/-/activities/heart/date/2019-02-27/2019-02-27/1sec/time/19:41:49/19:58:52.json",
            "heartRateZones": [
                {
                    "max": 78,
                    "min": 30,
                    "minutes": 1,
                    "name": "Out of Range"
                },
                {
                    "max": 109,
                    "min": 78,
                    "minutes": 13,
                    "name": "Fat Burn"
                },
                {
                    "max": 132,
                    "min": 109,
                    "minutes": 3,
                    "name": "Cardio"
                },
                {
                    "max": 220,
                    "min": 132,
                    "minutes": 0,
                    "name": "Peak"
                }
            ],
            "lastModified": "2019-02-27T19:31:12.240Z",
            "logId": 20186097059,
            "logType": "auto_detected",
            "manualValuesSpecified": {
                "calories": false,
                "distance": false,
                "steps": false
            },
            "originalDuration": 1023000,
            "originalStartTime": "2019-02-27T19:41:49.000+11:00",
            "shouldFetchDetails": false,
            "startTime": "2019-02-27T19:41:49.000+11:00"
        }
    ],
    "goals": {
        "activeMinutes": 30,
        "calories": 2133,
        "distance": 5,
        "distanceUnit": "Mile",
        "steps": 10000
    },
    "summary": {
        "activityLevels": [
            {
                "distance": 0,
                "minutes": 382,
                "name": "sedentary"
            },
            {
                "distance": 3.05,
                "minutes": 310,
                "name": "lightly"
            },
            {
                "distance": 1.41,
                "minutes": 67,
                "name": "moderately"
            },
            {
                "distance": 0.54,
                "minutes": 25,
                "name": "very"
            }
        ],
        "calories": {
            "bmr": 1138,
            "total": 2068
        },
        "customHeartRateZones": [],
        "distance": 5.01,
        "heartRateZones": [
            {
                "caloriesOut": 1297.06428,
                "max": 78,
                "min": 30,
                "minutes": 1211,
                "name": "Out of Range"
            },
            {
                "caloriesOut": 604.33201,
                "max": 109,
                "min": 78,
                "minutes": 192,
                "name": "Fat Burn"
            },
            {
                "caloriesOut": 156.79581,
                "max": 132,
                "min": 109,
                "minutes": 31,
                "name": "Cardio"
            },
            {
                "caloriesOut": 0,
                "max": 220,
                "min": 132,
                "minutes": 0,
                "name": "Peak"
            }
        ],
        "steps": 11145
    }
}```
aanchan commented 5 years ago

Discrepancies between summary object calories and activitity time series calories -> total caloriesBMR -> bmr activityCalories -> activity_calories