refactor(server): replace legacy sync endpoint with cache refresh
All checks were successful
CVE Scan & Docker Build / security-scan (push) Successful in 29s
CVE Scan & Docker Build / build-and-push (push) Successful in 51s

Replaces legacy API-Football DB sync logic with cache invalidation. New endpoint clears DB and API caches, forcing fresh data fetch on next request. Updates dashboard button text and icon to reflect 'Refresh Cache'. Adjusts toast and result messages to report cache clearing duration.
This commit is contained in:
2026-04-11 19:24:46 +00:00
parent 362bf787f7
commit cc5c700921
2 changed files with 22 additions and 8 deletions

View File

@@ -849,6 +849,21 @@ async def api_cache_invalidate():
}) })
@dashboard.post("/api/sync")
async def api_sync():
"""Cache refresh — replaces the legacy API-Football DB sync."""
start = time.perf_counter()
db.invalidate_cache("%")
api.clear_cache()
duration = round(time.perf_counter() - start, 2)
return JSONResponse({
"ok": True,
"result": {"players": 0, "seasons": {}},
"duration_s": duration,
"note": "Cache cleared; fresh data will be fetched on next request.",
})
# ── Tool runner API ─────────────────────────────────────── # ── Tool runner API ───────────────────────────────────────
_TOOLS: dict[str, Any] = {} # populated after tool definitions exist _TOOLS: dict[str, Any] = {} # populated after tool definitions exist

View File

@@ -225,7 +225,7 @@
</div> </div>
<div> <div>
<button id="syncBtn" class="btn btn-warning w-100 fw-bold" onclick="triggerSync()"> <button id="syncBtn" class="btn btn-warning w-100 fw-bold" onclick="triggerSync()">
<i class="bi bi-cloud-download-fill me-2"></i>Sync TFC Data <i class="bi bi-arrow-clockwise me-2"></i>Refresh Cache
</button> </button>
<div id="syncResult" class="mt-2 small"></div> <div id="syncResult" class="mt-2 small"></div>
</div> </div>
@@ -437,29 +437,28 @@
const btn = document.getElementById('syncBtn'); const btn = document.getElementById('syncBtn');
const result = document.getElementById('syncResult'); const result = document.getElementById('syncResult');
btn.disabled = true; btn.disabled = true;
btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Syncing…'; btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Refreshing…';
result.textContent = ''; result.textContent = '';
document.getElementById('syncToastBody').textContent = 'Sync started — this takes ~10 seconds…'; document.getElementById('syncToastBody').textContent = 'Clearing cache…';
toast.show(); toast.show();
try { try {
const resp = await fetch('/api/sync', { method: 'POST' }); const resp = await fetch('/api/sync', { method: 'POST' });
const data = await resp.json(); const data = await resp.json();
if (data.ok) { if (data.ok) {
const r = data.result; result.innerHTML = `<span class="text-success">✅ Cache cleared in ${data.duration_s}s</span>`;
result.innerHTML = `<span class="text-success">✅ Synced ${r.players} players, ${Object.values(r.seasons || {}).reduce((a,b)=>a+b,0)} fixtures in ${data.duration_s}s</span>`; document.getElementById('syncToastBody').textContent = `Cache cleared in ${data.duration_s}s`;
document.getElementById('syncToastBody').textContent = `Sync complete in ${data.duration_s}s`;
await refreshStatus(); await refreshStatus();
} else { } else {
result.innerHTML = `<span class="text-danger">❌ ${data.error}</span>`; result.innerHTML = `<span class="text-danger">❌ ${data.error}</span>`;
document.getElementById('syncToastBody').textContent = 'Sync failed: ' + data.error; document.getElementById('syncToastBody').textContent = 'Refresh failed: ' + data.error;
} }
} catch (e) { } catch (e) {
result.innerHTML = `<span class="text-danger">❌ Network error</span>`; result.innerHTML = `<span class="text-danger">❌ Network error</span>`;
} finally { } finally {
btn.disabled = false; btn.disabled = false;
btn.innerHTML = '<i class="bi bi-cloud-download-fill me-2"></i>Sync TFC Data'; btn.innerHTML = '<i class="bi bi-arrow-clockwise me-2"></i>Refresh Cache';
} }
} }