/* Admin stats : groupes AS + chargement pages via flux RSS XML /trending?period=14d */
// ── Groupes de réseaux ────────────────────────────────────────────────────────
(function () {
var addBtn = document.getElementById('as-group-add');
if (!addBtn) { return; }
addBtn.addEventListener('click', function () {
var tpl = document.getElementById('as-group-tpl').content.cloneNode(true);
document.getElementById('as-groups-list').appendChild(tpl);
});
document.getElementById('as-groups-list').addEventListener('click', function (e) {
if (e.target.classList.contains('as-group-delete')) {
e.target.closest('.as-group-row').remove();
}
});
}());
// ── 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 esc(s) {
return String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
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(' ');
// Zone remplie sous la courbe
var first = padX.toFixed(1) + ',' + (H - padY).toFixed(1);
var last = (W - padX).toFixed(1) + ',' + (H - padY).toFixed(1);
return '';
}
function trendChart(totals) {
var trendEl = document.getElementById('stats-trend-container');
if (!trendEl || !totals.length) { return; }
var days = totals.length;
var maxV = Math.max.apply(null, totals) || 1;
var W = 1000; // viewBox, s'adapte en CSS
var H = 80;
var padX = 4;
var padY = 8;
var barW = Math.floor((W - 2 * padX) / days) - 2;
// Dates des jours (index 0 = il y a 13 jours)
var now = new Date();
var labels = totals.map(function (_, i) {
var d = new Date(now);
d.setDate(d.getDate() - (days - 1 - i));
return d.toLocaleDateString('fr-FR', { day: 'numeric', month: 'short' });
});
var bars = totals.map(function (v, i) {
var x = padX + i * (W - 2 * padX) / days + 1;
var bh = Math.max(2, (v / maxV) * (H - padY - 16));
var y = H - padY - bh;
var lx = x + barW / 2;
var label = labels[i];
var showLabel = (i === 0 || i === days - 1 || i === Math.floor(days / 2));
return '
Trafic total — 14 derniers jours
' + ''; } 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 = 'Aucune donnée.
'; 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 }; }); // Graphique global : somme de tous les articles par jour var nDays = 14; 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); var html = '| ' + (i + 1) + ' | ' + '' + esc(row.title || row.slug) + ' | ' + '' + spk + ' | ' + '' + vis + ' vis. | ' + '
Impossible de charger le flux.
'; }); }());