fix(garmin): format sync payload with canonical list keys and verify synced counts

This commit is contained in:
Moritz Graf
2026-10-18 20:31:45 +02:00
parent 9f2c2c33d3
commit fc6a32697f
+23 -3
View File
@@ -428,9 +428,11 @@ def sync_date_range(
try:
sync_payload = {
"date": d,
"daily": [summary] if summary else [],
"sleep": [sleep] if sleep else [],
"activities": activities or [],
"daily_summary": summary,
"sleep_data": sleep,
"activities": activities,
}
sync_url = f"{target_backend}/garmin/sync"
req = urllib.request.Request(
@@ -440,9 +442,27 @@ def sync_date_range(
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as resp:
if resp.status == 200:
logger.info(f"Successfully posted Garmin sync for {d} to backend")
resp_body = {}
try:
resp_body = json.loads(resp.read().decode("utf-8"))
except Exception:
pass
expected_records = (1 if summary else 0) + (1 if sleep else 0) + (len(activities) if activities else 0)
synced_records = (
resp_body.get("synced_daily", 0)
+ resp_body.get("synced_sleep", 0)
+ resp_body.get("synced_activities", 0)
)
if resp.status == 200 and (expected_records == 0 or synced_records > 0):
logger.info(f"Successfully posted Garmin sync for {d} to backend: {resp_body}")
synced_via_backend = True
else:
logger.warning(
f"Backend returned HTTP {resp.status} for {d} but synced {synced_records} records "
f"(expected {expected_records}): {resp_body}. Falling back to local writer."
)
except Exception as e:
logger.warning(f"Failed to sync via backend {target_backend} for {d}: {e}. Falling back to local writer if available.")