156 lines
5.3 KiB
TypeScript
156 lines
5.3 KiB
TypeScript
"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";
|
||
|
|
|
||
|
|
const PAGE = 100;
|
||
|
|
|
||
|
|
const SOURCES: Record<string, string> = { analysis: "Elemzés", question: "Kérdés-válasz", preview: "Prompt-próba" };
|
||
|
|
const KINDS: Record<string, string> = {
|
||
|
|
summary: "összefoglaló",
|
||
|
|
natal: "képletelemzés",
|
||
|
|
synastry: "kapcsolati",
|
||
|
|
forecast: "előrejelzés",
|
||
|
|
question: "válasz",
|
||
|
|
};
|
||
|
|
const OUTCOMES: Record<GenerationError["outcome"], { label: string; tone: string }> = {
|
||
|
|
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 [errors, setErrors] = useState<GenerationError[] | null>(null);
|
||
|
|
const [total, setTotal] = useState(0);
|
||
|
|
const [lastDay, setLastDay] = useState(0);
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
const load = useCallback(
|
||
|
|
async (offset: number) => {
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
const params = new URLSearchParams({ limit: String(PAGE), offset: String(offset) });
|
||
|
|
if (source) params.set("source", source);
|
||
|
|
if (outcome) params.set("outcome", outcome);
|
||
|
|
try {
|
||
|
|
const page = await api<Page>(`/admin/errors?${params}`);
|
||
|
|
setErrors((all) => (offset === 0 ? page.errors : [...(all ?? []), ...page.errors]));
|
||
|
|
setTotal(page.total);
|
||
|
|
setLastDay(page.last_day);
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
[source, outcome],
|
||
|
|
);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
load(0);
|
||
|
|
}, [load]);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<section className="card" style={{ gap: 12 }}>
|
||
|
|
<div className="row" style={{ gap: 12, flexWrap: "wrap", alignItems: "flex-end" }}>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="error-source">Mi</label>
|
||
|
|
<select id="error-source" className="inp" value={source} onChange={(e) => setSource(e.target.value)}>
|
||
|
|
<option value="">mind</option>
|
||
|
|
{Object.entries(SOURCES).map(([v, l]) => (
|
||
|
|
<option key={v} value={v}>
|
||
|
|
{l}
|
||
|
|
</option>
|
||
|
|
))}
|
||
|
|
</select>
|
||
|
|
</div>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="error-outcome">Következmény</label>
|
||
|
|
<select id="error-outcome" className="inp" value={outcome} onChange={(e) => setOutcome(e.target.value)}>
|
||
|
|
<option value="">mind</option>
|
||
|
|
{Object.entries(OUTCOMES).map(([v, o]) => (
|
||
|
|
<option key={v} value={v}>
|
||
|
|
{o.label}
|
||
|
|
</option>
|
||
|
|
))}
|
||
|
|
</select>
|
||
|
|
</div>
|
||
|
|
<p className="hint" style={{ margin: 0 }}>
|
||
|
|
{total} bejegyzés, ebből {lastDay} az elmúlt 24 órában
|
||
|
|
</p>
|
||
|
|
<button type="button" className="btn btn-sm btn-ghost" onClick={() => load(0)} disabled={busy}>
|
||
|
|
Frissítés
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
{errors === null ? (
|
||
|
|
<p className="muted">Betöltés…</p>
|
||
|
|
) : errors.length === 0 ? (
|
||
|
|
<p className="muted">Nincs hiba. 🎉</p>
|
||
|
|
) : (
|
||
|
|
<div className="table-wrap">
|
||
|
|
<table className="error-log">
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th>Időpont</th>
|
||
|
|
<th>Mi</th>
|
||
|
|
<th>Modell</th>
|
||
|
|
<th>Kísérlet</th>
|
||
|
|
<th>Következmény</th>
|
||
|
|
<th>Hiba</th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
{errors.map((e) => (
|
||
|
|
<tr key={e.id}>
|
||
|
|
<td className="num">{formatDate(e.created_at, true)}</td>
|
||
|
|
<td>
|
||
|
|
{SOURCES[e.source] ?? e.source}
|
||
|
|
{e.source !== "question" && ` (${KINDS[e.kind] ?? e.kind})`}
|
||
|
|
{e.user && <span className="hint">{e.user}</span>}
|
||
|
|
<code className="hint" title={e.item_id}>
|
||
|
|
{e.item_id.slice(0, 8)}
|
||
|
|
</code>
|
||
|
|
</td>
|
||
|
|
<td>
|
||
|
|
<code>{e.model}</code>
|
||
|
|
</td>
|
||
|
|
<td className="num">
|
||
|
|
{e.attempt}.{e.seconds !== null && <span className="hint">{e.seconds} mp</span>}
|
||
|
|
</td>
|
||
|
|
<td>
|
||
|
|
<span className={`outcome tone-${OUTCOMES[e.outcome].tone}`}>{OUTCOMES[e.outcome].label}</span>
|
||
|
|
</td>
|
||
|
|
<td className="error-text">{e.error}</td>
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
{errors && errors.length < total && (
|
||
|
|
<button type="button" className="btn btn-sm" onClick={() => load(errors.length)} disabled={busy}>
|
||
|
|
{busy ? "Betöltés…" : "Régebbiek"}
|
||
|
|
</button>
|
||
|
|
)}
|
||
|
|
</section>
|
||
|
|
);
|
||
|
|
}
|