feat: NAS 部署 + auth-hub 认证 + 同步逻辑修复
部署: - 后端 Python/Flask,数据层可插拔 SQLite/MariaDB - NAS 部署路径 /volume1/web/garmin-health-lab,端口 8124 - Gunicorn 生产服务器 (2 workers / 4 threads) - 开机自启脚本 deploy/S99garmin.sh - frp 隧道甲骨文 8124 → NAS 8124,外网访问 - 前端构建产物纳入版本管理 (backend/static/) 认证: - auth-hub OAuth2/OIDC 统一登录接入 - 新建 NAS 专用 client,注册内外网回调地址 - 前端 LoginPage 支持回调路由 /auth/callback - 数据同步页增加 Garmin 邮箱输入框 同步逻辑修复: - 自动同步调度器读取用户 history_days 设置,不再固定 2 天 - 前端 0(全部历史)不再被 || 吞掉,改为 ?? 处理 - 后端路由和 sync_data 中 0 不再被当成 falsy 回退默认值 - sync_data 和调度器中 0 → 730 天(全部历史=最大范围) - 已同步天数显示数据库实际总天数 (totalDays) - 历史范围新增「自上次同步」增量选项 (days=-1) - 后端 settings.py HISTORY 添加 -1 值 项目文档: - 创建 PROGRESS.md 跟踪项目进度
This commit is contained in:
@@ -54,17 +54,24 @@ def _set_sync_status(user_id, status, now, **fields):
|
||||
|
||||
def get_sync_status(user_id):
|
||||
row = query_one("SELECT * FROM sync_status WHERE user_id = ?", [user_id])
|
||||
# Count distinct days actually stored in the database for this user.
|
||||
count = query_one(
|
||||
"SELECT COUNT(DISTINCT date) AS cnt FROM health_data WHERE user_id = ?", [user_id]
|
||||
)
|
||||
total_days = count["cnt"] if count else 0
|
||||
if not row:
|
||||
return {
|
||||
"status": "idle",
|
||||
"lastSyncTime": None,
|
||||
"recordsSynced": 0,
|
||||
"totalDays": total_days,
|
||||
"lastError": None,
|
||||
}
|
||||
return {
|
||||
"status": row["status"],
|
||||
"lastSyncTime": row["last_sync_time"],
|
||||
"recordsSynced": row["records_synced"],
|
||||
"totalDays": total_days,
|
||||
"lastError": row["last_error"],
|
||||
"progressCurrent": row.get("progress_current"),
|
||||
"progressTotal": row.get("progress_total"),
|
||||
@@ -360,6 +367,10 @@ def _connect(creds, user_id=None):
|
||||
Garmin = _import_garmin()
|
||||
client = Garmin(is_cn=_is_cn())
|
||||
|
||||
# Garmin SSO can be slow from some networks; the default 10s timeout in
|
||||
# the underlying garth library is too tight for the initial login.
|
||||
client.garth.configure(timeout=30)
|
||||
|
||||
token = load_token(user_id) if user_id else None
|
||||
if token:
|
||||
client.garth.loads(token)
|
||||
@@ -937,7 +948,7 @@ def start_sync(user_id, creds, days=None):
|
||||
Progress lands in sync_status, which the UI polls; a full backfill runs
|
||||
far longer than any sensible HTTP timeout.
|
||||
"""
|
||||
days = days or DEFAULT_SYNC_DAYS
|
||||
days = DEFAULT_SYNC_DAYS if days is None else days
|
||||
now = datetime.datetime.utcnow().isoformat(timespec="seconds")
|
||||
until, msg = _rate_limit_block(user_id)
|
||||
if until:
|
||||
@@ -968,7 +979,9 @@ def sync_data(user_id, creds, days=None, client=None):
|
||||
|
||||
`client` exists so tests can inject a stub instead of reaching Garmin.
|
||||
"""
|
||||
days = days or DEFAULT_SYNC_DAYS
|
||||
days = DEFAULT_SYNC_DAYS if days is None else days
|
||||
if days == 0:
|
||||
days = 730 # 全部历史 → 最大范围
|
||||
now = datetime.datetime.utcnow().isoformat(timespec="seconds")
|
||||
until, msg = _rate_limit_block(user_id)
|
||||
if until:
|
||||
@@ -1004,6 +1017,23 @@ def sync_data(user_id, creds, days=None, client=None):
|
||||
}
|
||||
|
||||
today = datetime.date.today()
|
||||
|
||||
# -1 means "incremental sync": pick up from the latest date already in the
|
||||
# local database rather than pulling a fixed window.
|
||||
if days == -1:
|
||||
row = query_one("SELECT MAX(date) FROM health_daily WHERE user_id = ?", (user_id,))
|
||||
latest = row[0] if row and row[0] else None
|
||||
if latest is None:
|
||||
days = DEFAULT_SYNC_DAYS # first sync → fall back to default window
|
||||
else:
|
||||
days = max(1, (today - datetime.date.fromisoformat(latest)).days)
|
||||
# Rewrite the progress total now that we know the real day count.
|
||||
_set_sync_status(
|
||||
user_id, "syncing", now,
|
||||
records_synced=0, progress_current=0, progress_total=days,
|
||||
stage="连接 Garmin",
|
||||
)
|
||||
|
||||
start_date = (today - datetime.timedelta(days=days - 1)).isoformat()
|
||||
|
||||
days_synced = 0
|
||||
|
||||
Reference in New Issue
Block a user