63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
// 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 += `<p>Liczba serwerów z discord: ${summary.total_servers}</p>`;
|
|
html += `<p>Suma wszystkich odtworzeń: ${summary.total_plays}</p>`;
|
|
|
|
// Render each server and its top 5 tracks
|
|
const servers = summary.servers || {};
|
|
for (const [server, info] of Object.entries(servers)) {
|
|
html += `<section class="server-block"><h3>Serwer: ${server}</h3>`;
|
|
html += `<p>Łącznie odtworzeń: ${info.total}</p>`;
|
|
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 += `<p>Brak danych o utworach.</p>`;
|
|
} else {
|
|
html += `<ol>`;
|
|
html += `<br>`;
|
|
for (const [tname, plays] of sorted) {
|
|
html += `<li style="margin-top: 10px;">${tname} — ${plays} odtworzeń</li>`;
|
|
}
|
|
html += `</ol>`;
|
|
}
|
|
html += `</section>`;
|
|
}
|
|
|
|
out.innerHTML = html;
|
|
} catch (err) {
|
|
out.textContent = 'Błąd sieci: ' + err.message;
|
|
}
|
|
}
|
|
|
|
// Auto-run when included from danebota.html
|
|
if (typeof window !== 'undefined') {
|
|
window.addEventListener('DOMContentLoaded', renderStats);
|
|
}
|
|
|