同步进度增加详细天数反馈,优化日期范围避免重复请求无数据的历史日期
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+13
-3
@@ -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()
|
||||
]
|
||||
# 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()
|
||||
|
||||
|
||||
@@ -193,8 +193,10 @@
|
||||
|
||||
<script>
|
||||
var syncTimer = null;
|
||||
var progressShownAt = 0;
|
||||
|
||||
function showProgress() {
|
||||
progressShownAt = Date.now();
|
||||
document.getElementById('sync-progress').style.display = 'block';
|
||||
document.getElementById('sync-progress-bar').style.width = '0%';
|
||||
document.getElementById('sync-progress-pct').textContent = '';
|
||||
@@ -203,6 +205,7 @@ function showProgress() {
|
||||
|
||||
function hideProgress() {
|
||||
clearInterval(syncTimer);
|
||||
syncTimer = null;
|
||||
document.getElementById('sync-progress').style.display = 'none';
|
||||
}
|
||||
|
||||
@@ -216,23 +219,27 @@ function pollProgress() {
|
||||
document.getElementById('sync-progress-label').textContent = data.label;
|
||||
|
||||
if (data.finished) {
|
||||
var elapsed = Date.now() - progressShownAt;
|
||||
var delay = elapsed < 1000 ? 1000 - elapsed : 0;
|
||||
setTimeout(function() {
|
||||
clearInterval(syncTimer);
|
||||
syncTimer = null;
|
||||
document.getElementById('sync-progress-bar').style.width = '100%';
|
||||
document.getElementById('sync-progress-pct').textContent = '100%';
|
||||
document.getElementById('sync-progress-label').textContent = data.label + ' 完成';
|
||||
document.getElementById('sync-progress-pct').textContent = '✓';
|
||||
document.getElementById('sync-progress-label').textContent = data.label + ' ✓';
|
||||
document.getElementById('sync-progress-pct').textContent = '';
|
||||
}, delay);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function startSync(url) {
|
||||
if (syncTimer) { clearInterval(syncTimer); }
|
||||
if (syncTimer) { clearInterval(syncTimer); syncTimer = null; }
|
||||
showProgress();
|
||||
fetch(url, { method: 'POST' })
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
if (!data.ok) { hideProgress(); return; }
|
||||
syncTimer = setInterval(pollProgress, 600);
|
||||
syncTimer = setInterval(pollProgress, 500);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user