Fix sync_lookback_days actually controlling the sync window
Activities: lookback_days was ignored once last_sync_at was set (since always took priority). Now lookback_days always sets the window; -1 is all-time on first sync then incremental. Wellness: lookback_days was never passed to sync_wellness at all — hardcoded 90-day cap regardless of settings. Fixed by adding lookback_days param and wiring it through from the Celery task. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -74,9 +74,9 @@ def sync_activities(garmin, user_id: int, since: Optional[datetime],
|
|||||||
List activities from Garmin Connect, skip any already in the DB, download
|
List activities from Garmin Connect, skip any already in the DB, download
|
||||||
FIT ZIPs for new ones, and queue them for processing.
|
FIT ZIPs for new ones, and queue them for processing.
|
||||||
|
|
||||||
On first sync (since=None) the start date is determined by lookback_days:
|
lookback_days controls the start date on every sync:
|
||||||
-1 → full history back to 2010; N → today minus N days.
|
-1 → full history back to 2010 on first sync, then incremental (since-1d)
|
||||||
On incremental syncs fetches from one day before last_sync_at.
|
N → always look back N days from today (dedup prevents re-downloading)
|
||||||
Returns the number of new activities queued.
|
Returns the number of new activities queued.
|
||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
@@ -84,10 +84,9 @@ def sync_activities(garmin, user_id: int, since: Optional[datetime],
|
|||||||
from app.models.user import Activity
|
from app.models.user import Activity
|
||||||
from sqlalchemy import select, func
|
from sqlalchemy import select, func
|
||||||
|
|
||||||
if since:
|
if lookback_days == -1:
|
||||||
start_date = (since - timedelta(days=1)).date()
|
# All-time: full pull on first sync, incremental thereafter
|
||||||
elif lookback_days == -1:
|
start_date = (since - timedelta(days=1)).date() if since else date(2010, 1, 1)
|
||||||
start_date = date(2010, 1, 1)
|
|
||||||
else:
|
else:
|
||||||
start_date = date.today() - timedelta(days=max(lookback_days, 1))
|
start_date = date.today() - timedelta(days=max(lookback_days, 1))
|
||||||
end_date = date.today()
|
end_date = date.today()
|
||||||
@@ -173,16 +172,23 @@ def sync_activities(garmin, user_id: int, since: Optional[datetime],
|
|||||||
|
|
||||||
# ── Wellness sync ─────────────────────────────────────────────────────────────
|
# ── Wellness sync ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def sync_wellness(garmin, user_id: int, since: Optional[datetime], db) -> int:
|
def sync_wellness(garmin, user_id: int, since: Optional[datetime], db,
|
||||||
|
lookback_days: int = 90) -> int:
|
||||||
"""
|
"""
|
||||||
Fetch daily stats / sleep / HRV from the Garmin Connect JSON API for each
|
Fetch daily stats / sleep / HRV from the Garmin Connect JSON API for each
|
||||||
day since `since` and upsert into health_metrics.
|
day in the window and upsert into health_metrics.
|
||||||
|
|
||||||
|
lookback_days controls the window on every sync:
|
||||||
|
-1 → full history back to 2010 on first sync, then incremental (since-1d)
|
||||||
|
N → always cover the last N days (upsert is safe to re-run)
|
||||||
Returns the number of days upserted.
|
Returns the number of days upserted.
|
||||||
"""
|
"""
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
|
|
||||||
# First sync: 90 days of wellness history; incremental: from last sync
|
if lookback_days == -1:
|
||||||
start_date = since.date() if since else (date.today() - timedelta(days=90))
|
start_date = (since - timedelta(days=1)).date() if since else date(2010, 1, 1)
|
||||||
|
else:
|
||||||
|
start_date = date.today() - timedelta(days=max(lookback_days, 1))
|
||||||
days = (date.today() - start_date).days + 1
|
days = (date.today() - start_date).days + 1
|
||||||
processed = 0
|
processed = 0
|
||||||
|
|
||||||
|
|||||||
@@ -507,7 +507,10 @@ def sync_garmin_connect_user(user_id: int):
|
|||||||
|
|
||||||
if cfg.sync_wellness:
|
if cfg.sync_wellness:
|
||||||
try:
|
try:
|
||||||
wellness_days = sync_wellness(garmin, user_id, cfg.last_sync_at, db)
|
wellness_days = sync_wellness(
|
||||||
|
garmin, user_id, cfg.last_sync_at, db,
|
||||||
|
lookback_days=cfg.sync_lookback_days if cfg.sync_lookback_days is not None else 90,
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
errors.append(f"wellness: {exc}")
|
errors.append(f"wellness: {exc}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user