同步进度增加详细天数反馈,优化日期范围避免重复请求无数据的历史日期

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-25 14:03:33 +08:00
parent ca98967af0
commit 58327a9a66
2 changed files with 28 additions and 11 deletions
+14 -4
View File
@@ -52,6 +52,7 @@ def sync_contract_bars_bg(contract_code: str) -> bool:
with _lock:
_progress["total"] = len(bars)
total_bars = len(bars)
inserted = 0
min_date = None
for i, bar in enumerate(bars):
@@ -71,6 +72,7 @@ def sync_contract_bars_bg(contract_code: str) -> bool:
min_date = bar["date"]
with _lock:
_progress["done"] = i + 1
_progress["label"] = f"同步行情 {code} · {i + 1}/{total_bars}"
if inserted > 0:
db.flush()
@@ -102,10 +104,19 @@ def sync_positions_bg(contract_code: str) -> bool:
.filter(DailyBar.contract == code)
.order_by(DailyBar.date).all()
]
missing = [d for d in bar_dates if d not in existing_dates]
# Only sync dates within or after existing position date range;
# akshare may not have data for very old dates
if existing_dates:
min_pos = min(existing_dates)
missing = [d for d in bar_dates if d not in existing_dates and d >= min_pos]
else:
missing = [d for d in bar_dates if d not in existing_dates]
total_missing = len(missing)
with _lock:
_progress["total"] = len(missing)
_progress["total"] = total_missing
if total_missing == 0:
_progress["label"] = f"同步持仓 {code} · 已是最新"
inserted = 0
for i, d in enumerate(missing):
@@ -120,8 +131,7 @@ def sync_positions_bg(contract_code: str) -> bool:
db.flush()
with _lock:
_progress["done"] = i + 1
db.commit()
_progress["label"] = f"同步持仓 {code} · {i + 1}/{total_missing}"
finally:
db.close()