"use client"; import { useCallback, useEffect, useState } from "react"; import { api, errorText } from "@/lib/api"; import { formatDate } from "@/lib/astro"; import type { GenerationError } from "@/lib/types"; import { confirmDialog } from "@/components/Dialogs"; const PAGE = 50; const SOURCES: Record = { analysis: "Elemzés", question: "Kérdés-válasz", preview: "Prompt-próba" }; const KINDS: Record = { summary: "összefoglaló", weekly: "heti horoszkóp", natal: "képletelemzés", synastry: "kapcsolati", forecast: "előrejelzés", celebrity: "híres ember", question: "válasz", }; const OUTCOMES: Record = { retry: { label: "újrapróbálva", tone: "neutral" }, fallback: { label: "tartalék modell vette át", tone: "neutral" }, failed: { label: "végleg sikertelen", tone: "tense" }, }; interface Page { errors: GenerationError[]; total: number; last_day: number; } /** The admin error log with filters and paging. */ export function ErrorLog() { const [source, setSource] = useState(""); const [outcome, setOutcome] = useState(""); const [page, setPage] = useState(0); const [errors, setErrors] = useState(null); const [total, setTotal] = useState(0); const [lastDay, setLastDay] = useState(0); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const filter = useCallback(() => { const params = new URLSearchParams(); if (source) params.set("source", source); if (outcome) params.set("outcome", outcome); return params; }, [source, outcome]); const load = useCallback(async () => { setBusy(true); setError(null); const params = filter(); params.set("limit", String(PAGE)); params.set("offset", String(page * PAGE)); try { const res = await api(`/admin/errors?${params}`); // The last page emptied by deleting: one page back. if (res.errors.length === 0 && page > 0) return setPage(Math.max(0, Math.ceil(res.total / PAGE) - 1)); setErrors(res.errors); setTotal(res.total); setLastDay(res.last_day); } catch (e) { setError(errorText(e)); } finally { setBusy(false); } }, [filter, page]); useEffect(() => { load(); }, [load]); async function removeOne(id: number) { setError(null); try { await api(`/admin/errors/${id}`, { method: "DELETE" }); await load(); } catch (e) { setError(errorText(e)); } } async function removeMatching() { const what = source || outcome ? `a szűrésnek megfelelő ${total} bejegyzést` : `mind a ${total} bejegyzést`; const ok = await confirmDialog({ title: `Törlöd ${what}?`, text: "Nem vonható vissza.", confirm: "Törlés", danger: true, }); if (!ok) return; setBusy(true); setError(null); try { await api(`/admin/errors?${filter()}`, { method: "DELETE" }); setPage(0); await load(); } catch (e) { setError(errorText(e)); } finally { setBusy(false); } } const pages = Math.max(1, Math.ceil(total / PAGE)); return (

{total} bejegyzés, ebből {lastDay} az elmúlt 24 órában

{total > 0 && ( )}
{error && (

{error}

)} {errors === null ? (

Betöltés…

) : errors.length === 0 ? (

Nincs hiba. 🎉

) : (
{errors.map((e) => ( ))}
Időpont Mi Modell Kísérlet Következmény Hiba Törlés
{formatDate(e.created_at, true)} {SOURCES[e.source] ?? e.source} {e.source !== "question" && ` (${KINDS[e.kind] ?? e.kind})`} {e.user && {e.user}} {e.item_id.slice(0, 8)} {e.model} {e.attempt}.{e.seconds !== null && {e.seconds} mp} {OUTCOMES[e.outcome].label} {e.error}
)} {total > PAGE && ( )}
); }