import { overviewStats, usageSeries, usageByModel, topTools, recentSessions, hourOfDayActivity, cacheEfficiency, usageGauges, type Bucket, type TimeWindow } from '$lib/server/queries'; import { resolveRange } from '$lib/server/range'; import { getCostByProject } from '$lib/server/stats/byProject'; import { getGaugeHistory } from '$lib/server/stats/gaugeHistory'; import { getWebUsage } from '$lib/server/stats/webUsage'; import { getToolErrors } from '$lib/server/stats/toolErrors'; import { getTopActivity } from '$lib/server/stats/topActivity'; import { getLatencyTrends } from '$lib/server/stats/latency'; import { getActivityCalendar } from '$lib/server/stats/calendar'; import { getPunchcard } from '$lib/server/stats/punchcard'; import type { PageServerLoad } from './$types'; const TOP_TOOLS_LIMIT = 10; const RECENT_SESSIONS_LIMIT = 7; // dashboard shows a short preview; full list lives at /sessions const DAY_MS = 86_400_000; export const load: PageServerLoad = async ({ url }) => { const range = resolveRange(url); const window: TimeWindow = { since: range.since, until: range.until }; // usageSeries needs concrete bounds; fall back to a 30-day span if the DB is empty. const seriesSince = range.since ?? new Date(Date.now() - 30 * DAY_MS).toISOString(); const bucket: Bucket = range.bucket; const byModel = usageByModel(window); return { range, overview: overviewStats(window), series: usageSeries(seriesSince, range.until, bucket), modelSet: byModel.map((m) => m.model), // stacking order (cost desc) byModel, hourly: hourOfDayActivity(window), cache: cacheEfficiency(window), topTools: topTools(TOP_TOOLS_LIMIT, window), recentSessions: recentSessions(RECENT_SESSIONS_LIMIT, window), gauges: usageGauges(), projects: getCostByProject(window), gaugeHistory: getGaugeHistory(window), webUsage: getWebUsage(window), toolErrors: getToolErrors(window), topActivity: getTopActivity(window), speed: getLatencyTrends(window), activityCalendar: getActivityCalendar(), punchcard: getPunchcard(window) }; };