/* Admin stats : graphiques, sparklines, accordéon pays/AS/IP */ function esc(s) { return String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } // ── 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 pour le drill-down 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 }); }); Object.keys(ipsByAsn).forEach(function (k) { ipsByAsn[k].sort(function (a, b) { return b.hits - a.hits; }); }); // Mini sparkline (80x20px polyline) pour chaque IP 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 '' + '' + ''; } // Agréger par pays var byCountry = {}, asByCountry = {}; asList.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); if (!countries.length) { el.innerHTML = '

Aucune donnée.

'; return; } var maxH = countries[0].hits || 1; var html = '
'; 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] || []; // Lignes IP avec mini sparkline + chemins triés par date desc var ipRows = ips.slice(0, 20).map(function (ipInfo) { var articles = [], books = []; 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 (path.indexOf('/post/') === 0) { articles.push({ path: path, cnt: cnt, ts: ts }); } else if (path.indexOf('/book/') === 0) { books.push({ path: path, cnt: cnt, ts: ts }); } }); articles.sort(function (a, b) { return b.ts - a.ts; }); books.sort(function (a, b) { return b.ts - a.ts; }); function pathLine(p, prefix) { var slug = decodeURIComponent(p.path.replace(prefix, '')); var label = slug.length > 40 ? slug.slice(0, 40) + '…' : slug; return '
' + '' + esc(label) + '' + ' (' + p.cnt + ')
'; } var pathsHtml = ''; if (articles.length) { pathsHtml += '
Articles
' + articles.map(function (p) { return pathLine(p, '/post/'); }).join(''); } if (books.length) { pathsHtml += '
Livres
' + books.map(function (p) { return pathLine(p, '/book/'); }).join(''); } if (!pathsHtml) { pathsHtml = ''; } return '
' + '' + esc(ipInfo.ip) + '' + '
' + ipSparkline(ipInfo.daily || []) + '
' + '
' + pathsHtml + '
' + '
' + (ipInfo.hits || 0).toLocaleString('fr-FR') + '
' + '
'; }).join(''); var hasIps = ips.length > 0; var toggleAttrs = hasIps ? ' data-bs-toggle="collapse" data-bs-target="#' + asId + '" role="button"' : ''; var chevron = hasIps ? '' : ''; return '
' + '
' + '
' + esc(n.name || '?') + (n.asn ? ' AS' + esc(n.asn) + '' : '') + chevron + '
' + '
' + '
' + '
' + '
' + n.hits.toLocaleString('fr-FR') + '
' + '
' + (hasIps ? '
' + '
' + ipRows + '
' + '
' : '') + '
'; }).join(''); html += '
' + '' + '
' + '
' + netRows + '
' + '
' + '
'; }); html += '
'; el.innerHTML = html; }()); // ── 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 '' + '' + '' + '' + '' + '' + '' + ''; } 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 += ''; yLabels += '' + val + ''; } var xLabels = ''; pts.forEach(function (p, i) { if (i % 2 === 0 || i === n - 1) { xLabels += '' + p.l + ''; } }); var dots = pts.map(function (p) { return '' + '' + esc(p.l) + ' : ' + p.v + ' vis.' + '' + ''; }).join(''); trendEl.innerHTML = '

Trafic total — 14 derniers jours

' + '' + '' + '' + '' + '' + '' + '' + grid + '' + '' + dots + yLabels + xLabels + ''; } 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 n = 14; 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 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 += ''; yLabels += '' + val + ''; } var xLabels = ''; labels.forEach(function (lbl, i) { if (i % 2 === 0 || i === n - 1) { var x = (ml + i * W / (n - 1)).toFixed(1); xLabels += '' + lbl + ''; } }); 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 '' + esc(p.l) + ' — ' + esc(s.title) + ' : ' + p.v + ' vis.' + ''; }).join(''); return '' + dots; }).join(''); var legend = series.map(function (s, si) { var color = COLORS[si % COLORS.length]; var short = s.title.length > 32 ? s.title.slice(0, 32) + '…' : s.title; return '' + '' + '' + esc(short) + ''; }).join(''); el.innerHTML = '

Par article — 14 derniers jours

' + '' + grid + lines + yLabels + xLabels + '' + '
' + legend + '
'; } 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 }; }); 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); multiLineChart(pagesByDay, rows); var html = '
'; rows.forEach(function (row, i) { var vis = row.vis.toLocaleString('fr-FR'); var spk = row.daily ? sparkline(row.daily) : ''; html += '' + '' + '' + '' + '' + ''; }); html += '
' + (i + 1) + '' + esc(row.title || row.slug) + '' + spk + '' + vis + ' vis.
'; if (badge) { badge.textContent = rows.length + ' URLs'; } container.innerHTML = html; }) .catch(function () { container.innerHTML = '

Impossible de charger le flux.

'; }); }());