Fix bugs of Fitbit mutation scripts

pull/143/head
Meng Li 2021-06-11 18:18:33 -04:00
parent 4b8698a4c6
commit f248b6c97d
7 changed files with 13 additions and 17 deletions

View File

@ -18,7 +18,7 @@ def parseCaloriesData(calories_data):
dataset = record["activities-calories-intraday"]["dataset"] dataset = record["activities-calories-intraday"]["dataset"]
for data in dataset: for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time() d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
d_datetime = datetime.combine(curr_date, d_time) d_datetime = datetime.combine(curr_date, d_time).strftime("%Y-%m-%d %H:%M:%S")
row_intraday = (device_id, data["level"], data["mets"], data["value"], d_datetime, 0) row_intraday = (device_id, data["level"], data["mets"], data["value"], d_datetime, 0)
records_intraday.append(row_intraday) records_intraday.append(row_intraday)

View File

@ -32,13 +32,13 @@ def parseHeartrateZones(heartrate_data):
def parseHeartrateIntradayData(records_intraday, dataset, device_id, curr_date, heartrate_zones_range): def parseHeartrateIntradayData(records_intraday, dataset, device_id, curr_date, heartrate_zones_range):
for data in dataset: for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time() d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
d_datetime = datetime.combine(curr_date, d_time) d_datetime = datetime.combine(curr_date, d_time).strftime("%Y-%m-%d %H:%M:%S")
d_hr = data["value"] d_hr = data["value"]
# Get heartrate zone by range: min <= heartrate < max # Get heartrate zone by range: min <= heartrate < max
d_hrzone = None d_hrzone = None
for hrzone, hrrange in heartrate_zones_range.items(): for hrzone, hrrange in heartrate_zones_range.items():
if d_hr >= hrrange[0] and d_hr < hrrange[1]: if d_hr >= hrrange[0] and d_hr <= hrrange[1]:
d_hrzone = hrzone d_hrzone = hrzone
break break

View File

@ -1,6 +1,5 @@
import json import json
import pandas as pd import pandas as pd
from datetime import datetime
HR_SUMMARY_COLUMNS = ("device_id", HR_SUMMARY_COLUMNS = ("device_id",
@ -55,7 +54,7 @@ def parseHeartrateData(heartrate_data):
for record in heartrate_data.json_fitbit_column: for record in heartrate_data.json_fitbit_column:
record = json.loads(record) # Parse text into JSON record = json.loads(record) # Parse text into JSON
if "activities-heart" in record: if "activities-heart" in record:
curr_date = datetime.strptime(record["activities-heart"][0]["dateTime"], "%Y-%m-%d") curr_date = record["activities-heart"][0]["dateTime"] + " 00:00:00"
record_summary = record["activities-heart"][0] record_summary = record["activities-heart"][0]
row_summary = parseHeartrateSummaryData(record_summary, device_id, curr_date) row_summary = parseHeartrateSummaryData(record_summary, device_id, curr_date)

View File

@ -64,7 +64,7 @@ def parseOneRecordForV1(record, device_id, d_is_main_sleep, records_intraday, ty
d_time = datetime.strptime(data["dateTime"], '%H:%M:%S').time() d_time = datetime.strptime(data["dateTime"], '%H:%M:%S').time()
if is_before_midnight and d_time.hour == 0: if is_before_midnight and d_time.hour == 0:
curr_date = end_date curr_date = end_date
d_datetime = datetime.combine(curr_date, d_time) d_datetime = datetime.combine(curr_date, d_time).strftime("%Y-%m-%d %H:%M:%S")
# API 1.2 stores original_level as strings, so we convert original_levels of API 1 to strings too # API 1.2 stores original_level as strings, so we convert original_levels of API 1 to strings too
# (1: "asleep", 2: "restless", 3: "awake") # (1: "asleep", 2: "restless", 3: "awake")
@ -86,7 +86,7 @@ def parseOneRecordForV12(record, device_id, d_is_main_sleep, records_intraday, t
if sleep_record_type == "classic": if sleep_record_type == "classic":
for data in record["levels"]["data"]: for data in record["levels"]["data"]:
d_datetime = dateutil.parser.parse(data["dateTime"]) d_datetime = data["dateTime"][:19].replace("T", " ")
row_intraday = (device_id, type_episode_id, data["seconds"], row_intraday = (device_id, type_episode_id, data["seconds"],
data["level"], d_is_main_sleep, sleep_record_type, data["level"], d_is_main_sleep, sleep_record_type,
@ -95,9 +95,10 @@ def parseOneRecordForV12(record, device_id, d_is_main_sleep, records_intraday, t
else: else:
# For sleep type "stages" # For sleep type "stages"
for data in mergeLongAndShortData(record["levels"]): for data in mergeLongAndShortData(record["levels"]):
d_datetime = data[0].strftime("%Y-%m-%d %H:%M:%S")
row_intraday = (device_id, type_episode_id, 30, row_intraday = (device_id, type_episode_id, 30,
data[1], d_is_main_sleep, sleep_record_type, data[1], d_is_main_sleep, sleep_record_type,
data[0], 0) d_datetime, 0)
records_intraday.append(row_intraday) records_intraday.append(row_intraday)

View File

@ -1,8 +1,5 @@
import json, yaml import json
import pandas as pd import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import dateutil.parser
SLEEP_SUMMARY_COLUMNS = ("device_id", "efficiency", SLEEP_SUMMARY_COLUMNS = ("device_id", "efficiency",
"minutes_after_wakeup", "minutes_asleep", "minutes_awake", "minutes_to_fall_asleep", "minutes_in_bed", "minutes_after_wakeup", "minutes_asleep", "minutes_awake", "minutes_to_fall_asleep", "minutes_in_bed",
@ -16,8 +13,8 @@ def parseOneSleepRecord(record, device_id, d_is_main_sleep, records_summary, epi
sleep_record_type = episode_type sleep_record_type = episode_type
d_start_datetime = datetime.strptime(record["startTime"][:18], "%Y-%m-%dT%H:%M:%S") d_start_datetime = record["startTime"][:19].replace("T", " ")
d_end_datetime = datetime.strptime(record["endTime"][:18], "%Y-%m-%dT%H:%M:%S") d_end_datetime = record["endTime"][:19].replace("T", " ")
# Summary data # Summary data
row_summary = (device_id, record["efficiency"], row_summary = (device_id, record["efficiency"],
record["minutesAfterWakeup"], record["minutesAsleep"], record["minutesAwake"], record["minutesToFallAsleep"], record["timeInBed"], record["minutesAfterWakeup"], record["minutesAsleep"], record["minutesAwake"], record["minutesToFallAsleep"], record["timeInBed"],

View File

@ -23,7 +23,7 @@ def parseStepsData(steps_data):
dataset = record["activities-steps-intraday"]["dataset"] dataset = record["activities-steps-intraday"]["dataset"]
for data in dataset: for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time() d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
d_datetime = datetime.combine(curr_date, d_time) d_datetime = datetime.combine(curr_date, d_time).strftime("%Y-%m-%d %H:%M:%S")
row_intraday = (device_id, row_intraday = (device_id,
data["value"], data["value"],

View File

@ -1,6 +1,5 @@
import json import json
import pandas as pd import pandas as pd
from datetime import datetime
STEPS_COLUMNS = ("device_id", "steps", "local_date_time", "timestamp") STEPS_COLUMNS = ("device_id", "steps", "local_date_time", "timestamp")
@ -16,7 +15,7 @@ def parseStepsData(steps_data):
for record in steps_data.json_fitbit_column: for record in steps_data.json_fitbit_column:
record = json.loads(record) # Parse text into JSON record = json.loads(record) # Parse text into JSON
if "activities-steps" in record.keys(): if "activities-steps" in record.keys():
curr_date = datetime.strptime(record["activities-steps"][0]["dateTime"], "%Y-%m-%d") curr_date = record["activities-steps"][0]["dateTime"] + " 00:00:00"
row_summary = (device_id, row_summary = (device_id,
record["activities-steps"][0]["value"], record["activities-steps"][0]["value"],