Skip to content

Commit 4072cd2

Browse files
A1L13Naoclaude
authored
learn: count page views and show them on the admin dashboard (#1163)
- page_views table: one counter per page per UTC day; no IPs, cookies or user ids - on_fetch records successful HTML page loads; skips bots, prefetches, API, assets and the admin area - GET <ADMIN_URL>/api/views (Basic Auth): today/7d/30d/all-time totals, 30-day series, top pages and top activities - admin.html rebuilt on the design system with views first and table counts below Co-authored-by: ao <ao@a.lan> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 46edbae commit 4072cd2

3 files changed

Lines changed: 430 additions & 114 deletions

File tree

‎learn/public/admin.html‎

Lines changed: 177 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3,148 +3,212 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Alpha One Labs Admin</title>
6+
<meta name="robots" content="noindex" />
7+
<title>Admin · Alpha One Labs</title>
78
<link rel="icon" type="image/png" href="/images/logo.png" />
8-
<script src="https://cdn.tailwindcss.com/3.4.17"></script>
9-
<script src="/js/a1-tailwind-theme.js?v=1"></script>
9+
<link rel="preconnect" href="https://fonts.googleapis.com" />
10+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@800;900&family=Inter:wght@400;500;700;800&family=JetBrains+Mono:wght@500;700&display=swap" />
1011
<link rel="stylesheet" href="https://design.alphaonelabs.com/brand.css" />
12+
<script>
13+
(function () {
14+
try {
15+
var t = localStorage.getItem('theme');
16+
var dark = t ? t === 'dark' : window.matchMedia('(prefers-color-scheme: dark)').matches;
17+
document.documentElement.classList.toggle('dark', dark);
18+
document.documentElement.setAttribute('data-a1-theme', dark ? 'dark' : 'light');
19+
} catch (e) {}
20+
})();
21+
</script>
1122
<style>
12-
:root {
13-
--ink: #0a0a0a;
14-
--muted: #737373;
15-
--line: #e6e6e6;
16-
--bg: #fafafa;
17-
--card: #ffffff;
18-
--accent: #7a6418;
19-
--accent-soft: #ccfbf1;
20-
}
21-
body {
22-
font-family: "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
23-
background: radial-gradient(circle at 10% 10%, #d1fae5 0%, #fafafa 35%),
24-
radial-gradient(circle at 90% 20%, #bfdbfe 0%, #fafafa 40%);
25-
color: var(--ink);
26-
}
27-
.glass {
28-
background: rgba(255, 255, 255, 0.82);
29-
backdrop-filter: blur(8px);
30-
border: 1px solid rgba(226, 232, 240, 0.85);
31-
}
32-
.pulse {
33-
animation: pulse 1.2s ease-in-out infinite;
34-
}
35-
@keyframes pulse {
36-
0% { opacity: 0.45; }
37-
50% { opacity: 1; }
38-
100% { opacity: 0.45; }
23+
:root { --bg: #fff; --fg: #0a0a0a; --muted: #4a4a4a; --soft: #e6e6e6; --hard: #0a0a0a; --raised: #f5f5f5; --gold: #d4af37; --gold-ink: #7a6418; }
24+
html.dark { --bg: #000; --fg: #fafafa; --muted: #a3a3a3; --soft: #2a2a2a; --hard: #e1e1e1; --raised: #121212; --gold: #e1b746; --gold-ink: #e1b746; }
25+
* { box-sizing: border-box; }
26+
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.5 Inter, system-ui, sans-serif; }
27+
main { max-width: 1120px; margin: 0 auto; padding: 32px 16px 64px; }
28+
.eyebrow { font: 700 12px/1.3 "JetBrains Mono", ui-monospace, monospace; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 0; }
29+
h1, h2 { font-family: "Barlow Condensed", "Arial Narrow", sans-serif; font-weight: 900; text-transform: uppercase; letter-spacing: -.01em; line-height: .95; margin: 0; }
30+
h1 { font-size: clamp(40px, 6vw, 64px); margin-top: 6px; }
31+
h2 { font-size: 30px; }
32+
.rule { width: 56px; height: 6px; background: var(--gold); border: 0; margin: 18px 0 28px; }
33+
.head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; }
34+
.actions { display: flex; gap: 8px; }
35+
.btn { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 16px; border: 1px solid var(--hard); border-radius: 4px; background: var(--bg); color: var(--fg); font: 800 14px/1 Inter, sans-serif; cursor: pointer; }
36+
.btn:hover { background: var(--raised); }
37+
.btn--gold { background: var(--gold); border-color: var(--gold); color: #080808; }
38+
.btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
39+
section { margin-top: 40px; }
40+
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--hard); padding-bottom: 10px; margin-bottom: 18px; }
41+
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--hard); border-radius: 6px; }
42+
.tile { padding: 18px 20px; border-left: 1px solid var(--soft); min-width: 0; }
43+
.tile:first-child { border-left: 0; }
44+
.tile .num { font: 900 clamp(32px, 4vw, 48px)/1 "Barlow Condensed", sans-serif; margin-top: 8px; overflow-wrap: anywhere; }
45+
.tile .num.gold { color: var(--gold-ink); }
46+
.chart { border: 1px solid var(--hard); border-radius: 6px; padding: 18px 18px 10px; margin-top: 16px; }
47+
.bars { display: flex; align-items: flex-end; gap: 4px; height: 180px; }
48+
.bar { flex: 1; min-width: 0; background: var(--gold); border-radius: 2px 2px 0 0; min-height: 1px; position: relative; }
49+
.bar.zero { background: var(--soft); }
50+
.bar:hover { outline: 2px solid var(--fg); }
51+
.axis { display: flex; justify-content: space-between; margin-top: 8px; font: 500 12px/1 "JetBrains Mono", monospace; color: var(--muted); }
52+
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-top: 24px; }
53+
table { width: 100%; border-collapse: collapse; border: 1px solid var(--hard); border-radius: 6px; overflow: hidden; }
54+
th { text-align: left; font: 700 12px/1.2 "JetBrains Mono", monospace; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding: 12px 14px; border-bottom: 1px solid var(--hard); }
55+
td { padding: 10px 14px; border-top: 1px solid var(--soft); vertical-align: top; overflow-wrap: anywhere; }
56+
td.n, th.n { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
57+
td a { color: inherit; text-decoration: underline; text-underline-offset: 3px; }
58+
td a:hover { color: var(--gold-ink); }
59+
.meter { height: 4px; background: var(--soft); margin-top: 6px; }
60+
.meter span { display: block; height: 100%; background: var(--gold); }
61+
.status { color: var(--muted); font-size: 13px; margin-top: 12px; }
62+
.empty { color: var(--muted); }
63+
.err { color: #dc2626; }
64+
.pulse { animation: pulse 1.2s ease-in-out infinite; }
65+
@keyframes pulse { 0%, 100% { opacity: .45; } 50% { opacity: 1; } }
66+
@media (max-width: 760px) {
67+
.tiles { grid-template-columns: 1fr 1fr; }
68+
.tile:nth-child(3) { border-left: 0; }
69+
.tile:nth-child(n+3) { border-top: 1px solid var(--soft); }
70+
.cols { grid-template-columns: 1fr; }
71+
.bars { gap: 2px; height: 140px; }
3972
}
4073
</style>
4174
</head>
4275
<body>
43-
<main class="max-w-4xl mx-auto px-4 py-10 sm:py-16">
44-
<section class="glass rounded-3xl shadow-xl p-6 sm:p-10">
45-
<div class="flex flex-wrap items-start justify-between gap-4 mb-6">
46-
<div>
47-
<p class="text-xs font-semibold uppercase tracking-widest text-slate-500">Restricted area</p>
48-
<h1 class="text-3xl font-black mt-1">Database Table Counts</h1>
49-
<p class="text-sm text-slate-500 mt-2">Protected by HTTP Basic Auth and loaded from live D1 metadata.</p>
50-
</div>
51-
<button id="reload" class="a1-btn a1-btn--primary a1-btn--sm">Refresh</button>
76+
<main>
77+
<div class="head">
78+
<div>
79+
<p class="eyebrow">Restricted area · learn.alphaonelabs.com</p>
80+
<h1>Admin dashboard</h1>
81+
</div>
82+
<div class="actions">
83+
<button id="theme" class="btn" type="button" aria-label="Toggle dark mode">Theme</button>
84+
<button id="reload" class="btn btn--gold" type="button">Refresh</button>
5285
</div>
86+
</div>
87+
<hr class="rule" />
5388

54-
<div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mb-8">
55-
<div class="rounded-2xl border border-slate-200 bg-white p-4">
56-
<p class="text-xs uppercase tracking-wider text-slate-500">Tables</p>
57-
<p id="stat-tables" class="text-2xl font-extrabold mt-1">-</p>
58-
</div>
59-
<div class="rounded-2xl border border-slate-200 bg-white p-4">
60-
<p class="text-xs uppercase tracking-wider text-slate-500">Rows Total</p>
61-
<p id="stat-rows" class="text-2xl font-extrabold mt-1">-</p>
89+
<section aria-labelledby="views-title" style="margin-top:0">
90+
<div class="section-head">
91+
<h2 id="views-title">Page views</h2>
92+
<p class="eyebrow" id="views-since"></p>
93+
</div>
94+
<div class="tiles">
95+
<div class="tile"><p class="eyebrow">Today</p><p class="num gold" id="v-today">-</p></div>
96+
<div class="tile"><p class="eyebrow">Last 7 days</p><p class="num" id="v-7">-</p></div>
97+
<div class="tile"><p class="eyebrow">Last 30 days</p><p class="num" id="v-30">-</p></div>
98+
<div class="tile"><p class="eyebrow">All time</p><p class="num" id="v-all">-</p></div>
99+
</div>
100+
<div class="chart">
101+
<p class="eyebrow">Views per day, last 30 days (UTC)</p>
102+
<div class="bars" id="bars" role="img" aria-label="Views per day"></div>
103+
<div class="axis"><span id="axis-start"></span><span id="axis-end"></span></div>
104+
</div>
105+
<div class="cols">
106+
<div>
107+
<p class="eyebrow" style="margin-bottom:10px">Top activities · 30 days</p>
108+
<table><thead><tr><th>Activity</th><th class="n">Views</th></tr></thead><tbody id="top-acts"></tbody></table>
62109
</div>
63-
<div class="rounded-2xl border border-slate-200 bg-white p-4">
64-
<p class="text-xs uppercase tracking-wider text-slate-500">Largest Table</p>
65-
<p id="stat-largest" class="text-xl font-extrabold mt-1">-</p>
110+
<div>
111+
<p class="eyebrow" style="margin-bottom:10px">Top pages · 30 days</p>
112+
<table><thead><tr><th>Page</th><th class="n">Views</th></tr></thead><tbody id="top-pages"></tbody></table>
66113
</div>
67114
</div>
115+
<p class="status">Counts page loads by real browsers. Bots, prefetches, API calls and assets are skipped. No IP addresses, cookies or user ids are stored.</p>
116+
</section>
68117

69-
<div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
70-
<table class="w-full text-left">
71-
<thead class="bg-slate-50 text-slate-600 text-xs uppercase tracking-wide">
72-
<tr>
73-
<th class="px-4 py-3">Table</th>
74-
<th class="px-4 py-3">Rows</th>
75-
</tr>
76-
</thead>
77-
<tbody id="rows" class="text-sm"></tbody>
78-
</table>
118+
<section aria-labelledby="tables-title">
119+
<div class="section-head">
120+
<h2 id="tables-title">Database</h2>
121+
<p class="eyebrow" id="tables-summary"></p>
79122
</div>
80-
81-
<p id="status" class="text-sm text-slate-500 mt-4">Loading table counts...</p>
123+
<table><thead><tr><th>Table</th><th class="n">Rows</th></tr></thead><tbody id="rows"></tbody></table>
82124
</section>
125+
126+
<p id="status" class="status">Loading…</p>
83127
</main>
84128

85129
<script>
86130
function esc(s) {
87-
return String(s)
88-
.replace(/&/g, '&amp;')
89-
.replace(/</g, '&lt;')
90-
.replace(/>/g, '&gt;')
91-
.replace(/"/g, '&quot;');
131+
return String(s == null ? '' : s)
132+
.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
92133
}
134+
function fmt(n) { return Number(n || 0).toLocaleString(); }
135+
var base = window.location.pathname.replace(/\/+$/, '');
136+
var loadingRow = function (cols) {
137+
return '<tr><td class="pulse empty" colspan="' + cols + '">Loading…</td></tr>';
138+
};
139+
function emptyRow(cols, text) { return '<tr><td class="empty" colspan="' + cols + '">' + esc(text) + '</td></tr>'; }
93140

94-
function setLoading() {
95-
const body = document.getElementById('rows');
96-
body.innerHTML =
97-
'<tr><td class="px-4 py-3 pulse text-slate-400">Loading...</td><td class="px-4 py-3 pulse text-slate-400">Loading...</td></tr>';
98-
document.getElementById('status').textContent = 'Loading table counts...';
99-
}
141+
function renderViews(d) {
142+
var t = d.totals || {};
143+
document.getElementById('v-today').textContent = fmt(t.today);
144+
document.getElementById('v-7').textContent = fmt(t.last7);
145+
document.getElementById('v-30').textContent = fmt(t.last30);
146+
document.getElementById('v-all').textContent = fmt(t.all_time);
147+
document.getElementById('views-since').textContent = t.since ? 'Tracking since ' + t.since : 'No views recorded yet';
100148

101-
function render(rows) {
102-
const tbody = document.getElementById('rows');
103-
if (!rows.length) {
104-
tbody.innerHTML = '<tr><td class="px-4 py-4 text-slate-400" colspan="2">No tables found.</td></tr>';
105-
document.getElementById('status').textContent = 'No application tables available.';
106-
document.getElementById('stat-tables').textContent = '0';
107-
document.getElementById('stat-rows').textContent = '0';
108-
document.getElementById('stat-largest').textContent = '-';
109-
return;
110-
}
149+
var daily = d.daily || [];
150+
var max = Math.max.apply(null, daily.map(function (x) { return x.views; }).concat([1]));
151+
document.getElementById('bars').innerHTML = daily.map(function (x) {
152+
var h = x.views ? Math.max(2, Math.round(x.views / max * 100)) : 1;
153+
return '<div class="bar' + (x.views ? '' : ' zero') + '" style="height:' + h + '%" title="' + esc(x.day + ': ' + fmt(x.views) + ' views') + '"></div>';
154+
}).join('');
155+
document.getElementById('axis-start').textContent = daily.length ? daily[0].day : '';
156+
document.getElementById('axis-end').textContent = daily.length ? daily[daily.length - 1].day : '';
111157

112-
rows.sort((a, b) => b.count - a.count);
113-
tbody.innerHTML = rows.map((r) =>
114-
'<tr class="border-t border-slate-100">' +
115-
'<td class="px-4 py-3 font-medium text-slate-700">' + esc(r.table) + '</td>' +
116-
'<td class="px-4 py-3 text-slate-900">' + Number(r.count).toLocaleString() + '</td>' +
117-
'</tr>'
118-
).join('');
158+
function list(rows, cell) {
159+
if (!rows.length) return emptyRow(2, 'No views yet.');
160+
var top = rows[0].views || 1;
161+
return rows.map(function (r) {
162+
return '<tr><td>' + cell(r) + '<div class="meter"><span style="width:' + Math.round(r.views / top * 100) + '%"></span></div></td>' +
163+
'<td class="n">' + fmt(r.views) + '</td></tr>';
164+
}).join('');
165+
}
166+
document.getElementById('top-acts').innerHTML = list(d.top_activities || [], function (r) {
167+
return '<a href="' + esc(r.path) + '" target="_blank" rel="noopener">' + esc(r.title || r.slug || r.id) + '</a>';
168+
});
169+
document.getElementById('top-pages').innerHTML = list(d.top_pages || [], function (r) {
170+
return '<a href="' + esc(r.path) + '" target="_blank" rel="noopener"><code>' + esc(r.path) + '</code></a>';
171+
});
172+
}
119173

120-
const totalRows = rows.reduce((sum, r) => sum + Number(r.count || 0), 0);
121-
const largest = rows[0];
174+
function renderTables(rows) {
175+
rows.sort(function (a, b) { return b.count - a.count; });
176+
var total = rows.reduce(function (s, r) { return s + Number(r.count || 0); }, 0);
177+
document.getElementById('tables-summary').textContent = rows.length + ' tables · ' + fmt(total) + ' rows';
178+
document.getElementById('rows').innerHTML = rows.length ? rows.map(function (r) {
179+
return '<tr><td><code>' + esc(r.table) + '</code></td><td class="n">' + fmt(r.count) + '</td></tr>';
180+
}).join('') : emptyRow(2, 'No tables found.');
181+
}
122182

123-
document.getElementById('stat-tables').textContent = String(rows.length);
124-
document.getElementById('stat-rows').textContent = totalRows.toLocaleString();
125-
document.getElementById('stat-largest').textContent = largest.table + ' (' + Number(largest.count).toLocaleString() + ')';
126-
document.getElementById('status').textContent = 'Updated at ' + new Date().toLocaleTimeString();
183+
async function getJson(path) {
184+
var res = await fetch(base + path, { cache: 'no-store', credentials: 'same-origin' });
185+
if (!res.ok) throw new Error('HTTP ' + res.status + ' from ' + path + '. Open the admin page through ADMIN_URL and sign in with Basic Auth.');
186+
return res.json();
127187
}
128188

129-
async function loadCounts() {
130-
setLoading();
131-
try {
132-
const adminBase = window.location.pathname.replace(/\/+$/, '');
133-
const res = await fetch(adminBase + '/api/table-counts', { cache: 'no-store', credentials: 'same-origin' });
134-
if (!res.ok) {
135-
throw new Error('HTTP ' + res.status + ' while loading admin stats. Open the admin page through the configured ADMIN_URL and sign in with Basic Auth.');
136-
}
137-
const data = await res.json();
138-
render(data.tables || []);
139-
} catch (e) {
140-
document.getElementById('rows').innerHTML =
141-
'<tr><td class="px-4 py-4 text-red-600" colspan="2">Failed to load counts.</td></tr>';
142-
document.getElementById('status').textContent = e.message;
143-
}
189+
async function load() {
190+
['top-acts', 'top-pages', 'rows'].forEach(function (id) { document.getElementById(id).innerHTML = loadingRow(2); });
191+
var status = document.getElementById('status');
192+
status.className = 'status';
193+
status.textContent = 'Loading…';
194+
var results = await Promise.allSettled([getJson('/api/views'), getJson('/api/table-counts')]);
195+
var errors = [];
196+
if (results[0].status === 'fulfilled') renderViews(results[0].value);
197+
else { errors.push(results[0].reason.message); document.getElementById('top-acts').innerHTML = document.getElementById('top-pages').innerHTML = emptyRow(2, 'Failed to load views.'); }
198+
if (results[1].status === 'fulfilled') renderTables(results[1].value.tables || []);
199+
else { errors.push(results[1].reason.message); document.getElementById('rows').innerHTML = emptyRow(2, 'Failed to load table counts.'); }
200+
status.className = errors.length ? 'status err' : 'status';
201+
status.textContent = errors.length ? errors.join(' ') : 'Updated at ' + new Date().toLocaleTimeString();
144202
}
145203

146-
document.getElementById('reload').addEventListener('click', loadCounts);
147-
loadCounts();
204+
document.getElementById('reload').addEventListener('click', load);
205+
document.getElementById('theme').addEventListener('click', function () {
206+
var dark = !document.documentElement.classList.contains('dark');
207+
document.documentElement.classList.toggle('dark', dark);
208+
document.documentElement.setAttribute('data-a1-theme', dark ? 'dark' : 'light');
209+
try { localStorage.setItem('theme', dark ? 'dark' : 'light'); } catch (e) {}
210+
});
211+
load();
148212
</script>
149213
</body>
150214
</html>

0 commit comments

Comments
 (0)