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

@@ -0,0 +1,178 @@
"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { Alert, AlertSeverity } from "@/lib/types";
const severityIcon: Record<AlertSeverity, string> = {
danger:
"M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z",
warning:
"M12 9v3.75m9.303 3.376c.866 1.5-.217 3.374-1.948 3.374H2.645c-1.73 0-2.813-1.874-1.948-3.374l7.658-13.25c.866-1.5 3.032-1.5 3.898 0zM12 15.75h.007v.008H12v-.008z",
info: "m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z",
};
const severityColor: Record<AlertSeverity, string> = {
danger: "text-red-400",
warning: "text-yellow-400",
info: "text-blue-400",
};
function timeAgo(iso: string): string {
const diff = Date.now() - new Date(iso).getTime();
const mins = Math.floor(diff / 60_000);
if (mins < 60) return `${mins}m ago`;
const hrs = Math.floor(mins / 60);
if (hrs < 24) return `${hrs}h ago`;
const days = Math.floor(hrs / 24);
return `${days}d ago`;
}
export default function AlertsPanel() {
const [alerts, setAlerts] = useState<Alert[]>([]);
const [loading, setLoading] = useState(true);
const [filter, setFilter] = useState<"open" | "all">("open");
useEffect(() => {
fetch("/api/alerts")
.then((r) => r.json())
.then(setAlerts)
.finally(() => setLoading(false));
}, []);
const filtered =
filter === "open" ? alerts.filter((a) => a.status === "open") : alerts;
const openCount = alerts.filter((a) => a.status === "open").length;
if (loading) {
return (
<div className="text-slate-600 text-sm py-12 text-center">
Loading alerts
</div>
);
}
return (
<div className="space-y-4">
{/* Toolbar */}
<div className="flex items-center gap-3 flex-wrap">
<div className="flex rounded-lg overflow-hidden border border-slate-700 text-xs font-medium">
{(["open", "all"] as const).map((f) => (
<button
key={f}
onClick={() => setFilter(f)}
className={`px-3 py-1.5 transition-colors ${
filter === f
? "bg-slate-700 text-white"
: "bg-transparent text-slate-500 hover:text-slate-300"
}`}
>
{f === "open" ? `Open (${openCount})` : `All (${alerts.length})`}
</button>
))}
</div>
<span className="text-xs text-slate-600">
Click an alert to view details, add notes, or close it.
</span>
</div>
{filtered.length === 0 && (
<div className="rounded-xl border border-slate-700/60 bg-slate-800/30 p-12 text-center">
<p className="text-slate-500 text-sm">
{filter === "open"
? "No open alerts — everything looks healthy."
: "No alerts recorded yet."}
</p>
</div>
)}
{/* Alert rows */}
<div className="space-y-2">
{filtered.map((alert) => (
<Link
key={alert.id}
href={`/alerts/${alert.id}`}
className={`flex items-start gap-3 rounded-xl border p-4 transition-colors ${
alert.status === "closed"
? "border-slate-700/40 bg-slate-800/20 opacity-50 hover:opacity-70"
: "border-slate-700/60 bg-slate-800/60 hover:bg-slate-800"
}`}
>
{/* Severity icon */}
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className={`h-5 w-5 mt-0.5 shrink-0 ${severityColor[alert.severity]}`}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d={severityIcon[alert.severity]}
/>
</svg>
{/* Content */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-semibold text-white text-sm leading-snug">
{alert.title}
</span>
{alert.status === "closed" && (
<span
className={`text-xs rounded-full px-2 py-0.5 font-medium ${
alert.closeReason === "resolved"
? "bg-teal-950/60 text-teal-500 border border-teal-800/60"
: "bg-slate-700/60 text-slate-500 border border-slate-600/60"
}`}
>
{alert.closeReason === "resolved" ? "Auto-resolved" : "Closed"}
</span>
)}
{alert.comments.length > 0 && (
<span className="inline-flex items-center gap-1 text-xs text-slate-600">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="h-3.5 w-3.5"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 0 1 1.037-.443 48.282 48.282 0 0 0 5.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0 0 12 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018Z"
/>
</svg>
{alert.comments.length}
</span>
)}
</div>
<p className="text-sm text-slate-500 mt-0.5 line-clamp-2 leading-relaxed">
{alert.description}
</p>
</div>
{/* Time + chevron */}
<div className="flex items-center gap-1 shrink-0 mt-0.5">
<span className="text-xs text-slate-700">{timeAgo(alert.status === "open" ? alert.firstSeen : (alert.closedAt ?? alert.firstSeen))}</span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={2}
stroke="currentColor"
className="h-3.5 w-3.5 text-slate-700"
>
<path strokeLinecap="round" strokeLinejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
</svg>
</div>
</Link>
))}
</div>
</div>
);
}

View File

@@ -0,0 +1,189 @@
"use client";
import { useState } from "react";
import { UserStat } from "@/lib/types";
type SortKey = "totalBytes" | "requestCount" | "avgGB" | "plays" | "watchHours";
interface LeaderboardTableProps {
users: UserStat[];
hasTautulli: boolean;
}
function formatGB(gb: number): string {
return gb >= 1000 ? `${(gb / 1000).toFixed(2)} TB` : `${gb.toFixed(1)} GB`;
}
function formatHours(h: number): string {
if (h >= 1000) return `${(h / 1000).toFixed(1)}k h`;
return `${h.toFixed(0)}h`;
}
function Rank({ rank, total }: { rank: number | null; total: number }) {
if (rank === null) return <span className="text-slate-700"></span>;
return (
<span className="text-xs font-mono text-slate-500">
#{rank}<span className="text-slate-700">/{total}</span>
</span>
);
}
function SortChevrons({ active, asc }: { active: boolean; asc: boolean }) {
return (
<span className="ml-1 inline-flex flex-col gap-px opacity-0 group-hover:opacity-100 transition-opacity">
{active ? (
<svg
className="h-3 w-3 text-yellow-400"
viewBox="0 0 12 12"
fill="currentColor"
>
{asc
? <path d="M6 2 L10 8 L2 8 Z" />
: <path d="M6 10 L10 4 L2 4 Z" />
}
</svg>
) : (
<svg className="h-3 w-3 text-slate-600" viewBox="0 0 12 12" fill="currentColor">
<path d="M6 1 L9 5 L3 5 Z M6 11 L9 7 L3 7 Z" />
</svg>
)}
</span>
);
}
export default function LeaderboardTable({
users,
hasTautulli,
}: LeaderboardTableProps) {
const [sortKey, setSortKey] = useState<SortKey>("totalBytes");
const [sortAsc, setSortAsc] = useState(false);
const sorted = [...users].sort((a, b) => {
const av = (a[sortKey] ?? -1) as number;
const bv = (b[sortKey] ?? -1) as number;
return sortAsc ? av - bv : bv - av;
});
function handleSort(key: SortKey) {
if (key === sortKey) setSortAsc((p) => !p);
else { setSortKey(key); setSortAsc(false); }
}
function Th({
label,
col,
right,
}: {
label: string;
col?: SortKey;
right?: boolean;
}) {
const active = col === sortKey;
return (
<th
onClick={col ? () => handleSort(col) : undefined}
className={[
"group py-3 px-4 text-xs font-semibold uppercase tracking-wider whitespace-nowrap select-none",
right ? "text-right" : "text-left",
col ? "cursor-pointer" : "",
active ? "text-yellow-400" : "text-slate-500 hover:text-slate-300",
].join(" ")}
>
{label}
{col && <SortChevrons active={active} asc={sortAsc} />}
</th>
);
}
const total = users.length;
return (
<div className="overflow-x-auto rounded-xl border border-slate-700/60">
<table className="w-full text-sm">
<thead className="bg-slate-800/80 border-b border-slate-700/60">
<tr>
<Th label="#" />
<Th label="User" />
<Th label="Requests" col="requestCount" right />
<Th label="Storage" col="totalBytes" right />
<Th label="Avg / Req" col="avgGB" right />
{hasTautulli && <Th label="Plays" col="plays" right />}
{hasTautulli && <Th label="Watch Time" col="watchHours" right />}
</tr>
</thead>
<tbody className="divide-y divide-slate-700/30">
{sorted.map((user, idx) => (
<tr
key={user.userId}
className="bg-slate-900 hover:bg-slate-800/50 transition-colors"
>
{/* Row index */}
<td className="py-3 px-4 text-slate-700 font-mono text-xs w-10 tabular-nums">
{idx + 1}
</td>
{/* User */}
<td className="py-3 px-4">
<div className="font-medium text-white leading-snug">{user.displayName}</div>
<div className="text-xs text-slate-600 mt-0.5">{user.email}</div>
</td>
{/* Requests */}
<td className="py-3 px-4 text-right">
<div className="text-slate-300 font-mono tabular-nums">
{user.requestCount.toLocaleString()}
</div>
<div className="mt-0.5 flex justify-end">
<Rank rank={user.requestRank} total={total} />
</div>
</td>
{/* Storage */}
<td className="py-3 px-4 text-right">
<div className="text-white font-semibold font-mono tabular-nums">
{formatGB(user.totalGB)}
</div>
<div className="mt-0.5 flex justify-end">
<Rank rank={user.storageRank} total={total} />
</div>
</td>
{/* Avg / Request */}
<td className="py-3 px-4 text-right text-slate-500 font-mono text-xs tabular-nums">
{user.requestCount > 0 ? formatGB(user.avgGB) : "—"}
</td>
{/* Plays */}
{hasTautulli && (
<td className="py-3 px-4 text-right">
<div className="text-slate-300 font-mono tabular-nums">
{user.plays !== null ? user.plays.toLocaleString() : "—"}
</div>
<div className="mt-0.5 flex justify-end">
<Rank rank={user.playsRank} total={total} />
</div>
</td>
)}
{/* Watch Time */}
{hasTautulli && (
<td className="py-3 px-4 text-right">
<div className="text-slate-500 font-mono text-xs tabular-nums">
{user.watchHours !== null && user.watchHours > 0
? formatHours(user.watchHours)
: user.watchHours === 0
? "0h"
: "—"}
</div>
<div className="mt-0.5 flex justify-end">
<Rank rank={user.watchRank} total={total} />
</div>
</td>
)}
</tr>
))}
</tbody>
</table>
</div>
);
}

View File

@@ -0,0 +1,23 @@
interface PercentileBadgeProps {
label: string;
}
const colorMap: Record<string, string> = {
"Top 1%": "bg-yellow-400 text-black",
"Top 5%": "bg-yellow-500 text-black",
"Top 10%": "bg-green-500 text-white",
"Top 25%": "bg-green-700 text-white",
"Top 50%": "bg-blue-600 text-white",
"Bottom 50%": "bg-slate-600 text-slate-300",
};
export default function PercentileBadge({ label }: PercentileBadgeProps) {
const colorClass = colorMap[label] ?? "bg-slate-600 text-slate-300";
return (
<span
className={`inline-block rounded-full px-2.5 py-0.5 text-xs font-semibold ${colorClass}`}
>
{label}
</span>
);
}

View File

@@ -0,0 +1,32 @@
"use client";
interface RefreshButtonProps {
onRefresh: () => void;
loading: boolean;
}
export default function RefreshButton({ onRefresh, loading }: RefreshButtonProps) {
return (
<button
onClick={onRefresh}
disabled={loading}
className="flex items-center gap-2 rounded-lg bg-slate-800 hover:bg-slate-700 border border-slate-700 hover:border-slate-600 disabled:opacity-40 disabled:cursor-not-allowed px-3.5 py-1.5 text-sm font-medium text-slate-300 hover:text-white transition-colors"
>
<svg
className={`h-3.5 w-3.5 ${loading ? "animate-spin" : ""}`}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
{loading ? "Loading…" : "Refresh"}
</button>
);
}

View File

@@ -0,0 +1,123 @@
interface SummaryCardsProps {
totalUsers: number;
totalRequests: number;
totalStorageGB: number;
totalWatchHours: number | null;
openAlertCount: number;
onAlertsClick?: () => void;
}
function formatStorage(gb: number): string {
if (gb >= 1000) return `${(gb / 1000).toFixed(1)} TB`;
return `${gb.toFixed(1)} GB`;
}
function formatHours(h: number): string {
if (h >= 1000) return `${(h / 1000).toFixed(1)}k hrs`;
return `${h.toFixed(0)} hrs`;
}
// Heroicons outline paths
const ICONS = {
users: "M15 19.128a9.38 9.38 0 0 0 2.625.372 9.337 9.337 0 0 0 4.121-.952 4.125 4.125 0 0 0-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 0 1 8.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0 1 11.964-3.07M12 6.375a3.375 3.375 0 1 1-6.75 0 3.375 3.375 0 0 1 6.75 0Zm8.25 2.25a2.625 2.625 0 1 1-5.25 0 2.625 2.625 0 0 1 5.25 0Z",
requests: "M3.75 9.776c.112-.017.227-.026.344-.026h15.812c.117 0 .232.009.344.026m-16.5 0a2.25 2.25 0 0 0-1.883 2.542l.857 6a2.25 2.25 0 0 0 2.227 1.932H19.05a2.25 2.25 0 0 0 2.227-1.932l.857-6a2.25 2.25 0 0 0-1.883-2.542m-16.5 0V6A2.25 2.25 0 0 1 6 3.75h3.879a1.5 1.5 0 0 1 1.06.44l2.122 2.12a1.5 1.5 0 0 0 1.06.44H18A2.25 2.25 0 0 1 20.25 9v.776",
storage: "M20.25 6.375c0 2.278-3.694 4.125-8.25 4.125S3.75 8.653 3.75 6.375m16.5 0c0-2.278-3.694-4.125-8.25-4.125S3.75 4.097 3.75 6.375m16.5 0v11.25c0 2.278-3.694 4.125-8.25 4.125s-8.25-1.847-8.25-4.125V6.375m16.5 2.625c0 2.278-3.694 4.125-8.25 4.125s-8.25-1.847-8.25-4.125m16.5 2.625c0 2.278-3.694 4.125-8.25 4.125s-8.25-1.847-8.25-4.125",
watch: "M3.375 19.5h17.25m-17.25 0a1.125 1.125 0 0 1-1.125-1.125M3.375 19.5h1.5C5.496 19.5 6 18.996 6 18.375m-3.75.125v-6.375c0-.621.504-1.125 1.125-1.125H4.5m0-3.375v3.375m0-3.375h12M4.5 7.5H18a.75.75 0 0 1 .75.75v9.375c0 .621-.504 1.125-1.125 1.125h-1.5m-13.5 0h13.5m0 0v-3.375m0 3.375v-9.375c0-.621.504-1.125 1.125-1.125H21",
alert: "M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z",
};
function Card({
label,
value,
sub,
icon,
accent,
onClick,
}: {
label: string;
value: string;
sub?: string;
icon: keyof typeof ICONS;
accent?: "yellow" | "green";
onClick?: () => void;
}) {
const base = "rounded-xl border p-5 flex flex-col gap-2 transition-colors";
const interactive = onClick ? "cursor-pointer" : "";
let colorClass: string;
if (accent === "yellow") {
colorClass = "bg-yellow-950/30 border-yellow-700/50 hover:bg-yellow-950/50";
} else if (accent === "green") {
colorClass = "bg-green-950/30 border-green-800/50";
} else {
colorClass = `bg-slate-800/60 border-slate-700/60 ${onClick ? "hover:bg-slate-800 hover:border-slate-600" : ""}`;
}
return (
<div onClick={onClick} className={`${base} ${colorClass} ${interactive}`}>
<div className="flex items-center justify-between">
<span className="text-xs font-semibold uppercase tracking-widest text-slate-500">
{label}
</span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className={`h-4 w-4 ${
accent === "yellow"
? "text-yellow-500/70"
: accent === "green"
? "text-green-500/70"
: "text-slate-600"
}`}
>
<path strokeLinecap="round" strokeLinejoin="round" d={ICONS[icon]} />
</svg>
</div>
<span className={`text-3xl font-bold tabular-nums ${accent === "yellow" ? "text-yellow-300" : "text-white"}`}>
{value}
</span>
{sub && (
<span className={`text-xs ${accent === "yellow" ? "text-yellow-600" : "text-slate-600"}`}>
{sub}
</span>
)}
</div>
);
}
export default function SummaryCards({
totalUsers,
totalRequests,
totalStorageGB,
totalWatchHours,
openAlertCount,
onAlertsClick,
}: SummaryCardsProps) {
const colCount = (totalWatchHours !== null ? 1 : 0) + 4;
return (
<div
className={`grid grid-cols-2 gap-3 ${
colCount >= 5 ? "sm:grid-cols-5" : "sm:grid-cols-4"
}`}
>
<Card label="Users" value={totalUsers.toLocaleString()} icon="users" />
<Card label="Requests" value={totalRequests.toLocaleString()} icon="requests" />
<Card label="Storage" value={formatStorage(totalStorageGB)} icon="storage" />
{totalWatchHours !== null && (
<Card label="Watch Time" value={formatHours(totalWatchHours)} icon="watch" accent="green" />
)}
<Card
label="Open Alerts"
value={openAlertCount.toString()}
sub={openAlertCount > 0 ? "Tap to review" : "All clear"}
icon="alert"
accent={openAlertCount > 0 ? "yellow" : undefined}
onClick={openAlertCount > 0 ? onAlertsClick : undefined}
/>
</div>
);
}