Initial commit

This commit is contained in:
Eugenius Naumenko
2026-06-07 22:20:57 +00:00
commit a7d8f72129
357 changed files with 168337 additions and 0 deletions
+201
View File
@@ -0,0 +1,201 @@
import { Activity, Database, RefreshCw, Server, Settings, Waves } from 'lucide-react';
import type { ReactNode } from 'react';
import { useEffect, useState } from 'react';
import { ApiError } from '../api/client';
import {
fetchDebugAzure,
fetchDebugDB,
fetchDebugFlowise,
fetchDebugRuntime,
fetchDebugStatus,
type DebugCheck,
type DebugDB,
type DebugStatus
} from '../api/debug';
type DebugError = {
message: string;
status?: number;
requestId?: string;
};
export function DebugPage() {
const [status, setStatus] = useState<DebugStatus | null>(null);
const [db, setDB] = useState<DebugDB | null>(null);
const [flowise, setFlowise] = useState<DebugCheck | null>(null);
const [azure, setAzure] = useState<DebugCheck | null>(null);
const [runtime, setRuntime] = useState<Record<string, unknown> | null>(null);
const [error, setError] = useState<DebugError | null>(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
void refreshAll();
}, []);
async function refreshAll() {
setLoading(true);
setError(null);
try {
const next = await fetchDebugStatus();
setStatus(next);
setDB(next.checks.db);
setFlowise(next.checks.flowise);
setAzure(next.checks.azure);
const runtimeResponse = await fetchDebugRuntime();
setRuntime(runtimeResponse.runtime);
} catch (err) {
setError(toDebugError(err));
} finally {
setLoading(false);
}
}
async function runDB() {
await runCheck(async () => setDB((await fetchDebugDB()).db));
}
async function runFlowise() {
await runCheck(async () => setFlowise((await fetchDebugFlowise()).flowise));
}
async function runAzure() {
await runCheck(async () => setAzure((await fetchDebugAzure()).azure));
}
async function runCheck(fn: () => Promise<void>) {
setLoading(true);
setError(null);
try {
await fn();
} catch (err) {
setError(toDebugError(err));
} finally {
setLoading(false);
}
}
return (
<div className="view-stack">
<header className="view-header">
<div>
<p className="eyebrow">Systemdiagnose</p>
<h1>Debug</h1>
</div>
<button className="primary-button" onClick={refreshAll} disabled={loading}>
<RefreshCw size={18} /> Neu pruefen
</button>
</header>
{error && (
<section className="panel result-panel debug-error">
<h2>Fehler</h2>
<p>{error.message}</p>
<p className="muted">
{error.status ? `HTTP ${error.status}` : 'Kein HTTP-Status'} {error.requestId ? `Request-ID ${error.requestId}` : ''}
</p>
</section>
)}
<section className="debug-grid">
<DebugCheckCard icon={<Database size={21} />} title="Datenbank" check={dbToCheck(db)} onRefresh={runDB} />
<DebugCheckCard icon={<Waves size={21} />} title="Azure Speech" check={azure} onRefresh={runAzure} />
<DebugCheckCard icon={<Activity size={21} />} title="Flowise" check={flowise} onRefresh={runFlowise} />
<DebugObjectCard icon={<Server size={21} />} title="Runtime" value={runtime} />
</section>
<section className="panel result-panel">
<div className="debug-title">
<Settings size={21} />
<h2>Konfiguration</h2>
</div>
<DebugObject value={status?.config ?? null} />
</section>
</div>
);
}
function DebugCheckCard({ icon, title, check, onRefresh }: { icon: ReactNode; title: string; check: DebugCheck | null; onRefresh: () => void }) {
const tone = check?.ok ? 'good' : 'weak';
return (
<section className={`panel debug-card debug-${tone}`}>
<div className="debug-card-head">
<div className="debug-title">
{icon}
<h2>{title}</h2>
</div>
<button className="ghost-button icon-only" onClick={onRefresh} aria-label={`${title} pruefen`} title={`${title} pruefen`}>
<RefreshCw size={17} />
</button>
</div>
{check ? (
<>
<strong>{check.ok ? 'OK' : 'Problem'}</strong>
<span>{check.status}</span>
{check.http_status ? <span>HTTP {check.http_status}</span> : null}
{check.duration_ms ? <span>{check.duration_ms} ms</span> : null}
{check.error ? <p className="form-error">{check.error}</p> : null}
<DebugObject value={check.details ?? null} />
</>
) : (
<p className="muted">Noch nicht geprueft.</p>
)}
</section>
);
}
function DebugObjectCard({ icon, title, value }: { icon: ReactNode; title: string; value: Record<string, unknown> | null }) {
return (
<section className="panel debug-card">
<div className="debug-title">
{icon}
<h2>{title}</h2>
</div>
<DebugObject value={value} />
</section>
);
}
function DebugObject({ value }: { value: Record<string, unknown> | null }) {
if (!value) return <p className="muted">Keine Daten.</p>;
return (
<dl className="debug-object">
{Object.entries(value).map(([key, val]) => (
<div key={key}>
<dt>{key}</dt>
<dd>{formatValue(val)}</dd>
</div>
))}
</dl>
);
}
function dbToCheck(db: DebugDB | null): DebugCheck | null {
if (!db) return null;
return {
ok: db.ok,
status: db.ok ? 'reachable' : 'error',
error: db.error,
details: {
...db.counts,
seed_language_count: db.seed_language_count,
seed_exercise_count: db.seed_exercise_count
}
};
}
function formatValue(value: unknown) {
if (Array.isArray(value)) return value.join(', ');
if (typeof value === 'object' && value !== null) return JSON.stringify(value);
if (typeof value === 'boolean') return value ? 'true' : 'false';
return String(value ?? '');
}
function toDebugError(err: unknown): DebugError {
if (err instanceof ApiError) {
return { message: err.message, status: err.status, requestId: err.requestId };
}
if (err instanceof Error) {
return { message: err.message };
}
return { message: 'Unbekannter Fehler' };
}