-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.html
More file actions
484 lines (436 loc) · 20.8 KB
/
Copy pathanalytics.html
File metadata and controls
484 lines (436 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Signal — Analytics</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--ink:#141b26; --ink-2:#1c2532; --paper:#f4f3ee; --card:#ffffff;
--text:#1a2230; --muted:#717c8c; --line:#e6e4dc;
--urgent:#cf4035; --urgent-bg:#fbe9e7;
--accent:#1d7a8c; --accent-bg:#e4f1f3;
--discover:#6f54b8; --discover-bg:#eee9f8;
--ok:#2e7d52; --ok-bg:#e8f5ee;
--display:'Space Grotesk',sans-serif;
--body:'Inter',sans-serif;
--mono:'JetBrains Mono',monospace;
}
*{box-sizing:border-box;margin:0;padding:0;}
body{background:var(--paper);color:var(--text);font-family:var(--body);font-size:16px;-webkit-font-smoothing:antialiased;}
.wrap{max-width:1360px;margin:0 auto;padding:0 24px;}
/* ── header ── */
header{background:var(--ink);color:#eef1f5;position:sticky;top:0;z-index:20;border-bottom:1px solid #0c121b;}
.head-row{display:flex;align-items:center;gap:14px;padding:16px 0;}
.brand{font-family:var(--display);font-weight:700;font-size:24px;letter-spacing:-.02em;display:flex;align-items:center;gap:10px;}
.brand small{font-family:var(--mono);font-weight:400;font-size:13px;color:#8b97a8;letter-spacing:0;}
.badge{font-family:var(--mono);font-size:12px;padding:4px 11px;border-radius:20px;background:var(--accent);color:#fff;letter-spacing:.06em;}
.spacer{flex:1;}
.updated{font-family:var(--mono);font-size:12px;color:#6b7686;}
.hbtn{font-family:var(--mono);font-size:13px;color:#aab4c2;background:none;border:1px solid #2c3848;border-radius:8px;padding:7px 15px;cursor:pointer;text-decoration:none;transition:background .15s,color .15s;}
.hbtn:hover{background:var(--ink-2);color:#fff;}
/* ── page ── */
.page{padding:28px 0 64px;}
/* ── section header ── */
.sh{display:flex;align-items:center;gap:12px;margin-bottom:16px;margin-top:36px;}
.sh:first-of-type{margin-top:0;}
.sh h2{font-family:var(--display);font-size:14px;font-weight:600;letter-spacing:.14em;text-transform:uppercase;color:var(--muted);white-space:nowrap;}
.sh .rule{flex:1;height:1px;background:var(--line);}
/* ── summary cards ── */
.cards{display:grid;grid-template-columns:repeat(7,1fr);gap:16px;margin-bottom:6px;}
@media(max-width:1100px){.cards{grid-template-columns:repeat(4,1fr);}}
@media(max-width:640px){.cards{grid-template-columns:repeat(2,1fr);}}
.card{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:20px 22px;}
.card .lbl{font-family:var(--mono);font-size:11px;color:var(--muted);letter-spacing:.12em;text-transform:uppercase;margin-bottom:10px;}
.card .val{font-family:var(--display);font-size:36px;font-weight:700;line-height:1;}
.card .sub{font-family:var(--mono);font-size:11px;color:var(--muted);margin-top:7px;}
.card.c-accent{border-left:4px solid var(--accent);}
.card.c-ok{border-left:4px solid var(--ok);}
.card.c-urgent{border-left:4px solid var(--urgent);}
.card.c-discover{border-left:4px solid var(--discover);}
.card.c-action{border-left:4px solid #e07b00;}
/* ── table ── */
.tbl-wrap{background:var(--card);border:1px solid var(--line);border-radius:14px;overflow:auto;}
table{width:100%;border-collapse:collapse;font-size:15px;}
thead{background:var(--ink);color:#dfe5ec;position:sticky;top:0;}
thead th{font-family:var(--mono);font-size:12px;font-weight:500;letter-spacing:.08em;text-transform:uppercase;padding:14px 16px;text-align:right;white-space:nowrap;cursor:pointer;user-select:none;transition:background .12s;}
thead th:first-child{text-align:left;}
thead th:hover{background:var(--ink-2);}
thead th.asc::after{content:' ▲';color:#36b0c4;}
thead th.desc::after{content:' ▼';color:#36b0c4;}
tbody tr{border-bottom:1px solid var(--line);}
tbody tr:last-child{border-bottom:none;}
tbody tr:hover{background:#fafaf8;}
td{padding:14px 16px;vertical-align:middle;}
/* article cell */
.art-name{font-family:var(--display);font-weight:600;font-size:15px;line-height:1.35;margin-bottom:6px;}
.chips{display:flex;gap:5px;flex-wrap:wrap;}
.chip{font-family:var(--mono);font-size:11px;padding:3px 8px;border-radius:4px;}
.chip-cat{background:#eef0f1;color:#5a6573;}
.chip-urgent{background:var(--urgent-bg);color:var(--urgent);}
.chip-action{background:#fdf3e3;color:#b06d12;}
.chip-fyi{background:#eef0f1;color:#778090;}
.chip-mand{background:var(--urgent-bg);color:var(--urgent);font-weight:600;}
/* number cells */
.n{font-family:var(--mono);font-size:15px;text-align:right;white-space:nowrap;}
.n .pct{font-size:12px;color:var(--muted);display:block;margin-top:2px;}
.mini-bar{height:4px;border-radius:2px;margin-top:5px;min-width:40px;background:var(--line);}
.mini-bar span{display:block;height:100%;border-radius:2px;}
.bar-expand{background:var(--accent);}
.bar-action{background:var(--ok);}
.bar-dismiss{background:var(--urgent);}
.dash{font-family:var(--mono);font-size:15px;color:#c8cdd5;text-align:right;}
/* score */
.score{font-family:var(--mono);font-size:16px;font-weight:600;text-align:right;}
.s-pos{color:var(--ok);}
.s-neg{color:var(--urgent);}
.s-neu{color:var(--muted);}
/* ── bottom grid ── */
.bottom{display:grid;grid-template-columns:1fr 1fr;gap:20px;}
@media(max-width:760px){.bottom{grid-template-columns:1fr;}}
.panel{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:22px 26px;}
.panel h3{font-family:var(--display);font-size:14px;font-weight:600;letter-spacing:.12em;text-transform:uppercase;color:var(--muted);margin-bottom:20px;}
/* h-bar chart */
.hb-row{margin-bottom:14px;}
.hb-row:last-child{margin-bottom:0;}
.hb-lbl{display:flex;justify-content:space-between;font-family:var(--mono);font-size:13px;margin-bottom:5px;}
.hb-lbl .ct{color:var(--muted);}
.hb-track{height:9px;background:var(--line);border-radius:4px;overflow:hidden;}
.hb-fill{height:100%;border-radius:4px;background:linear-gradient(90deg,var(--accent),#36b0c4);}
/* ── recent activity ── */
.activity{list-style:none;}
.act-row{display:flex;align-items:flex-start;gap:13px;padding:12px 0;border-bottom:1px solid var(--line);}
.act-row:last-child{border-bottom:none;}
.ev-dot{width:10px;height:10px;border-radius:50%;margin-top:5px;flex-shrink:0;}
.d-expand{background:var(--accent);}
.d-action{background:var(--ok);}
.d-feedback_pos{background:var(--ok);}
.d-feedback_neg{background:var(--urgent);}
.d-dismiss{background:var(--urgent);}
.d-impression{background:#c0c6ce;}
.d-dwell{background:var(--discover);}
.d-open{background:#8b97a8;}
.act-body{flex:1;min-width:0;}
.act-type{font-family:var(--mono);font-size:12px;font-weight:500;text-transform:uppercase;letter-spacing:.06em;color:var(--accent);margin-bottom:3px;}
.act-title{font-size:15px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.act-meta{font-family:var(--mono);font-size:12px;color:var(--muted);margin-top:2px;}
.act-ts{font-family:var(--mono);font-size:12px;color:var(--muted);white-space:nowrap;}
/* ── states ── */
.loading{font-family:var(--mono);font-size:15px;color:var(--muted);padding:48px 0;text-align:center;}
.empty{font-family:var(--mono);font-size:13px;color:var(--muted);padding:20px 0;text-align:center;line-height:1.8;}
</style>
</head>
<body>
<header>
<div class="wrap">
<div class="head-row">
<div class="brand">Signal <small>internal comms, routed</small></div>
<span class="badge">Analytics</span>
<span class="spacer"></span>
<span class="updated" id="updated-ts"></span>
<button class="hbtn" onclick="loadData()">↻ Refresh</button>
<a class="hbtn" href="/">← Feed</a>
</div>
</div>
</header>
<div class="wrap page">
<div id="loading" class="loading">Loading analytics…</div>
<div id="content" style="display:none">
<!-- Summary -->
<div class="sh"><h2>Overview</h2><div class="rule"></div></div>
<div class="cards">
<div class="card c-accent">
<div class="lbl">Total events</div>
<div class="val" id="s-total">—</div>
<div class="sub">all interaction types</div>
</div>
<div class="card c-discover">
<div class="lbl">Active users</div>
<div class="val" id="s-users">—</div>
<div class="sub">distinct user IDs</div>
</div>
<div class="card c-ok">
<div class="lbl">Actions taken</div>
<div class="val" id="s-actions">—</div>
<div class="sub">registrations & RSVPs</div>
</div>
<div class="card c-ok">
<div class="lbl">Useful votes</div>
<div class="val" id="s-useful">—</div>
<div class="sub">▲ explicit positive</div>
</div>
<div class="card c-urgent">
<div class="lbl">Not useful</div>
<div class="val" id="s-notuseful">—</div>
<div class="sub">▼ explicit negative</div>
</div>
<div class="card c-urgent">
<div class="lbl">Dismissals</div>
<div class="val" id="s-dismiss">—</div>
<div class="sub">articles swiped away</div>
</div>
<div class="card c-action">
<div class="lbl">Total read time</div>
<div class="val" id="s-dwell">—</div>
<div class="sub">across all dwell events</div>
</div>
</div>
<!-- Article table -->
<div class="sh" style="margin-top:28px"><h2>Article Performance</h2><div class="rule"></div></div>
<div class="tbl-wrap">
<table>
<thead>
<tr>
<th id="th-title" onclick="sortBy('title')">Article</th>
<th id="th-impression" onclick="sortBy('impression')">Shown</th>
<th id="th-expand" onclick="sortBy('expand')">Read More</th>
<th id="th-avg_dwell" onclick="sortBy('avg_dwell')">Avg Read</th>
<th id="th-action" onclick="sortBy('action')">Action Btn</th>
<th id="th-feedback_pos" onclick="sortBy('feedback_pos')">Useful ▲</th>
<th id="th-feedback_neg" onclick="sortBy('feedback_neg')">Not Useful ▼</th>
<th id="th-dismiss" onclick="sortBy('dismiss')">Dismissed</th>
<th id="th-score" onclick="sortBy('score')">Eng. Score</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
</div>
<!-- Breakdown charts -->
<div class="sh" style="margin-top:28px"><h2>Breakdown</h2><div class="rule"></div></div>
<div class="bottom">
<div class="panel">
<h3>Events by type</h3>
<div id="by-type"></div>
</div>
<div class="panel">
<h3>Activity by role</h3>
<div id="by-role"></div>
</div>
</div>
<!-- Recent activity -->
<div class="sh" style="margin-top:28px"><h2>Recent Activity</h2><div class="rule"></div></div>
<div class="panel">
<ul class="activity" id="recent"></ul>
</div>
</div><!-- /content -->
</div><!-- /wrap -->
<script>
const API = 'http://localhost:8000';
const EV_LABEL = {
impression:'Impression', open:'Opened', expand:'Read More',
dwell:'Dwell (read)', action:'Action taken',
feedback_pos:'Useful ▲', feedback_neg:'Not Useful ▼', dismiss:'Dismissed',
};
// Weights mirror Python EVENT_VALUE in recommender.py
const EV_WEIGHT = {
impression:0, open:0.25, expand:0.4, dwell:0.6,
action:0.9, feedback_pos:1.0, feedback_neg:-1.0, dismiss:-0.6,
};
let _data = null;
let _stats = null; // item_id -> aggregated counts
let _sortCol = 'impression';
let _sortDir = -1; // -1 = descending
// ── data load ────────────────────────────────────────────────────────────────
async function loadData() {
document.getElementById('loading').style.display = 'block';
document.getElementById('content').style.display = 'none';
try {
const r = await fetch(API + '/analytics/data');
if (!r.ok) throw new Error(r.status);
_data = await r.json();
_stats = buildStats(_data);
render();
document.getElementById('updated-ts').textContent =
'Updated ' + new Date().toLocaleTimeString();
} catch(e) {
document.getElementById('loading').textContent =
'Could not reach the backend. Start it with: uvicorn main:app --port 8000';
}
}
function buildStats({ items, item_events }) {
const s = {};
for (const it of items) {
s[it.id] = {
impression:0, open:0, expand:0, dwell:0, action:0,
feedback_pos:0, feedback_neg:0, dismiss:0,
total_dwell_ms:0, dwell_count:0,
};
}
for (const ev of item_events) {
if (!s[ev.item_id]) continue;
const r = s[ev.item_id];
r[ev.event_type] = (r[ev.event_type] || 0) + ev.count;
if (ev.event_type === 'dwell' && ev.avg_dwell_ms) {
r.total_dwell_ms += ev.avg_dwell_ms * ev.count;
r.dwell_count += ev.count;
}
}
return s;
}
function engScore(s) {
const imp = s.impression || 0;
// Only compute a score if we have at least one meaningful event
const anyEvent = s.expand || s.action || s.feedback_pos || s.feedback_neg || s.dismiss;
if (!anyEvent) return null;
const denom = Math.max(imp, 1);
return (
(s.expand || 0) * 0.4 +
(s.dwell || 0) * 0.6 +
(s.action || 0) * 0.9 +
(s.feedback_pos || 0) * 1.0 +
(s.feedback_neg || 0) * -1.0 +
(s.dismiss || 0) * -0.6
) / denom;
}
// ── render ───────────────────────────────────────────────────────────────────
function render() {
const { summary, by_type, by_role, recent, items, personas } = _data;
// Summary cards
document.getElementById('s-total').textContent = fmt(summary.total_events || 0);
document.getElementById('s-users').textContent = fmt(summary.unique_users || 0);
document.getElementById('s-actions').textContent = fmt(summary.total_actions || 0);
document.getElementById('s-useful').textContent = fmt(summary.total_useful || 0);
document.getElementById('s-notuseful').textContent= fmt(summary.total_not_useful|| 0);
document.getElementById('s-dismiss').textContent = fmt(summary.total_dismissals|| 0);
const sec = Math.round((summary.total_dwell_ms || 0) / 1000);
document.getElementById('s-dwell').textContent =
sec >= 3600 ? (sec/3600).toFixed(1)+'h' :
sec >= 60 ? Math.round(sec/60)+'m' : sec+'s';
// Table
renderTable(items);
// Event type chart
const maxType = Math.max(...by_type.map(r => r.count), 1);
document.getElementById('by-type').innerHTML = by_type.length
? by_type.map(r => hbar(EV_LABEL[r.event_type] || r.event_type, r.count, maxType)).join('')
: '<p class="empty">No events yet.</p>';
// Role chart
const personaMap = Object.fromEntries((personas||[]).map(p => [p.id, p.label]));
const maxRole = Math.max(...by_role.map(r => r.events), 1);
document.getElementById('by-role').innerHTML = by_role.length
? by_role.map(r => hbar(personaMap[r.role] || r.role, r.events, maxRole)).join('')
: '<p class="empty">No role activity yet.</p>';
// Recent
const itemMap = Object.fromEntries(items.map(it => [it.id, it]));
document.getElementById('recent').innerHTML = recent.length
? recent.map(ev => {
const it = itemMap[ev.item_id];
return `<li class="act-row">
<div class="ev-dot d-${ev.event_type}"></div>
<div class="act-body">
<div class="act-type">${EV_LABEL[ev.event_type] || ev.event_type}</div>
<div class="act-title">${it ? it.title : ev.item_id}</div>
<div class="act-meta">${ev.role || ev.user_id}${ev.dwell_ms ? ' · ' + (ev.dwell_ms/1000).toFixed(1)+'s read' : ''}</div>
</div>
<div class="act-ts">${timeAgo(ev.ts)}</div>
</li>`;
}).join('')
: '<li class="empty">No activity recorded yet. Use the feed then come back.</li>';
document.getElementById('loading').style.display = 'none';
document.getElementById('content').style.display = 'block';
}
function renderTable(items) {
// Update sort indicators
document.querySelectorAll('thead th').forEach(th => th.classList.remove('asc','desc'));
const activeTh = document.getElementById('th-' + _sortCol);
if (activeTh) activeTh.classList.add(_sortDir === -1 ? 'desc' : 'asc');
const rows = items.map(it => {
const s = _stats[it.id] || {};
const imp = s.impression || 0;
const exp = s.expand || 0;
const act = s.action || 0;
const dis = s.dismiss || 0;
const avgDwell = s.dwell_count > 0 ? s.total_dwell_ms / s.dwell_count : null;
const score = engScore(s);
return { it, s, imp, exp, act, dis, avgDwell, score };
});
// Sort
rows.sort((a, b) => {
if (_sortCol === 'title')
return _sortDir * a.it.title.localeCompare(b.it.title);
const map = {
impression:'imp', expand:'exp', action:'act', dismiss:'dis', score:'score',
avg_dwell:'avgDwell', feedback_pos:'feedback_pos', feedback_neg:'feedback_neg',
};
const k = map[_sortCol];
const av = k === 'feedback_pos' || k === 'feedback_neg'
? (a.s[k] || 0) : (a[k] ?? -999);
const bv = k === 'feedback_pos' || k === 'feedback_neg'
? (b.s[k] || 0) : (b[k] ?? -999);
return _sortDir * (av - bv);
});
const maxImp = Math.max(...rows.map(r => r.imp), 1);
document.getElementById('tbody').innerHTML = rows.length === 0
? `<tr><td colspan="9"><div class="empty" style="padding:40px">No articles found.</div></td></tr>`
: rows.map(({ it, s, imp, exp, act, dis, avgDwell, score }) => {
const expPct = imp > 0 ? exp / imp * 100 : 0;
const actPct = imp > 0 ? act / imp * 100 : 0;
const disPct = imp > 0 ? dis / imp * 100 : 0;
const nc = (n, pct, barCls) => n > 0
? `<td class="n">${fmt(n)}${pct !== null
? `<span class="pct">${pct.toFixed(0)}%</span>
<div class="mini-bar"><span class="${barCls}" style="width:${Math.min(100,pct).toFixed(1)}%"></span></div>`
: ''}</td>`
: `<td class="dash">—</td>`;
const dwellTd = avgDwell
? `<td class="n">${(avgDwell/1000).toFixed(1)}s</td>`
: `<td class="dash">—</td>`;
const scoreTd = score === null
? `<td class="dash">—</td>`
: (() => {
const cls = score > 0.15 ? 's-pos' : score < -0.05 ? 's-neg' : 's-neu';
return `<td class="score ${cls}">${score > 0 ? '+' : ''}${score.toFixed(2)}</td>`;
})();
const fpTd = (s.feedback_pos||0) > 0
? `<td class="n">${s.feedback_pos}</td>` : `<td class="dash">—</td>`;
const fnTd = (s.feedback_neg||0) > 0
? `<td class="n">${s.feedback_neg}</td>` : `<td class="dash">—</td>`;
return `<tr>
<td style="min-width:240px;max-width:340px">
<div class="art-name">${it.title}</div>
<div class="chips">
<span class="chip chip-${it.urgency}">${it.urgency}</span>
<span class="chip chip-cat">${it.category}</span>
${it.mandatory ? '<span class="chip chip-mand">mandatory</span>' : ''}
</div>
</td>
${nc(imp, null, '')}
${nc(exp, expPct, 'bar-expand')}
${dwellTd}
${nc(act, actPct, 'bar-action')}
${fpTd}
${fnTd}
${nc(dis, disPct, 'bar-dismiss')}
${scoreTd}
</tr>`;
}).join('');
}
// ── sort ─────────────────────────────────────────────────────────────────────
function sortBy(col) {
_sortDir = (_sortCol === col) ? _sortDir * -1 : -1;
_sortCol = col;
if (_data) renderTable(_data.items);
}
// ── helpers ───────────────────────────────────────────────────────────────────
function hbar(label, count, max) {
return `<div class="hb-row">
<div class="hb-lbl"><span>${label}</span><span class="ct">${fmt(count)}</span></div>
<div class="hb-track"><div class="hb-fill" style="width:${(count/max*100).toFixed(1)}%"></div></div>
</div>`;
}
function fmt(n) {
return (n || 0).toLocaleString();
}
function timeAgo(ts) {
const s = Date.now()/1000 - ts;
if (s < 60) return 'just now';
if (s < 3600) return Math.round(s/60) + 'm ago';
if (s < 86400) return Math.round(s/3600) + 'h ago';
return Math.round(s/86400) + 'd ago';
}
loadData();
</script>
</body>
</html>