Files
Homelab-manager/web/src/pages/Jellyfin.tsx
T
bobbanandClaude Sonnet 5.5 0146eb8bca Jellyfin: who is watching, and don't update a machine mid-film
New Jellyfin integration (read-only, every 2 minutes): version, restart wanted,
streams and transcoding, failed scheduled tasks, item counts, failed sign-ins.
Monitoring -> Jellyfin page with a peak-streams history, three alerts, /metrics.

When Jellyfin runs on a managed server, an update run asks first if people are
streaming (409 "watching" unless startAnyway), and a patch plan waits up to two
hours for them before updating that server.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 22:01:18 +02:00

200 lines
8.6 KiB
TypeScript

import { useEffect, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import { api, type JellyfinHistory, type JellyfinInfo } from "../api/client";
import EmptyState from "../components/EmptyState";
import RangeSelect from "../components/RangeSelect";
import TimeAgo from "../components/TimeAgo";
import TimeChart from "../components/TimeChart";
import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { useChartRange } from "../utils/chartRanges";
import { readableError } from "../utils/errors";
function Badge({ j }: { j: JellyfinInfo }) {
if (!j.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
if (!j.state) return <span className="badge bg-secondary-lt text-secondary">Not read yet</span>;
if (!j.fresh) return <span className="badge bg-yellow-lt text-yellow">Can&rsquo;t be read</span>;
if (j.problems.some((p) => p.severity === "critical")) return <span className="badge bg-red-lt text-red">Needs a look</span>;
if (j.problems.length > 0) return <span className="badge bg-yellow-lt text-yellow">Needs a look</span>;
return <span className="badge bg-green-lt text-green">Up</span>;
}
const count = (n: number | null, one: string, many: string) => (n === null ? null : `${n.toLocaleString()} ${n === 1 ? one : many}`);
function History({ id, name }: { id: number; name: string }) {
const [days, setDays] = useChartRange(7);
const shown = days > 30 ? 30 : days;
const [history, setHistory] = useState<JellyfinHistory | null>(null);
const [error, setError] = useState<string | null>(null);
const [to, setTo] = useState(() => Date.now());
useEffect(() => {
let alive = true;
api.jellyfin
.history(id, shown)
.then((res) => {
if (!alive) return;
setHistory(res);
setTo(Date.now());
setError(null);
})
.catch((err) => alive && setError(readableError(err)));
return () => {
alive = false;
};
}, [id, shown]);
const from = to - shown * 86_400_000;
const streams = useMemo(
() => [
{ name: "Streaming", color: "#4299e1", points: (history?.points ?? []).map((p) => ({ t: p.t, v: p.streams })) },
{ name: "Transcoded", color: "#f76707", dash: "4 3", points: (history?.points ?? []).map((p) => ({ t: p.t, v: p.transcodes })) },
],
[history],
);
return (
<div>
<div className="d-flex align-items-center mb-2">
<h4 className="mb-0">Who was watching</h4>
<div className="ms-auto">
<RangeSelect value={shown} onChange={setDays} only={[1, 7, 30]} />
</div>
</div>
<ErrorNotice error={error} />
{!history && !error && <BlockSkeleton lines={3} />}
{history && <TimeChart series={streams} from={from} to={to} unit="value" compact label={`${name}: streams at the busiest`} empty="Nothing read in this stretch yet." format={(v) => `${Math.round(v)}`} />}
<div className="text-secondary small mt-1">The most that were streaming at once in each stretch — an average would hide the evening somebody watched.</div>
</div>
);
}
function Card({ j }: { j: JellyfinInfo }) {
const s = j.state;
const failed = (s?.tasks ?? []).filter((t) => t.lastStatus === "Failed");
const libraries = s ? [count(s.counts.movies, "film", "films"), count(s.counts.series, "series", "series"), count(s.counts.episodes, "episode", "episodes"), count(s.counts.songs, "song", "songs")].filter(Boolean) : [];
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">
{j.name} <Badge j={j} />
</h3>
<div className="card-actions text-secondary small">
{j.runsOn && (
<>
runs on <Link to={`/servers/${j.runsOn.id}`}>{j.runsOn.name}</Link> ·{" "}
</>
)}
{j.status ? <>read <TimeAgo value={j.status.at} /></> : "not read yet"}
</div>
</div>
<div className="card-body">
{j.status && !j.status.ok && <div className="text-yellow mb-2">The last attempt to read it failed: {j.status.error ?? "no answer"}.</div>}
{j.problems.length > 0 && (
<ul className="mb-3">
{j.problems.map((p) => (
<li key={p.key} className={p.severity === "critical" ? "text-red" : ""}>
{p.message}
</li>
))}
</ul>
)}
{s && (
<>
<div className="text-secondary small mb-3">
{[s.serverName && s.serverName !== j.name ? s.serverName : null, s.version ? `version ${s.version}` : null, s.os, s.updateAvailable ? "an update is available" : null].filter(Boolean).join(" · ")}
{libraries.length > 0 && <div>{libraries.join(" · ")}</div>}
</div>
<h4>Streaming now</h4>
{s.streams.length === 0 ? (
<div className="text-secondary mb-3">Nobody.</div>
) : (
<div className="table-responsive mb-3">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Who</th>
<th>Watching</th>
<th>On</th>
<th>How</th>
</tr>
</thead>
<tbody>
{s.streams.map((x, i) => (
<tr key={`${x.user}-${i}`}>
<td>{x.user}</td>
<td>
{x.title}
{x.paused && <span className="text-secondary"> (paused)</span>}
</td>
<td className="text-secondary">{[x.client, x.device].filter(Boolean).join(" · ") || "—"}</td>
<td>{x.transcoding ? <span className="badge bg-orange-lt text-orange">Transcoded</span> : <span className="badge bg-green-lt text-green">Direct</span>}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{failed.length > 0 && (
<>
<h4>Scheduled tasks that failed</h4>
<ul className="mb-3">
{failed.map((t) => (
<li key={t.name}>
<strong>{t.name}</strong>
{t.lastEndedAt && (
<>
{" "}
— <TimeAgo value={t.lastEndedAt} />
</>
)}
{t.error && <div className="text-secondary small">{t.error}</div>}
</li>
))}
</ul>
</>
)}
{s.failedLogins24h !== null && s.failedLogins24h > 0 && <div className="text-secondary small mb-3">{s.failedLogins24h} failed sign-in{s.failedLogins24h === 1 ? "" : "s"} in the last day.</div>}
</>
)}
{j.enabled && s && <History id={j.id} name={j.name} />}
</div>
</div>
);
}
/** Jellyfin: whether it is up, who is streaming from it right now and whether that is being transcoded, and the things that need a look. */
export default function Jellyfin() {
const [data, setData] = useState<JellyfinInfo[] | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const load = () =>
api.jellyfin
.list()
.then((r) => {
setData(r.jellyfins);
setError(null);
})
.catch((err) => setError(readableError(err)));
void load();
const t = setInterval(load, 30_000);
return () => clearInterval(t);
}, []);
return (
<>
<h2 className="page-title">Jellyfin</h2>
<p className="text-secondary">
Whether Jellyfin is up and who is watching. It is read every couple of minutes with an API key (see <Link to="/integrations">Integrations</Link>). A Jellyfin that <strong>wants a restart</strong>, a <strong>scheduled task that failed</strong> or a burst of{" "}
<strong>failed sign-ins</strong> is an alert; and an update run on the machine it lives on <strong>asks first when somebody is streaming</strong>.
</p>
<ErrorNotice error={error} />
{!data && !error && <BlockSkeleton lines={4} />}
{data && data.length === 0 && (
<div className="card">
<div className="card-body">
<EmptyState title="No Jellyfin connected" hint="Add one under Integrations with its address and an API key (Jellyfin → Dashboard → API Keys)." />
</div>
</div>
)}
{data?.map((j) => <Card key={j.id} j={j} />)}
</>
);
}