Build OverSnitch dashboard

Full implementation on top of the Next.js scaffold:

- Leaderboard with per-user request count, storage, avg GB/req, and
  optional Tautulli watch stats (plays, watch hours), each with dense
  per-metric rank (#N/total)
- SWR cache on /api/stats (5-min stale, force-refresh via button);
  client-side localStorage seed so the UI is instant on return visits
- Alerting system: content-centric alerts (unfulfilled downloads,
  partial TV downloads, stale pending requests) and user-behavior
  alerts (ghost requester, low watch rate, declined streak)
- Partial TV detection: flags ended series with <90% of episodes on disk
- Alert persistence in data/alerts.json with open/closed state,
  auto-resolve when condition clears, manual close with per-category
  cooldown, and per-alert notes
- Alert detail page rendered as a server component for instant load
- Dark UI with Tailwind v4, severity-colored left borders, summary
  cards with icons, sortable leaderboard table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-12 11:13:57 -04:00
parent ef061ea910
commit f871f86284
25 changed files with 2084 additions and 86 deletions

View File

@@ -1,65 +1,188 @@
import Image from "next/image";
"use client";
import { useEffect, useState, useCallback, useRef, Suspense } from "react";
import { useSearchParams, useRouter } from "next/navigation";
import { DashboardStats } from "@/lib/types";
import SummaryCards from "@/components/SummaryCards";
import LeaderboardTable from "@/components/LeaderboardTable";
import AlertsPanel from "@/components/AlertsPanel";
import RefreshButton from "@/components/RefreshButton";
type Tab = "leaderboard" | "alerts";
const LS_KEY = "oversnitch_stats";
function timeAgo(iso: string): string {
const diff = Date.now() - new Date(iso).getTime();
const mins = Math.floor(diff / 60_000);
if (mins < 1) return "just now";
if (mins < 60) return `${mins}m ago`;
const hrs = Math.floor(mins / 60);
if (hrs < 24) return `${hrs}h ago`;
return new Date(iso).toLocaleDateString();
}
function DashboardContent() {
const searchParams = useSearchParams();
const router = useRouter();
const tab = (searchParams.get("tab") ?? "leaderboard") as Tab;
const [data, setData] = useState<DashboardStats | null>(null);
const [loading, setLoading] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const [error, setError] = useState<string | null>(null);
const didInit = useRef(false);
const load = useCallback(async (force = false) => {
setData((prev) => {
if (prev) setRefreshing(true);
else setLoading(true);
return prev;
});
setError(null);
try {
const res = await fetch(force ? "/api/stats?force=1" : "/api/stats");
const json = await res.json();
if (!res.ok) throw new Error(json.error ?? `HTTP ${res.status}`);
const stats = json as DashboardStats;
setData(stats);
try { localStorage.setItem(LS_KEY, JSON.stringify(stats)); } catch {}
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
} finally {
setLoading(false);
setRefreshing(false);
}
}, []);
useEffect(() => {
if (didInit.current) return;
didInit.current = true;
try {
const raw = localStorage.getItem(LS_KEY);
if (raw) setData(JSON.parse(raw) as DashboardStats);
} catch {}
load();
}, [load]);
function setTab(t: Tab) {
const params = new URLSearchParams(searchParams.toString());
params.set("tab", t);
router.push(`?${params.toString()}`, { scroll: false });
}
const hasTautulli = data?.summary.totalWatchHours !== null;
const openAlertCount = data?.summary.openAlertCount ?? 0;
const generatedAt = data?.generatedAt ?? null;
export default function Home() {
return (
<div className="flex flex-col flex-1 items-center justify-center bg-zinc-50 font-sans dark:bg-black">
<main className="flex flex-1 w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={100}
height={20}
priority
/>
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
To get started, edit the page.tsx file.
<main className="mx-auto max-w-6xl px-4 py-8 space-y-6">
{/* Header */}
<div className="flex items-start justify-between gap-4">
<div>
<h1 className="text-3xl font-bold tracking-tight">
Over<span className="text-yellow-400">Snitch</span>
</h1>
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
Looking for a starting point or more instructions? Head over to{" "}
<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="font-medium text-zinc-950 dark:text-zinc-50"
>
Templates
</a>{" "}
or the{" "}
<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="font-medium text-zinc-950 dark:text-zinc-50"
>
Learning
</a>{" "}
center.
</p>
<p className="text-slate-400 text-sm mt-1.5">Request &amp; usage analytics</p>
</div>
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
<a
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="/vercel.svg"
alt="Vercel logomark"
width={16}
height={16}
/>
Deploy Now
</a>
<a
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
<div className="flex flex-col items-end gap-1.5 shrink-0">
<RefreshButton onRefresh={() => load(true)} loading={refreshing || loading} />
{generatedAt && (
<span className="text-xs text-slate-600">
{refreshing
? <span className="text-yellow-600/80">Refreshing</span>
: <>Updated {timeAgo(generatedAt)}</>
}
</span>
)}
</div>
</main>
</div>
</div>
{/* First-ever load spinner */}
{loading && !data && (
<div className="flex flex-col items-center justify-center py-24 gap-4">
<svg
className="animate-spin h-8 w-8 text-yellow-400"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8H4z" />
</svg>
<div className="text-center space-y-1">
<p className="text-slate-300 text-sm font-medium">Fetching data</p>
<p className="text-slate-600 text-xs">This only takes a moment on first load.</p>
</div>
</div>
)}
{/* Error */}
{error && (
<div className="rounded-xl border border-red-800 bg-red-950/40 px-5 py-4 text-sm">
<span className="font-semibold text-red-400">Error: </span>
<span className="text-red-300">{error}</span>
</div>
)}
{data && (
<>
<SummaryCards
totalUsers={data.summary.totalUsers}
totalRequests={data.summary.totalRequests}
totalStorageGB={data.summary.totalStorageGB}
totalWatchHours={data.summary.totalWatchHours}
openAlertCount={data.summary.openAlertCount}
onAlertsClick={() => setTab("alerts")}
/>
{/* Tab bar */}
<div className="border-b border-slate-700/60">
<div className="flex gap-1 -mb-px">
{(["leaderboard", "alerts"] as const).map((t) => (
<button
key={t}
onClick={() => setTab(t)}
className={`px-4 py-2.5 text-sm font-medium border-b-2 transition-colors ${
tab === t
? "border-yellow-400 text-white"
: "border-transparent text-slate-500 hover:text-slate-300"
}`}
>
{t === "alerts" ? (
<span className="flex items-center gap-2">
Alerts
{openAlertCount > 0 && (
<span className="rounded-full bg-yellow-500 text-black text-xs font-bold px-1.5 py-0.5 leading-none">
{openAlertCount}
</span>
)}
</span>
) : (
"Leaderboard"
)}
</button>
))}
</div>
</div>
{tab === "leaderboard" && (
<LeaderboardTable users={data.users} hasTautulli={hasTautulli} />
)}
{tab === "alerts" && <AlertsPanel />}
</>
)}
</main>
);
}
export default function Page() {
return (
<Suspense>
<DashboardContent />
</Suspense>
);
}