1e41ef207e
- Carte visiteurs uniques non-bot : 7 / 14 / 30 jours en tête de /admin/stats - Bouton ✕ par AS pour l'exclure des stats ; section AS exclus avec ↺ - Alerte IPs sans résolution AS dans la carte pays - Parser : fenêtre 30 jours, calcul visiteurs uniques toutes IPs non-bot - Graphiques adaptés à 30 jours (labels x/3) - Réactions articles : 👍 uniquement (suppression 🔥 et 🤔) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
722 lines
35 KiB
JavaScript
722 lines
35 KiB
JavaScript
/* Admin stats : graphiques, sparklines, accordéon pays/AS/IP, agents */
|
|
|
|
function esc(s) {
|
|
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
}
|
|
|
|
function trunc(s, n) {
|
|
return s.length > n ? s.slice(0, n) + '…' : s;
|
|
}
|
|
|
|
// Détection de bot par correspondance partielle insensible à la casse
|
|
var _botPatterns = (typeof FOLIO_BOT_PATTERNS !== 'undefined') ? FOLIO_BOT_PATTERNS : [];
|
|
function isBot(ua) {
|
|
if (!ua) { return false; }
|
|
var lo = ua.toLowerCase();
|
|
for (var i = 0; i < _botPatterns.length; i++) {
|
|
if (lo.indexOf(_botPatterns[i].toLowerCase()) !== -1) { return true; }
|
|
}
|
|
return false;
|
|
}
|
|
function botBadge(ua) {
|
|
return isBot(ua) ? '<span title="Bot connu" style="font-size:.85rem">🤖</span> ' : '';
|
|
}
|
|
|
|
// AS exclus (modifié dynamiquement par les boutons)
|
|
var _excludedAs = (typeof FOLIO_EXCLUDED_AS !== 'undefined') ? FOLIO_EXCLUDED_AS.slice() : [];
|
|
var _csrf = (typeof FOLIO_CSRF !== 'undefined') ? FOLIO_CSRF : '';
|
|
|
|
// ── Résumé visiteurs ─────────────────────────────────────────────────────────
|
|
(function () {
|
|
var el = document.getElementById('stats-summary-container');
|
|
var uv = (typeof FOLIO_UNIQUE_VISITORS !== 'undefined') ? FOLIO_UNIQUE_VISITORS : {};
|
|
var ipd = (typeof FOLIO_IP_DATA !== 'undefined') ? FOLIO_IP_DATA : {};
|
|
if (!el) { return; }
|
|
|
|
function computeCounts() {
|
|
var base = { 7: uv[7] || 0, 14: uv[14] || 0, 30: uv[30] || 0 };
|
|
// Soustraire les IPs des AS exclus (top 200 uniquement — approximation)
|
|
Object.keys(ipd).forEach(function (ip) {
|
|
var d = ipd[ip];
|
|
if (!d.asn || _excludedAs.indexOf(d.asn) === -1) { return; }
|
|
var daily = d.daily || [];
|
|
var n = daily.length;
|
|
if (daily.some(function (v) { return v > 0; })) { base[30] = Math.max(0, base[30] - 1); }
|
|
if (daily.slice(Math.max(0, n - 14)).some(function (v) { return v > 0; })) { base[14] = Math.max(0, base[14] - 1); }
|
|
if (daily.slice(Math.max(0, n - 7)).some(function (v) { return v > 0; })) { base[7] = Math.max(0, base[7] - 1); }
|
|
});
|
|
return base;
|
|
}
|
|
|
|
function render() {
|
|
var c = computeCounts();
|
|
el.innerHTML =
|
|
'<div class="card mb-4">'
|
|
+ '<div class="card-body py-2 px-3">'
|
|
+ '<div class="d-flex flex-wrap gap-4 align-items-center">'
|
|
+ '<span class="small fw-semibold text-muted">Visiteurs uniques non-bot</span>'
|
|
+ '<span class="d-flex flex-column align-items-center"><span class="fs-5 fw-bold">' + c[7].toLocaleString('fr-FR') + '</span><span class="text-muted" style="font-size:.7rem">7 jours</span></span>'
|
|
+ '<span class="d-flex flex-column align-items-center"><span class="fs-5 fw-bold">' + c[14].toLocaleString('fr-FR') + '</span><span class="text-muted" style="font-size:.7rem">14 jours</span></span>'
|
|
+ '<span class="d-flex flex-column align-items-center"><span class="fs-5 fw-bold">' + c[30].toLocaleString('fr-FR') + '</span><span class="text-muted" style="font-size:.7rem">30 jours</span></span>'
|
|
+ (_excludedAs.length ? '<span class="badge bg-warning text-dark" style="font-size:.65rem">' + _excludedAs.length + ' AS exclu(s)</span>' : '')
|
|
+ '</div>'
|
|
+ '</div>'
|
|
+ '</div>';
|
|
}
|
|
|
|
render();
|
|
document.addEventListener('folio:excluded-as-changed', render);
|
|
}());
|
|
|
|
// ── Visiteurs par pays ────────────────────────────────────────────────────────
|
|
(function () {
|
|
var el = document.getElementById('stats-country-container');
|
|
var asList = (typeof FOLIO_AS_LIST !== 'undefined') ? FOLIO_AS_LIST : [];
|
|
var ipData = (typeof FOLIO_IP_DATA !== 'undefined') ? FOLIO_IP_DATA : {};
|
|
if (!el || !asList.length) { return; }
|
|
|
|
var dispNames = null;
|
|
try { dispNames = new Intl.DisplayNames(['fr'], { type: 'region' }); } catch (e) {}
|
|
function countryName(code) {
|
|
if (!code || code === '??') { return 'Inconnu'; }
|
|
try { return dispNames ? dispNames.of(code) : code; } catch (e) { return code; }
|
|
}
|
|
function flag(code) {
|
|
if (!code || code.length !== 2) { return ''; }
|
|
var cp = Array.from(code.toUpperCase()).map(function (c) { return 0x1F1E6 + c.charCodeAt(0) - 65; });
|
|
return String.fromCodePoint(cp[0], cp[1]) + ' ';
|
|
}
|
|
|
|
// Index IPs par ASN
|
|
var ipsByAsn = {};
|
|
Object.keys(ipData).forEach(function (ip) {
|
|
var d = ipData[ip];
|
|
var key = d.asn || '__unknown__';
|
|
if (!ipsByAsn[key]) { ipsByAsn[key] = []; }
|
|
ipsByAsn[key].push({ ip: ip, hits: d.hits, daily: d.daily, paths: d.paths, agents: d.agents || [] });
|
|
});
|
|
Object.keys(ipsByAsn).forEach(function (k) {
|
|
ipsByAsn[k].sort(function (a, b) { return b.hits - a.hits; });
|
|
});
|
|
|
|
// IPs sans AS
|
|
var noAsCount = Object.keys(ipData).filter(function (ip) { return !ipData[ip].asn; }).length;
|
|
|
|
function ipSparkline(daily) {
|
|
if (!daily || !daily.length) { return ''; }
|
|
var W = 80, H = 20, padX = 1, padY = 2;
|
|
var max = Math.max.apply(null, daily) || 1;
|
|
var n = daily.length;
|
|
var pts = daily.map(function (v, i) {
|
|
var x = padX + i * (W - 2 * padX) / (n - 1);
|
|
var y = H - padY - (v / max) * (H - 2 * padY);
|
|
return x.toFixed(1) + ',' + y.toFixed(1);
|
|
}).join(' ');
|
|
return '<svg xmlns="http://www.w3.org/2000/svg" width="' + W + '" height="' + H
|
|
+ '" style="display:block;flex-shrink:0">'
|
|
+ '<polyline points="' + pts + '" fill="none" stroke="#6c757d"'
|
|
+ ' stroke-width="1.2" stroke-linejoin="round" stroke-linecap="round"/>'
|
|
+ '</svg>';
|
|
}
|
|
|
|
function excludeAs(asn, name) {
|
|
var fd = new FormData();
|
|
fd.append('_csrf', _csrf);
|
|
fd.append('asn', asn);
|
|
fetch('/?action=admin_add_excluded_as', { method: 'POST', body: fd })
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (d.ok && _excludedAs.indexOf(asn) === -1) {
|
|
_excludedAs.push(asn);
|
|
document.dispatchEvent(new CustomEvent('folio:excluded-as-changed'));
|
|
renderCountry();
|
|
}
|
|
});
|
|
}
|
|
|
|
function includeAs(asn) {
|
|
var fd = new FormData();
|
|
fd.append('_csrf', _csrf);
|
|
fd.append('asn', asn);
|
|
fetch('/?action=admin_remove_excluded_as', { method: 'POST', body: fd })
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (d.ok) {
|
|
_excludedAs = _excludedAs.filter(function (a) { return a !== asn; });
|
|
document.dispatchEvent(new CustomEvent('folio:excluded-as-changed'));
|
|
renderCountry();
|
|
}
|
|
});
|
|
}
|
|
|
|
function buildIpRow(ipInfo) {
|
|
// Agents sous l'IP avec badge bot (UA en entier)
|
|
var agentsHtml = '';
|
|
(ipInfo.agents || []).forEach(function (ua) {
|
|
agentsHtml += '<div style="font-size:.65rem;color:#adb5bd;line-height:1.4;word-break:break-all">'
|
|
+ botBadge(ua) + esc(ua) + '</div>';
|
|
});
|
|
|
|
// Chemins triés : /post/ et /book/ avec ts, reste sans ts
|
|
var postBook = [], other = [];
|
|
Object.keys(ipInfo.paths || {}).forEach(function (path) {
|
|
var p = ipInfo.paths[path];
|
|
var cnt = (p && typeof p === 'object') ? p.n : p;
|
|
var ts = (p && typeof p === 'object') ? p.ts : 0;
|
|
if (ts > 0) { postBook.push({ path: path, cnt: cnt, ts: ts }); }
|
|
else { other.push({ path: path, cnt: cnt }); }
|
|
});
|
|
postBook.sort(function (a, b) { return b.ts - a.ts; });
|
|
other.sort(function (a, b) { return b.cnt - a.cnt; });
|
|
|
|
function pathLine(p, prefix) {
|
|
var raw = p.path.replace(prefix, '');
|
|
var slug = '';
|
|
try { slug = decodeURIComponent(raw); } catch (e) { slug = raw; }
|
|
return '<div style="font-size:.75rem;line-height:1.5">'
|
|
+ '<a href="' + esc(p.path) + '" target="_blank" style="color:#495057">'
|
|
+ esc(trunc(slug || p.path, 40)) + '</a>'
|
|
+ ' <span style="color:#adb5bd">(' + p.cnt + ')</span></div>';
|
|
}
|
|
function otherLine(p) {
|
|
return '<div style="font-size:.72rem;color:#868e96;line-height:1.4">'
|
|
+ '<code style="font-size:.72rem;color:#868e96">' + esc(trunc(p.path, 44)) + '</code>'
|
|
+ ' <span style="color:#adb5bd">(' + p.cnt + ')</span></div>';
|
|
}
|
|
|
|
var pathsHtml = '';
|
|
var articles = postBook.filter(function (p) { return p.path.indexOf('/post/') === 0; });
|
|
var books = postBook.filter(function (p) { return p.path.indexOf('/book/') === 0; });
|
|
if (articles.length) {
|
|
pathsHtml += '<div style="font-size:.7rem;color:#adb5bd;margin-top:2px">Articles</div>'
|
|
+ articles.map(function (p) { return pathLine(p, '/post/'); }).join('');
|
|
}
|
|
if (books.length) {
|
|
pathsHtml += '<div style="font-size:.7rem;color:#adb5bd;margin-top:2px">Livres</div>'
|
|
+ books.map(function (p) { return pathLine(p, '/book/'); }).join('');
|
|
}
|
|
if (other.length) {
|
|
pathsHtml += '<div style="font-size:.7rem;color:#adb5bd;margin-top:2px">Autres chemins</div>'
|
|
+ other.map(otherLine).join('');
|
|
}
|
|
if (!pathsHtml) { pathsHtml = '<span style="font-size:.75rem;color:#adb5bd">—</span>'; }
|
|
|
|
return '<div class="d-flex gap-2 py-2 border-bottom align-items-start">'
|
|
+ '<div style="width:9rem;flex-shrink:0">'
|
|
+ '<code style="font-size:.72rem;color:#6c757d">' + esc(ipInfo.ip) + '</code>'
|
|
+ agentsHtml
|
|
+ '</div>'
|
|
+ '<div style="flex-shrink:0;padding-top:2px">' + ipSparkline(ipInfo.daily || []) + '</div>'
|
|
+ '<div class="flex-grow-1">' + pathsHtml + '</div>'
|
|
+ '<div class="text-end text-muted small" style="width:4rem;flex-shrink:0;padding-top:2px">'
|
|
+ (ipInfo.hits || 0).toLocaleString('fr-FR') + '</div>'
|
|
+ '</div>';
|
|
}
|
|
|
|
function renderCountry() {
|
|
// Filtrer les AS actifs / exclus
|
|
var activeLists = asList.filter(function (as) { return _excludedAs.indexOf(as.asn) === -1; });
|
|
var excludedLists = asList.filter(function (as) { return _excludedAs.indexOf(as.asn) !== -1; });
|
|
|
|
// Agréger par pays (AS actifs uniquement)
|
|
var byCountry = {}, asByCountry = {};
|
|
activeLists.forEach(function (as) {
|
|
var c = as.country || '??';
|
|
byCountry[c] = (byCountry[c] || 0) + as.hits;
|
|
if (!asByCountry[c]) { asByCountry[c] = []; }
|
|
asByCountry[c].push(as);
|
|
});
|
|
|
|
var countries = Object.keys(byCountry).map(function (c) {
|
|
return { code: c, hits: byCountry[c], networks: asByCountry[c] };
|
|
}).sort(function (a, b) { return b.hits - a.hits; }).slice(0, 20);
|
|
|
|
// En-tête avec alerte IPs sans AS
|
|
var headerExtra = '';
|
|
if (noAsCount > 0) {
|
|
headerExtra = '<span class="badge bg-warning text-dark ms-2" style="font-size:.65rem" '
|
|
+ 'title="' + noAsCount + ' IP(s) parmi le top 200 sans résolution AS">'
|
|
+ noAsCount + ' IP(s) sans AS</span>';
|
|
}
|
|
|
|
var countryCard = document.querySelector('#stats-country-container');
|
|
if (countryCard) {
|
|
var hdr = countryCard.closest('.card')
|
|
? countryCard.closest('.card').querySelector('.card-header')
|
|
: null;
|
|
if (hdr && !hdr.querySelector('.no-as-badge')) {
|
|
var span = document.createElement('span');
|
|
span.className = 'no-as-badge';
|
|
span.innerHTML = headerExtra;
|
|
hdr.appendChild(span);
|
|
}
|
|
}
|
|
|
|
if (!countries.length) { el.innerHTML = '<p class="text-muted mb-0">Aucune donnée.</p>'; return; }
|
|
|
|
var maxH = countries[0].hits || 1;
|
|
var html = '<div class="accordion accordion-flush" id="acc-countries">';
|
|
|
|
countries.forEach(function (c, ci) {
|
|
var pct = Math.round(c.hits / maxH * 100);
|
|
var cname = flag(c.code) + countryName(c.code);
|
|
var vis = c.hits.toLocaleString('fr-FR');
|
|
var accId = 'acc-country-' + ci;
|
|
var nets = c.networks.slice().sort(function (a, b) { return b.hits - a.hits; });
|
|
var maxN = nets[0] ? nets[0].hits : 1;
|
|
|
|
var netRows = nets.map(function (n, ni) {
|
|
var npct = Math.round(n.hits / maxN * 100);
|
|
var asId = 'acc-as-' + ci + '-' + ni;
|
|
var asnKey = n.asn || '__unknown__';
|
|
var ips = ipsByAsn[asnKey] || [];
|
|
|
|
var ipRows = ips.slice(0, 20).map(buildIpRow).join('');
|
|
|
|
var hasIps = ips.length > 0;
|
|
var toggleAttrs = hasIps ? ' data-bs-toggle="collapse" data-bs-target="#' + asId + '" role="button"' : '';
|
|
var chevron = hasIps ? '<span class="text-muted ms-1" style="font-size:.65rem">▾</span>' : '';
|
|
var excludeBtn = n.asn
|
|
? '<button class="btn btn-sm py-0 px-1 ms-2 text-muted border-0 exclude-as-btn" style="font-size:.65rem" title="Exclure cet AS des stats" data-asn="' + esc(n.asn) + '" data-name="' + esc(n.name || '') + '">✕</button>'
|
|
: '';
|
|
|
|
return '<div>'
|
|
+ '<div class="d-flex align-items-center gap-2 py-1"' + toggleAttrs + '>'
|
|
+ '<div class="small d-flex align-items-center" style="width:9rem;flex-shrink:0">'
|
|
+ esc(n.name || '?')
|
|
+ (n.asn ? ' <span class="text-muted">AS' + esc(n.asn) + '</span>' : '')
|
|
+ chevron
|
|
+ excludeBtn
|
|
+ '</div>'
|
|
+ '<div class="flex-grow-1"><div class="progress" style="height:4px">'
|
|
+ '<div class="progress-bar bg-info" style="width:' + npct + '%"></div>'
|
|
+ '</div></div>'
|
|
+ '<div class="text-end text-muted small" style="width:4rem;flex-shrink:0">'
|
|
+ n.hits.toLocaleString('fr-FR') + '</div>'
|
|
+ '</div>'
|
|
+ (hasIps ? '<div id="' + asId + '" class="collapse">'
|
|
+ '<div class="border-start ms-3 ps-2 pb-1">' + ipRows + '</div>'
|
|
+ '</div>' : '')
|
|
+ '</div>';
|
|
}).join('');
|
|
|
|
html += '<div class="accordion-item border-0">'
|
|
+ '<div class="d-flex align-items-center gap-2 py-2 px-0" data-bs-toggle="collapse"'
|
|
+ ' data-bs-target="#' + accId + '" role="button" aria-expanded="false">'
|
|
+ '<div class="fw-medium" style="width:10rem;flex-shrink:0">' + cname + '</div>'
|
|
+ '<div class="flex-grow-1"><div class="progress" style="height:6px">'
|
|
+ '<div class="progress-bar" style="width:' + pct + '%"></div>'
|
|
+ '</div></div>'
|
|
+ '<div class="text-end fw-semibold" style="width:5rem;flex-shrink:0">'
|
|
+ vis + ' <span class="text-muted fw-normal small">vis.</span></div>'
|
|
+ '<span class="text-muted" style="width:1rem;flex-shrink:0;font-size:.7rem">▾</span>'
|
|
+ '</div>'
|
|
+ '<div id="' + accId + '" class="collapse">'
|
|
+ '<div class="ps-2 pb-2 border-start ms-3">' + netRows + '</div>'
|
|
+ '</div>'
|
|
+ '</div>';
|
|
});
|
|
|
|
html += '</div>';
|
|
|
|
// Section AS exclus
|
|
if (excludedLists.length) {
|
|
html += '<div class="mt-3 pt-2 border-top">'
|
|
+ '<div class="small fw-semibold text-muted mb-2">AS exclus des stats (' + excludedLists.length + ')</div>'
|
|
+ '<div class="d-flex flex-wrap gap-2">';
|
|
excludedLists.forEach(function (n) {
|
|
html += '<span class="badge border text-muted fw-normal d-inline-flex align-items-center gap-1" style="font-size:.7rem">'
|
|
+ esc(n.name || '?')
|
|
+ (n.asn ? ' <span class="text-muted">AS' + esc(n.asn) + '</span>' : '')
|
|
+ '<button class="btn btn-sm p-0 ms-1 border-0 include-as-btn" data-asn="' + esc(n.asn || '') + '" title="Inclure" style="line-height:1;color:inherit;background:none">↺</button>'
|
|
+ '</span>';
|
|
});
|
|
html += '</div></div>';
|
|
}
|
|
|
|
el.innerHTML = html;
|
|
|
|
// Délégation : boutons exclure / inclure
|
|
el.addEventListener('click', function (e) {
|
|
var btn = e.target.closest('.exclude-as-btn');
|
|
if (btn) { excludeAs(btn.getAttribute('data-asn'), btn.getAttribute('data-name')); return; }
|
|
btn = e.target.closest('.include-as-btn');
|
|
if (btn) { includeAs(btn.getAttribute('data-asn')); }
|
|
}, { once: true });
|
|
}
|
|
|
|
renderCountry();
|
|
document.addEventListener('folio:excluded-as-changed', renderCountry);
|
|
}());
|
|
|
|
// ── Liste consolidée de tous les agents ──────────────────────────────────────
|
|
(function () {
|
|
var el = document.getElementById('stats-agents-container');
|
|
var badge = document.getElementById('agents-count');
|
|
var allUas = (typeof FOLIO_ALL_UAS !== 'undefined') ? FOLIO_ALL_UAS : {};
|
|
var csrf = (typeof FOLIO_CSRF !== 'undefined') ? FOLIO_CSRF : '';
|
|
if (!el) { return; }
|
|
|
|
var agents = Object.keys(allUas).map(function (ua) {
|
|
return { ua: ua, hits: allUas[ua], bot: isBot(ua) };
|
|
}).sort(function (a, b) {
|
|
if (a.bot !== b.bot) { return a.bot ? -1 : 1; }
|
|
return b.hits - a.hits;
|
|
});
|
|
|
|
if (!agents.length) {
|
|
el.innerHTML = '<p class="text-muted p-3 mb-0">Aucun agent détecté.</p>';
|
|
return;
|
|
}
|
|
|
|
var bots = agents.filter(function (a) { return a.bot; });
|
|
var unknown = agents.filter(function (a) { return !a.bot; });
|
|
if (badge) { badge.textContent = '— ' + bots.length + ' bot(s) sur ' + agents.length; }
|
|
|
|
function addBot(ua, btn) {
|
|
btn.disabled = true;
|
|
var fd = new FormData();
|
|
fd.append('_csrf', csrf);
|
|
fd.append('pattern', ua);
|
|
fetch('/?action=admin_add_bot', { method: 'POST', body: fd })
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (d.ok) {
|
|
_botPatterns.push(ua);
|
|
btn.closest('tr').querySelector('td:first-child').innerHTML = '<span title="Bot">🤖</span>';
|
|
btn.remove();
|
|
} else {
|
|
btn.disabled = false;
|
|
}
|
|
})
|
|
.catch(function () { btn.disabled = false; });
|
|
}
|
|
|
|
function agentRow(a) {
|
|
var addBtn = (!a.bot)
|
|
? '<button class="btn btn-outline-secondary btn-sm py-0 px-1 ms-2 add-bot-btn"'
|
|
+ ' style="font-size:.65rem;white-space:nowrap" title="Ajouter aux bots">+ bot</button>'
|
|
: '';
|
|
return '<tr>'
|
|
+ '<td class="ps-3" style="width:1.5rem;vertical-align:top;padding-top:6px">'
|
|
+ (a.bot ? '<span title="Bot">🤖</span>' : '<span class="text-muted" title="Inconnu">?</span>') + '</td>'
|
|
+ '<td style="word-break:break-all;vertical-align:top">'
|
|
+ '<code style="font-size:.72rem">' + esc(a.ua) + '</code>'
|
|
+ addBtn + '</td>'
|
|
+ '<td class="text-end text-muted small pe-3" style="width:5rem;vertical-align:top;white-space:nowrap">'
|
|
+ a.hits.toLocaleString('fr-FR') + '</td>'
|
|
+ '</tr>';
|
|
}
|
|
|
|
var botsHtml = bots.map(agentRow).join('');
|
|
var unknownHtml = unknown.map(agentRow).join('');
|
|
|
|
var html = '<div class="table-responsive">'
|
|
+ '<table class="table table-sm table-hover mb-0 small">'
|
|
+ '<thead class="table-light"><tr>'
|
|
+ '<th class="ps-3" style="width:1.5rem"></th>'
|
|
+ '<th>User-Agent</th>'
|
|
+ '<th class="text-end pe-3" style="width:5rem">Req.</th>'
|
|
+ '</tr></thead>'
|
|
+ '<tbody>';
|
|
|
|
if (botsHtml) {
|
|
html += '<tr class="table-light"><td colspan="3" class="ps-3 py-1">'
|
|
+ '<small class="fw-semibold text-muted">Bots connus (' + bots.length + ')</small></td></tr>'
|
|
+ botsHtml;
|
|
}
|
|
if (unknownHtml) {
|
|
html += '<tr class="table-light"><td colspan="3" class="ps-3 py-1">'
|
|
+ '<small class="fw-semibold text-muted">Agents non classés (' + unknown.length + ')</small></td></tr>'
|
|
+ unknownHtml;
|
|
}
|
|
|
|
html += '</tbody></table></div>';
|
|
el.innerHTML = html;
|
|
|
|
// Délégation : boutons "+ bot"
|
|
el.addEventListener('click', function (e) {
|
|
var btn = e.target.closest('.add-bot-btn');
|
|
if (!btn) { return; }
|
|
var row = btn.closest('tr');
|
|
var code = row ? row.querySelector('code') : null;
|
|
if (code) { addBot(code.textContent, btn); }
|
|
});
|
|
}());
|
|
|
|
// ── Pages les plus visitées (RSS XML + sparklines) ───────────────────────────
|
|
(function () {
|
|
var container = document.getElementById('stats-pages-container');
|
|
var badge = document.getElementById('stats-pages-count');
|
|
var pagesByDay = (typeof FOLIO_PAGES_BY_DAY !== 'undefined') ? FOLIO_PAGES_BY_DAY : {};
|
|
if (!container) { return; }
|
|
|
|
function sparkline(data) {
|
|
var W = 120, H = 28, padX = 2, padY = 3;
|
|
var max = Math.max.apply(null, data) || 1;
|
|
var n = data.length;
|
|
var pts = data.map(function (v, i) {
|
|
var x = padX + i * (W - 2 * padX) / (n - 1);
|
|
var y = H - padY - (v / max) * (H - 2 * padY);
|
|
return x.toFixed(1) + ',' + y.toFixed(1);
|
|
}).join(' ');
|
|
var first = padX.toFixed(1) + ',' + (H - padY).toFixed(1);
|
|
var last = (W - padX).toFixed(1) + ',' + (H - padY).toFixed(1);
|
|
return '<svg xmlns="http://www.w3.org/2000/svg" width="' + W + '" height="' + H + '" style="display:block;overflow:visible">'
|
|
+ '<defs><linearGradient id="spk-grad" x1="0" y1="0" x2="0" y2="1">'
|
|
+ '<stop offset="0%" stop-color="var(--bs-primary,#0d6efd)" stop-opacity="0.18"/>'
|
|
+ '<stop offset="100%" stop-color="var(--bs-primary,#0d6efd)" stop-opacity="0"/>'
|
|
+ '</linearGradient></defs>'
|
|
+ '<polygon points="' + first + ' ' + pts + ' ' + last + '" fill="url(#spk-grad)"/>'
|
|
+ '<polyline points="' + pts + '" fill="none" stroke="var(--bs-primary,#0d6efd)" stroke-width="1.5" stroke-linejoin="round" stroke-linecap="round"/>'
|
|
+ '</svg>';
|
|
}
|
|
|
|
function trendChart(totals) {
|
|
var trendEl = document.getElementById('stats-trend-container');
|
|
if (!trendEl || !totals.length) { return; }
|
|
|
|
var n = totals.length;
|
|
var VW = 900, VH = 480;
|
|
var ml = 44, mr = 12, mt = 12, mb = 28;
|
|
var W = VW - ml - mr;
|
|
var H = VH - mt - mb;
|
|
|
|
var rawMax = Math.max.apply(null, totals) || 1;
|
|
var mag = Math.pow(10, Math.floor(Math.log(rawMax) / Math.LN10));
|
|
var maxV = Math.ceil(rawMax / mag) * mag;
|
|
var nTicks = 4;
|
|
|
|
var now = new Date();
|
|
var labels = totals.map(function (_, i) {
|
|
var d = new Date(now);
|
|
d.setDate(d.getDate() - (n - 1 - i));
|
|
return d.toLocaleDateString('fr-FR', { day: 'numeric', month: 'short' });
|
|
});
|
|
|
|
var pts = totals.map(function (v, i) {
|
|
return { x: ml + i * W / (n - 1), y: mt + H - (v / maxV) * H, v: v, l: labels[i] };
|
|
});
|
|
|
|
function smoothPath(points) {
|
|
var d = 'M ' + points[0].x.toFixed(1) + ' ' + points[0].y.toFixed(1);
|
|
for (var i = 0; i < points.length - 1; i++) {
|
|
var p0 = points[i > 0 ? i - 1 : i];
|
|
var p1 = points[i];
|
|
var p2 = points[i + 1];
|
|
var p3 = points[i + 2 < points.length ? i + 2 : i + 1];
|
|
var t = 0.35;
|
|
var cp1x = p1.x + t * (p2.x - p0.x) / 2;
|
|
var cp1y = p1.y + t * (p2.y - p0.y) / 2;
|
|
var cp2x = p2.x - t * (p3.x - p1.x) / 2;
|
|
var cp2y = p2.y - t * (p3.y - p1.y) / 2;
|
|
d += ' C ' + cp1x.toFixed(1) + ' ' + cp1y.toFixed(1)
|
|
+ ', ' + cp2x.toFixed(1) + ' ' + cp2y.toFixed(1)
|
|
+ ', ' + p2.x.toFixed(1) + ' ' + p2.y.toFixed(1);
|
|
}
|
|
return d;
|
|
}
|
|
|
|
var linePath = smoothPath(pts);
|
|
var areaPath = linePath
|
|
+ ' L ' + pts[n - 1].x.toFixed(1) + ' ' + (mt + H)
|
|
+ ' L ' + pts[0].x.toFixed(1) + ' ' + (mt + H) + ' Z';
|
|
|
|
var grid = '', yLabels = '';
|
|
for (var t = 0; t <= nTicks; t++) {
|
|
var val = Math.round(maxV * t / nTicks);
|
|
var gy = (mt + H - (val / maxV) * H).toFixed(1);
|
|
grid += '<line x1="' + ml + '" y1="' + gy + '" x2="' + (VW - mr) + '" y2="' + gy
|
|
+ '" stroke="#e9ecef" stroke-width="1"/>';
|
|
yLabels += '<text x="' + (ml - 6) + '" y="' + gy + '" text-anchor="end" dominant-baseline="middle"'
|
|
+ ' font-size="11" fill="#adb5bd">' + val + '</text>';
|
|
}
|
|
|
|
var xLabels = '';
|
|
pts.forEach(function (p, i) {
|
|
if (i % 3 === 0 || i === n - 1) {
|
|
xLabels += '<text x="' + p.x.toFixed(1) + '" y="' + (VH - 4) + '" text-anchor="middle"'
|
|
+ ' font-size="11" fill="#adb5bd">' + p.l + '</text>';
|
|
}
|
|
});
|
|
|
|
var dots = pts.map(function (p) {
|
|
return '<circle cx="' + p.x.toFixed(1) + '" cy="' + p.y.toFixed(1) + '" r="14"'
|
|
+ ' fill="transparent" cursor="default">'
|
|
+ '<title>' + esc(p.l) + ' : ' + p.v + ' vis.</title>'
|
|
+ '</circle>'
|
|
+ '<circle cx="' + p.x.toFixed(1) + '" cy="' + p.y.toFixed(1) + '" r="3"'
|
|
+ ' fill="var(--bs-primary,#0d6efd)" stroke="#fff" stroke-width="1.5" pointer-events="none"/>';
|
|
}).join('');
|
|
|
|
trendEl.innerHTML =
|
|
'<p class="small text-muted mb-2 fw-semibold">Trafic total — 30 derniers jours</p>'
|
|
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + VW + ' ' + VH + '"'
|
|
+ ' style="width:100%;height:480px;display:block;overflow:visible">'
|
|
+ '<defs>'
|
|
+ '<linearGradient id="area-grad" x1="0" y1="0" x2="0" y2="1">'
|
|
+ '<stop offset="0%" stop-color="var(--bs-primary,#0d6efd)" stop-opacity="0.2"/>'
|
|
+ '<stop offset="100%" stop-color="var(--bs-primary,#0d6efd)" stop-opacity="0"/>'
|
|
+ '</linearGradient>'
|
|
+ '</defs>'
|
|
+ grid
|
|
+ '<path d="' + areaPath + '" fill="url(#area-grad)"/>'
|
|
+ '<path d="' + linePath + '" fill="none" stroke="var(--bs-primary,#0d6efd)"'
|
|
+ ' stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>'
|
|
+ dots + yLabels + xLabels
|
|
+ '</svg>';
|
|
}
|
|
|
|
function multiLineChart(pagesByDay, rssRows) {
|
|
var el = document.getElementById('stats-multiline-container');
|
|
if (!el) { return; }
|
|
|
|
var COLORS = ['#0d6efd','#198754','#dc3545','#fd7e14','#6f42c1',
|
|
'#20c997','#0dcaf0','#e63946','#f4a261','#457b9d'];
|
|
var VW = 900, VH = 480;
|
|
var ml = 44, mr = 12, mt = 12, mb = 28;
|
|
var W = VW - ml - mr;
|
|
var H = VH - mt - mb;
|
|
|
|
var series = [];
|
|
rssRows.forEach(function (row) {
|
|
var pm = row.link.match(/\/post\/[^?#]*/);
|
|
var data = pm ? (pagesByDay[pm[0]] || null) : null;
|
|
if (data && series.length < 10) {
|
|
series.push({ title: row.title || row.slug, data: data });
|
|
}
|
|
});
|
|
if (!series.length) { return; }
|
|
|
|
var n = series[0].data.length;
|
|
var allVals = series.reduce(function (acc, s) { return acc.concat(s.data); }, []);
|
|
var rawMax = Math.max.apply(null, allVals) || 1;
|
|
var mag = Math.pow(10, Math.floor(Math.log(rawMax) / Math.LN10));
|
|
var maxV = Math.ceil(rawMax / mag) * mag;
|
|
var nTicks = 4;
|
|
|
|
var now = new Date();
|
|
var labels = series[0].data.map(function (_, i) {
|
|
var d = new Date(now);
|
|
d.setDate(d.getDate() - (n - 1 - i));
|
|
return d.toLocaleDateString('fr-FR', { day: 'numeric', month: 'short' });
|
|
});
|
|
|
|
function smoothPath(pts) {
|
|
var d = 'M ' + pts[0].x.toFixed(1) + ' ' + pts[0].y.toFixed(1);
|
|
for (var i = 0; i < pts.length - 1; i++) {
|
|
var p0 = pts[i > 0 ? i - 1 : i];
|
|
var p1 = pts[i], p2 = pts[i + 1];
|
|
var p3 = pts[i + 2 < pts.length ? i + 2 : i + 1];
|
|
var t = 0.35;
|
|
var cp1x = p1.x + t * (p2.x - p0.x) / 2, cp1y = p1.y + t * (p2.y - p0.y) / 2;
|
|
var cp2x = p2.x - t * (p3.x - p1.x) / 2, cp2y = p2.y - t * (p3.y - p1.y) / 2;
|
|
d += ' C ' + cp1x.toFixed(1) + ' ' + cp1y.toFixed(1)
|
|
+ ', ' + cp2x.toFixed(1) + ' ' + cp2y.toFixed(1)
|
|
+ ', ' + p2.x.toFixed(1) + ' ' + p2.y.toFixed(1);
|
|
}
|
|
return d;
|
|
}
|
|
|
|
var grid = '', yLabels = '';
|
|
for (var t = 0; t <= nTicks; t++) {
|
|
var val = Math.round(maxV * t / nTicks);
|
|
var gy = (mt + H - (val / maxV) * H).toFixed(1);
|
|
grid += '<line x1="' + ml + '" y1="' + gy + '" x2="' + (VW - mr) + '" y2="' + gy
|
|
+ '" stroke="#e9ecef" stroke-width="1"/>';
|
|
yLabels += '<text x="' + (ml - 6) + '" y="' + gy + '" text-anchor="end" dominant-baseline="middle"'
|
|
+ ' font-size="11" fill="#adb5bd">' + val + '</text>';
|
|
}
|
|
|
|
var xLabels = '';
|
|
labels.forEach(function (lbl, i) {
|
|
if (i % 3 === 0 || i === n - 1) {
|
|
var x = (ml + i * W / (n - 1)).toFixed(1);
|
|
xLabels += '<text x="' + x + '" y="' + (VH - 4) + '" text-anchor="middle"'
|
|
+ ' font-size="11" fill="#adb5bd">' + lbl + '</text>';
|
|
}
|
|
});
|
|
|
|
var lines = series.map(function (s, si) {
|
|
var color = COLORS[si % COLORS.length];
|
|
var pts = s.data.map(function (v, i) {
|
|
return { x: ml + i * W / (n - 1), y: mt + H - (v / maxV) * H, v: v, l: labels[i] };
|
|
});
|
|
var dots = pts.map(function (p) {
|
|
return '<circle cx="' + p.x.toFixed(1) + '" cy="' + p.y.toFixed(1) + '" r="14"'
|
|
+ ' fill="transparent"><title>' + esc(p.l) + ' — ' + esc(s.title) + ' : ' + p.v + ' vis.</title></circle>'
|
|
+ '<circle cx="' + p.x.toFixed(1) + '" cy="' + p.y.toFixed(1) + '" r="2.5"'
|
|
+ ' fill="' + color + '" stroke="#fff" stroke-width="1" pointer-events="none"/>';
|
|
}).join('');
|
|
return '<path d="' + smoothPath(pts) + '" fill="none" stroke="' + color
|
|
+ '" stroke-width="1.8" stroke-linejoin="round" stroke-linecap="round"/>' + dots;
|
|
}).join('');
|
|
|
|
var legend = series.map(function (s, si) {
|
|
var color = COLORS[si % COLORS.length];
|
|
return '<span class="d-inline-flex align-items-center gap-1 me-3 mb-1 small">'
|
|
+ '<svg width="16" height="3" style="flex-shrink:0"><line x1="0" y1="1.5" x2="16" y2="1.5"'
|
|
+ ' stroke="' + color + '" stroke-width="2.5" stroke-linecap="round"/></svg>'
|
|
+ '<span class="text-truncate" style="max-width:160px" title="' + esc(s.title) + '">'
|
|
+ esc(trunc(s.title, 32)) + '</span></span>';
|
|
}).join('');
|
|
|
|
el.innerHTML =
|
|
'<p class="small text-muted mb-2 fw-semibold">Par article — 30 derniers jours</p>'
|
|
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + VW + ' ' + VH + '"'
|
|
+ ' style="width:100%;height:480px;display:block;overflow:visible">'
|
|
+ grid + lines + yLabels + xLabels + '</svg>'
|
|
+ '<div class="d-flex flex-wrap mt-2">' + legend + '</div>';
|
|
}
|
|
|
|
fetch('/trending?period=14d')
|
|
.then(function (r) { return r.ok ? r.text() : Promise.reject(); })
|
|
.then(function (xml) {
|
|
var doc = new DOMParser().parseFromString(xml, 'application/xml');
|
|
var items = Array.from(doc.querySelectorAll('item'));
|
|
if (!items.length) {
|
|
container.innerHTML = '<p class="text-muted p-3 mb-0">Aucune donnée.</p>';
|
|
return;
|
|
}
|
|
var rows = items.map(function (item) {
|
|
var raw = (item.querySelector('title') || { textContent: '' }).textContent;
|
|
var link = ((item.querySelector('link') || {}).textContent || '').trim();
|
|
var m = raw.match(/\((\d+)\s+visiteurs?\)$/);
|
|
var vis = m ? parseInt(m[1], 10) : 0;
|
|
var title = raw.replace(/\s*\(\d+\s+visiteurs?\)$/, '');
|
|
var slug = decodeURIComponent(link.replace(/.*\/post\//, ''));
|
|
var pm = link.match(/\/post\/[^?#]*/);
|
|
var daily = pm ? (pagesByDay[pm[0]] || null) : null;
|
|
return { title: title, link: link, slug: slug, vis: vis, daily: daily };
|
|
});
|
|
var nDays = Object.values(pagesByDay)[0] ? Object.values(pagesByDay)[0].length : 30;
|
|
var totals = new Array(nDays).fill(0);
|
|
Object.values(pagesByDay).forEach(function (arr) {
|
|
arr.forEach(function (v, i) { if (i < nDays) { totals[i] += v; } });
|
|
});
|
|
trendChart(totals);
|
|
multiLineChart(pagesByDay, rows);
|
|
|
|
var html = '<div class="table-responsive"><table class="table table-sm table-hover mb-0 small w-100"><tbody>';
|
|
rows.forEach(function (row, i) {
|
|
var vis = row.vis.toLocaleString('fr-FR');
|
|
var spk = row.daily ? sparkline(row.daily) : '';
|
|
html += '<tr>'
|
|
+ '<td class="text-muted ps-3" style="width:2rem;vertical-align:middle">' + (i + 1) + '</td>'
|
|
+ '<td style="vertical-align:middle"><a href="' + esc(row.link) + '" target="_blank"'
|
|
+ ' class="text-decoration-none" title="' + esc(row.slug) + '">'
|
|
+ esc(row.title || row.slug) + '</a></td>'
|
|
+ '<td style="width:130px;vertical-align:middle;padding:4px 8px">' + spk + '</td>'
|
|
+ '<td class="text-end fw-semibold pe-3" style="width:5rem;vertical-align:middle">'
|
|
+ vis + ' <span class="text-muted fw-normal">vis.</span></td>'
|
|
+ '</tr>';
|
|
});
|
|
html += '</tbody></table></div>';
|
|
if (badge) { badge.textContent = rows.length + ' URLs'; }
|
|
container.innerHTML = html;
|
|
})
|
|
.catch(function () {
|
|
container.innerHTML = '<p class="text-muted p-3 mb-0">Impossible de charger le flux.</p>';
|
|
});
|
|
}());
|