diff --git a/src/lib/server/analytics-range.test.ts b/src/lib/server/analytics-range.test.ts new file mode 100644 index 00000000..59f3c8e8 --- /dev/null +++ b/src/lib/server/analytics-range.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'bun:test'; +import { parseRange } from './analytics'; + +test('analytics accepts the two and three day ranges while retaining the fallback', () => { + expect(parseRange('48h')).toBe('48h'); + expect(parseRange('3d')).toBe('3d'); + expect(parseRange('7d')).toBe('7d'); + expect(parseRange('unknown')).toBe('24h'); +}); diff --git a/src/lib/server/analytics.ts b/src/lib/server/analytics.ts index beda9cbe..492bff8d 100644 --- a/src/lib/server/analytics.ts +++ b/src/lib/server/analytics.ts @@ -13,23 +13,27 @@ import { liveFactions } from './matches'; import { MAX_COVER_S } from './rollups'; -export type Range = '24h' | '7d' | '30d'; +export type Range = '24h' | '48h' | '3d' | '7d' | '30d'; const RANGE_MS: Record = { '24h': 86400000, + '48h': 2 * 86400000, + '3d': 3 * 86400000, '7d': 7 * 86400000, '30d': 30 * 86400000 }; /** Bucket width per range so a chart gets roughly 300 points. */ -const BUCKET_S: Record = { '24h': 300, '7d': 1800, '30d': 7200 }; +const BUCKET_S: Record = { '24h': 300, '48h': 600, '3d': 900, '7d': 1800, '30d': 7200 }; /** Ranges longer than this read the hourly rollups (the 30-day charts); shorter ones read raw rows. */ const ROLLED_BEYOND_MS = 14 * 86400_000; -export const parseRange = (v: string | null): Range => (v === '7d' || v === '30d' ? v : '24h'); +export const parseRange = (v: string | null): Range => (v === '48h' || v === '3d' || v === '7d' || v === '30d' ? v : '24h'); export type PeriodUnit = 'hour' | 'day'; /** The periods a range is cut into for the per-period charts: hours over a day, days beyond. */ const PERIODS: Record = { '24h': { unit: 'hour', n: 24 }, + '48h': { unit: 'hour', n: 48 }, + '3d': { unit: 'day', n: 3 }, '7d': { unit: 'day', n: 7 }, '30d': { unit: 'day', n: 30 } }; diff --git a/src/routes/(app)/server/[id]/analytics/+page.svelte b/src/routes/(app)/server/[id]/analytics/+page.svelte index 759e9a04..4dfeb2d1 100644 --- a/src/routes/(app)/server/[id]/analytics/+page.svelte +++ b/src/routes/(app)/server/[id]/analytics/+page.svelte @@ -110,7 +110,7 @@ range; if (perPeriod) return poll(loadPeriods, 60000); }); - let periodWord = $derived(range === '24h' ? 'hour' : 'day'); + let periodWord = $derived(range === '24h' || range === '48h' ? 'hour' : 'day'); const SHOWN = [ ['online', 'Players online'], ['players', 'Players per'], @@ -149,6 +149,8 @@ const RANGES: { key: Range; label: string }[] = [ { key: '24h', label: '24 hours' }, + { key: '48h', label: '48 hours' }, + { key: '3d', label: '3 days' }, { key: '7d', label: '7 days' }, { key: '30d', label: '30 days' } ];