2026-10-23 11:43:49 -05:00
|
|
|
'use client';
|
|
|
|
|
|
2026-11-17 00:24:33 +06:00
|
|
|
import { PageShell } from '@/components/layout/PageShell';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { Button } from '@/components/ui/Button';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { Panel } from '@/components/ui/Panel';
|
|
|
|
|
import { StatStrip } from '@/components/ui/StatStrip';
|
2026-10-23 11:43:49 -05:00
|
|
|
import {
|
2026-11-17 00:24:33 +06:00
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableEmpty,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
TableWrap,
|
2026-10-23 11:43:49 -05:00
|
|
|
} from '@/components/ui/Table';
|
|
|
|
|
import { useToast } from '@/components/ui/Toast';
|
2026-11-16 23:10:44 +06:00
|
|
|
import { getCached, setCached } from '@/lib/vconCache';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
interface StorageStatsSummary {
|
|
|
|
|
totalMB?: string;
|
|
|
|
|
totalFiles?: number;
|
|
|
|
|
totalRows?: number;
|
|
|
|
|
rotationRule?: string;
|
|
|
|
|
rotationMb?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface StoragePartitionFile {
|
|
|
|
|
clientId: string;
|
|
|
|
|
filename: string;
|
|
|
|
|
sizeBytes: number;
|
|
|
|
|
linesCount: number;
|
|
|
|
|
modifiedAt: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
export default function StoragePage() {
|
2026-11-19 00:42:38 +06:00
|
|
|
const { success, warning, error } = useToast();
|
2026-11-19 20:22:01 +06:00
|
|
|
const [stats, setStats] = useState<StorageStatsSummary | null>(() =>
|
|
|
|
|
getCached<StorageStatsSummary | null>('vcon:storage-stats', null)
|
2026-11-17 00:24:33 +06:00
|
|
|
);
|
2026-11-19 20:22:01 +06:00
|
|
|
const [files, setFiles] = useState<StoragePartitionFile[]>(() =>
|
|
|
|
|
getCached<StoragePartitionFile[]>('vcon:storage-files', [])
|
2026-11-17 00:24:33 +06:00
|
|
|
);
|
2026-11-19 20:22:01 +06:00
|
|
|
const [refreshing, setRefreshing] = useState(false);
|
2026-10-23 11:43:49 -05:00
|
|
|
const [flushing, setFlushing] = useState(false);
|
|
|
|
|
|
|
|
|
|
const fetchStorageData = useCallback(async () => {
|
2026-11-19 20:22:01 +06:00
|
|
|
setRefreshing(true);
|
2026-10-23 11:43:49 -05:00
|
|
|
try {
|
2026-11-19 20:22:01 +06:00
|
|
|
const res = await fetch('/api/storage', { cache: 'no-store' });
|
|
|
|
|
if (res.status === 401) {
|
|
|
|
|
window.location.assign('/vcon/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const data = await res.json().catch(() => ({}));
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(data.error || 'Unable to load storage partitions');
|
|
|
|
|
}
|
|
|
|
|
setStats(setCached('vcon:storage-stats', data.stats ?? null));
|
2026-11-16 23:10:44 +06:00
|
|
|
setFiles(setCached('vcon:storage-files', data.files || []));
|
2026-11-19 20:22:01 +06:00
|
|
|
} catch (loadErr) {
|
|
|
|
|
error(
|
|
|
|
|
'Storage load failed',
|
|
|
|
|
loadErr instanceof Error
|
|
|
|
|
? loadErr.message
|
|
|
|
|
: 'Could not load storage partitions.'
|
|
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
setRefreshing(false);
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
2026-11-16 21:58:18 +06:00
|
|
|
}, [error]);
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-11-19 20:22:01 +06:00
|
|
|
void fetchStorageData();
|
2026-10-23 11:43:49 -05:00
|
|
|
}, [fetchStorageData]);
|
|
|
|
|
|
|
|
|
|
const handleManualFlush = async () => {
|
|
|
|
|
setFlushing(true);
|
|
|
|
|
try {
|
2026-11-19 20:22:01 +06:00
|
|
|
const res = await fetch('/api/storage', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
cache: 'no-store',
|
|
|
|
|
});
|
|
|
|
|
if (res.status === 401) {
|
|
|
|
|
window.location.assign('/vcon/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-19 00:42:38 +06:00
|
|
|
const data = await res.json().catch(() => ({}));
|
|
|
|
|
|
|
|
|
|
if (res.ok && data.success) {
|
|
|
|
|
const count = Number(data.flushedCount) || 0;
|
2026-11-19 20:22:01 +06:00
|
|
|
if (data.warning) {
|
|
|
|
|
warning(
|
|
|
|
|
'Partial Flush (Web Only)',
|
|
|
|
|
`Flushed ${count} event${count === 1 ? '' : 's'}. ${String(data.warning)}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
success(
|
|
|
|
|
'Buffer Flushed',
|
|
|
|
|
`Flushed ${count} event${count === 1 ? '' : 's'} to CSV.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
await fetchStorageData();
|
|
|
|
|
} else if (data.flushedCount !== undefined && Number(data.flushedCount) > 0) {
|
2026-11-19 00:42:38 +06:00
|
|
|
warning(
|
|
|
|
|
'Partial Flush',
|
|
|
|
|
`Flushed ${data.flushedCount} local events. Ingestion server: ${data.error || 'flush failed'}`
|
|
|
|
|
);
|
2026-11-19 20:22:01 +06:00
|
|
|
await fetchStorageData();
|
2026-10-23 11:43:49 -05:00
|
|
|
} else {
|
2026-11-19 00:42:38 +06:00
|
|
|
error('Flush Failed', data.error || 'Could not flush buffer');
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
2026-11-19 00:42:38 +06:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const msg = err instanceof Error ? err.message : 'Unknown network error';
|
|
|
|
|
error('Flush Error', msg);
|
2026-10-23 11:43:49 -05:00
|
|
|
} finally {
|
|
|
|
|
setFlushing(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-17 00:24:33 +06:00
|
|
|
<PageShell
|
|
|
|
|
title="Storage"
|
|
|
|
|
actions={
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
size="xs"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
icon="RotateCw"
|
2026-11-19 20:22:01 +06:00
|
|
|
loading={refreshing}
|
|
|
|
|
disabled={refreshing || flushing}
|
|
|
|
|
onClick={() => void fetchStorageData()}
|
2026-11-17 00:24:33 +06:00
|
|
|
>
|
|
|
|
|
Refresh
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="xs"
|
|
|
|
|
variant="primary"
|
|
|
|
|
icon="HardDrive"
|
|
|
|
|
loading={flushing}
|
2026-11-19 20:22:01 +06:00
|
|
|
disabled={flushing}
|
|
|
|
|
onClick={() => void handleManualFlush()}
|
2026-11-17 00:24:33 +06:00
|
|
|
>
|
|
|
|
|
Flush Buffer
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
>
|
2026-11-16 21:58:18 +06:00
|
|
|
<StatStrip
|
|
|
|
|
items={[
|
|
|
|
|
{ label: 'Disk usage', value: `${stats?.totalMB ?? '0.00'} MB` },
|
|
|
|
|
{ label: 'CSV files', value: stats?.totalFiles ?? 0 },
|
2026-11-17 00:24:33 +06:00
|
|
|
{
|
|
|
|
|
label: 'Persisted rows',
|
|
|
|
|
value: stats?.totalRows ? stats.totalRows.toLocaleString() : 0,
|
|
|
|
|
},
|
2026-11-17 20:05:00 +06:00
|
|
|
{
|
|
|
|
|
label: 'Rotation',
|
|
|
|
|
value: stats?.rotationRule || `${stats?.rotationMb ?? 100}MB / Daily`,
|
|
|
|
|
},
|
2026-11-16 21:58:18 +06:00
|
|
|
]}
|
|
|
|
|
/>
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<Panel title="CSV partitions" meta={`${files.length}`}>
|
|
|
|
|
<TableWrap className="border-0 bg-transparent">
|
2026-10-23 11:43:49 -05:00
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
2026-11-16 21:58:18 +06:00
|
|
|
<TableHead>Client</TableHead>
|
|
|
|
|
<TableHead>Filename</TableHead>
|
|
|
|
|
<TableHead>Size</TableHead>
|
|
|
|
|
<TableHead>Rows</TableHead>
|
|
|
|
|
<TableHead>Modified</TableHead>
|
2026-10-23 11:43:49 -05:00
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{files.length === 0 ? (
|
2026-11-16 21:58:18 +06:00
|
|
|
<TableEmpty colSpan={5} message="No CSV files" />
|
2026-10-23 11:43:49 -05:00
|
|
|
) : (
|
2026-11-19 20:22:01 +06:00
|
|
|
files.map((file) => (
|
|
|
|
|
<TableRow key={`${file.clientId}/${file.filename}`}>
|
2026-11-17 00:24:33 +06:00
|
|
|
<TableCell className="font-mono text-[var(--accent)]">
|
2026-10-23 11:43:49 -05:00
|
|
|
{file.clientId}
|
|
|
|
|
</TableCell>
|
2026-11-17 00:24:33 +06:00
|
|
|
<TableCell className="font-mono font-medium text-[var(--text)]">
|
2026-10-23 11:43:49 -05:00
|
|
|
{file.filename}
|
|
|
|
|
</TableCell>
|
2026-11-17 00:24:33 +06:00
|
|
|
<TableCell className="font-mono tabular-nums">
|
2026-10-23 11:43:49 -05:00
|
|
|
{(file.sizeBytes / 1024).toFixed(1)} KB
|
|
|
|
|
</TableCell>
|
2026-11-17 00:24:33 +06:00
|
|
|
<TableCell className="font-mono tabular-nums">
|
2026-10-23 11:43:49 -05:00
|
|
|
{file.linesCount.toLocaleString()}
|
|
|
|
|
</TableCell>
|
2026-11-17 00:24:33 +06:00
|
|
|
<TableCell className="font-mono tabular-nums text-[var(--text-dim)]">
|
2026-10-23 11:43:49 -05:00
|
|
|
{new Date(file.modifiedAt).toLocaleString()}
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableWrap>
|
2026-11-16 21:58:18 +06:00
|
|
|
</Panel>
|
2026-11-17 00:24:33 +06:00
|
|
|
</PageShell>
|
2026-10-23 11:43:49 -05:00
|
|
|
);
|
|
|
|
|
}
|