mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-06 08:58:05 +00:00
38aabe7064
* feat: classify stack deploy and update failures with suggested next actions Failed deploy and update responses now carry a failure classification (cause category, headline, and suggested next step) derived from the compose error output. The recovery panel and chip render the classification and include it in copied diagnostics, and gateway-style failures surface as a node-unreachable cause. * feat: add update and rollback readiness reports for stacks Before a manual update, Sencho now shows an advisory readiness verdict computed from the stored preflight result, open drift findings, live container health, the pending image change, the rollback backup slot, and node disk headroom. The Stack Dossier gains a rollback readiness section that states what a rollback can restore and explicitly discloses that volume and bind-mounted data are not covered. Toolbar and sidebar updates now share one update path, and admins can create a fleet snapshot from the readiness dialog before updating. Nodes that do not advertise the capability keep the direct update flow. * feat: observe stack health after updates with a post-deploy health gate After a deploy or update succeeds, Sencho now watches the stack for a configurable observation window and records a passed, failed, or unknown verdict: containers must stay running, healthchecks must report healthy, and restart loops or disappearing containers fail the gate. The deploy panel shows the observation live and holds off auto-closing until the verdict lands, a failed gate surfaces the existing recovery actions including rollback, and the stack timeline records update started and gate verdict events. Scheduled, webhook, bulk, and git-source updates are gated the same way; rollbacks and installs are deliberately not. The gate is observational only and can be tuned or disabled per node under host alert settings. * docs: document health-gated updates and rollback readiness New operator page covering the update readiness dialog, the post-update health gate and its settings, the rollback readiness disclosure, and classified failures, with cross-links from the atomic deployments and deploy progress pages. The API reference gains the readiness and health-gate endpoints, the healthGateId success field, and the failure classification schema on deploy and update error responses. * feat: withhold the success verdict while the health gate observes An update used to show a green Succeeded that a failed health gate then contradicted moments later. The deploy modal now reports Verifying health while the gate observes, shows success only when the gate passes, and makes a failed or unknown gate the headline result; success toasts soften to a verifying message while a gate runs. The mobile recovery card groups its actions behind one bottom-right Take action menu so it stays compact on a phone, with the classified cause still visible on the card. A successful image update now also counts as the last known-good marker in rollback readiness, and the docs gain screenshots of the readiness dialog, gate states, dossier section, and settings. * fix: harden log format strings and the env existence path check Log calls that interpolated the stack name into the console format string now use constant format strings with placeholder arguments, and envExists validates path containment inline at its filesystem access, matching the established patterns used elsewhere in the same files. * test: adapt deploy modal success specs to the post-deploy health gate The deploy feedback modal now withholds its success verdict while the health gate observes the new containers, showing "Verifying health" until the gate passes. The two success-path E2E tests waited for "Succeeded" within the gate's 90s default window and timed out. Shorten the observation window to the 15s minimum for these tests via the settings API, assert the verify-then-succeed sequence the modal actually renders, and restore the default window afterward so the test value does not leak into later runs. * fix: serialize health gate polling and harden gate observation Address race conditions in the post-update health gate found in review. Backend: the gate poller used setInterval, so a Docker observe slower than the 5s tick could overlap the next poll and corrupt the restart and missing-container accounting, and a wedged socket could leave a poll pending forever. Polling is now single-flight: each cycle self-schedules the next only after it settles, and the observe is bounded by an 8s timeout so a hung probe counts as a poll error and resolves the gate unknown after three in a row. Frontend: the gate poller could overlap requests, letting a slow earlier "observing" response overwrite an already-applied terminal verdict. It is now single-flight with a terminal latch, so a late response can never roll the UI back from passed or failed. Also reject a non-digit nodeId on the snapshot coverage route instead of letting parseInt coerce it, document that turning off the deploy progress panel opts out of the live gate UI while the gate still runs server-side, and add gate-coverage tests for the webhook, git source, and auto-update apply paths plus the new single-flight, observe-timeout, and recovery cases.
299 lines
10 KiB
TypeScript
299 lines
10 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
import {
|
|
Rocket, RefreshCcw, CircleStop, Play, ArrowUp, Activity, Loader2, AlertCircle,
|
|
TriangleAlert, CircleCheck, HeartPulse, HeartCrack,
|
|
} from 'lucide-react';
|
|
import type { LucideIcon } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { toast } from '@/components/ui/toast-store';
|
|
import { apiFetch } from '@/lib/api';
|
|
import { formatTimeAgo } from '@/lib/relativeTime';
|
|
import type { NotificationItem } from '@/components/dashboard/types';
|
|
|
|
type ActivityLevel = 'info' | 'warning' | 'error';
|
|
const ACTIVITY_LEVELS: readonly string[] = ['info', 'warning', 'error'];
|
|
|
|
interface ActivityEvent {
|
|
id: number;
|
|
level: ActivityLevel;
|
|
category?: string;
|
|
message: string;
|
|
timestamp: number;
|
|
stack_name?: string;
|
|
actor_username?: string | null;
|
|
}
|
|
|
|
interface StackActivityTimelineProps {
|
|
stackName: string;
|
|
liveEvents?: NotificationItem[];
|
|
}
|
|
|
|
const CATEGORY_ICON: Record<string, LucideIcon> = {
|
|
deploy_success: Rocket,
|
|
stack_restarted: RefreshCcw,
|
|
stack_stopped: CircleStop,
|
|
stack_started: Play,
|
|
image_update_applied: ArrowUp,
|
|
drift_detected: TriangleAlert,
|
|
drift_resolved: CircleCheck,
|
|
update_started: ArrowUp,
|
|
health_gate_passed: HeartPulse,
|
|
health_gate_failed: HeartCrack,
|
|
};
|
|
|
|
const DAY_MS = 86_400_000;
|
|
const PAGE_SIZE = 50;
|
|
const DAY_BUCKET_REFRESH_MS = 60_000;
|
|
|
|
const SYSTEM_ACTOR_LABEL: Record<string, string> = {
|
|
'system': 'System',
|
|
'system:autoheal': 'Auto-Heal',
|
|
'system:scheduler': 'Scheduler',
|
|
'system:image-update': 'Image Update',
|
|
'system:docker-events': 'Docker',
|
|
'system:blueprint': 'Blueprint',
|
|
'system:monitor': 'Monitor',
|
|
'system:policy': 'Policy',
|
|
};
|
|
|
|
function formatActor(actor: string): { label: string; isSystem: boolean } {
|
|
const isSystem = actor === 'system' || actor.startsWith('system:');
|
|
return { label: SYSTEM_ACTOR_LABEL[actor] ?? actor, isSystem };
|
|
}
|
|
|
|
function isActivityEvent(value: unknown): value is ActivityEvent {
|
|
if (!value || typeof value !== 'object') return false;
|
|
const v = value as Record<string, unknown>;
|
|
return typeof v.id === 'number'
|
|
&& typeof v.timestamp === 'number'
|
|
&& typeof v.message === 'string'
|
|
&& typeof v.level === 'string'
|
|
&& ACTIVITY_LEVELS.includes(v.level);
|
|
}
|
|
|
|
function dayLabel(ts: number, now: number): 'Today' | 'Yesterday' | 'Earlier' {
|
|
const todayStart = new Date(now);
|
|
todayStart.setHours(0, 0, 0, 0);
|
|
const todayMs = todayStart.getTime();
|
|
if (ts >= todayMs) return 'Today';
|
|
if (ts >= todayMs - DAY_MS) return 'Yesterday';
|
|
return 'Earlier';
|
|
}
|
|
|
|
function groupEvents(events: ActivityEvent[], now: number): { label: string; events: ActivityEvent[] }[] {
|
|
const groups: Record<string, ActivityEvent[]> = {};
|
|
const order: string[] = [];
|
|
for (const e of events) {
|
|
const label = dayLabel(e.timestamp, now);
|
|
if (!groups[label]) { groups[label] = []; order.push(label); }
|
|
groups[label].push(e);
|
|
}
|
|
return order.map(label => ({ label, events: groups[label] }));
|
|
}
|
|
|
|
export function StackActivityTimeline({ stackName, liveEvents }: StackActivityTimelineProps) {
|
|
const [events, setEvents] = useState<ActivityEvent[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [loadingMore, setLoadingMore] = useState(false);
|
|
const [hasMore, setHasMore] = useState(true);
|
|
const [error, setError] = useState(false);
|
|
const [reloadKey, setReloadKey] = useState(0);
|
|
const [now, setNow] = useState(() => Date.now());
|
|
const [liveDisconnected, setLiveDisconnected] = useState(false);
|
|
const seenIdsRef = useRef(new Set<number>());
|
|
|
|
const mergeEvents = useCallback((incoming: ActivityEvent[]) => {
|
|
setEvents(prev => {
|
|
const next = [...prev];
|
|
let added = false;
|
|
for (const e of incoming) {
|
|
if (seenIdsRef.current.has(e.id)) continue;
|
|
seenIdsRef.current.add(e.id);
|
|
next.push(e);
|
|
added = true;
|
|
}
|
|
if (!added) return prev;
|
|
next.sort((a, b) => b.timestamp - a.timestamp || b.id - a.id);
|
|
return next;
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
setLoading(true);
|
|
setError(false);
|
|
seenIdsRef.current = new Set();
|
|
setEvents([]);
|
|
setHasMore(true);
|
|
|
|
apiFetch(`/stacks/${stackName}/activity?limit=${PAGE_SIZE + 1}`)
|
|
.then(async r => {
|
|
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
|
return r.json() as Promise<{ events: ActivityEvent[] }>;
|
|
})
|
|
.then(data => {
|
|
if (cancelled) return;
|
|
const more = data.events.length > PAGE_SIZE;
|
|
const trimmed = more ? data.events.slice(0, PAGE_SIZE) : data.events;
|
|
setHasMore(more);
|
|
trimmed.forEach(e => seenIdsRef.current.add(e.id));
|
|
setEvents(trimmed);
|
|
})
|
|
.catch(() => {
|
|
if (cancelled) return;
|
|
setError(true);
|
|
setHasMore(false);
|
|
})
|
|
.finally(() => { if (!cancelled) setLoading(false); });
|
|
|
|
return () => { cancelled = true; };
|
|
}, [stackName, reloadKey]);
|
|
|
|
useEffect(() => {
|
|
if (!liveEvents || liveEvents.length === 0) return;
|
|
const safe = liveEvents.filter(isActivityEvent);
|
|
if (safe.length === 0) return;
|
|
mergeEvents(safe);
|
|
}, [liveEvents, mergeEvents]);
|
|
|
|
useEffect(() => {
|
|
// Skip the day-bucket refresh while the tab is hidden so a backgrounded
|
|
// panel does not re-render every minute for no visible effect.
|
|
const id = window.setInterval(() => {
|
|
if (typeof document !== 'undefined' && document.hidden) return;
|
|
setNow(Date.now());
|
|
}, DAY_BUCKET_REFRESH_MS);
|
|
return () => window.clearInterval(id);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
// Upstream useNotifications dispatches this event when its WebSocket
|
|
// flips. A single layout-level listener keeps the timeline honest about
|
|
// whether new events would actually arrive.
|
|
const handler = (e: Event) => {
|
|
const detail = (e as CustomEvent<{ connected: boolean }>).detail;
|
|
setLiveDisconnected(detail?.connected === false);
|
|
};
|
|
window.addEventListener('sencho:notifications-connection', handler);
|
|
return () => window.removeEventListener('sencho:notifications-connection', handler);
|
|
}, []);
|
|
|
|
const loadMore = useCallback(async () => {
|
|
const oldestEvent = events[events.length - 1];
|
|
if (!oldestEvent) return;
|
|
setLoadingMore(true);
|
|
try {
|
|
const params = new URLSearchParams({
|
|
limit: String(PAGE_SIZE + 1),
|
|
before: String(oldestEvent.timestamp),
|
|
beforeId: String(oldestEvent.id),
|
|
});
|
|
const r = await apiFetch(`/stacks/${stackName}/activity?${params.toString()}`);
|
|
if (!r.ok) {
|
|
toast.error('Failed to load more activity');
|
|
// 5xx is likely server-side; stop offering a button that keeps failing.
|
|
if (r.status >= 500) setHasMore(false);
|
|
return;
|
|
}
|
|
const data: { events: ActivityEvent[] } = await r.json();
|
|
const more = data.events.length > PAGE_SIZE;
|
|
const trimmed = more ? data.events.slice(0, PAGE_SIZE) : data.events;
|
|
setHasMore(more);
|
|
mergeEvents(trimmed);
|
|
} catch (err) {
|
|
console.error('[StackActivity] loadMore failed:', err);
|
|
toast.error('Failed to load more activity');
|
|
setHasMore(false);
|
|
} finally {
|
|
setLoadingMore(false);
|
|
}
|
|
}, [events, stackName, mergeEvents]);
|
|
|
|
const groups = useMemo(() => groupEvents(events, now), [events, now]);
|
|
|
|
if (loading) {
|
|
return (
|
|
<div className="flex items-center justify-center py-8">
|
|
<Loader2 className="w-4 h-4 animate-spin text-muted-foreground" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (error && events.length === 0) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center py-8 gap-2">
|
|
<AlertCircle className="w-5 h-5 text-destructive/60" />
|
|
<span className="font-mono text-[11px] text-muted-foreground">Activity unavailable</span>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-7 font-mono text-[10px]"
|
|
onClick={() => setReloadKey(k => k + 1)}
|
|
>
|
|
Retry
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (events.length === 0) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center py-8 gap-2">
|
|
<Activity className="w-5 h-5 text-muted-foreground/40" />
|
|
<span className="font-mono text-[11px] text-muted-foreground">No activity recorded yet</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col gap-3 py-3">
|
|
{liveDisconnected && (
|
|
<div
|
|
role="status"
|
|
className="font-mono text-[10px] text-stat-subtitle italic px-1"
|
|
>
|
|
Live updates offline; reconnecting…
|
|
</div>
|
|
)}
|
|
{groups.map(g => (
|
|
<div key={g.label} role="list" aria-label={`Stack activity, ${g.label}`}>
|
|
<div className="font-mono text-[9px] uppercase tracking-[0.18em] text-stat-subtitle mb-1.5 px-1">{g.label}</div>
|
|
{g.events.map(e => {
|
|
const Icon = CATEGORY_ICON[e.category ?? ''] ?? Activity;
|
|
const actor = e.actor_username ? formatActor(e.actor_username) : null;
|
|
return (
|
|
<div
|
|
key={e.id}
|
|
role="listitem"
|
|
className="flex items-start gap-2 py-1.5 px-1 rounded-md hover:bg-glass-highlight/30 transition-colors"
|
|
>
|
|
<Icon className="w-3 h-3 mt-0.5 shrink-0 text-brand/70" strokeWidth={1.5} />
|
|
<div className="flex-1 min-w-0">
|
|
<span className="font-mono text-[11px] text-foreground/90">{e.message}</span>
|
|
{actor && (
|
|
<span className={`ml-1.5 font-mono text-[10px] text-stat-subtitle${actor.isSystem ? ' italic' : ''}`}>
|
|
{actor.isSystem ? `via ${actor.label}` : `by ${actor.label}`}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<span className="font-mono text-[10px] text-stat-subtitle shrink-0">{formatTimeAgo(e.timestamp)}</span>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
))}
|
|
{hasMore && (
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="w-full h-7 font-mono text-[10px] text-muted-foreground"
|
|
onClick={() => void loadMore()}
|
|
disabled={loadingMore}
|
|
>
|
|
{loadingMore ? <Loader2 className="w-3 h-3 animate-spin" /> : 'Load more'}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|