feat(auth): harden session management and security

- Implement server-side session revocation on logout
- Add IP extraction utility and admin setup error handling
- Improve error codes and response consistency for auth endpoints
- Refine landing page redirect logic to handle transient DB outages
- Update deployment documentation and configuration defaults
This commit is contained in:
Team Dark Devil committed 2026-11-19 20:22:01 +06:00
1 parent 935f12a1e9
commit 89e4d68690
81 files changed
+11530 -6916

No files matched your search

+5 -1
View File
@@ -16,15 +16,19 @@ function safeNextPath() {
export function AdminLogin({
showDemoHint = false,
databaseReady = true,
}: {
showDemoHint?: boolean;
databaseReady?: boolean;
}) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [remember, setRemember] = useState(true);
const [showPassword, setShowPassword] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [errorMessage, setErrorMessage] = useState(
databaseReady ? '' : 'Database connection unavailable. Retry once PostgreSQL recovers.',
);
const [theme, setTheme] = useState<'dark' | 'light'>('dark');
useEffect(() => {
+32 -3
View File
@@ -4,13 +4,15 @@ import { Button } from '@/components/ui/Button';
import { Input } from '@/components/ui/Input';
import { ArrowRight } from 'lucide-react';
import Link from 'next/link';
import { useState } from 'react';
import { useEffect, useState } from 'react';
export function AdminSetup({
databaseReady = true,
databaseError = '',
initialEmail = '',
}: {
databaseReady?: boolean;
databaseError?: string;
initialEmail?: string;
}) {
const [email, setEmail] = useState(initialEmail || 'admin@datastack.dev');
@@ -19,8 +21,25 @@ export function AdminSetup({
const [setupKey, setSetupKey] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState(
databaseReady ? '' : 'Database connection unavailable.',
databaseReady ? '' : databaseError || 'Database connection unavailable.',
);
const [theme, setTheme] = useState<'dark' | 'light'>('dark');
useEffect(() => {
const savedTheme = localStorage.getItem('datastack-theme');
const initialTheme = savedTheme === 'light' ? 'light' : 'dark';
setTheme(initialTheme);
document.documentElement.classList.toggle('light', initialTheme === 'light');
document.documentElement.classList.toggle('dark', initialTheme === 'dark');
}, []);
const toggleTheme = () => {
const nextTheme = theme === 'dark' ? 'light' : 'dark';
setTheme(nextTheme);
localStorage.setItem('datastack-theme', nextTheme);
document.documentElement.classList.toggle('light', nextTheme === 'light');
document.documentElement.classList.toggle('dark', nextTheme === 'dark');
};
const submit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
@@ -59,7 +78,17 @@ export function AdminSetup({
return (
<main className="relative grid min-h-dvh place-items-center bg-[var(--bg)] p-4 text-[var(--text)]">
<section className="w-full max-w-[320px] border border-[var(--border)] bg-[var(--surface)]">
<Button
type="button"
variant="icon"
size="icon"
icon={theme === 'dark' ? 'Sun' : 'Moon'}
aria-label="Toggle theme"
title="Toggle theme"
className="fixed right-3 top-3 z-10 h-7 w-7 border border-[var(--border)] text-[var(--text-muted)]"
onClick={toggleTheme}
/>
<section className="z-0 w-full max-w-[320px] border border-[var(--border)] bg-[var(--surface)]">
<header className="flex items-center justify-between border-b border-[var(--border)] px-4 py-3">
<Link href="/" className="flex items-center gap-2 text-[12.5px] font-medium text-[var(--text)]">
<span className="grid h-3 w-3 place-items-center bg-[var(--accent)]" aria-hidden="true">
+52 -14
View File
@@ -43,12 +43,18 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
type: 'rotate' | 'revoke' | 'delete';
client: ClientItem;
} | null>(null);
const [purgeFilesOnDelete, setPurgeFilesOnDelete] = useState(false);
const [editClient, setEditClient] = useState<ClientItem | null>(null);
const [editName, setEditName] = useState('');
const [editRateLimit, setEditRateLimit] = useState('100');
const [editError, setEditError] = useState('');
const [processing, setProcessing] = useState(false);
const closeConfirmModal = () => {
setConfirmAction(null);
setPurgeFilesOnDelete(false);
};
const openEditModal = (client: ClientItem) => {
setEditClient(client);
setEditName(client.name);
@@ -66,8 +72,8 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
setEditError('Display name is required.');
return;
}
if (!Number.isFinite(numericRateLimit) || numericRateLimit < 1 || numericRateLimit > 10000) {
setEditError('Rate limit must be between 1 and 10,000 events/s.');
if (!Number.isInteger(numericRateLimit) || numericRateLimit < 1 || numericRateLimit > 100000) {
setEditError('Rate limit must be an integer between 1 and 100,000 events/s.');
return;
}
@@ -147,20 +153,30 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
}
};
const executeDeleteClient = async (client: ClientItem) => {
const executeDeleteClient = async (client: ClientItem, purgeFiles: boolean) => {
setProcessing(true);
try {
const response = await fetch(`/api/clients/${encodeURIComponent(client.clientId)}`, {
method: 'DELETE',
});
const query = purgeFiles ? '?purgeFiles=true' : '';
const response = await fetch(
`/api/clients/${encodeURIComponent(client.clientId)}${query}`,
{
method: 'DELETE',
},
);
if (response.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await response.json();
if (!response.ok) throw new Error(data.error || 'Unable to delete client');
setConfirmAction(null);
success('Client deleted', `${client.clientId} has been permanently removed.`);
closeConfirmModal();
const purgedCount = Number(data?.purgedFiles ?? 0);
success(
'Client deleted',
purgeFiles
? `${client.clientId} removed and ${purgedCount} stored CSV file${purgedCount === 1 ? '' : 's'} purged.`
: `${client.clientId} has been permanently removed (stored CSV files retained).`,
);
await onStatusChange();
} catch (deleteError) {
error('Client deletion failed', deleteError instanceof Error ? deleteError.message : 'Unknown error');
@@ -171,6 +187,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
const toggleStatus = (client: ClientItem) => {
if (client.status === 'active') {
setPurgeFilesOnDelete(false);
setConfirmAction({ type: 'revoke', client });
} else {
void executeStatusChange(client, 'active');
@@ -182,7 +199,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
if (confirmAction.type === 'rotate') {
await executeRotateKey(confirmAction.client);
} else if (confirmAction.type === 'delete') {
await executeDeleteClient(confirmAction.client);
await executeDeleteClient(confirmAction.client, purgeFilesOnDelete);
} else {
await executeStatusChange(confirmAction.client, 'revoked');
}
@@ -302,7 +319,10 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
className="h-6 w-6 border border-transparent hover:border-[var(--danger)] hover:text-[var(--danger)]"
title={`Delete ${client.clientId}`}
aria-label={`Delete ${client.clientId}`}
onClick={() => setConfirmAction({ type: 'delete', client })}
onClick={() => {
setPurgeFilesOnDelete(false);
setConfirmAction({ type: 'delete', client });
}}
/>
</div>
</TableCell>
@@ -350,7 +370,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
compact
type="number"
min="1"
max="10000"
max="100000"
value={editRateLimit}
onChange={(event) => setEditRateLimit(event.target.value)}
required
@@ -365,7 +385,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
<Modal
isOpen={Boolean(confirmAction)}
onClose={() => !processing && setConfirmAction(null)}
onClose={() => !processing && closeConfirmModal()}
title={
confirmAction?.type === 'rotate'
? 'Rotate credentials'
@@ -376,7 +396,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
maxWidth="xs"
footer={
<>
<Button type="button" variant="ghost" size="xs" disabled={processing} onClick={() => setConfirmAction(null)}>
<Button type="button" variant="ghost" size="xs" disabled={processing} onClick={closeConfirmModal}>
Cancel
</Button>
<Button
@@ -402,6 +422,22 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
? `Permanently delete client ${confirmAction.client.clientId}? This removes the client registration and credentials.`
: `Revoke access for ${confirmAction?.client.clientId || ''}?`}
</p>
{confirmAction?.type === 'delete' && (
<label className="mt-3 flex cursor-pointer items-start gap-2 border border-[var(--border)] bg-[var(--surface-2)] p-2.5 text-[11.5px] text-[var(--text)]">
<input
type="checkbox"
checked={purgeFilesOnDelete}
onChange={(event) => setPurgeFilesOnDelete(event.target.checked)}
className="mt-0.5 h-3.5 w-3.5 shrink-0 accent-[var(--danger)]"
/>
<span>
Also purge stored CSV files for this client
<span className="mt-0.5 block text-[10.5px] text-[var(--text-faint)]">
Deletes partitioned disk CSV files along with the client registration.
</span>
</span>
</label>
)}
</Modal>
<Modal
@@ -420,7 +456,6 @@ export function CredentialsDisplay({ credentials }: { credentials: ClientCredent
return (
<div className="flex flex-col gap-2.5">
<CredentialItem label="Client ID" value={credentials.clientId} />
<CredentialItem label="Secret key" value={credentials.secret} />
<div className="flex flex-col gap-1">
<div className="flex items-center justify-between gap-2">
<label htmlFor={`token-${credentials.clientId}`} className="text-[10px] uppercase tracking-wider text-[var(--text-faint)]">
@@ -435,6 +470,9 @@ export function CredentialsDisplay({ credentials }: { credentials: ClientCredent
rows={3}
className="font-mono text-[11px] text-[var(--accent)]"
/>
<p className="text-[10.5px] text-[var(--text-faint)]">
Use this signed JWT token for both Socket.IO handshakes and HTTP Bearer ingestion.
</p>
</div>
</div>
);
+102 -19
View File
@@ -15,7 +15,14 @@ import {
TableRow,
TableWrap,
} from '@/components/ui/Table';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import {
countMatchingBufferedEvents,
extractContentDispositionFilename,
formatEventTimeLabel,
type BufferedSummary,
} from '@/lib/vconUiHelpers';
import Link from 'next/link';
import React, { useCallback, useEffect, useState } from 'react';
@@ -40,6 +47,12 @@ type DashboardMetrics = {
totalFiles: number;
totalRows: number;
};
runtime?: {
ingestionBridgeStatus?: 'online' | 'auth_error' | 'error' | 'offline';
ingestionBridgeError?: string;
serverPort?: number;
webPort?: number;
};
};
type StorageFile = {
@@ -51,6 +64,7 @@ type StorageFile = {
};
export const DashboardOverview: React.FC = () => {
const { error, info } = useToast();
const [metrics, setMetrics] = useState<DashboardMetrics | null>(() =>
getCached<DashboardMetrics | null>('vcon:metrics', null)
);
@@ -63,7 +77,11 @@ export const DashboardOverview: React.FC = () => {
const [files, setFiles] = useState<StorageFile[]>(() =>
getCached<StorageFile[]>('vcon:storage-files', [])
);
const [bufferedSummary, setBufferedSummary] = useState<BufferedSummary | null>(() =>
getCached<BufferedSummary | null>('vcon:buffered-summary', null)
);
const [isRefreshing, setIsRefreshing] = useState(false);
const [isExporting, setIsExporting] = useState(false);
const [fetchError, setFetchError] = useState('');
const refresh = useCallback(async () => {
@@ -72,7 +90,7 @@ export const DashboardOverview: React.FC = () => {
const [metricsResponse, eventsResponse, storageResponse, clientsResponse] =
await Promise.all([
fetch('/api/metrics', { cache: 'no-store' }),
fetch('/api/events?limit=8', { cache: 'no-store' }),
fetch('/api/events?limit=8&includeBufferedSummary=true', { cache: 'no-store' }),
fetch('/api/storage', { cache: 'no-store' }),
fetch('/api/clients', { cache: 'no-store' }),
]);
@@ -99,6 +117,9 @@ export const DashboardOverview: React.FC = () => {
if (eventsResponse.ok) {
const data = await eventsResponse.json();
setEvents(setCached('vcon:dashboard-events', data.events ?? []));
if (data.bufferedSummary) {
setBufferedSummary(setCached('vcon:buffered-summary', data.bufferedSummary));
}
} else {
errors.push('events');
}
@@ -145,12 +166,30 @@ export const DashboardOverview: React.FC = () => {
.filter((file) => file.date === today)
.reduce((total, file) => total + file.linesCount, 0);
const bufferedCount = metrics?.bufferStats.currentBufferSize ?? 0;
const eventsToday = storedRecordsToday + bufferedCount;
const bufferedTodayRaw = bufferedSummary
? countMatchingBufferedEvents(bufferedSummary, {
clientId: 'all',
fromDate: today,
toDate: today,
}).matchingCount
: 0;
const bufferedTodayCount = metrics
? Math.min(bufferedTodayRaw, bufferedCount)
: bufferedTodayRaw;
const eventsToday = storedRecordsToday + bufferedTodayCount;
const activeClients =
metrics?.activeClients ??
clients.filter((client) => client.status === 'active').length;
const totalClients = metrics?.totalClients ?? clients.length;
const activeSockets = metrics?.activeSockets ?? 0;
const bridgeStatus = metrics?.runtime?.ingestionBridgeStatus;
const bridgeAuthError = bridgeStatus === 'auth_error';
const socketSuffix =
bridgeStatus === 'auth_error'
? `${activeSockets} ws · auth err`
: bridgeStatus === 'offline'
? `${activeSockets} ws · offline`
: `${activeSockets} ws`;
const totalStorage = metrics?.storageStats.totalMB ?? '0.00';
const totalStorageBytes = metrics?.storageStats.totalBytes ?? 0;
const storageValue =
@@ -165,6 +204,41 @@ export const DashboardOverview: React.FC = () => {
metrics?.storageStats.totalRows ??
files.reduce((sum, file) => sum + file.linesCount, 0);
const handleQuickExport = async () => {
setIsExporting(true);
try {
const res = await fetch('/api/export?clientId=all', { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (!res.ok) {
const errJson = await res.json().catch(() => ({}));
error('Export failed', errJson.error || `Export failed with HTTP ${res.status}.`);
return;
}
const blob = await res.blob();
const filename = extractContentDispositionFilename(
res.headers.get('content-disposition'),
`datastack_export_all_${new Date().toISOString().slice(0, 10)}.csv`
);
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(objectUrl);
info('Export downloaded', `${filename} downloaded.`);
await refresh();
} catch (err) {
error('Export failed', err instanceof Error ? err.message : 'Unable to export dataset.');
} finally {
setIsExporting(false);
}
};
return (
<PageShell
title="Dashboard"
@@ -179,11 +253,16 @@ export const DashboardOverview: React.FC = () => {
loading={isRefreshing}
onClick={() => void refresh()}
/>
<a href="/api/export?clientId=all" download>
<Button size="xs" variant="secondary" icon="Download">
Export
</Button>
</a>
<Button
size="xs"
variant="secondary"
icon="Download"
loading={isExporting}
disabled={isExporting}
onClick={() => void handleQuickExport()}
>
Export
</Button>
<Link href="/vcon/clients">
<Button size="xs" variant="primary" icon="Plus">
New client
@@ -192,12 +271,14 @@ export const DashboardOverview: React.FC = () => {
</>
}
>
{fetchError && (
{(fetchError || bridgeAuthError) && (
<div
role="alert"
className="mb-3 border border-[var(--danger)] bg-[var(--danger-soft)] px-3 py-2 text-[11.5px] text-[var(--text)]"
>
{fetchError}
{bridgeAuthError
? `Ingestion bridge authentication mismatch (${metrics?.runtime?.ingestionBridgeError || '401'}). Check DATASTACK_INTERNAL_SECRET parity.`
: fetchError}
</div>
)}
@@ -206,8 +287,13 @@ export const DashboardOverview: React.FC = () => {
{ label: 'Events today', value: eventsToday.toLocaleString() },
{
label: 'Active clients · Sockets',
value: `${activeClients} / ${totalClients} · ${activeSockets} ws`,
dot: activeClients > 0 || activeSockets > 0 ? 'ok' : 'off',
value: `${activeClients} / ${totalClients} · ${socketSuffix}`,
dot:
bridgeAuthError
? 'off'
: activeSockets > 0 || (activeClients > 0 && bridgeStatus === 'online')
? 'ok'
: 'off',
},
{ label: 'Storage used', value: `${storageValue} ${storageUnit}` },
{ label: 'Stored rows', value: totalRows.toLocaleString() },
@@ -248,9 +334,7 @@ export const DashboardOverview: React.FC = () => {
events.map((event) => (
<TableRow key={event.eventId}>
<TableCell className="font-mono tabular-nums text-[var(--text-faint)]">
{event.timestamp.includes('T')
? new Date(event.timestamp).toLocaleTimeString()
: event.timestamp}
{formatEventTimeLabel(event.timestamp)}
</TableCell>
<TableCell className="font-mono text-[var(--accent)]">
{event.clientId}
@@ -288,13 +372,12 @@ export const DashboardOverview: React.FC = () => {
title="Storage"
meta={`${totalFiles}`}
action={
<a
href="/api/export?clientId=all"
download
<Link
href="/vcon/storage"
className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]"
>
Export ↓
</a>
Files →
</Link>
}
>
<div className="divide-y divide-[var(--border)]">
+33 -11
View File
@@ -19,6 +19,7 @@ import {
TableRow,
TableWrap,
} from '@/components/ui/Table';
import { formatEventTimeLabel } from '@/lib/vconUiHelpers';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect, useState } from 'react';
@@ -86,8 +87,10 @@ export function DataBrowser() {
window.location.assign('/vcon/login');
return;
}
if (!response.ok) throw new Error('Unable to load events');
const data = await response.json();
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || 'Unable to load events');
}
setEvents(data.events || []);
setTotal(data.total || 0);
setPageCount(data.pageCount || 1);
@@ -124,6 +127,8 @@ export function DataBrowser() {
fetchEvents();
}, [fetchEvents, filtersReady]);
const invalidRange = Boolean(fromFilter && toFilter && fromFilter > toFilter);
const firstVisible = total === 0 ? 0 : (page - 1) * PAGE_SIZE + 1;
const lastVisible = Math.min(page * PAGE_SIZE, total);
const firstPage = Math.max(1, Math.min(page - 1, pageCount - 2));
@@ -133,6 +138,11 @@ export function DataBrowser() {
);
const applyFilters = () => {
if (invalidRange) {
setErrorMessage('Start date ("From") must be on or before end date ("To").');
return;
}
setErrorMessage('');
setAppliedFilters({
clientId: clientFilter,
from: fromFilter,
@@ -147,6 +157,7 @@ export function DataBrowser() {
setFromFilter('');
setToFilter('');
setSearchFilter('');
setErrorMessage('');
setAppliedFilters({ clientId: 'all', from: '', to: '', search: '' });
setPage(1);
};
@@ -189,7 +200,13 @@ export function DataBrowser() {
}
>
<Card compact className="p-0">
<div className="grid grid-cols-1 gap-px bg-[var(--border)] sm:grid-cols-2 lg:grid-cols-5">
<form
onSubmit={(e) => {
e.preventDefault();
applyFilters();
}}
className="grid grid-cols-1 gap-px bg-[var(--border)] sm:grid-cols-2 lg:grid-cols-5"
>
<div className="bg-[var(--surface)] p-2.5">
<Select
label="Client"
@@ -237,11 +254,21 @@ export function DataBrowser() {
<Button type="button" size="xs" icon="X" onClick={resetFilters}>
Reset
</Button>
<Button type="button" size="xs" variant="primary" onClick={applyFilters}>
<Button
type="submit"
size="xs"
variant="primary"
disabled={invalidRange}
>
Apply
</Button>
</div>
</div>
</form>
{invalidRange && (
<p role="alert" className="border-t border-[var(--border)] bg-[var(--surface-2)] px-3 py-1.5 text-[11px] text-[var(--danger)]">
Start date must be on or before end date.
</p>
)}
</Card>
<Panel
@@ -274,12 +301,7 @@ export function DataBrowser() {
onClick={() => setSelectedEvent(event)}
>
<TableCell className="font-mono tabular-nums text-[var(--text)]">
{new Date(event.timestamp).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
})}
{formatEventTimeLabel(event.timestamp)}
</TableCell>
<TableCell className="font-mono text-[var(--accent)]">
{event.clientId}
+145 -76
View File
@@ -8,9 +8,16 @@ import { Pill } from '@/components/ui/Pill';
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import {
countMatchingBufferedEvents,
extractContentDispositionFilename,
getUtcDateRangeForPreset,
matchesSelectedClientFolder,
type BufferedSummary,
} from '@/lib/vconUiHelpers';
import { Braces, FileText } from 'lucide-react';
import Link from 'next/link';
import { useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
interface Client {
clientId: string;
@@ -29,10 +36,6 @@ interface StorageFile {
type ExportFormat = 'csv' | 'json';
type RangePreset = 'today' | '7d' | '30d' | 'all' | 'custom';
function dateString(date: Date) {
return date.toISOString().slice(0, 10);
}
function formatBytes(bytes: number) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 ** 2) return `${(bytes / 1024).toFixed(1)} KB`;
@@ -41,7 +44,9 @@ function formatBytes(bytes: number) {
}
function relativeTime(value: string, now: number) {
const elapsedSeconds = Math.max(0, Math.floor((now - new Date(value).getTime()) / 1000));
const ts = new Date(value).getTime();
if (!Number.isFinite(ts)) return '—';
const elapsedSeconds = Math.max(0, Math.floor((now - ts) / 1000));
if (elapsedSeconds < 60) return `${elapsedSeconds}s ago`;
if (elapsedSeconds < 3600) return `${Math.floor(elapsedSeconds / 60)}m ago`;
if (elapsedSeconds < 86_400) return `${Math.floor(elapsedSeconds / 3600)}h ago`;
@@ -57,6 +62,7 @@ export function ExportWorkspace() {
getCached<StorageFile[]>('vcon:storage-files', [])
);
const [bufferedCount, setBufferedCount] = useState<number>(0);
const [bufferedSummary, setBufferedSummary] = useState<BufferedSummary | null>(null);
const [selectedClient, setSelectedClient] = useState('all');
const [fromDate, setFromDate] = useState('');
const [toDate, setToDate] = useState('');
@@ -65,48 +71,63 @@ export function ExportWorkspace() {
const [loading, setLoading] = useState(
() => getCached<StorageFile[]>('vcon:storage-files', []).length === 0
);
const [downloading, setDownloading] = useState(false);
const [loadError, setLoadError] = useState('');
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
Promise.all([
fetch('/api/clients', { cache: 'no-store' }).then(async (response) => {
if (response.status === 401) {
window.location.assign('/vcon/login');
return { clients: [] };
}
if (!response.ok) throw new Error('Unable to load clients');
return response.json();
}),
fetch('/api/storage', { cache: 'no-store' }).then(async (response) => {
if (response.status === 401) {
window.location.assign('/vcon/login');
return { files: [] };
}
if (!response.ok) throw new Error('Unable to load stored files');
return response.json();
}),
fetch('/api/metrics', { cache: 'no-store' })
.then(async (response) => (response.ok ? response.json() : null))
.catch(() => null),
])
.then(([clientData, storageData, metricsData]) => {
setClients(setCached('vcon:clients', clientData.clients || []));
setFiles(setCached('vcon:storage-files', storageData.files || []));
if (storageData.stats) setCached('vcon:storage-stats', storageData.stats);
if (metricsData?.bufferStats) {
const refreshExportData = useCallback(async () => {
try {
const [clientData, storageData, metricsData, bufferedData] = await Promise.all([
fetch('/api/clients', { cache: 'no-store' }).then(async (response) => {
if (response.status === 401) {
window.location.assign('/vcon/login');
return { clients: [] };
}
if (!response.ok) throw new Error('Unable to load clients');
return response.json();
}),
fetch('/api/storage', { cache: 'no-store' }).then(async (response) => {
if (response.status === 401) {
window.location.assign('/vcon/login');
return { files: [] };
}
if (!response.ok) throw new Error('Unable to load stored files');
return response.json();
}),
fetch('/api/metrics', { cache: 'no-store' })
.then(async (response) => (response.ok ? response.json() : null))
.catch(() => null),
fetch('/api/events?bufferedOnly=true&limit=1', { cache: 'no-store' })
.then(async (response) => (response.ok ? response.json() : null))
.catch(() => null),
]);
setClients(setCached('vcon:clients', clientData.clients || []));
setFiles(setCached('vcon:storage-files', storageData.files || []));
if (storageData.stats) setCached('vcon:storage-stats', storageData.stats);
if (metricsData) {
setCached('vcon:metrics', metricsData);
if (metricsData.bufferStats) {
setBufferedCount(Number(metricsData.bufferStats.currentBufferSize) || 0);
}
setLoadError('');
})
.catch((fetchError) => {
setLoadError(
fetchError instanceof Error ? fetchError.message : 'Unable to load export data'
);
})
.finally(() => setLoading(false));
}
if (bufferedData?.bufferedSummary) {
setBufferedSummary(setCached('vcon:buffered-summary', bufferedData.bufferedSummary));
}
setLoadError('');
} catch (fetchError) {
setLoadError(
fetchError instanceof Error ? fetchError.message : 'Unable to load export data'
);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
void refreshExportData();
}, [refreshExportData]);
useEffect(() => {
const updateClock = () => setNow(Date.now());
updateClock();
@@ -118,7 +139,7 @@ export function ExportWorkspace() {
() =>
files.filter(
(file) =>
(selectedClient === 'all' || file.clientId === selectedClient) &&
matchesSelectedClientFolder(file.clientId, selectedClient) &&
(!fromDate || file.date >= fromDate) &&
(!toDate || file.date <= toDate)
),
@@ -156,18 +177,9 @@ export function ExportWorkspace() {
const setQuickRange = (nextPreset: Exclude<RangePreset, 'custom'>) => {
setPreset(nextPreset);
if (nextPreset === 'all') {
setFromDate('');
setToDate('');
return;
}
const today = new Date();
const start = new Date(today);
if (nextPreset === '7d') start.setDate(today.getDate() - 6);
if (nextPreset === '30d') start.setDate(today.getDate() - 29);
setFromDate(dateString(start));
setToDate(dateString(today));
const range = getUtcDateRangeForPreset(nextPreset);
setFromDate(range.from);
setToDate(range.to);
};
const exportUrl = useMemo(() => {
@@ -177,10 +189,31 @@ export function ExportWorkspace() {
return `/api/export?${params.toString()}`;
}, [format, fromDate, selectedClient, toDate]);
const invalidRange = Boolean(fromDate && toDate && fromDate > toDate);
const nothingToExport = preview.rows === 0 && bufferedCount === 0;
const filteredBuffered = useMemo(
() =>
countMatchingBufferedEvents(
bufferedSummary,
{ clientId: selectedClient, fromDate, toDate },
bufferedCount,
),
[bufferedCount, bufferedSummary, fromDate, selectedClient, toDate],
);
const downloadExport = () => {
const invalidRange = Boolean(fromDate && toDate && fromDate > toDate);
const nothingToExport = preview.rows === 0 && !filteredBuffered.hasPotentialBuffered;
const triggerBlobDownload = (blob: Blob, filename: string) => {
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(objectUrl);
};
const downloadExport = async () => {
if (invalidRange) {
error('Invalid date range', 'Start date must be on or before end date.');
return;
@@ -189,33 +222,60 @@ export function ExportWorkspace() {
error('Nothing to export', 'No stored rows or buffered events match the selected filters.');
return;
}
window.location.assign(exportUrl);
info(
'Export started',
bufferedCount > 0
? `${format.toUpperCase()} download (${preview.rows.toLocaleString()} stored + ${bufferedCount} buffered auto-flushed).`
: `${format.toUpperCase()} download for ${preview.rows.toLocaleString()} rows.`
);
setDownloading(true);
try {
const res = await fetch(exportUrl, { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (!res.ok) {
const errorJson = await res.json().catch(() => ({}));
error(
'Export failed',
errorJson.error || `Export endpoint returned HTTP ${res.status}.`
);
return;
}
const blob = await res.blob();
const defaultName = `datastack_export_${selectedClient || 'all'}_${new Date().toISOString().slice(0, 10)}.${format}`;
const filename = extractContentDispositionFilename(
res.headers.get('content-disposition'),
defaultName
);
triggerBlobDownload(blob, filename);
info(
'Export downloaded',
filteredBuffered.matchingCount > 0
? `${format.toUpperCase()} download (${preview.rows.toLocaleString()} stored + ${filteredBuffered.matchingCount.toLocaleString()} buffered flushed).`
: filteredBuffered.unattributedCount > 0
? `${format.toUpperCase()} download (${preview.rows.toLocaleString()} stored + pending backlog flushed).`
: `${format.toUpperCase()} download for ${preview.rows.toLocaleString()} rows.`
);
await refreshExportData();
} catch (err: unknown) {
error('Export failed', err instanceof Error ? err.message : 'Unable to export dataset.');
} finally {
setDownloading(false);
}
};
const downloadFile = async (file: StorageFile) => {
const url = `/api/export?clientId=${encodeURIComponent(file.clientId)}&filename=${encodeURIComponent(file.filename)}`;
try {
const res = await fetch(url, { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (!res.ok) {
const errorJson = await res.json().catch(() => ({}));
error('Download Failed', errorJson.error || 'Requested CSV file could not be exported.');
return;
}
const blob = await res.blob();
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
a.download = file.filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(objectUrl);
triggerBlobDownload(blob, file.filename);
await refreshExportData();
} catch (err: unknown) {
error('Download Failed', err instanceof Error ? err.message : 'Export download error');
}
@@ -348,11 +408,19 @@ export function ExportWorkspace() {
</strong>{' '}
files
</span>
{bufferedCount > 0 && (
{filteredBuffered.matchingCount > 0 && (
<>
<span aria-hidden="true">·</span>
<span className="text-[var(--warning)]">
+{bufferedCount} buffered (flushed on export)
+{filteredBuffered.matchingCount.toLocaleString()} buffered (flushed on export)
</span>
</>
)}
{filteredBuffered.matchingCount === 0 && filteredBuffered.unattributedCount > 0 && (
<>
<span aria-hidden="true">·</span>
<span className="text-[var(--warning)]">
+{filteredBuffered.unattributedCount.toLocaleString()} queued backlog (flushed on export)
</span>
</>
)}
@@ -382,8 +450,9 @@ export function ExportWorkspace() {
variant="primary"
size="xs"
icon="Download"
disabled={loading || invalidRange || nothingToExport}
onClick={downloadExport}
loading={downloading}
disabled={loading || downloading || invalidRange || nothingToExport}
onClick={() => void downloadExport()}
>
Download {format.toUpperCase()}
</Button>
+11 -3
View File
@@ -30,14 +30,22 @@ export function NewClientModal({ open, onClose, onCreated }: NewClientModalProps
error('Validation error', 'Client ID and Name are required.');
return;
}
const clampedRateLimit = Math.max(1, Math.min(5000, Number(rateLimit) || 100));
const numericRate = Number(rateLimit);
if (!Number.isInteger(numericRate) || numericRate < 1 || numericRate > 100000) {
error('Validation error', 'Rate limit must be an integer between 1 and 100,000 events/s.');
return;
}
setSubmitting(true);
try {
const response = await fetch('/api/clients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ clientId: trimmedId, name: trimmedName, rateLimit: clampedRateLimit }),
body: JSON.stringify({ clientId: trimmedId, name: trimmedName, rateLimit: numericRate }),
});
if (response.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await response.json();
if (!response.ok) throw new Error(data.error || 'Client registration failed');
@@ -91,7 +99,7 @@ export function NewClientModal({ open, onClose, onCreated }: NewClientModalProps
label="Rate limit (req/s)"
type="number"
min="1"
max="5000"
max="100000"
value={rateLimit}
onChange={(event) => setRateLimit(event.target.value)}
/>
+37 -15
View File
@@ -11,7 +11,11 @@ import {
} from '@/components/ui/SettingsLayout';
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import { getCached, setCached, clearAuthCaches } from '@/lib/vconCache';
import {
formatSessionExpiryLabel,
shouldRedirectProfileUnauthorized,
} from '@/lib/vconUiHelpers';
import Link from 'next/link';
import { useEffect, useState } from 'react';
@@ -49,20 +53,31 @@ export function ProfileWorkspace() {
const [signingOut, setSigningOut] = useState(false);
useEffect(() => {
fetch('/api/auth/profile', { credentials: 'include' })
fetch('/api/auth/profile', {
credentials: 'include',
cache: 'no-store',
})
.then(async (res) => {
if (!res.ok) throw new Error('Unable to load profile');
return res.json();
if (res.status === 401) {
window.location.assign('/vcon/login');
return null;
}
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body.error || 'Unable to load profile');
return body;
})
.then((data) => {
if (data.profile) {
if (data?.profile) {
setProfile(setCached('vcon:profile', data.profile));
setEmail(data.profile.email);
setCached('vcon:profile-loaded', true);
}
})
.catch(() => {
error('Profile load failed', 'Could not load administrator profile.');
.catch((loadErr) => {
error(
'Profile load failed',
loadErr instanceof Error ? loadErr.message : 'Could not load administrator profile.'
);
})
.finally(() => setLoading(false));
}, [error]);
@@ -78,6 +93,10 @@ export function ProfileWorkspace() {
error('Validation error', 'Enter your current password to save changes.');
return;
}
if (newPassword && (newPassword.length < 8 || newPassword.length > 256)) {
error('Validation error', 'New password must be between 8 and 256 characters.');
return;
}
if (newPassword && newPassword !== confirmPassword) {
error('Validation error', 'New password and confirmation do not match.');
return;
@@ -89,16 +108,24 @@ export function ProfileWorkspace() {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
cache: 'no-store',
body: JSON.stringify({
email: trimmedEmail,
currentPassword,
newPassword: newPassword || undefined,
}),
});
const data = await response.json();
const data = await response.json().catch(() => ({}));
if (shouldRedirectProfileUnauthorized(response.status, data)) {
window.location.assign('/vcon/login');
return;
}
if (!response.ok) throw new Error(data.error || 'Profile update failed');
setProfile(setCached('vcon:profile', data.profile));
setEmail(data.profile.email);
window.dispatchEvent(
new CustomEvent('datastack:profile-updated', { detail: data.profile })
);
setCurrentPassword('');
setNewPassword('');
setConfirmPassword('');
@@ -123,6 +150,7 @@ export function ProfileWorkspace() {
credentials: 'include',
});
if (!response.ok) throw new Error('Unable to sign out');
await clearAuthCaches();
window.location.assign('/vcon/login');
} catch {
error('Sign out failed', 'Please try again.');
@@ -160,13 +188,7 @@ export function ProfileWorkspace() {
{ label: 'Auth Store', value: profile.storageMode },
{
label: 'Session Expires',
value: profile.sessionExpiresAt
? new Date(profile.sessionExpiresAt).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
hour12: false,
})
: 'Active',
value: formatSessionExpiryLabel(profile.sessionExpiresAt),
},
]}
/>
+3 -3
View File
@@ -7,8 +7,8 @@ export const SdkSection: React.FC = () => (
<SectionHeading index="02" label="Python SDK" title="Drop in the SDK. Start streaming." />
<div className="grid grid-cols-1 gap-px border border-[var(--border)] bg-[var(--border)] md:grid-cols-2">
<CodeBlock>
<span className="text-[var(--text-faint)]"># Install the official Python SDK</span>{'\n'}
<span className="text-[var(--text)]">pip install ./sdk/python/datastack</span>{'\n\n'}
<span className="text-[var(--text-faint)]"># Install the bundled Python SDK (from repo or local wheel/path)</span>{'\n'}
<span className="text-[var(--text)]">pip install /path/to/datastack/sdk/python/datastack</span>{'\n\n'}
<span className="text-[var(--text-faint)]"># Auto-loads config.datastack.py from project root</span>{'\n'}
<span className="text-[var(--text)]">from datastack import DataStack</span>{'\n\n'}
<span className="text-[var(--text)]">client = DataStack()</span>{'\n'}
@@ -20,7 +20,7 @@ export const SdkSection: React.FC = () => (
<p className="mb-2 text-[11px] uppercase text-[var(--text-faint)]">How it works</p>
<h3 className="mb-2 text-[14px] font-medium text-[var(--text)]">Zero-boilerplate config. Auto-recovery built in.</h3>
<p className="text-[12px] leading-relaxed text-[var(--text-muted)]">
Drop <code className="text-[var(--text)]">config.datastack.py</code> into your Python project to stream single events, batches, or <code className="text-[var(--text)]">.csv</code> / <code className="text-[var(--text)]">.json</code> files directly—with automatic HTTP fallback, offline disk spooling, and Heartbeat IDLE recovery.
Install the self-hosted SDK from <code className="text-[var(--text)]">sdk/python/datastack</code> (or build a wheel with <code className="text-[var(--text)]">pip wheel ./sdk/python/datastack -w dist/</code>) and drop <code className="text-[var(--text)]">config.datastack.py</code> into your Python project to stream single events, batches, or <code className="text-[var(--text)]">.csv</code> / <code className="text-[var(--text)]">.json</code> files directly—with automatic HTTP fallback, offline disk spooling, and Heartbeat IDLE recovery.
</p>
</div>
</div>
+264 -181
View File
@@ -15,34 +15,31 @@ import {
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import {
DEFAULT_EDITABLE_SETTINGS,
describeSettingsBridgeSync,
type EditableSettings,
mapBridgeStatusToSocketUiState,
normalizeEditableSettings,
NUMERIC_SETTING_KEYS,
type NumericSettingDrafts,
type NumericSettingKey,
resolvePublicEndpoints,
SETTINGS_LIMITS,
settingsToNumericDrafts,
type SettingsValidationErrors,
type SocketHealthUiState,
validateEditableSettingsDraft,
validateSingleNumericSetting,
} from '@/lib/vconUiHelpers';
import { useCallback, useEffect, useMemo, useState } from 'react';
interface EditableSettings {
maxEventsPerSec: number;
maxPayloadSizeKb: number;
batchFlushThreshold: number;
batchFlushIntervalMs: number;
fileRotationMb: number;
enableIdempotency: boolean;
autoPruneDays: number;
}
interface PublicSettings extends EditableSettings {
serverPort: number;
webPort: number;
updatedAt: string;
}
const DEFAULT_SETTINGS: EditableSettings = {
maxEventsPerSec: 100,
maxPayloadSizeKb: 100,
batchFlushThreshold: 500,
batchFlushIntervalMs: 2000,
fileRotationMb: 100,
enableIdempotency: true,
autoPruneDays: 30,
};
const TABS = [
{ id: 'general', label: 'General' },
{ id: 'connections', label: 'Connections' },
@@ -51,16 +48,32 @@ const TABS = [
];
type TabId = (typeof TABS)[number]['id'];
type SocketHealthState = 'idle' | 'checking' | 'online' | 'offline';
const FIELD_TO_TAB: Record<NumericSettingKey, TabId> = {
maxEventsPerSec: 'general',
batchFlushThreshold: 'connections',
batchFlushIntervalMs: 'connections',
fileRotationMb: 'storage',
autoPruneDays: 'storage',
maxPayloadSizeKb: 'security',
};
export function SettingsWorkspace() {
const { success, error } = useToast();
const [settings, setSettings] = useState<EditableSettings>(() =>
getCached<EditableSettings>('vcon:settings', DEFAULT_SETTINGS)
);
const { success, warning, error } = useToast();
const [savedSettings, setSavedSettings] = useState<EditableSettings>(() =>
getCached<EditableSettings>('vcon:settings', DEFAULT_SETTINGS)
getCached<EditableSettings>('vcon:settings', DEFAULT_EDITABLE_SETTINGS)
);
const [numericDrafts, setNumericDrafts] = useState<NumericSettingDrafts>(() =>
settingsToNumericDrafts(
getCached<EditableSettings>('vcon:settings', DEFAULT_EDITABLE_SETTINGS)
)
);
const [enableIdempotency, setEnableIdempotency] = useState<boolean>(
() =>
getCached<EditableSettings>('vcon:settings', DEFAULT_EDITABLE_SETTINGS)
.enableIdempotency
);
const [fieldErrors, setFieldErrors] = useState<SettingsValidationErrors>({});
const [activeTab, setActiveTab] = useState<TabId>('general');
const [jwtSecret, setJwtSecret] = useState('');
const [jwtError, setJwtError] = useState('');
@@ -69,8 +82,8 @@ export function SettingsWorkspace() {
getCached<string>('vcon:storage-path', '')
);
const [baseUrl, setBaseUrl] = useState('');
const [webSocketUrl, setWebSocketUrl] = useState('');
const [socketStatus, setSocketStatus] = useState<SocketHealthState>('idle');
const [socketStatus, setSocketStatus] = useState<SocketHealthUiState>('idle');
const [bridgeError, setBridgeError] = useState('');
const [activeSockets, setActiveSockets] = useState<number>(0);
const [serverPort, setServerPort] = useState(() =>
getCached<number>('vcon:server-port', 4000)
@@ -80,48 +93,46 @@ export function SettingsWorkspace() {
);
const [saving, setSaving] = useState(false);
const probeSocketServer = useCallback(async (port: number) => {
setSocketStatus('checking');
const isLocalHost =
window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1' ||
window.location.port === '3000';
const httpHost = isLocalHost
? `${window.location.hostname}:${port}`
: window.location.host;
const healthUrl = `${window.location.protocol}//${httpHost}/health`;
const webSocketUrl = useMemo(() => {
if (!baseUrl || typeof window === 'undefined') return '';
return resolvePublicEndpoints(window.location, serverPort).webSocketUrl;
}, [baseUrl, serverPort]);
const probeSocketServer = useCallback(async () => {
setSocketStatus('checking');
try {
const response = await fetch(healthUrl, {
const response = await fetch('/api/metrics', {
cache: 'no-store',
signal: AbortSignal.timeout(1500),
signal: AbortSignal.timeout(3000),
});
setSocketStatus(response.ok ? 'online' : 'offline');
} catch {
// In remote/preview environments where Port 4000 is proxied through web API or reverse proxy, probe /api/health
try {
const fallbackRes = await fetch('/api/health', {
cache: 'no-store',
signal: AbortSignal.timeout(1500),
});
setSocketStatus(fallbackRes.ok ? 'online' : 'offline');
} catch {
setSocketStatus('offline');
if (response.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (!response.ok) {
setSocketStatus('offline');
setBridgeError(`Metrics probe returned HTTP ${response.status}`);
return;
}
const metricsData = await response.json();
const runtimePort = Number(metricsData?.runtime?.serverPort);
if (Number.isFinite(runtimePort) && runtimePort > 0) {
setServerPort(setCached('vcon:server-port', runtimePort));
}
setActiveSockets(Number(metricsData?.activeSockets) || 0);
const nextState = mapBridgeStatusToSocketUiState(
metricsData?.runtime?.ingestionBridgeStatus
);
setSocketStatus(nextState);
setBridgeError(String(metricsData?.runtime?.ingestionBridgeError || ''));
} catch {
setSocketStatus('offline');
setBridgeError('Unable to reach /api/metrics bridge probe');
}
}, []);
useEffect(() => {
setBaseUrl(window.location.origin);
const isLocalHost =
window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1' ||
window.location.port === '3000';
const socketProtocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const socketHost = isLocalHost
? `${window.location.hostname}:${serverPort}`
: window.location.host;
setWebSocketUrl(`${socketProtocol}://${socketHost}`);
Promise.all([
fetch('/api/settings', { cache: 'no-store' }).then(async (response) => {
@@ -141,46 +152,58 @@ export function SettingsWorkspace() {
return response.json();
}),
fetch('/api/metrics', { cache: 'no-store' })
.then(async (response) => (response.ok ? response.json() : null))
.then(async (response) => {
if (response.status === 401) {
window.location.assign('/vcon/login');
return null;
}
return response.ok ? response.json() : null;
})
.catch(() => null),
])
.then(([settingsData, storageData, metricsData]) => {
if (!settingsData || !storageData) return;
const loadedPort = Number(
const runtimePort = Number(metricsData?.runtime?.serverPort);
const settingsPort = Number(
(settingsData.settings as PublicSettings).serverPort ?? 4000
);
const loadedPort =
Number.isFinite(runtimePort) && runtimePort > 0 ? runtimePort : settingsPort;
const loaded: EditableSettings = {
maxEventsPerSec: Number(
settingsData.settings.maxEventsPerSec ??
DEFAULT_SETTINGS.maxEventsPerSec
DEFAULT_EDITABLE_SETTINGS.maxEventsPerSec
),
maxPayloadSizeKb: Number(
settingsData.settings.maxPayloadSizeKb ??
DEFAULT_SETTINGS.maxPayloadSizeKb
DEFAULT_EDITABLE_SETTINGS.maxPayloadSizeKb
),
batchFlushThreshold: Number(
settingsData.settings.batchFlushThreshold ??
DEFAULT_SETTINGS.batchFlushThreshold
DEFAULT_EDITABLE_SETTINGS.batchFlushThreshold
),
batchFlushIntervalMs: Number(
settingsData.settings.batchFlushIntervalMs ??
DEFAULT_SETTINGS.batchFlushIntervalMs
DEFAULT_EDITABLE_SETTINGS.batchFlushIntervalMs
),
fileRotationMb: Number(
settingsData.settings.fileRotationMb ??
DEFAULT_SETTINGS.fileRotationMb
DEFAULT_EDITABLE_SETTINGS.fileRotationMb
),
enableIdempotency: Boolean(
settingsData.settings.enableIdempotency ??
DEFAULT_SETTINGS.enableIdempotency
DEFAULT_EDITABLE_SETTINGS.enableIdempotency
),
autoPruneDays: Number(
settingsData.settings.autoPruneDays ??
DEFAULT_SETTINGS.autoPruneDays
DEFAULT_EDITABLE_SETTINGS.autoPruneDays
),
};
setSettings(setCached('vcon:settings', loaded));
setSavedSettings(loaded);
const cachedLoaded = setCached('vcon:settings', loaded);
setSavedSettings(cachedLoaded);
setNumericDrafts(settingsToNumericDrafts(cachedLoaded));
setEnableIdempotency(cachedLoaded.enableIdempotency);
setFieldErrors({});
setServerPort(setCached('vcon:server-port', loadedPort));
setStoragePath(
setCached(
@@ -188,69 +211,51 @@ export function SettingsWorkspace() {
String(storageData.stats?.storageDirectory ?? '')
)
);
if (metricsData && typeof metricsData.activeSockets === 'number') {
setActiveSockets(metricsData.activeSockets);
if (metricsData.activeSockets > 0) {
setSocketStatus('online');
}
if (metricsData) {
setActiveSockets(Number(metricsData.activeSockets) || 0);
setSocketStatus(
mapBridgeStatusToSocketUiState(
metricsData.runtime?.ingestionBridgeStatus
)
);
setBridgeError(String(metricsData.runtime?.ingestionBridgeError || ''));
}
setCached('vcon:settings-loaded', true);
})
.catch(() => {
error('Settings load failed', 'Using default runtime settings.');
error('Settings load failed', 'Using last cached runtime settings.');
})
.finally(() => setLoading(false));
}, [error, serverPort]);
}, [error]);
const isDirty = useMemo(
() =>
jwtSecret.trim().length > 0 ||
JSON.stringify(settings) !== JSON.stringify(savedSettings),
[jwtSecret, savedSettings, settings]
enableIdempotency !== savedSettings.enableIdempotency ||
NUMERIC_SETTING_KEYS.some(
(key) => numericDrafts[key].trim() !== String(savedSettings[key])
),
[enableIdempotency, jwtSecret, numericDrafts, savedSettings]
);
const updateNumber = (key: keyof EditableSettings, value: string) => {
setSettings((current) => ({ ...current, [key]: Number(value) || 0 }));
const updateNumberDraft = (key: NumericSettingKey, value: string) => {
setNumericDrafts((current) => ({
...current,
[key]: value,
}));
const check = validateSingleNumericSetting(key, value);
setFieldErrors((current) => {
if (check.valid) {
if (!current[key]) return current;
const next = { ...current };
delete next[key];
return next;
}
return { ...current, [key]: check.error };
});
};
const buildNormalizedSettings = (): EditableSettings => {
const parsedPrune = Number(settings.autoPruneDays);
return {
maxEventsPerSec: Math.max(
1,
Math.min(
10000,
settings.maxEventsPerSec || DEFAULT_SETTINGS.maxEventsPerSec
)
),
maxPayloadSizeKb: Math.max(
1,
Math.min(
5120,
settings.maxPayloadSizeKb || DEFAULT_SETTINGS.maxPayloadSizeKb
)
),
batchFlushThreshold: Math.max(
1,
settings.batchFlushThreshold || DEFAULT_SETTINGS.batchFlushThreshold
),
batchFlushIntervalMs: Math.max(
100,
settings.batchFlushIntervalMs || DEFAULT_SETTINGS.batchFlushIntervalMs
),
fileRotationMb: Math.max(
1,
settings.fileRotationMb || DEFAULT_SETTINGS.fileRotationMb
),
enableIdempotency: settings.enableIdempotency,
autoPruneDays: Number.isFinite(parsedPrune)
? Math.max(0, Math.min(365, Math.floor(parsedPrune)))
: DEFAULT_SETTINGS.autoPruneDays,
};
};
const executeSave = async () => {
const normalized = buildNormalizedSettings();
const executeSave = async (validated: EditableSettings) => {
const trimmedSecret = jwtSecret.trim();
setConfirmRotateOpen(false);
setSaving(true);
@@ -259,7 +264,7 @@ export function SettingsWorkspace() {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
...normalized,
...validated,
...(trimmedSecret ? { jwtSecret: trimmedSecret } : {}),
}),
});
@@ -269,16 +274,36 @@ export function SettingsWorkspace() {
}
const data = await response.json();
if (!response.ok) throw new Error(data.error || 'Settings update failed');
setSettings(setCached('vcon:settings', normalized));
setSavedSettings(normalized);
const synced = normalizeEditableSettings(data.settings ?? validated);
setCached('vcon:settings', synced);
setSavedSettings(synced);
setNumericDrafts(settingsToNumericDrafts(synced));
setEnableIdempotency(synced.enableIdempotency);
setFieldErrors({});
setJwtSecret('');
setJwtError('');
success(
'Settings saved',
trimmedSecret
? 'Runtime settings saved and client JWT credentials rotated.'
: 'Runtime settings updated.'
);
const bridgeSummary = describeSettingsBridgeSync(data.bridgeSync);
if (bridgeSummary.level === 'error') {
setSocketStatus('auth_error');
setBridgeError(bridgeSummary.message || '');
error(bridgeSummary.title, bridgeSummary.message);
} else if (bridgeSummary.level === 'warning') {
setSocketStatus('offline');
setBridgeError(bridgeSummary.message || '');
warning(bridgeSummary.title, bridgeSummary.message);
} else {
if (Boolean(data.bridgeSync?.ok ?? data.bridgeSync?.synced)) {
setSocketStatus('online');
setBridgeError('');
}
success(
'Settings saved',
trimmedSecret
? 'Runtime settings saved and client JWT credentials rotated.'
: 'Runtime settings updated across Web & Ingestion.'
);
}
} catch (saveError) {
error(
'Save failed',
@@ -293,6 +318,24 @@ export function SettingsWorkspace() {
const handleFormSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const validation = validateEditableSettingsDraft(
numericDrafts,
enableIdempotency
);
if (!validation.valid || !validation.values) {
setFieldErrors(validation.errors);
if (validation.firstInvalidKey) {
setActiveTab(FIELD_TO_TAB[validation.firstInvalidKey]);
}
const firstMessage =
(validation.firstInvalidKey &&
validation.errors[validation.firstInvalidKey]) ||
'Review highlighted numeric settings before saving.';
error('Invalid setting value', firstMessage);
return;
}
setFieldErrors({});
const trimmedSecret = jwtSecret.trim();
if (trimmedSecret.length > 0 && trimmedSecret.length < 32) {
const message = 'JWT secret must be at least 32 characters.';
@@ -308,30 +351,57 @@ export function SettingsWorkspace() {
return;
}
await executeSave();
await executeSave(validation.values);
};
const handleConfirmedRotateSave = async () => {
const validation = validateEditableSettingsDraft(
numericDrafts,
enableIdempotency
);
if (!validation.valid || !validation.values) {
setConfirmRotateOpen(false);
setFieldErrors(validation.errors);
if (validation.firstInvalidKey) {
setActiveTab(FIELD_TO_TAB[validation.firstInvalidKey]);
}
return;
}
await executeSave(validation.values);
};
const discardChanges = () => {
setSettings(savedSettings);
setNumericDrafts(settingsToNumericDrafts(savedSettings));
setEnableIdempotency(savedSettings.enableIdempotency);
setFieldErrors({});
setJwtSecret('');
setJwtError('');
};
const rateLimitStat = numericDrafts.maxEventsPerSec.trim() || '—';
const flushThresholdStat = numericDrafts.batchFlushThreshold.trim() || '—';
const flushIntervalStat = numericDrafts.batchFlushIntervalMs.trim() || '—';
const rotationStat = numericDrafts.fileRotationMb.trim() || '—';
const pruneStat =
numericDrafts.autoPruneDays.trim() === '0'
? 'Keep all'
: numericDrafts.autoPruneDays.trim()
? `${numericDrafts.autoPruneDays.trim()}d`
: '—';
return (
<PageShell title="Settings">
<StatStrip
items={[
{ label: 'Instance', value: 'DataStack', dot: 'ok' },
{ label: 'Rate limit', value: `${settings.maxEventsPerSec} / sec` },
{ label: 'Rate limit', value: `${rateLimitStat} / sec` },
{
label: 'Batch flush',
value: `${settings.batchFlushThreshold} / ${settings.batchFlushIntervalMs}ms`,
value: `${flushThresholdStat} / ${flushIntervalStat}ms`,
},
{
label: 'CSV rotation',
value: `${settings.fileRotationMb}MB · ${
settings.autoPruneDays === 0 ? 'Keep all' : `${settings.autoPruneDays}d`
}`,
value: `${rotationStat}MB · ${pruneStat}`,
},
]}
/>
@@ -343,7 +413,7 @@ export function SettingsWorkspace() {
onChange={(tab) => setActiveTab(tab as TabId)}
/>
<form onSubmit={handleFormSubmit} className="w-full">
<form onSubmit={handleFormSubmit} noValidate className="w-full">
<SettingsPanel
id={`settings-panel-${activeTab}`}
role="tabpanel"
@@ -374,24 +444,20 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="1"
max="10000"
value={settings.maxEventsPerSec}
min={SETTINGS_LIMITS.maxEventsPerSec.min}
max={SETTINGS_LIMITS.maxEventsPerSec.max}
value={numericDrafts.maxEventsPerSec}
error={fieldErrors.maxEventsPerSec}
onChange={(event) =>
updateNumber('maxEventsPerSec', event.target.value)
updateNumberDraft('maxEventsPerSec', event.target.value)
}
/>
</SettingsField>
<SettingsField label="Idempotency">
<FormSwitch
label="Deduplicate events"
checked={settings.enableIdempotency}
onChange={(enableIdempotency) =>
setSettings((current) => ({
...current,
enableIdempotency,
}))
}
checked={enableIdempotency}
onChange={setEnableIdempotency}
/>
</SettingsField>
</SettingsGroup>
@@ -403,7 +469,10 @@ export function SettingsWorkspace() {
<SettingsGroup title="WebSocket server">
<SettingsField label="Live health">
<div className="flex items-center justify-between gap-2 border border-[var(--border)] bg-[var(--surface-2)] px-2.5 py-1.5">
<span className="inline-flex items-center gap-1.5 font-mono text-[11.5px] text-[var(--text)]">
<span
className="inline-flex items-center gap-1.5 font-mono text-[11.5px] text-[var(--text)]"
title={bridgeError || undefined}
>
<span
aria-hidden="true"
className={`h-1.5 w-1.5 ${
@@ -411,7 +480,7 @@ export function SettingsWorkspace() {
? 'bg-[var(--accent)]'
: socketStatus === 'checking'
? 'bg-[var(--warning)]'
: socketStatus === 'offline'
: socketStatus === 'auth_error' || socketStatus === 'offline'
? 'bg-[var(--danger)]'
: 'bg-[var(--text-faint)]'
}`}
@@ -419,17 +488,19 @@ export function SettingsWorkspace() {
{socketStatus === 'online'
? `Online (Port ${serverPort} · ${activeSockets} active)`
: socketStatus === 'checking'
? 'Probing ingestion server...'
: socketStatus === 'offline'
? `Offline (Port ${serverPort} unreachable)`
: `Port ${serverPort} · ${activeSockets} active socket(s)`}
? 'Probing ingestion bridge...'
: socketStatus === 'auth_error'
? `Bridge Auth Error (Port ${serverPort})`
: socketStatus === 'offline'
? `Offline (Port ${serverPort} unreachable)`
: `Port ${serverPort} · ${activeSockets} active socket(s)`}
</span>
<Button
type="button"
size="xs"
variant="ghost"
icon="RefreshCw"
onClick={() => void probeSocketServer(serverPort)}
onClick={() => void probeSocketServer()}
>
Probe
</Button>
@@ -443,14 +514,18 @@ export function SettingsWorkspace() {
className="border-[var(--border)] bg-[var(--surface-2)] font-mono text-[11px] text-[var(--text-muted)]"
/>
</SettingsField>
<SettingsField label="Socket port">
<Input
compact
type="number"
value={serverPort}
readOnly
className="border-[var(--border)] bg-[var(--surface-2)] font-mono text-[var(--text-muted)]"
/>
<SettingsField label="Socket port (PORT_SOCKET)">
<div className="flex flex-col gap-1">
<Input
compact
value={String(serverPort)}
readOnly
className="border-[var(--border)] bg-[var(--surface-2)] font-mono text-[11px] text-[var(--text-muted)]"
/>
<span className="text-[10.5px] text-[var(--text-faint)]">
Read-only runtime port managed via PORT_SOCKET environment variable.
</span>
</div>
</SettingsField>
</SettingsGroup>
<SettingsGroup title="Event buffer">
@@ -458,10 +533,12 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="1"
value={settings.batchFlushThreshold}
min={SETTINGS_LIMITS.batchFlushThreshold.min}
max={SETTINGS_LIMITS.batchFlushThreshold.max}
value={numericDrafts.batchFlushThreshold}
error={fieldErrors.batchFlushThreshold}
onChange={(event) =>
updateNumber('batchFlushThreshold', event.target.value)
updateNumberDraft('batchFlushThreshold', event.target.value)
}
/>
</SettingsField>
@@ -469,11 +546,13 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="100"
min={SETTINGS_LIMITS.batchFlushIntervalMs.min}
max={SETTINGS_LIMITS.batchFlushIntervalMs.max}
suffix="ms"
value={settings.batchFlushIntervalMs}
value={numericDrafts.batchFlushIntervalMs}
error={fieldErrors.batchFlushIntervalMs}
onChange={(event) =>
updateNumber('batchFlushIntervalMs', event.target.value)
updateNumberDraft('batchFlushIntervalMs', event.target.value)
}
className="font-mono"
/>
@@ -497,11 +576,13 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="1"
min={SETTINGS_LIMITS.fileRotationMb.min}
max={SETTINGS_LIMITS.fileRotationMb.max}
suffix="MB"
value={settings.fileRotationMb}
value={numericDrafts.fileRotationMb}
error={fieldErrors.fileRotationMb}
onChange={(event) =>
updateNumber('fileRotationMb', event.target.value)
updateNumberDraft('fileRotationMb', event.target.value)
}
className="font-mono"
/>
@@ -510,12 +591,13 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="0"
max="365"
min={SETTINGS_LIMITS.autoPruneDays.min}
max={SETTINGS_LIMITS.autoPruneDays.max}
suffix="days"
value={settings.autoPruneDays}
value={numericDrafts.autoPruneDays}
error={fieldErrors.autoPruneDays}
onChange={(event) =>
updateNumber('autoPruneDays', event.target.value)
updateNumberDraft('autoPruneDays', event.target.value)
}
className="font-mono"
/>
@@ -557,12 +639,13 @@ export function SettingsWorkspace() {
<Input
compact
type="number"
min="1"
max="5120"
min={SETTINGS_LIMITS.maxPayloadSizeKb.min}
max={SETTINGS_LIMITS.maxPayloadSizeKb.max}
suffix="KB"
value={settings.maxPayloadSizeKb}
value={numericDrafts.maxPayloadSizeKb}
error={fieldErrors.maxPayloadSizeKb}
onChange={(event) =>
updateNumber('maxPayloadSizeKb', event.target.value)
updateNumberDraft('maxPayloadSizeKb', event.target.value)
}
className="font-mono"
/>
@@ -628,7 +711,7 @@ export function SettingsWorkspace() {
variant="danger"
size="xs"
loading={saving}
onClick={() => void executeSave()}
onClick={() => void handleConfirmedRotateSave()}
>
Rotate & Save
</Button>