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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/* Unified admin panel client behaviour.
* Lives in an external file because the site's enforced CSP (nginx) uses
* script-src 'self' with no 'unsafe-inline' — inline <script> would be blocked.
*/
(function () {
// ---- Blog: markdown toolbar + live preview ----
var ed = document.getElementById('md-editor');
var pv = document.getElementById('md-preview');
if (ed) {
var esc = function (s) {
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
};
var render = function (t) {
return esc(t)
.replace(/```([\s\S]*?)```/g, function (_, c) { return '<pre><code>' + c + '</code></pre>'; })
.replace(/^### (.*)$/gm, '<h3>$1</h3>')
.replace(/^## (.*)$/gm, '<h2>$1</h2>')
.replace(/^# (.*)$/gm, '<h1>$1</h1>')
.replace(/^> (.*)$/gm, '<blockquote>$1</blockquote>')
.replace(/\*\*([^*]+)\*\*/g, '<b>$1</b>')
.replace(/\*([^*]+)\*/g, '<i>$1</i>')
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img alt="$1" src="$2" style="max-width:100%">')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>')
.replace(/\n/g, '<br>');
};
var upd = function () {
if (pv) pv.innerHTML = ed.value.trim() ? render(ed.value) : '<em class="muted">Type to preview…</em>';
};
ed.addEventListener('input', upd);
upd();
document.querySelectorAll('.md-toolbar button[data-md]').forEach(function (btn) {
btn.addEventListener('click', function () {
var mode = btn.dataset.md, a = btn.dataset.a || '', b = btn.dataset.b || '';
var s = ed.selectionStart, e = ed.selectionEnd, v = ed.value;
if (mode === 'wrap') {
ed.setRangeText(a + v.substring(s, e) + b, s, e, 'end');
} else if (mode === 'line') {
var ls = s; while (ls > 0 && v[ls - 1] !== '\n') ls--;
ed.setRangeText(a, ls, ls, 'end');
} else {
ed.setRangeText(a, s, e, 'end');
}
ed.dispatchEvent(new Event('input'));
ed.focus();
});
});
}
// ---- MOTD: live XMPP preview (emoji / heading / body / url) ----
var motdPreview = document.getElementById('motd-live-preview');
if (motdPreview) {
var motdVal = function (id) {
var el = document.getElementById(id);
return el ? String(el.value || '').trim() : '';
};
var motdPaint = function () {
var emoji = motdVal('motd-emoji');
var heading = motdVal('motd-heading');
var body = motdVal('motd-body');
var url = motdVal('motd-url');
var head = [emoji, heading].filter(Boolean).join(' ');
var lines = [];
if (head) lines.push(head);
if (body) lines.push(body);
if (url) lines.push(url);
motdPreview.textContent = lines.join('\n') || '// fill in a body';
};
['motd-emoji', 'motd-heading', 'motd-body', 'motd-url'].forEach(function (id) {
var el = document.getElementById(id);
if (el) el.addEventListener('input', motdPaint);
});
motdPaint();
}
// ---- Vibe: live "detected type" badge ----
var url = document.getElementById('v-url');
var badge = document.getElementById('v-type');
if (url && badge) {
var detect = function (u) {
if (!u) return 'none';
if (/\.(jpe?g|png|gif|webp|bmp|svg|avif)(\?|$)/i.test(u)) return 'image';
if (/\.(mp4|webm|ogv|mov)(\?|$)/i.test(u)) return 'video';
if (/\.(mp3|flac|wav|ogg|aac|opus|m4a)(\?|$)/i.test(u)) return 'audio';
if (/(youtube\.com\/watch\?.*v=|youtu\.be\/|youtube\.com\/shorts\/)[\w-]{11}/.test(u)) return 'youtube';
if (/vimeo\.com\/(video\/)?\d+/.test(u)) return 'vimeo';
if (/(dailymotion\.com\/video|dai\.ly)\/[\w]+/i.test(u)) return 'dailymotion';
if (/bilibili\.com\/video\/(BV[\w]+|av\d+)/i.test(u)) return 'bilibili';
if (/(nicovideo\.jp\/watch|nico\.ms)\/(sm|nm|so)?\d+/i.test(u)) return 'niconico';
return 'link';
};
url.addEventListener('input', function () { badge.textContent = detect(url.value.trim()); });
}
// ---- Traffic analytics charts + live numbers (vanilla canvas, no CDN) ----
(function initTrafficLive() {
var liveRoot = document.getElementById('traffic-live-root');
var chartRoot = document.getElementById('traffic-analytics');
var dataEl = document.getElementById('traffic-analytics-data');
// Need at least KPI root or charts to run live updates
if (!liveRoot && !chartRoot) return;
var data = {};
if (dataEl) {
try {
data = JSON.parse(dataEl.textContent || '{}');
} catch (e) {
data = {};
}
}
var light = document.body.classList.contains('light-mode');
var colors = {
bg: light ? '#f4f7fa' : '#050000',
grid: light ? 'rgba(0,0,0,0.08)' : 'rgba(255,255,255,0.08)',
axis: light ? 'rgba(0,0,0,0.45)' : 'rgba(200,200,210,0.55)',
hits: light ? '#1a7a12' : '#39ff14',
hitsFill: light ? 'rgba(26,122,18,0.15)' : 'rgba(57,255,20,0.12)',
uniq: light ? '#1a4f9c' : '#5b9dff',
uniqFill: light ? 'rgba(26,79,156,0.12)' : 'rgba(91,157,255,0.12)',
bar: light ? '#1a7a12' : '#39ff14',
barAlt: light ? '#1a4f9c' : '#5b9dff',
bot: light ? '#a65c00' : '#ff9f1a',
botFill: light ? 'rgba(166,92,0,0.12)' : 'rgba(255,159,26,0.14)',
botAlt: light ? '#8a3d00' : '#ff6b35',
text: light ? 'rgba(20,20,30,0.75)' : 'rgba(200,200,210,0.8)',
muted: light ? 'rgba(20,20,30,0.45)' : 'rgba(160,160,170,0.7)',
};
function setupCanvas(canvas) {
if (!canvas) return null;
var dpr = Math.max(1, Math.min(2, window.devicePixelRatio || 1));
var rect = canvas.getBoundingClientRect();
var cssW = Math.max(280, Math.floor(rect.width || canvas.width || 600));
var cssH = Math.max(160, Math.floor(canvas.height * (cssW / (canvas.width || cssW))));
// Prefer intrinsic aspect from attributes
var attrW = parseInt(canvas.getAttribute('width'), 10) || 600;
var attrH = parseInt(canvas.getAttribute('height'), 10) || 240;
cssH = Math.round(cssW * (attrH / attrW));
canvas.style.width = cssW + 'px';
canvas.style.height = cssH + 'px';
canvas.width = Math.floor(cssW * dpr);
canvas.height = Math.floor(cssH * dpr);
var ctx = canvas.getContext('2d');
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
return { ctx: ctx, w: cssW, h: cssH };
}
function maxOf(arr) {
var m = 0;
for (var i = 0; i < arr.length; i++) if (arr[i] > m) m = arr[i];
return m || 1;
}
function niceMax(n) {
if (n <= 1) return 1;
var p = Math.pow(10, Math.floor(Math.log10(n)));
var m = n / p;
var nice = m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10;
return nice * p;
}
function drawEmpty(ctx, w, h, msg) {
ctx.fillStyle = colors.bg;
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = colors.muted;
ctx.font = '12px ui-monospace, Menlo, Consolas, monospace';
ctx.textAlign = 'center';
ctx.fillText(msg || 'No data yet', w / 2, h / 2);
}
/** Multi-series line chart */
function drawLineChart(canvas, series, labels, opts) {
var s = setupCanvas(canvas);
if (!s) return;
var ctx = s.ctx, w = s.w, h = s.h;
var pad = { t: 16, r: 12, b: 36, l: 40 };
var plotW = w - pad.l - pad.r;
var plotH = h - pad.t - pad.b;
ctx.fillStyle = colors.bg;
ctx.fillRect(0, 0, w, h);
var all = [];
series.forEach(function (ser) {
ser.values.forEach(function (v) { all.push(v); });
});
if (!labels.length || maxOf(all) === 0) {
drawEmpty(ctx, w, h, 'No hits in this range yet');
return;
}
var yMax = niceMax(maxOf(all));
var n = labels.length;
var xAt = function (i) {
return pad.l + (n === 1 ? plotW / 2 : (i / (n - 1)) * plotW);
};
var yAt = function (v) {
return pad.t + plotH - (v / yMax) * plotH;
};
// grid
ctx.strokeStyle = colors.grid;
ctx.lineWidth = 1;
ctx.fillStyle = colors.axis;
ctx.font = '10px ui-monospace, Menlo, Consolas, monospace';
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
var ticks = 4;
for (var t = 0; t <= ticks; t++) {
var val = Math.round((yMax * t) / ticks);
var y = yAt(val);
ctx.beginPath();
ctx.moveTo(pad.l, y);
ctx.lineTo(pad.l + plotW, y);
ctx.stroke();
ctx.fillText(String(val), pad.l - 6, y);
}
// x labels (sparse)
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillStyle = colors.axis;
var step = Math.max(1, Math.ceil(n / 8));
for (var i = 0; i < n; i += step) {
var lab = labels[i];
if (opts && opts.labelFmt) lab = opts.labelFmt(lab);
ctx.fillText(lab, xAt(i), pad.t + plotH + 8);
}
series.forEach(function (ser) {
// fill
if (ser.fill) {
ctx.beginPath();
ctx.moveTo(xAt(0), yAt(0));
for (var j = 0; j < ser.values.length; j++) {
ctx.lineTo(xAt(j), yAt(ser.values[j]));
}
ctx.lineTo(xAt(ser.values.length - 1), yAt(0));
ctx.closePath();
ctx.fillStyle = ser.fill;
ctx.fill();
}
// line
ctx.beginPath();
for (var k = 0; k < ser.values.length; k++) {
var x = xAt(k), y = yAt(ser.values[k]);
if (k === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = ser.color;
ctx.lineWidth = 2;
ctx.lineJoin = 'round';
ctx.stroke();
// dots on last points / sparse
ctx.fillStyle = ser.color;
for (var d = 0; d < ser.values.length; d++) {
if (ser.values[d] <= 0 && d !== ser.values.length - 1) continue;
if (d % Math.max(1, Math.floor(n / 12)) !== 0 && d !== n - 1) continue;
ctx.beginPath();
ctx.arc(xAt(d), yAt(ser.values[d]), 2.5, 0, Math.PI * 2);
ctx.fill();
}
});
}
/**
* Vertical bar chart.
* values: number[] (single series) OR { human: number[], bots: number[] }
* Bots draw in orange beside human bars.
*/
function drawBarChart(canvas, values, labels, opts) {
var s = setupCanvas(canvas);
if (!s) return;
var ctx = s.ctx, w = s.w, h = s.h;
var pad = { t: 16, r: 12, b: 36, l: 40 };
var plotW = w - pad.l - pad.r;
var plotH = h - pad.t - pad.b;
ctx.fillStyle = colors.bg;
ctx.fillRect(0, 0, w, h);
var human = Array.isArray(values) ? values : (values.human || []);
var bots = Array.isArray(values) ? null : (values.bots || null);
var dual = !!(bots && bots.length === human.length);
var all = human.slice();
if (dual) bots.forEach(function (v) { all.push(v); });
if (!human.length || maxOf(all) === 0) {
drawEmpty(ctx, w, h, 'No hits in this range yet');
return;
}
var yMax = niceMax(maxOf(all));
var n = human.length;
var gap = 2;
var groupW = plotW / n;
var barW = dual
? Math.max(2, (groupW - gap) / 2 - 1)
: Math.max(2, groupW - gap);
ctx.strokeStyle = colors.grid;
ctx.fillStyle = colors.axis;
ctx.font = '10px ui-monospace, Menlo, Consolas, monospace';
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
for (var t = 0; t <= 4; t++) {
var val = Math.round((yMax * t) / 4);
var y = pad.t + plotH - (val / yMax) * plotH;
ctx.beginPath();
ctx.moveTo(pad.l, y);
ctx.lineTo(pad.l + plotW, y);
ctx.stroke();
ctx.fillText(String(val), pad.l - 6, y);
}
for (var i = 0; i < n; i++) {
var gx = pad.l + i * groupW + gap / 2;
// Human (green)
var bh = (human[i] / yMax) * plotH;
var hy = pad.t + plotH - bh;
ctx.fillStyle = colors.bar;
ctx.globalAlpha = human[i] > 0 ? 0.9 : 0.15;
ctx.fillRect(gx, hy, barW, Math.max(bh, human[i] > 0 ? 1 : 0));
// Bots (orange) — separate colour, not mixed into human bars
if (dual) {
var bb = (bots[i] / yMax) * plotH;
var by = pad.t + plotH - bb;
ctx.fillStyle = colors.bot;
ctx.globalAlpha = bots[i] > 0 ? 0.9 : 0.15;
ctx.fillRect(gx + barW + 1, by, barW, Math.max(bb, bots[i] > 0 ? 1 : 0));
}
ctx.globalAlpha = 1;
}
ctx.fillStyle = colors.axis;
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
var step = Math.max(1, Math.ceil(n / 8));
for (var j = 0; j < n; j += step) {
var lab = labels[j] || '';
if (opts && opts.labelFmt) lab = opts.labelFmt(lab);
var lx = pad.l + j * groupW + groupW / 2;
ctx.fillText(lab, lx, pad.t + plotH + 8);
}
}
/** Horizontal bar chart. opts.palette: 'bot' uses orange bot colors. */
function drawHBarChart(canvas, items, opts) {
var s = setupCanvas(canvas);
if (!s) return;
var ctx = s.ctx, w = s.w, h = s.h;
ctx.fillStyle = colors.bg;
ctx.fillRect(0, 0, w, h);
if (!items || !items.length) {
drawEmpty(ctx, w, h, 'No data yet');
return;
}
var pad = { t: 8, r: 48, b: 8, l: 8 };
var rowH = Math.min(28, (h - pad.t - pad.b) / items.length);
var maxLabel = 0;
ctx.font = '11px ui-monospace, Menlo, Consolas, monospace';
items.forEach(function (it) {
var lab = truncate(it.label, 36);
maxLabel = Math.max(maxLabel, ctx.measureText(lab).width);
});
var labelW = Math.min(Math.max(80, maxLabel + 8), w * 0.45);
var barLeft = pad.l + labelW + 8;
var barMaxW = w - barLeft - pad.r;
var maxC = maxOf(items.map(function (it) { return it.count; }));
maxC = niceMax(maxC);
var cA = (opts && opts.palette === 'bot') ? colors.bot : colors.bar;
var cB = (opts && opts.palette === 'bot') ? colors.botAlt : colors.barAlt;
items.forEach(function (it, i) {
var y = pad.t + i * rowH + rowH * 0.15;
var bh = rowH * 0.7;
var lab = truncate(it.label, 36);
ctx.fillStyle = colors.text;
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
ctx.font = '11px ui-monospace, Menlo, Consolas, monospace';
ctx.fillText(lab, pad.l + labelW, y + bh / 2);
var bw = maxC ? (it.count / maxC) * barMaxW : 0;
ctx.fillStyle = i % 2 === 0 ? cA : cB;
ctx.globalAlpha = 0.85;
ctx.fillRect(barLeft, y, Math.max(bw, it.count > 0 ? 2 : 0), bh);
ctx.globalAlpha = 1;
ctx.fillStyle = colors.muted;
ctx.textAlign = 'left';
ctx.fillText(String(it.count), barLeft + bw + 6, y + bh / 2);
});
}
function truncate(s, n) {
s = String(s || '');
if (s.length <= n) return s;
return s.slice(0, n - 1) + '…';
}
function esc(s) {
return String(s == null ? '' : s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function pad2(n) {
n = String(n);
return n.length < 2 ? '0' + n : n;
}
function fmtUtc(ts, withSec) {
if (!ts) return '—';
var d = new Date(ts * 1000);
var s =
d.getUTCFullYear() +
'-' + pad2(d.getUTCMonth() + 1) +
'-' + pad2(d.getUTCDate()) +
' ' + pad2(d.getUTCHours()) +
':' + pad2(d.getUTCMinutes());
if (withSec) s += ':' + pad2(d.getUTCSeconds());
return s + ' UTC';
}
function drawAllCharts(analytics) {
analytics = analytics || {};
var daily = analytics.daily || [];
var dailySeries = [
{ values: daily.map(function (d) { return d.hits; }), color: colors.hits, fill: colors.hitsFill },
{ values: daily.map(function (d) { return d.unique; }), color: colors.uniq, fill: colors.uniqFill },
{ values: daily.map(function (d) { return d.bots || 0; }), color: colors.bot, fill: colors.botFill },
];
drawLineChart(
document.getElementById('chart-daily'),
dailySeries,
daily.map(function (d) { return d.day; }),
{ labelFmt: function (d) { return d.slice(5); } }
);
var hourly = analytics.hourly || [];
drawBarChart(
document.getElementById('chart-hourly'),
{
human: hourly.map(function (d) { return d.hits; }),
bots: hourly.map(function (d) { return d.bots || 0; }),
},
hourly.map(function (d) { return d.hour; }),
{ labelFmt: function (h) { return h.slice(11, 13) + 'h'; } }
);
drawHBarChart(document.getElementById('chart-pages'), analytics.top_pages || []);
drawHBarChart(document.getElementById('chart-visitors'), analytics.visitors || []);
drawHBarChart(document.getElementById('chart-bots'), analytics.bots || [], { palette: 'bot' });
drawHBarChart(document.getElementById('chart-hosts'), analytics.hosts || []);
drawHBarChart(document.getElementById('chart-langs'), analytics.langs || []);
drawHBarChart(document.getElementById('chart-refs'), analytics.refs || []);
}
// Animated count-up for KPI big numbers
var kpiRaf = {};
function animateKpi(el, from, to) {
var key = el.getAttribute('data-kpi') || el.id || 'x';
if (kpiRaf[key]) cancelAnimationFrame(kpiRaf[key]);
from = Math.round(Number(from) || 0);
to = Math.round(Number(to) || 0);
if (from === to) {
el.textContent = String(to);
el.setAttribute('data-value', String(to));
return;
}
var start = performance.now();
var dur = Math.min(700, 180 + Math.abs(to - from) * 12);
function frame(now) {
var t = Math.min(1, (now - start) / dur);
// ease-out cubic
var e = 1 - Math.pow(1 - t, 3);
var v = Math.round(from + (to - from) * e);
el.textContent = String(v);
if (t < 1) {
kpiRaf[key] = requestAnimationFrame(frame);
} else {
el.textContent = String(to);
el.setAttribute('data-value', String(to));
delete kpiRaf[key];
}
}
kpiRaf[key] = requestAnimationFrame(frame);
}
function updateKpis(summary, opts) {
if (!summary) return;
var animate = !opts || opts.animate !== false;
var keys = [
'total_hits', 'unique_sessions', 'today', 'week',
'bot_hits', 'bot_today', 'bot_week', 'total_pages',
];
keys.forEach(function (k) {
if (summary[k] == null) return;
var el = document.querySelector('[data-kpi="' + k + '"]');
if (!el) return;
var next = Math.round(Number(summary[k]) || 0);
var prev = parseInt(el.getAttribute('data-value') || el.textContent || '0', 10) || 0;
if (prev === next && el.textContent === String(next)) return;
el.classList.remove('traffic-kpi-flash');
void el.offsetWidth;
el.classList.add('traffic-kpi-flash');
if (animate && Math.abs(next - prev) < 5000) {
animateKpi(el, prev, next);
} else {
el.textContent = String(next);
el.setAttribute('data-value', String(next));
}
});
}
function updateTopTable(rows) {
var body = document.getElementById('traffic-top-body');
var table = document.getElementById('traffic-top-table');
var empty = document.getElementById('traffic-top-empty');
if (!body || !table) return;
rows = rows || [];
if (!rows.length) {
body.innerHTML = '';
table.hidden = true;
if (empty) empty.hidden = false;
return;
}
if (empty) empty.hidden = true;
table.hidden = false;
var html = '';
for (var i = 0; i < rows.length; i++) {
var p = rows[i];
html +=
'<tr>' +
'<td><code>' + esc((p.host || '') + (p.path || '')) + '</code></td>' +
'<td>' + (p.hits | 0) + '</td>' +
'<td>' + (p.unique_sessions | 0) + '</td>' +
'<td>' + (p.bot_hits | 0) + '</td>' +
'<td>' + (p.last_local_count | 0) + '</td>' +
'<td>' + esc(p.last_visitor || '—') + '</td>' +
'<td>' + esc(p.last_hit ? fmtUtc(p.last_hit, false) : '—') + '</td>' +
'</tr>';
}
body.innerHTML = html;
}
function updateRecentTable(rows) {
var body = document.getElementById('traffic-recent-body');
var table = document.getElementById('traffic-recent-table');
var empty = document.getElementById('traffic-recent-empty');
if (!body || !table) return;
rows = rows || [];
if (!rows.length) {
body.innerHTML = '';
table.hidden = true;
if (empty) empty.hidden = false;
return;
}
if (empty) empty.hidden = true;
table.hidden = false;
var html = '';
for (var i = 0; i < rows.length; i++) {
var r = rows[i];
var isBot = !!(r.is_bot | 0);
html +=
'<tr class="' + (isBot ? 'traffic-row--bot' : '') + '">' +
'<td>' + esc(fmtUtc(r.ts, true)) + '</td>' +
'<td>' + esc(r.visitor || 'Unknown visitor') + '</td>' +
'<td>' +
(isBot
? '<span class="type-badge traffic-badge--bot">bot</span>'
: '<span class="type-badge">human</span>') +
'</td>' +
'<td><code>' + esc((r.host || '') + (r.path || '')) + '</code></td>' +
'<td>' + (r.local_count | 0) + '</td>' +
'<td>' + esc(r.lang || '—') + '</td>' +
'<td>' + esc(r.ref_host || '—') + '</td>' +
'<td>' + (!isBot && (r.is_new | 0) ? 'yes' : '') + '</td>' +
'</tr>';
}
body.innerHTML = html;
}
function setLiveStatus(kind, msg) {
var bar = document.getElementById('traffic-live-bar');
var status = document.getElementById('traffic-live-status');
var ago = document.getElementById('traffic-live-ago');
if (bar) {
bar.classList.toggle('traffic-live-bar--err', kind === 'err');
bar.classList.toggle('traffic-live-bar--ok', kind === 'ok');
}
if (status) status.textContent = kind === 'err' ? 'Offline' : 'Live';
if (ago) ago.textContent = msg ? '· ' + msg : '';
}
// Initial paint from server-rendered payload
if (chartRoot && dataEl) {
drawAllCharts(data);
chartRoot.setAttribute('data-ready', '1');
}
// Redraw on resize (debounced)
var resizeT = null;
var lastAnalytics = data;
window.addEventListener('resize', function () {
clearTimeout(resizeT);
resizeT = setTimeout(function () {
if (chartRoot) drawAllCharts(lastAnalytics);
}, 150);
});
// ---- Live polling: numbers + charts + tables ----
var pollHost = liveRoot || chartRoot;
var pollUrl = (pollHost && pollHost.getAttribute('data-poll')) || '/api/traffic-stats.php';
var pollMs = parseInt((pollHost && pollHost.getAttribute('data-poll-ms')) || '2000', 10) || 2000;
pollMs = Math.max(1500, Math.min(30000, pollMs));
var lastRev = '';
var lastOkAt = 0;
var pollTimer = null;
var inFlight = false;
var aborted = false;
function agoLabel(ts) {
if (!ts) return 'waiting…';
var sec = Math.max(0, Math.floor((Date.now() - ts) / 1000));
if (sec < 2) return 'just now';
if (sec < 60) return sec + 's ago';
return Math.floor(sec / 60) + 'm ago';
}
function tickAgo() {
if (lastOkAt) setLiveStatus('ok', 'updated ' + agoLabel(lastOkAt));
}
function applyPayload(payload, force) {
if (!payload || !payload.ok) return;
lastOkAt = Date.now();
setLiveStatus('ok', 'updated ' + agoLabel(lastOkAt));
var changed = force || !payload.rev || payload.rev !== lastRev;
if (payload.rev) lastRev = payload.rev;
if (!changed) return;
// Numbers first so KPIs feel instant
if (payload.summary) updateKpis(payload.summary, { animate: true });
if (payload.analytics) {
lastAnalytics = payload.analytics;
if (dataEl) dataEl.textContent = JSON.stringify(payload.analytics);
if (chartRoot) drawAllCharts(payload.analytics);
}
if (payload.top) updateTopTable(payload.top);
if (payload.recent) updateRecentTable(payload.recent);
}
function pollOnce() {
if (aborted || inFlight || document.hidden) return;
inFlight = true;
var ctrl = typeof AbortController !== 'undefined' ? new AbortController() : null;
var to = setTimeout(function () {
if (ctrl) ctrl.abort();
}, 12000);
fetch(pollUrl + (pollUrl.indexOf('?') >= 0 ? '&' : '?') + '_=' + Date.now(), {
method: 'GET',
credentials: 'same-origin',
cache: 'no-store',
headers: { Accept: 'application/json' },
signal: ctrl ? ctrl.signal : undefined,
})
.then(function (res) {
if (res.status === 401) {
setLiveStatus('err', 'session expired — reload');
aborted = true;
return null;
}
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
})
.then(function (json) {
if (!json) return;
if (!json.ok) throw new Error(json.err || 'bad response');
applyPayload(json, false);
})
.catch(function () {
if (!aborted) setLiveStatus('err', 'retrying…');
})
.finally(function () {
clearTimeout(to);
inFlight = false;
});
}
function schedulePoll() {
clearTimeout(pollTimer);
if (aborted) return;
pollTimer = setTimeout(function () {
pollOnce();
schedulePoll();
}, pollMs);
}
// Immediate first poll so numbers move as soon as hits land
setTimeout(pollOnce, 400);
schedulePoll();
setInterval(tickAgo, 1000);
document.addEventListener('visibilitychange', function () {
if (!document.hidden && !aborted) {
pollOnce();
}
});
})();
})();
|