feat(outbox): UI fuer Event-Outbox — Modul 9/16 des UI-Backlogs
This commit is contained in:
@@ -0,0 +1,481 @@
|
||||
/**
|
||||
* Outbox page — transactional outbox monitoring & management (admin).
|
||||
*
|
||||
* Backend: /api/v1/outbox, all endpoints require the admin role.
|
||||
*
|
||||
* Features:
|
||||
* - Stats cards: pending / processing / published / failed / no_handlers,
|
||||
* total events, oldest pending age
|
||||
* - DLQ: failed events list with error details, replay single / replay all
|
||||
* - Consumer registry: event name → registered handlers
|
||||
* - Maintenance: recover stuck processing events, cleanup published events
|
||||
*/
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import clsx from 'clsx';
|
||||
import {
|
||||
AlertTriangle,
|
||||
Clock,
|
||||
Inbox,
|
||||
Lock,
|
||||
RefreshCw,
|
||||
RotateCcw,
|
||||
Server,
|
||||
Trash2,
|
||||
Wrench,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
useOutboxStats,
|
||||
useOutboxFailedEvents,
|
||||
useOutboxConsumerRegistry,
|
||||
useReplayFailedEvent,
|
||||
useReplayAllFailedEvents,
|
||||
useRecoverStuckEvents,
|
||||
useCleanupPublishedEvents,
|
||||
type FailedOutboxEvent,
|
||||
} from '@/api/outbox';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Modal } from '@/components/ui/Modal';
|
||||
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
const STATUS_ORDER = ['pending', 'processing', 'published', 'failed', 'no_handlers'] as const;
|
||||
|
||||
const STATUS_BADGE: Record<string, string> = {
|
||||
pending: 'bg-warning-100 text-warning-800 dark:bg-warning-900/30 dark:text-warning-300',
|
||||
processing: 'bg-info-100 text-info-800 dark:bg-info-900/30 dark:text-info-300',
|
||||
published: 'bg-success-100 text-success-800 dark:bg-success-900/30 dark:text-success-300',
|
||||
failed: 'bg-danger-100 text-danger-800 dark:bg-danger-900/30 dark:text-danger-300',
|
||||
no_handlers: 'bg-secondary-200 text-secondary-700 dark:bg-secondary-800 dark:text-secondary-300',
|
||||
};
|
||||
|
||||
function formatDateTime(iso: string | null): string {
|
||||
if (!iso) return '—';
|
||||
try {
|
||||
return new Date(iso).toLocaleString();
|
||||
} catch {
|
||||
return iso;
|
||||
}
|
||||
}
|
||||
|
||||
function formatAge(seconds: number): string {
|
||||
if (seconds < 60) return `${Math.round(seconds)}s`;
|
||||
if (seconds < 3600) return `${Math.round(seconds / 60)}min`;
|
||||
if (seconds < 86400) return `${Math.round(seconds / 3600)}h`;
|
||||
return `${Math.round(seconds / 86400)}d`;
|
||||
}
|
||||
|
||||
function FailedEventCard({
|
||||
event,
|
||||
isReplaying,
|
||||
onReplay,
|
||||
}: {
|
||||
event: FailedOutboxEvent;
|
||||
isReplaying: boolean;
|
||||
onReplay: (id: string) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<Card className="p-4 space-y-2" data-testid={`outbox-event-${event.id}`}>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span
|
||||
className={clsx(
|
||||
'inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium',
|
||||
STATUS_BADGE.failed,
|
||||
)}
|
||||
>
|
||||
{t('outbox.statusFailed')}
|
||||
</span>
|
||||
<span className="text-xs text-secondary-500">{t('outbox.attempts')}: {event.attempts}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-2 text-sm font-medium text-secondary-900 dark:text-secondary-100 break-words font-mono">
|
||||
{event.event_name}
|
||||
</p>
|
||||
<p className="text-xs text-secondary-400 mt-1" title={event.id}>
|
||||
<span className="font-mono">{event.id.slice(0, 8)}…</span>
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="primary"
|
||||
size="sm"
|
||||
onClick={() => onReplay(event.id)}
|
||||
disabled={isReplaying}
|
||||
data-testid={`outbox-replay-${event.id}`}
|
||||
aria-label={t('outbox.replay')}
|
||||
>
|
||||
<RotateCcw className="w-4 h-4" aria-hidden="true" />
|
||||
{t('outbox.replay')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{event.error_message && (
|
||||
<p
|
||||
className="text-xs text-danger-600 dark:text-danger-400 bg-danger-50 dark:bg-danger-900/20 rounded-md px-3 py-2 break-words"
|
||||
data-testid={`outbox-error-${event.id}`}
|
||||
>
|
||||
{event.error_message}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-4 text-xs text-secondary-400 border-t border-secondary-200 dark:border-secondary-700 pt-2">
|
||||
<span className="inline-flex items-center gap-1">
|
||||
<Clock className="w-3 h-3" aria-hidden="true" />
|
||||
{t('outbox.createdAt')}: {formatDateTime(event.created_at)}
|
||||
</span>
|
||||
{event.failed_at && (
|
||||
<span>
|
||||
{t('outbox.failedAt')}: {formatDateTime(event.failed_at)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export function OutboxPage() {
|
||||
const { t } = useTranslation();
|
||||
const user = useAuthStore((state) => state.user);
|
||||
const isAdmin = user?.is_system_admin === true;
|
||||
|
||||
const [offset, setOffset] = useState(0);
|
||||
const [maintenance, setMaintenance] = useState<'recover' | 'cleanup' | null>(null);
|
||||
const [timeoutSeconds, setTimeoutSeconds] = useState(120);
|
||||
const [retentionDays, setRetentionDays] = useState(30);
|
||||
|
||||
const statsQuery = useOutboxStats(isAdmin);
|
||||
const failedQuery = useOutboxFailedEvents(PAGE_SIZE, offset, isAdmin);
|
||||
const registryQuery = useOutboxConsumerRegistry(isAdmin);
|
||||
|
||||
const replayMut = useReplayFailedEvent();
|
||||
const replayAllMut = useReplayAllFailedEvents();
|
||||
const recoverMut = useRecoverStuckEvents();
|
||||
const cleanupMut = useCleanupPublishedEvents();
|
||||
|
||||
const stats = statsQuery.data;
|
||||
const events = failedQuery.data?.events ?? [];
|
||||
const registry = registryQuery.data?.registry ?? {};
|
||||
const registryEntries = Object.entries(registry).sort(([a], [b]) => a.localeCompare(b));
|
||||
|
||||
const isMutating =
|
||||
replayMut.isPending || replayAllMut.isPending || recoverMut.isPending || cleanupMut.isPending;
|
||||
|
||||
const handleMaintenance = () => {
|
||||
if (maintenance === 'recover') {
|
||||
recoverMut.mutate(timeoutSeconds, { onSuccess: () => setMaintenance(null) });
|
||||
} else if (maintenance === 'cleanup') {
|
||||
cleanupMut.mutate(retentionDays, { onSuccess: () => setMaintenance(null) });
|
||||
}
|
||||
};
|
||||
|
||||
// Admin-only guard — backend enforces require_admin on all outbox endpoints.
|
||||
if (!isAdmin) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-[50vh]" role="alert">
|
||||
<div className="text-center">
|
||||
<Lock className="w-10 h-10 mx-auto text-secondary-400" aria-hidden="true" />
|
||||
<p className="mt-3 text-sm text-secondary-600" data-testid="outbox-admin-only">
|
||||
{t('outbox.adminOnly')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4 max-w-4xl mx-auto" data-testid="outbox-page">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<h1 className="text-xl font-semibold text-secondary-900 dark:text-secondary-100">
|
||||
{t('outbox.title')}
|
||||
</h1>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
statsQuery.refetch();
|
||||
failedQuery.refetch();
|
||||
registryQuery.refetch();
|
||||
}}
|
||||
disabled={statsQuery.isFetching || failedQuery.isFetching}
|
||||
aria-label={t('outbox.refresh')}
|
||||
data-testid="outbox-refresh"
|
||||
>
|
||||
<RefreshCw
|
||||
className={clsx('w-4 h-4', (statsQuery.isFetching || failedQuery.isFetching) && 'animate-spin')}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Stats cards */}
|
||||
{statsQuery.isLoading ? (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<RefreshCw className="w-6 h-6 animate-spin text-secondary-400" aria-hidden="true" />
|
||||
</div>
|
||||
) : statsQuery.isError ? (
|
||||
<Card className="p-8 text-center">
|
||||
<AlertTriangle className="w-8 h-8 mx-auto text-danger-500" aria-hidden="true" />
|
||||
<p className="mt-2 text-sm text-secondary-600" data-testid="outbox-stats-error">
|
||||
{t('outbox.loadError')}
|
||||
</p>
|
||||
</Card>
|
||||
) : stats ? (
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-3">
|
||||
{STATUS_ORDER.map((status) => (
|
||||
<Card key={status} className="p-3 text-center" data-testid={`outbox-stat-${status}`}>
|
||||
<span
|
||||
className={clsx(
|
||||
'inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium mb-2',
|
||||
STATUS_BADGE[status],
|
||||
)}
|
||||
>
|
||||
{t(`outbox.status_${status}`)}
|
||||
</span>
|
||||
<p className="text-2xl font-semibold text-secondary-900 dark:text-secondary-100">
|
||||
{stats.counts[status] ?? 0}
|
||||
</p>
|
||||
</Card>
|
||||
))}
|
||||
<Card className="p-3 text-center" data-testid="outbox-stat-total">
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium mb-2 bg-primary-100 text-primary-800 dark:bg-primary-900/30 dark:text-primary-300">
|
||||
{t('outbox.total')}
|
||||
</span>
|
||||
<p className="text-2xl font-semibold text-secondary-900 dark:text-secondary-100">{stats.total}</p>
|
||||
</Card>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{stats && stats.oldest_pending_age_seconds != null && (
|
||||
<p className="text-xs text-secondary-500" data-testid="outbox-oldest-pending">
|
||||
<Clock className="w-3 h-3 inline mr-1" aria-hidden="true" />
|
||||
{t('outbox.oldestPending')}: {formatAge(stats.oldest_pending_age_seconds)}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{/* DLQ — failed events */}
|
||||
<section aria-labelledby="outbox-dlq-heading">
|
||||
<div className="flex items-center justify-between gap-3 mb-2">
|
||||
<h2
|
||||
id="outbox-dlq-heading"
|
||||
className="text-base font-semibold text-secondary-900 dark:text-secondary-100"
|
||||
>
|
||||
{t('outbox.dlqTitle')}
|
||||
{stats?.counts.failed != null && stats.counts.failed > 0 && (
|
||||
<span className="ml-2 text-xs font-normal text-danger-600 dark:text-danger-400">
|
||||
{stats.counts.failed}
|
||||
</span>
|
||||
)}
|
||||
</h2>
|
||||
{events.length > 0 && (
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => replayAllMut.mutate()}
|
||||
disabled={isMutating}
|
||||
data-testid="outbox-replay-all"
|
||||
>
|
||||
<RotateCcw className="w-4 h-4" aria-hidden="true" />
|
||||
{t('outbox.replayAll')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{failedQuery.isLoading ? (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<RefreshCw className="w-6 h-6 animate-spin text-secondary-400" aria-hidden="true" />
|
||||
</div>
|
||||
) : failedQuery.isError ? (
|
||||
<Card className="p-8 text-center">
|
||||
<AlertTriangle className="w-8 h-8 mx-auto text-danger-500" aria-hidden="true" />
|
||||
<p className="mt-2 text-sm text-secondary-600">{t('outbox.loadError')}</p>
|
||||
</Card>
|
||||
) : events.length === 0 ? (
|
||||
<Card className="p-10 text-center">
|
||||
<Inbox className="w-10 h-10 mx-auto text-secondary-300" aria-hidden="true" />
|
||||
<p className="mt-3 text-sm text-secondary-500" data-testid="outbox-dlq-empty">
|
||||
{t('outbox.dlqEmpty')}
|
||||
</p>
|
||||
</Card>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{events.map((event) => (
|
||||
<FailedEventCard
|
||||
key={event.id}
|
||||
event={event}
|
||||
isReplaying={replayMut.isPending}
|
||||
onReplay={(id) => replayMut.mutate(id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Pagination */}
|
||||
{events.length === PAGE_SIZE && (
|
||||
<div className="flex justify-center gap-2 mt-3">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setOffset(Math.max(0, offset - PAGE_SIZE))}
|
||||
disabled={offset === 0 || failedQuery.isFetching}
|
||||
>
|
||||
{t('outbox.prev')}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setOffset(offset + PAGE_SIZE)}
|
||||
disabled={events.length < PAGE_SIZE || failedQuery.isFetching}
|
||||
data-testid="outbox-next-page"
|
||||
>
|
||||
{t('outbox.next')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
|
||||
{/* Maintenance actions */}
|
||||
<section aria-labelledby="outbox-maintenance-heading">
|
||||
<h2
|
||||
id="outbox-maintenance-heading"
|
||||
className="text-base font-semibold text-secondary-900 dark:text-secondary-100 mb-2"
|
||||
>
|
||||
{t('outbox.maintenanceTitle')}
|
||||
</h2>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => setMaintenance('recover')}
|
||||
disabled={isMutating}
|
||||
data-testid="outbox-recover-stuck"
|
||||
>
|
||||
<Wrench className="w-4 h-4" aria-hidden="true" />
|
||||
{t('outbox.recoverStuck')}
|
||||
</Button>
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => setMaintenance('cleanup')}
|
||||
disabled={isMutating}
|
||||
data-testid="outbox-cleanup-published"
|
||||
>
|
||||
<Trash2 className="w-4 h-4" aria-hidden="true" />
|
||||
{t('outbox.cleanupPublished')}
|
||||
</Button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Consumer registry */}
|
||||
<section aria-labelledby="outbox-registry-heading">
|
||||
<h2
|
||||
id="outbox-registry-heading"
|
||||
className="text-base font-semibold text-secondary-900 dark:text-secondary-100 mb-2"
|
||||
>
|
||||
{t('outbox.registryTitle')} ({registryEntries.length})
|
||||
</h2>
|
||||
{registryQuery.isLoading ? (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<RefreshCw className="w-6 h-6 animate-spin text-secondary-400" aria-hidden="true" />
|
||||
</div>
|
||||
) : registryQuery.isError ? (
|
||||
<Card className="p-8 text-center">
|
||||
<AlertTriangle className="w-8 h-8 mx-auto text-danger-500" aria-hidden="true" />
|
||||
<p className="mt-2 text-sm text-secondary-600">{t('outbox.loadError')}</p>
|
||||
</Card>
|
||||
) : registryEntries.length === 0 ? (
|
||||
<Card className="p-10 text-center">
|
||||
<Server className="w-10 h-10 mx-auto text-secondary-300" aria-hidden="true" />
|
||||
<p className="mt-3 text-sm text-secondary-500" data-testid="outbox-registry-empty">
|
||||
{t('outbox.registryEmpty')}
|
||||
</p>
|
||||
</Card>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
{registryEntries.map(([eventName, handlers]) => (
|
||||
<Card key={eventName} className="p-3" data-testid={`outbox-registry-${eventName}`}>
|
||||
<p className="text-sm font-medium text-secondary-900 dark:text-secondary-100 font-mono break-all">
|
||||
{eventName}
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-1 mt-2">
|
||||
{handlers.map((handler) => (
|
||||
<span
|
||||
key={handler}
|
||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs bg-secondary-100 text-secondary-700 dark:bg-secondary-800 dark:text-secondary-300 font-mono break-all"
|
||||
>
|
||||
{handler}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
|
||||
{/* Maintenance modal */}
|
||||
<Modal
|
||||
open={maintenance !== null}
|
||||
onClose={() => setMaintenance(null)}
|
||||
title={maintenance === 'recover' ? t('outbox.recoverStuck') : t('outbox.cleanupPublished')}
|
||||
>
|
||||
<div className="space-y-4">
|
||||
{maintenance === 'recover' ? (
|
||||
<>
|
||||
<label
|
||||
htmlFor="outbox-timeout-input"
|
||||
className="block text-sm font-medium text-secondary-700 dark:text-secondary-300"
|
||||
>
|
||||
{t('outbox.timeoutSeconds')}
|
||||
</label>
|
||||
<input
|
||||
id="outbox-timeout-input"
|
||||
type="number"
|
||||
min={10}
|
||||
max={3600}
|
||||
value={timeoutSeconds}
|
||||
onChange={(e) => setTimeoutSeconds(Number(e.target.value))}
|
||||
className="w-full rounded-md border border-secondary-300 dark:border-secondary-600 bg-white dark:bg-secondary-800 px-3 py-2 text-sm"
|
||||
data-testid="outbox-timeout-input"
|
||||
/>
|
||||
<p className="text-xs text-secondary-500">{t('outbox.recoverStuckHelp')}</p>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<label
|
||||
htmlFor="outbox-retention-input"
|
||||
className="block text-sm font-medium text-secondary-700 dark:text-secondary-300"
|
||||
>
|
||||
{t('outbox.retentionDays')}
|
||||
</label>
|
||||
<input
|
||||
id="outbox-retention-input"
|
||||
type="number"
|
||||
min={1}
|
||||
max={365}
|
||||
value={retentionDays}
|
||||
onChange={(e) => setRetentionDays(Number(e.target.value))}
|
||||
className="w-full rounded-md border border-secondary-300 dark:border-secondary-600 bg-white dark:bg-secondary-800 px-3 py-2 text-sm"
|
||||
data-testid="outbox-retention-input"
|
||||
/>
|
||||
<p className="text-xs text-secondary-500">{t('outbox.cleanupPublishedHelp')}</p>
|
||||
</>
|
||||
)}
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="ghost" onClick={() => setMaintenance(null)}>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
<Button variant="primary" onClick={handleMaintenance} disabled={isMutating} data-testid="outbox-maintenance-confirm">
|
||||
{t('common.confirm')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user