cleaned up and added a logging system

This commit is contained in:
2025-06-29 17:44:44 +02:00
parent b0033a5671
commit 24ef59f0ed
4 changed files with 521 additions and 281 deletions

View File

@@ -21,6 +21,14 @@ interface HealthReport {
errors: string[];
}
interface LogEntry {
timestamp: string;
level: string;
message: string;
slug?: string;
details?: string;
}
export default function RustStatusPage() {
const [stats, setStats] = useState<PostStats[]>([]);
const [loading, setLoading] = useState(true);
@@ -28,6 +36,11 @@ export default function RustStatusPage() {
const [health, setHealth] = useState<HealthReport | null>(null);
const [healthLoading, setHealthLoading] = useState(true);
const [healthError, setHealthError] = useState<string | null>(null);
const [logs, setLogs] = useState<LogEntry[]>([]);
const [logsLoading, setLogsLoading] = useState(true);
const [logsError, setLogsError] = useState<string | null>(null);
const [logFilter, setLogFilter] = useState<string>('all'); // 'all', 'info', 'warning', 'error'
const [logSearch, setLogSearch] = useState<string>('');
// Summary calculations
const totalHits = stats.reduce((sum, s) => sum + s.cache_hits, 0);
@@ -65,11 +78,65 @@ export default function RustStatusPage() {
}
};
const fetchLogs = async () => {
setLogsLoading(true);
setLogsError(null);
try {
const res = await fetch('/api/admin/posts?logs=1');
if (!res.ok) throw new Error('Fehler beim Laden der Logs');
const data = await res.json();
setLogs(data);
} catch (e: any) {
setLogsError(e.message || 'Unbekannter Fehler');
} finally {
setLogsLoading(false);
}
};
const clearLogs = async () => {
try {
const res = await fetch('/api/admin/posts?clearLogs=1', { method: 'DELETE' });
if (!res.ok) throw new Error('Fehler beim Löschen der Logs');
await fetchLogs(); // Refresh logs after clearing
} catch (e: any) {
console.error('Error clearing logs:', e);
}
};
useEffect(() => {
fetchStats();
fetchHealth();
fetchLogs();
}, []);
// Filter logs based on level and search term
const filteredLogs = logs.filter(log => {
const matchesLevel = logFilter === 'all' || log.level === logFilter;
const matchesSearch = !logSearch ||
log.message.toLowerCase().includes(logSearch.toLowerCase()) ||
(log.slug && log.slug.toLowerCase().includes(logSearch.toLowerCase())) ||
(log.details && log.details.toLowerCase().includes(logSearch.toLowerCase()));
return matchesLevel && matchesSearch;
});
const getLevelColor = (level: string) => {
switch (level) {
case 'error': return 'text-red-600 bg-red-50';
case 'warning': return 'text-yellow-600 bg-yellow-50';
case 'info': return 'text-blue-600 bg-blue-50';
default: return 'text-gray-600 bg-gray-50';
}
};
const getLevelIcon = (level: string) => {
switch (level) {
case 'error': return '❌';
case 'warning': return '⚠️';
case 'info': return '';
default: return '📝';
}
};
return (
<div className="min-h-screen bg-gray-100 p-4 sm:p-6">
<div className="max-w-6xl mx-auto">
@@ -101,13 +168,17 @@ export default function RustStatusPage() {
{/* Refresh button */}
<button
onClick={fetchStats}
onClick={() => {
fetchStats();
fetchHealth();
fetchLogs();
}}
className="p-2 sm:px-4 sm:py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg shadow flex items-center gap-1 transition-colors"
title="Aktualisieren"
disabled={loading}
disabled={loading || healthLoading || logsLoading}
>
<svg
className={`w-5 h-5 ${loading ? 'animate-spin' : ''}`}
className={`w-5 h-5 ${(loading || healthLoading || logsLoading) ? 'animate-spin' : ''}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
@@ -196,6 +267,93 @@ export default function RustStatusPage() {
</div>
</div>
{/* Parser Logs Section */}
<div className="bg-white rounded-lg shadow p-4 mb-6">
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-4">
<h2 className="text-lg font-semibold">Parser Logs</h2>
<div className="flex flex-col sm:flex-row gap-2">
<button
onClick={clearLogs}
className="px-3 py-2 bg-red-500 hover:bg-red-600 text-white rounded text-sm transition-colors"
title="Clear all logs"
>
Clear Logs
</button>
</div>
</div>
{/* Log Filters */}
<div className="flex flex-col sm:flex-row gap-4 mb-4">
<div className="flex-1">
<input
type="text"
placeholder="Search logs..."
value={logSearch}
onChange={(e) => setLogSearch(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="flex gap-2">
<select
value={logFilter}
onChange={(e) => setLogFilter(e.target.value)}
className="px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="all">All Levels</option>
<option value="info">Info</option>
<option value="warning">Warning</option>
<option value="error">Error</option>
</select>
</div>
</div>
{/* Logs Display */}
<div className="max-h-96 overflow-y-auto">
{logsLoading && <div className="text-center py-4">Loading logs...</div>}
{logsError && <div className="text-red-500 text-center py-4">{logsError}</div>}
{!logsLoading && !logsError && (
<div className="space-y-2">
{filteredLogs.length === 0 ? (
<div className="text-center py-4 text-gray-500">No logs found</div>
) : (
filteredLogs.map((log, index) => (
<div key={index} className={`p-3 rounded-lg border ${getLevelColor(log.level)}`}>
<div className="flex items-start gap-2">
<span className="text-lg">{getLevelIcon(log.level)}</span>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<span className="text-xs font-mono text-gray-500">
{new Date(log.timestamp).toLocaleString()}
</span>
<span className={`px-2 py-1 rounded text-xs font-medium ${
log.level === 'error' ? 'bg-red-200 text-red-800' :
log.level === 'warning' ? 'bg-yellow-200 text-yellow-800' :
'bg-blue-200 text-blue-800'
}`}>
{log.level.toUpperCase()}
</span>
{log.slug && (
<span className="px-2 py-1 bg-gray-200 text-gray-700 rounded text-xs font-mono">
{log.slug}
</span>
)}
</div>
<div className="text-sm font-medium">{log.message}</div>
{log.details && (
<div className="text-xs text-gray-600 mt-1 font-mono bg-gray-100 p-2 rounded">
{log.details}
</div>
)}
</div>
</div>
</div>
))
)}
</div>
)}
</div>
</div>
{/* Table */}
<div className="bg-white rounded-lg shadow p-3 sm:p-4 overflow-x-auto">
<h2 className="text-base sm:text-lg font-semibold mb-3">Rohdaten</h2>

View File

@@ -90,6 +90,26 @@ export async function GET(request: Request) {
});
}
}
const logs = searchParams.get('logs');
if (logs === '1') {
// Call the Rust backend for parser logs
const rustResult = spawnSync(
process.cwd() + '/markdown_backend/target/release/markdown_backend',
['logs'],
{ encoding: 'utf-8' }
);
if (rustResult.status === 0 && rustResult.stdout) {
return new Response(rustResult.stdout, {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} else {
return new Response(JSON.stringify({ error: rustResult.stderr || rustResult.error }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
// Return the current pinned.json object
try {
const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json');
@@ -150,4 +170,36 @@ export async function PUT(request: Request) {
console.error('Error editing post:', error);
return NextResponse.json({ error: 'Error editing post' }, { status: 500 });
}
}
export async function DELETE(request: Request) {
try {
const { searchParams } = new URL(request.url);
const clearLogs = searchParams.get('clearLogs');
if (clearLogs === '1') {
// Call the Rust backend to clear parser logs
const rustResult = spawnSync(
process.cwd() + '/markdown_backend/target/release/markdown_backend',
['clearLogs'],
{ encoding: 'utf-8' }
);
if (rustResult.status === 0 && rustResult.stdout) {
return new Response(rustResult.stdout, {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} else {
return new Response(JSON.stringify({ error: rustResult.stderr || rustResult.error }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
return NextResponse.json({ error: 'Invalid delete operation' }, { status: 400 });
} catch (error) {
console.error('Error clearing logs:', error);
return NextResponse.json({ error: 'Error clearing logs' }, { status: 500 });
}
}