Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/server/analytics-range.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
10 changes: 7 additions & 3 deletions src/lib/server/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Range, number> = {
'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<Range, number> = { '24h': 300, '7d': 1800, '30d': 7200 };
const BUCKET_S: Record<Range, number> = { '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<Range, { unit: PeriodUnit; n: number }> = {
'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 }
};
Expand Down
4 changes: 3 additions & 1 deletion src/routes/(app)/server/[id]/analytics/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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' }
];
Expand Down