heedy / heedy-fitbit-plugin

Sync your fitbit data to heedy
Other
4 stars 1 forks source link

Sync fails when no elevation data is present #11

Closed mammuth closed 2 years ago

mammuth commented 2 years ago

When the plugin syncs, Fitbitseems to return an error that isn't handled gracefully by the plugin:

heedy                  | INFO:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Syncing data for Max 
heedy                  | DEBUG:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2020-12-04/1d/1sec.json
heedy                  | DEBUG:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2020-12-04/1d/1min.json
heedy                  | DEBUG:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Querying: https://api.fitbit.com/1/user/-/activities/elevation/date/2021-04-03/1d/1min.json
heedy                  | DEBUG:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Error response: {"errors": [{"errorType": "validation", "fieldName": "resource path", "message": "Invalid time series resource path: /activities/elevation"}]}
heedy                  | ERROR:fitbit:46d59eed-feb4-4c95-a272-022e79f017c8:Sync failed
heedy                  | Traceback (most recent call last):
heedy                  |   File "/data/plugins/fitbit/syncer.py", line 340, in start
heedy                  |     await self.sync_intraday(s)
heedy                  |   File "/data/plugins/fitbit/syncer.py", line 206, in sync_intraday
heedy                  |     dpa = data[f"activities-{a['key']}-intraday"]["dataset"]
heedy                  | KeyError: 'activities-elevation-intraday'

Assumption: I have a fitbit device that does not track elevation, so fitbit simply doesn't have any elevation data.

Manually "removing" the elevation from syncer.py mitigates the issue and the other data is fetched correctly.

I can try to look into fixing this issue, however, I'm still unsure how to setup heedy, especially for development, since I need an installation behind a https proxy to be able to get the fitbit refresh token (or I have it once now and try to just copy-paste the auth token from the "public" instance to a development instance :thinking:

dkumor commented 2 years ago

Oh crap, you're right! I might be able to fix this later today, since this is a breaking bug.

Regarding debugging: Fitbit allows refresh tokens for localhost, so during development, I use a local instance for debugging.

When debugging, you can create a dev server:

./heedy create testdb
./heedy run testdb --development

This will create a database in testdb folder and run it in foreground

mammuth commented 2 years ago

Regarding debugging: Fitbit allows refresh tokens for localhost, so during development, I use a local instance for debugging.

Oh no, I thought I tried that in the beginning but then I went down the rabbit hole of setting it up behind a reverse proxy just to get fitbit authenticated :see_no_evil: :laughing:

Thanks for you development tipps :+1:

dkumor commented 2 years ago

I made a quick edit that might fix the issue. Would you be able to test it? If the fix works, then I'll release the update.

In the future, I will probably want to refactor a bit, so the timeseries isn't created if there is no elevation data.

mammuth commented 2 years ago

Seems to mitigate the crash :+1:

INFO:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Syncing data for Max
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-03-14/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-03-14/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/elevation/date/2021-04-03/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Error response: {"errors": [{"errorType": "validation", "fieldName": "resource path", "message": "Invalid time series resource path: /activities/elevation"}]}
WARNING:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:No data for elevation
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1.2/user/-/sleep/date/2021-04-03/2021-04-13.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-03-15/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-03-15/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/elevation/date/2022-05-05/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Error response: {"errors": [{"errorType": "validation", "fieldName": "resource path", "message": "Invalid time series resource path: /activities/elevation"}]}
WARNING:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:No data for elevation
dkumor commented 2 years ago

v0.1.1 is released. Hopefully, it fixed the issue!

The release includes another fix made a couple of minutes ago.

mammuth commented 2 years ago

Your latest commit also works fine for me :100:

INFO:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Syncing data for Max M
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-05-05/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-05-05/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/elevation/date/2021-04-03/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Error response: {"errors": [{"errorType": "validation", "fieldName": "resource path", "message": "Invalid time series resource path: /activities/elevation"}]}
WARNING:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:No data for elevation
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1.2/user/-/sleep/date/2022-05-07/2022-05-07.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-05-06/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-05-06/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-05-07/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-05-07/1d/1min.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/heart/date/2021-05-08/1d/1sec.json
DEBUG:fitbit:f053cea9-c750-43dd-a41b-fa1e51ac60e8:Querying: https://api.fitbit.com/1/user/-/activities/steps/date/2021-05-08/1d/1min.json