Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

src/visitor-counts.ts keeps today's peak viewers and joins per hour for the last 24 h from counts alone - module and test only #375

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/373-src-visitor-counts-ts-keeps-today-s-peak → main 1 commit fork 1 thread · 1 unresolved

  • Merged. smith/373-src-visitor-counts-ts-keeps-today-s-peak was merged into main as 1f96924.

Review

Sign in to review this change.
2 files changed +160 −0
Software/so101/src/visitor-counts.test.ts added +64
@@ -0,0 +1,64 @@
1 import { expect, test } from 'bun:test'
2 import { VisitorCounts } from './visitor-counts'
3
4 const at = (y: number, mo: number, d: number, h = 0, mi = 0) => new Date(y, mo - 1, d, h, mi).getTime()
5
6 test('peak follows the most at once and resets at local midnight', () => {
7 let t = at(2026, 10, 20, 22)
8 const c = new VisitorCounts(() => t)
9 c.joined(); c.joined(); c.joined()
10 c.left(); c.left()
11 c.joined()
12 expect(c.peakToday()).toBe(3)
13 t = at(2026, 10, 21, 0, 1)
14 expect(c.peakToday()).toBe(2)
15 c.left(); c.left(); c.left()
16 expect(c.peakToday()).toBe(2)
17 c.joined()
18 expect(c.peakToday()).toBe(2)
19 })
20
21 test('joins land in the right hour and roll off after 24 h', () => {
22 let t = at(2026, 10, 21, 10, 5)
23 const c = new VisitorCounts(() => t)
24 c.joined(); c.joined()
25 t += 3_600_000
26 c.joined()
27 let h = c.joinsPerHour()
28 expect(h.length).toBe(24)
29 expect(h[23]).toBe(1)
30 expect(h[22]).toBe(2)
31 t += 22 * 3_600_000
32 h = c.joinsPerHour()
33 expect(h[0]).toBe(2)
34 expect(h[1]).toBe(1)
35 t += 3_600_000
36 expect(c.joinsPerHour()[0]).toBe(1)
37 t += 3_600_000
38 expect(c.joinsPerHour().reduce((a, b) => a + b, 0)).toBe(0)
39 })
40
41 test('left never goes below zero', () => {
42 const c = new VisitorCounts(() => at(2026, 10, 21, 12))
43 c.left()
44 c.joined()
45 expect(c.peakToday()).toBe(1)
46 })
47
48 test('restore round-trips and ignores malformed input', () => {
49 const t = at(2026, 10, 21, 12)
50 const a = new VisitorCounts(() => t)
51 a.joined(); a.joined(); a.left()
52 const json = JSON.parse(JSON.stringify(a))
53 const b = new VisitorCounts(() => t)
54 b.restore(json)
55 expect(b.peakToday()).toBe(2)
56 expect(b.joinsPerHour()).toEqual(a.joinsPerHour())
57 expect(b.toJSON()).toEqual(a.toJSON())
58
59 const c = new VisitorCounts(() => t)
60 for (const bad of [null, 5, 'x', [], { peak: -1, hours: 'x' }, { peak: 1.5, here: NaN, hours: { a: 1, '5': -2 } }])
61 c.restore(bad)
62 expect(c.peakToday()).toBe(0)
63 expect(c.joinsPerHour().every(n => n === 0)).toBe(true)
64 })
Software/so101/src/visitor-counts.ts added +96
@@ -0,0 +1,96 @@
1 // Counts only: how many are here, today's peak, joins per hour. No names, ids or addresses.
2
3 const HOUR = 3_600_000
4 const HOURS = 24
5
6 export type VisitorCountsState = {
7 here: number
8 peak: number
9 day: string
10 hours: Record<string, number>
11 }
12
13 const count = (v: unknown): v is number => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0
14
15 export class VisitorCounts {
16 private here = 0
17 private peak = 0
18 private day: string
19 private hours = new Map<number, number>()
20
21 constructor(private now: () => number = Date.now) {
22 this.day = this.dayKey()
23 }
24
25 private dayKey(): string {
26 const d = new Date(this.now())
27 return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
28 }
29
30 private hourIndex(): number {
31 return Math.floor(this.now() / HOUR)
32 }
33
34 private roll(): void {
35 const day = this.dayKey()
36 if (day !== this.day) {
37 this.day = day
38 this.peak = this.here
39 }
40 const oldest = this.hourIndex() - (HOURS - 1)
41 for (const h of this.hours.keys()) if (h < oldest) this.hours.delete(h)
42 }
43
44 joined(): void {
45 this.roll()
46 this.here++
47 if (this.here > this.peak) this.peak = this.here
48 const h = this.hourIndex()
49 this.hours.set(h, (this.hours.get(h) ?? 0) + 1)
50 }
51
52 left(): void {
53 this.roll()
54 if (this.here > 0) this.here--
55 }
56
57 peakToday(): number {
58 this.roll()
59 return this.peak
60 }
61
62 /** 24 numbers, oldest first; the last is the current hour. */
63 joinsPerHour(): number[] {
64 this.roll()
65 const last = this.hourIndex()
66 return Array.from({ length: HOURS }, (_, i) => this.hours.get(last - (HOURS - 1) + i) ?? 0)
67 }
68
69 toJSON(): VisitorCountsState {
70 this.roll()
71 return {
72 here: this.here,
73 peak: this.peak,
74 day: this.day,
75 hours: Object.fromEntries([...this.hours].map(([h, n]) => [String(h), n])),
76 }
77 }
78
79 /** Restore from toJSON output; anything malformed is ignored. */
80 restore(data: unknown): void {
81 if (typeof data !== 'object' || data === null) return
82 const s = data as Record<string, unknown>
83 if (count(s.here)) this.here = s.here
84 if (count(s.peak)) this.peak = s.peak
85 if (typeof s.day === 'string' && s.day.length <= 16) this.day = s.day
86 if (typeof s.hours === 'object' && s.hours !== null) {
87 this.hours.clear()
88 for (const [k, n] of Object.entries(s.hours)) {
89 const h = Number(k)
90 if (Number.isSafeInteger(h) && count(n)) this.hours.set(h, n)
91 }
92 }

Future-dated hour keys survive restore, because roll() prunes only keys older than the window. A bad or clock-skewed state could keep unbounded entries. Fix: also skip h > hourIndex() while restoring.

93 if (this.peak < this.here) this.peak = this.here
94 this.roll()
95 }
96 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs