持仓同步改为后台执行,添加进度条实时反馈
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -34,18 +34,24 @@
|
||||
<form method="post" action="/admin/sync" style="display:inline;">
|
||||
<button type="submit" class="btn btn-primary" style="font-size:0.82rem;padding:6px 16px;">同步行情</button>
|
||||
</form>
|
||||
<form method="post" action="/admin/sync-positions" style="display:inline;">
|
||||
<button type="submit" class="btn btn-primary" style="font-size:0.82rem;padding:6px 16px;background:var(--success);">同步持仓</button>
|
||||
</form>
|
||||
<button id="btn-sync-pos" class="btn btn-primary" style="font-size:0.82rem;padding:6px 16px;background:var(--success);" onclick="startPosSync()">同步持仓</button>
|
||||
{% if request.query_params.get('synced') %}
|
||||
<span style="font-size:0.82rem;color:var(--success-fg);">✓ 行情 {{ request.query_params.synced }} 条</span>
|
||||
{% endif %}
|
||||
{% if request.query_params.get('pos_synced') %}
|
||||
<span style="font-size:0.82rem;color:var(--success-fg);">✓ 持仓 {{ request.query_params.pos_synced }} 条</span>
|
||||
{% endif %}
|
||||
<span id="sync-pos-msg" style="font-size:0.82rem;color:var(--sub);"></span>
|
||||
<span style="font-size:0.78rem;color:var(--sub);margin-left:auto;">{{ total_contracts }} 个合约</span>
|
||||
</div>
|
||||
|
||||
<div id="pos-progress" style="display:none;margin-bottom:20px;padding:14px 18px;background:var(--surface);border:1px solid var(--border);border-radius:10px;">
|
||||
<div style="display:flex;align-items:center;gap:10px;margin-bottom:8px;">
|
||||
<span id="pos-progress-label" style="font-size:0.82rem;color:var(--fg);">正在同步...</span>
|
||||
<span id="pos-progress-pct" style="font-size:0.82rem;color:var(--sub);margin-left:auto;"></span>
|
||||
</div>
|
||||
<div style="width:100%;height:6px;background:var(--border);border-radius:3px;overflow:hidden;">
|
||||
<div id="pos-progress-bar" style="width:0%;height:100%;background:var(--accent);border-radius:3px;transition:width .3s;"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if products %}
|
||||
{% for p in products %}
|
||||
<div style="margin-bottom:16px;border:1px solid var(--border);border-radius:10px;overflow:hidden;">
|
||||
@@ -196,6 +202,64 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var posSyncTimer = null;
|
||||
|
||||
function startPosSync() {
|
||||
var btn = document.getElementById('btn-sync-pos');
|
||||
btn.disabled = true;
|
||||
btn.textContent = '发起中...';
|
||||
document.getElementById('sync-pos-msg').textContent = '';
|
||||
|
||||
fetch('/admin/sync-positions', { method: 'POST' })
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
if (data.error) {
|
||||
document.getElementById('sync-pos-msg').textContent = '⚠ ' + data.error;
|
||||
btn.disabled = false;
|
||||
btn.textContent = '同步持仓';
|
||||
return;
|
||||
}
|
||||
document.getElementById('pos-progress').style.display = 'block';
|
||||
btn.textContent = '同步中...';
|
||||
posSyncTimer = setInterval(pollProgress, 800);
|
||||
});
|
||||
}
|
||||
|
||||
function pollProgress() {
|
||||
fetch('/admin/sync-status')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
var done = data.contract_done;
|
||||
var total = data.contract_total;
|
||||
var dDone = data.date_done;
|
||||
var dTotal = data.date_total;
|
||||
var pct = 0;
|
||||
|
||||
if (data.finished) {
|
||||
pct = 100;
|
||||
clearInterval(posSyncTimer);
|
||||
document.getElementById('pos-progress-pct').textContent = '100%';
|
||||
document.getElementById('pos-progress-label').textContent =
|
||||
'完成:' + total + ' 个合约,共同步 ' + data.rows + ' 条持仓数据';
|
||||
document.getElementById('sync-pos-msg').textContent = '✓ 已同步 ' + data.rows + ' 条';
|
||||
var btn = document.getElementById('btn-sync-pos');
|
||||
btn.disabled = false;
|
||||
btn.textContent = '同步持仓';
|
||||
} else if (total > 0) {
|
||||
// Weight: contracts account for most of the bar, dates for fine-grained within current contract
|
||||
pct = Math.round((done + (dTotal > 0 ? dDone / dTotal : 0)) / total * 100);
|
||||
document.getElementById('pos-progress-pct').textContent = pct + '%';
|
||||
var label = '合约 ' + (done + 1) + '/' + total + ':' + data.contract;
|
||||
if (dTotal > 0) {
|
||||
label += '(' + dDone + '/' + dTotal + ' 天)';
|
||||
}
|
||||
document.getElementById('pos-progress-label').textContent = label;
|
||||
}
|
||||
|
||||
document.getElementById('pos-progress-bar').style.width = pct + '%';
|
||||
});
|
||||
}
|
||||
|
||||
function switchTab(name) {
|
||||
document.querySelectorAll('.tab-btn').forEach(function(b) { b.classList.remove('active'); });
|
||||
document.querySelectorAll('.tab-panel').forEach(function(p) { p.classList.remove('active'); });
|
||||
|
||||
Reference in New Issue
Block a user