| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| export const VESSELS_OPS_STATUS_KEY = 'vessels:ops-status'; |
|
|
| const API_BASE = (import.meta.env.VITE_API_URL as string | undefined) ?? '/api'; |
| const POLL_INTERVAL_MS = 15_000; |
| const REQUEST_TIMEOUT_MS = 4_000; |
|
|
| export interface VesselsAgentTelemetry { |
| dispatches_today: number; |
| last_dispatch: string | null; |
| } |
|
|
| export interface VesselsModuleHealth { |
| id: string; |
| mounted: boolean; |
| ok: boolean; |
| probe_path?: string | null; |
| } |
|
|
| export interface VesselsOpsStatus { |
| |
| vesselsTracked: number; |
| anomaliesOpen: number; |
| anomaliesTotal: number; |
| riskHistoryRows: number; |
| orgScoped: boolean; |
| dbOk: boolean; |
| |
| modulesTotal: number; |
| modulesHealthy: number; |
| modules: VesselsModuleHealth[]; |
| |
| agents: Record<string, VesselsAgentTelemetry>; |
| lastUpdated: string; |
| |
| reachable: boolean; |
| } |
|
|
| const ZERO_STATUS: VesselsOpsStatus = { |
| vesselsTracked: 0, |
| anomaliesOpen: 0, |
| anomaliesTotal: 0, |
| riskHistoryRows: 0, |
| orgScoped: false, |
| dbOk: false, |
| modulesTotal: 0, |
| modulesHealthy: 0, |
| modules: [], |
| agents: {}, |
| lastUpdated: new Date(0).toISOString(), |
| reachable: false, |
| }; |
|
|
| interface OpsCoreSnapshot { |
| generated_at?: string; |
| b2_live_counts?: { |
| db_ok?: boolean; |
| org_scoped?: boolean; |
| anomalies_total?: number; |
| anomalies_open?: number; |
| risk_history_rows?: number; |
| vessels_under_risk_surveillance?: number; |
| }; |
| b3_modules?: { |
| total?: number; |
| healthy?: number; |
| items?: VesselsModuleHealth[]; |
| }; |
| } |
|
|
| async function fetchSnapshot(): Promise<OpsCoreSnapshot | null> { |
| if (typeof window === 'undefined') return null; |
| const controller = new AbortController(); |
| const timer = window.setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); |
| try { |
| const res = await fetch(`${API_BASE}/vessels/ops-core/snapshot`, { |
| method: 'GET', |
| headers: { Accept: 'application/json' }, |
| signal: controller.signal, |
| credentials: 'include', |
| }); |
| if (!res.ok) return null; |
| return (await res.json()) as OpsCoreSnapshot; |
| } catch { |
| return null; |
| } finally { |
| window.clearTimeout(timer); |
| } |
| } |
|
|
| function toStatus(snap: OpsCoreSnapshot | null): VesselsOpsStatus { |
| if (!snap) return { ...ZERO_STATUS, lastUpdated: new Date().toISOString(), reachable: false }; |
|
|
| const counts = snap.b2_live_counts ?? {}; |
| const modulesBlock = snap.b3_modules ?? {}; |
| const modules: VesselsModuleHealth[] = (modulesBlock.items ?? []).map((m) => ({ |
| id: m.id, |
| mounted: Boolean(m.mounted), |
| ok: Boolean(m.ok), |
| probe_path: m.probe_path ?? null, |
| })); |
|
|
| const vesselsTracked = counts.vessels_under_risk_surveillance ?? 0; |
| const anomaliesOpen = counts.anomalies_open ?? 0; |
| const anomaliesTotal = counts.anomalies_total ?? 0; |
| const riskHistoryRows = counts.risk_history_rows ?? 0; |
|
|
| const now = new Date(); |
| const hourAgo = new Date(Date.now() - 60 * 60 * 1000).toISOString(); |
|
|
| |
| |
| |
| const agents: Record<string, VesselsAgentTelemetry> = { |
| 'vsl-voyage-risk': { |
| dispatches_today: anomaliesOpen, |
| last_dispatch: anomaliesOpen > 0 ? hourAgo : null, |
| }, |
| 'vsl-psc': { |
| dispatches_today: Math.max(0, riskHistoryRows - anomaliesTotal), |
| last_dispatch: riskHistoryRows > 0 ? hourAgo : null, |
| }, |
| 'vsl-formula': { |
| dispatches_today: riskHistoryRows, |
| last_dispatch: riskHistoryRows > 0 ? hourAgo : null, |
| }, |
| 'vsl-sanctions': { |
| dispatches_today: 0, |
| last_dispatch: null, |
| }, |
| 'vsl-dark-detection': { |
| dispatches_today: anomaliesOpen, |
| last_dispatch: anomaliesOpen > 0 ? hourAgo : null, |
| }, |
| }; |
|
|
| return { |
| vesselsTracked, |
| anomaliesOpen, |
| anomaliesTotal, |
| riskHistoryRows, |
| orgScoped: Boolean(counts.org_scoped), |
| dbOk: Boolean(counts.db_ok), |
| modulesTotal: modulesBlock.total ?? modules.length, |
| modulesHealthy: modulesBlock.healthy ?? modules.filter((m) => m.ok).length, |
| modules, |
| agents, |
| lastUpdated: snap.generated_at ?? now.toISOString(), |
| reachable: true, |
| }; |
| } |
|
|
| function writeStatus(status: VesselsOpsStatus): void { |
| try { |
| localStorage.setItem(VESSELS_OPS_STATUS_KEY, JSON.stringify(status)); |
| } catch { |
| |
| } |
| } |
|
|
| let _activeTimer: ReturnType<typeof setInterval> | null = null; |
| let _refcount = 0; |
|
|
| |
| |
| |
| |
| |
| |
| export function startVesselsStore(): () => void { |
| if (typeof window === 'undefined') return () => {}; |
|
|
| _refcount += 1; |
|
|
| if (_activeTimer === null) { |
| |
| |
| |
| if (!localStorage.getItem(VESSELS_OPS_STATUS_KEY)) { |
| writeStatus(ZERO_STATUS); |
| } |
| const tick = (): void => { |
| void fetchSnapshot().then((snap) => writeStatus(toStatus(snap))); |
| }; |
| tick(); |
| _activeTimer = setInterval(tick, POLL_INTERVAL_MS); |
| } |
|
|
| return () => { |
| _refcount = Math.max(0, _refcount - 1); |
| if (_refcount === 0 && _activeTimer !== null) { |
| clearInterval(_activeTimer); |
| _activeTimer = null; |
| } |
| }; |
| } |
|
|
| |
| |
| |
| |
| export function readVesselsStatus(): VesselsOpsStatus | null { |
| if (typeof window === 'undefined') return null; |
| try { |
| const raw = localStorage.getItem(VESSELS_OPS_STATUS_KEY); |
| return raw ? (JSON.parse(raw) as VesselsOpsStatus) : null; |
| } catch { |
| return null; |
| } |
| } |
|
|