/** * Pipeline admin dashboard — video list with status, retrigger/revoke, * expandable event log with token usage and collapsible JSON viewer. */ import { useCallback, useEffect, useState } from "react"; import { fetchPipelineVideos, fetchPipelineEvents, fetchWorkerStatus, triggerPipeline, revokePipeline, type PipelineVideoItem, type PipelineEvent, type WorkerStatusResponse, } from "../api/public-client"; // ── Helpers ────────────────────────────────────────────────────────────────── function formatDate(iso: string | null): string { if (!iso) return "—"; return new Date(iso).toLocaleString(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit", }); } function formatTokens(n: number): string { if (n === 0) return "0"; if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`; return String(n); } function statusBadgeClass(status: string): string { switch (status) { case "completed": case "indexed": return "pipeline-badge--success"; case "processing": case "extracted": case "classified": case "synthesized": return "pipeline-badge--active"; case "failed": case "error": return "pipeline-badge--error"; case "pending": case "queued": return "pipeline-badge--pending"; default: return ""; } } function eventTypeIcon(eventType: string): string { switch (eventType) { case "start": return "▶"; case "complete": return "✓"; case "error": return "✗"; case "llm_call": return "🤖"; default: return "·"; } } // ── Collapsible JSON ───────────────────────────────────────────────────────── function JsonViewer({ data }: { data: Record | null }) { const [open, setOpen] = useState(false); if (!data || Object.keys(data).length === 0) return null; return (
{open && (
          {JSON.stringify(data, null, 2)}
        
)}
); } // ── Event Log ──────────────────────────────────────────────────────────────── function EventLog({ videoId }: { videoId: string }) { const [events, setEvents] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [offset, setOffset] = useState(0); const limit = 50; const load = useCallback(async () => { setLoading(true); setError(null); try { const res = await fetchPipelineEvents(videoId, { offset, limit }); setEvents(res.items); setTotal(res.total); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load events"); } finally { setLoading(false); } }, [videoId, offset]); useEffect(() => { void load(); }, [load]); if (loading) return
Loading events…
; if (error) return
Error: {error}
; if (events.length === 0) return
No events recorded.
; const hasNext = offset + limit < total; const hasPrev = offset > 0; return (
{total} event{total !== 1 ? "s" : ""}
{events.map((evt) => (
{eventTypeIcon(evt.event_type)} {evt.stage} {evt.event_type} {evt.model && {evt.model}} {evt.total_tokens != null && evt.total_tokens > 0 && ( {formatTokens(evt.total_tokens)} tok )} {evt.duration_ms != null && ( {evt.duration_ms}ms )} {formatDate(evt.created_at)}
))}
{(hasPrev || hasNext) && (
{offset + 1}–{Math.min(offset + limit, total)} of {total}
)}
); } // ── Worker Status ──────────────────────────────────────────────────────────── function WorkerStatus() { const [status, setStatus] = useState(null); const [error, setError] = useState(null); const load = useCallback(async () => { try { setError(null); const res = await fetchWorkerStatus(); setStatus(res); } catch (err) { setError(err instanceof Error ? err.message : "Failed"); } }, []); useEffect(() => { void load(); const id = setInterval(() => void load(), 15_000); return () => clearInterval(id); }, [load]); if (error) { return (
Worker: error ({error})
); } if (!status) { return (
Worker: checking…
); } return (
{status.online ? `${status.workers.length} worker${status.workers.length !== 1 ? "s" : ""} online` : "Workers offline"} {status.workers.map((w) => ( {w.active_tasks.length > 0 ? `${w.active_tasks.length} active` : "idle"} {w.pool_size != null && ` · pool ${w.pool_size}`} ))}
); } // ── Main Page ──────────────────────────────────────────────────────────────── export default function AdminPipeline() { const [videos, setVideos] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [expandedId, setExpandedId] = useState(null); const [actionLoading, setActionLoading] = useState(null); const [actionMessage, setActionMessage] = useState<{ id: string; text: string; ok: boolean } | null>(null); const load = useCallback(async () => { setLoading(true); setError(null); try { const res = await fetchPipelineVideos(); setVideos(res.items); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load videos"); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); const handleTrigger = async (videoId: string) => { setActionLoading(videoId); setActionMessage(null); try { const res = await triggerPipeline(videoId); setActionMessage({ id: videoId, text: `Triggered (${res.status})`, ok: true }); // Refresh after short delay to let status update setTimeout(() => void load(), 2000); } catch (err) { setActionMessage({ id: videoId, text: err instanceof Error ? err.message : "Trigger failed", ok: false, }); } finally { setActionLoading(null); } }; const handleRevoke = async (videoId: string) => { setActionLoading(videoId); setActionMessage(null); try { const res = await revokePipeline(videoId); setActionMessage({ id: videoId, text: res.tasks_revoked > 0 ? `Revoked ${res.tasks_revoked} task${res.tasks_revoked !== 1 ? "s" : ""}` : "No active tasks", ok: true, }); setTimeout(() => void load(), 2000); } catch (err) { setActionMessage({ id: videoId, text: err instanceof Error ? err.message : "Revoke failed", ok: false, }); } finally { setActionLoading(null); } }; const toggleExpand = (id: string) => { setExpandedId((prev) => (prev === id ? null : id)); }; return (

Pipeline Management

{videos.length} video{videos.length !== 1 ? "s" : ""}

{loading ? (
Loading videos…
) : error ? (
Error: {error}
) : videos.length === 0 ? (
No videos in pipeline.
) : (
{videos.map((video) => (
toggleExpand(video.id)} >
{video.filename} {video.creator_name}
{video.processing_status} {video.event_count} events {formatTokens(video.total_tokens_used)} tokens {formatDate(video.last_event_at)}
e.stopPropagation()}>
{actionMessage?.id === video.id && (
{actionMessage.text}
)} {expandedId === video.id && (
ID: {video.id.slice(0, 8)}… Created: {formatDate(video.created_at)} Updated: {formatDate(video.updated_at)}
)}
))}
)}
); }