// Fetch playcount data from backend API and expose a helper to render it async function fetchPlaycounts() { try { const resp = await fetch('/api/playcounts'); if (!resp.ok) throw new Error('Network response was not ok'); return await resp.json(); } catch (err) { return { error: err.message }; } } // Simple renderer for danebota.html - injects table counts async function renderStats() { const out = document.getElementById('bot-stats'); if (!out) return; out.textContent = 'Ładowanie…'; // prefer the summary endpoint which gives aggregated totals try { const resp = await fetch('/api/playcounts/summary'); if (!resp.ok) throw new Error('Network response was not ok'); const summary = await resp.json(); if (summary.error) { out.textContent = 'Błąd: ' + (summary.detail || summary.error); return; } let html = ''; html += `
Liczba serwerów z discord: ${summary.total_servers}
`; html += `Suma wszystkich odtworzeń: ${summary.total_plays}
`; // Render each server and its top 5 tracks const servers = summary.servers || {}; for (const [server, info] of Object.entries(servers)) { html += `Łącznie odtworzeń: ${info.total}
`; const tracks = info.tracks || {}; // sort tracks by plays desc const sorted = Object.entries(tracks).sort((a, b) => b[1] - a[1]).slice(0, 10000); if (sorted.length === 0) { html += `Brak danych o utworach.
`; } else { html += `