Files
Website/views/admin/analytics.ejs
T
2026-11-24 22:05:11 +01:00

327 lines
14 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<%
const palette = ['#cee5ff', '#39a8ff', '#ffcf5f', '#ff7a7a', '#c08bff', '#5ff0d0', '#ff9f43', '#8fbf00', '#ff5fd2', '#7a8cff'];
const flag = (cc) => {
if (!cc || cc.length !== 2) { return ''; }
return String.fromCodePoint(...[...cc.toUpperCase()].map(c => 0x1F1E6 + c.charCodeAt(0) - 65));
};
const maxOf = (arr, key) => arr.reduce((m, e) => Math.max(m, e[key]), 0) || 1;
const urlMax = maxOf(topUrls, 'count');
const countryMax = maxOf(topCountries, 'count');
const providerMax = maxOf(topProviders, 'count');
const browserMax = maxOf(browsers, 'count');
const osMax = maxOf(operatingSystems, 'count');
const deviceMax = maxOf(devices, 'count');
const dailyMax = maxOf(daily, 'count');
const hourMax = maxOf(hourly, 'count');
const dailyTotal = daily.reduce((s, d) => s + d.count, 0);
const hourlyTotal = hourly.reduce((s, h) => s + h.count, 0);
const totalCountry = topCountries.reduce((s, c) => s + c.count, 0) || 1;
let acc = 0;
const stops = [];
topCountries.slice(0, 6).forEach((c, i) => {
const start = (acc / totalCountry) * 360;
acc += c.count;
const end = (acc / totalCountry) * 360;
stops.push(`${palette[i]} ${start}deg ${end}deg`);
});
if (acc < totalCountry) {
stops.push(`#333 ${(acc / totalCountry) * 360}deg 360deg`);
}
const conic = stops.length ? `conic-gradient(${stops.join(', ')})` : 'var(--admin-surface-3)';
const dowLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const fmtRange = (d) => d ? new Date(d).toLocaleString() : 'n/a';
%>
<!DOCTYPE html>
<html lang="en">
<head>
<%- include('../partials/head'); -%>
<%- include('../partials/admin-base'); -%>
<title>Admin - Analytics</title>
</head>
<body>
<%- include('../winter'); -%>
<div class="admin-shell admin-shell--wide">
<div class="page-head">
<a class="btn btn-back" href="<%= endpoint %>/dashboard">Back to dashboard</a>
<h1>Analytics</h1>
<div class="spacer"></div>
<div class="range-tabs">
<a href="?range=7d" class="<%= range === '7d' ? 'active' : '' %>">7 days</a>
<a href="?range=30d" class="<%= range === '30d' ? 'active' : '' %>">30 days</a>
<a href="?range=90d" class="<%= range === '90d' ? 'active' : '' %>">90 days</a>
<a href="?range=all" class="<%= range === 'all' ? 'active' : '' %>">All time</a>
</div>
</div>
<form class="toolbar" method="get">
<input type="hidden" name="range" value="custom">
<label class="sub">From
<input type="date" name="start" value="<%= from %>">
</label>
<label class="sub">To
<input type="date" name="end" value="<%= to %>">
</label>
<button class="btn" type="submit">Apply</button>
</form>
<div class="stat-grid">
<div class="stat-card">
<span class="kpi"><%= kpi.totalVisits.toLocaleString() %></span>
<span class="label">Total visits</span>
<span class="foot">First: <%= fmtRange(span.first) %></span>
</div>
<div class="stat-card">
<span class="kpi"><%= kpi.uniqueIps.toLocaleString() %></span>
<span class="label">Unique visitors</span>
<span class="foot">by IP address</span>
</div>
<div class="stat-card">
<span class="kpi"><%= kpi.uniqueUrls.toLocaleString() %></span>
<span class="label">Distinct pages</span>
<span class="foot">Last visit: <%= fmtRange(span.last) %></span>
</div>
<div class="stat-card">
<span class="kpi"><%= kpi.countries.toLocaleString() %></span>
<span class="label">Countries</span>
<span class="foot">geolocated sources</span>
</div>
<div class="stat-card">
<span class="kpi"><%= peakHour.count.toLocaleString() %></span>
<span class="label">Peak hour</span>
<span class="foot"><%= String(peakHour.hour).padStart(2, '0') %>:00</span>
</div>
<div class="stat-card">
<span class="kpi"><%= busiestDay.count.toLocaleString() %></span>
<span class="label">Busiest day</span>
<span class="foot"><%= busiestDay.date %></span>
</div>
</div>
<div class="grid grid-2">
<section class="card">
<h2>Traffic over time <span class="hint">visits per day</span></h2>
<% if (dailyTotal > 0) { %>
<div class="vchart">
<% daily.forEach(d => { %>
<div class="vbar" title="<%= d.date %>: <%= d.count %> visits">
<span class="vtip"><%= d.date %> · <%= d.count %></span>
<span class="vfill" style="height: <%= Math.round((d.count / dailyMax) * 100) %>%"></span>
<span class="vlabel"><%= d.label %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No visits recorded in this period.</p>
<% } %>
</section>
<section class="card">
<h2>Visits by hour <span class="hint">server time</span></h2>
<% if (hourlyTotal > 0) { %>
<div class="vchart">
<% hourly.forEach(h => { %>
<div class="vbar" title="<%= String(h.hour).padStart(2, '0') %>:00: <%= h.count %> visits">
<span class="vtip"><%= String(h.hour).padStart(2, '0') %>:00 · <%= h.count %></span>
<span class="vfill" style="height: <%= Math.round((h.count / hourMax) * 100) %>%"></span>
<span class="vlabel"><%= h.hour %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No visits recorded in this period.</p>
<% } %>
</section>
</div>
<div class="grid grid-2">
<section class="card">
<h2>Most visited pages</h2>
<% if (topUrls.length) { %>
<div class="bars">
<% topUrls.forEach(u => { %>
<div class="bar-row">
<span class="bar-label" title="<%= u.url %>"><%= u.url %></span>
<span class="bar-track"><span class="bar-fill" style="width: <%= Math.round((u.count / urlMax) * 100) %>%"></span></span>
<span class="bar-value"><%= u.count %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No page data yet.</p>
<% } %>
</section>
<section class="card">
<h2>Top countries</h2>
<% if (topCountries.length) { %>
<div class="donut-wrap">
<div class="donut" style="background: <%= conic %>;">
<div class="donut-center">
<strong><%= kpi.countries %></strong>
<span>countries</span>
</div>
</div>
<div class="legend">
<% topCountries.slice(0, 6).forEach((c, i) => { %>
<div class="lg-item">
<span class="swatch" style="background: <%= palette[i] %>;"></span>
<span class="flag"><%= flag(c.code) %></span>
<span><%= c.country %></span>
<span class="lg-val"><%= c.count %></span>
</div>
<% }) %>
<% if (topCountries.length > 6) { %>
<div class="lg-item">
<span class="swatch" style="background: #333;"></span>
<span>Other</span>
<span class="lg-val"><%= topCountries.slice(6).reduce((s, c) => s + c.count, 0) %></span>
</div>
<% } %>
</div>
</div>
<% } else { %>
<p class="none">No geolocation data yet.</p>
<% } %>
</section>
</div>
<div class="grid grid-2">
<section class="card">
<h2>Top providers / ISPs</h2>
<% if (topProviders.length) { %>
<div class="bars">
<% topProviders.forEach(p => { %>
<div class="bar-row">
<span class="bar-label" title="<%= p.provider %>"><%= p.provider %></span>
<span class="bar-track"><span class="bar-fill" style="width: <%= Math.round((p.count / providerMax) * 100) %>%"></span></span>
<span class="bar-value"><%= p.count %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No provider data yet.</p>
<% } %>
</section>
<section class="card">
<h2>Browsers</h2>
<% if (browsers.length) { %>
<div class="bars">
<% browsers.forEach(b => { %>
<div class="bar-row">
<span class="bar-label"><%= b.name %></span>
<span class="bar-track"><span class="bar-fill" style="width: <%= Math.round((b.count / browserMax) * 100) %>%"></span></span>
<span class="bar-value"><%= b.count %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No user agent data yet.</p>
<% } %>
</section>
</div>
<div class="grid grid-2">
<section class="card">
<h2>Operating systems</h2>
<% if (operatingSystems.length) { %>
<div class="bars">
<% operatingSystems.forEach(o => { %>
<div class="bar-row">
<span class="bar-label"><%= o.name %></span>
<span class="bar-track"><span class="bar-fill" style="width: <%= Math.round((o.count / osMax) * 100) %>%"></span></span>
<span class="bar-value"><%= o.count %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No OS data yet.</p>
<% } %>
</section>
<section class="card">
<h2>Devices</h2>
<% if (devices.length) { %>
<div class="bars">
<% devices.forEach(d => { %>
<div class="bar-row">
<span class="bar-label"><%= d.name %></span>
<span class="bar-track"><span class="bar-fill" style="width: <%= Math.round((d.count / deviceMax) * 100) %>%"></span></span>
<span class="bar-value"><%= d.count %></span>
</div>
<% }) %>
</div>
<% } else { %>
<p class="none">No device data yet.</p>
<% } %>
</section>
</div>
<section class="card">
<h2>Activity heatmap <span class="hint">day of week × hour, darker = busier</span></h2>
<% if (heatMax > 0) { %>
<div class="heatmap">
<span class="hm-row-label"></span>
<% for (let h = 0; h < 24; h++) { %>
<span class="hm-col-label"><%= h %></span>
<% } %>
<% for (let d = 0; d < 7; d++) { %>
<span class="hm-row-label"><%= dowLabels[d] %></span>
<% for (let h = 0; h < 24; h++) { const v = heatmap[d][h]; const a = heatMax ? v / heatMax : 0; %>
<span class="hm-cell" title="<%= dowLabels[d] %> <%= String(h).padStart(2, '0') %>:00 · <%= v %> visits"
style="background: <%= v ? `rgba(206, 229, 255, ${0.08 + a * 0.85})` : '' %>;"></span>
<% } %>
<% } %>
</div>
<% } else { %>
<p class="none">No activity to display yet.</p>
<% } %>
</section>
<section class="card">
<h2>Top visitors</h2>
<div class="table-wrap">
<table class="admin-table">
<thead>
<tr>
<th>IP</th>
<th>Location</th>
<th>ISP</th>
<th>Visits</th>
<th></th>
</tr>
</thead>
<tbody>
<% topIps.forEach(ip => { %>
<tr>
<td><a class="ip-link" style="color:var(--accent);font-weight:600;font-family:'Courier New',monospace;" href="<%= endpoint %>/stalker/<%= ip.id %>"><%= ip.ip %></a></td>
<td>
<% if (ip.info.country) { %>
<span class="flag"><%= flag(ip.info.countryCode) %></span>
<%= [ip.info.city, ip.info.country].filter(Boolean).join(', ') %>
<% } else { %>
<span class="none">Unknown</span>
<% } %>
</td>
<td><%= ip.info.isp || ip.info.org || '—' %></td>
<td class="num"><%= ip.count %></td>
<td><a class="btn" href="<%= endpoint %>/stalker/<%= ip.id %>">Details</a></td>
</tr>
<% }) %>
<% if (!topIps.length) { %>
<tr><td colspan="5" class="empty">No visitors yet.</td></tr>
<% } %>
</tbody>
</table>
</div>
</section>
</div>
<%- include('../footer'); -%>
</body>
</html>