refactor(server): replace legacy sync endpoint with cache refresh
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:
@@ -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 ───────────────────────────────────────
|
||||
|
||||
_TOOLS: dict[str, Any] = {} # populated after tool definitions exist
|
||||
|
||||
@@ -225,7 +225,7 @@
|
||||
</div>
|
||||
<div>
|
||||
<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>
|
||||
<div id="syncResult" class="mt-2 small"></div>
|
||||
</div>
|
||||
@@ -437,29 +437,28 @@
|
||||
const btn = document.getElementById('syncBtn');
|
||||
const result = document.getElementById('syncResult');
|
||||
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 = '';
|
||||
|
||||
document.getElementById('syncToastBody').textContent = 'Sync started — this takes ~10 seconds…';
|
||||
document.getElementById('syncToastBody').textContent = 'Clearing cache…';
|
||||
toast.show();
|
||||
|
||||
try {
|
||||
const resp = await fetch('/api/sync', { method: 'POST' });
|
||||
const data = await resp.json();
|
||||
if (data.ok) {
|
||||
const r = data.result;
|
||||
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 = `Sync complete in ${data.duration_s}s`;
|
||||
result.innerHTML = `<span class="text-success">✅ Cache cleared in ${data.duration_s}s</span>`;
|
||||
document.getElementById('syncToastBody').textContent = `Cache cleared in ${data.duration_s}s`;
|
||||
await refreshStatus();
|
||||
} else {
|
||||
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) {
|
||||
result.innerHTML = `<span class="text-danger">❌ Network error</span>`;
|
||||
} finally {
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user