2026-10-23 19:56:38 -07:00
|
|
|
'use client';
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { Button } from '@/components/ui/Button';
|
|
|
|
|
import { ClientList, type ClientListItem } from '@/components/ui/ClientList';
|
|
|
|
|
import { Panel } from '@/components/ui/Panel';
|
2026-11-16 23:10:44 +06:00
|
|
|
import { StatStrip } from '@/components/ui/StatStrip';
|
2026-10-23 19:56:38 -07:00
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
TableWrap,
|
|
|
|
|
} from '@/components/ui/Table';
|
2026-11-16 23:10:44 +06:00
|
|
|
import { getCached, setCached } from '@/lib/vconCache';
|
2026-10-23 19:56:38 -07:00
|
|
|
import Link from 'next/link';
|
|
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
type EventItem = {
|
|
|
|
|
eventId: string;
|
|
|
|
|
clientId: string;
|
|
|
|
|
timestamp: string;
|
|
|
|
|
payload: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type DashboardMetrics = {
|
|
|
|
|
activeClients: number;
|
|
|
|
|
totalClients: number;
|
|
|
|
|
bufferStats: {
|
|
|
|
|
currentBufferSize: number;
|
|
|
|
|
totalFlushed: number;
|
|
|
|
|
};
|
|
|
|
|
storageStats: {
|
|
|
|
|
totalMB: string;
|
|
|
|
|
totalBytes: number;
|
|
|
|
|
totalFiles: number;
|
|
|
|
|
totalRows: number;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type StorageFile = {
|
|
|
|
|
clientId: string;
|
|
|
|
|
date: string;
|
|
|
|
|
filename: string;
|
|
|
|
|
sizeBytes: number;
|
|
|
|
|
linesCount: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DashboardOverview: React.FC = () => {
|
2026-11-16 23:10:44 +06:00
|
|
|
const [metrics, setMetrics] = useState<DashboardMetrics | null>(() => getCached<DashboardMetrics | null>('vcon:metrics', null));
|
|
|
|
|
const [events, setEvents] = useState<EventItem[]>(() => getCached<EventItem[]>('vcon:dashboard-events', []));
|
|
|
|
|
const [clients, setClients] = useState<ClientListItem[]>(() => getCached<ClientListItem[]>('vcon:clients', []));
|
|
|
|
|
const [files, setFiles] = useState<StorageFile[]>(() => getCached<StorageFile[]>('vcon:storage-files', []));
|
2026-10-23 19:56:38 -07:00
|
|
|
const [isRefreshing, setIsRefreshing] = useState(false);
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
setIsRefreshing(true);
|
|
|
|
|
try {
|
|
|
|
|
const [metricsResponse, eventsResponse, storageResponse, clientsResponse] = await Promise.all([
|
|
|
|
|
fetch('/api/metrics', { cache: 'no-store' }),
|
|
|
|
|
fetch('/api/events?limit=8', { cache: 'no-store' }),
|
|
|
|
|
fetch('/api/storage', { cache: 'no-store' }),
|
|
|
|
|
fetch('/api/clients', { cache: 'no-store' }),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-16 23:10:44 +06:00
|
|
|
if (metricsResponse.ok) {
|
|
|
|
|
const nextMetrics = await metricsResponse.json();
|
|
|
|
|
setMetrics(setCached('vcon:metrics', nextMetrics));
|
|
|
|
|
}
|
2026-10-23 19:56:38 -07:00
|
|
|
if (eventsResponse.ok) {
|
|
|
|
|
const data = await eventsResponse.json();
|
2026-11-16 23:10:44 +06:00
|
|
|
setEvents(setCached('vcon:dashboard-events', data.events ?? []));
|
2026-10-23 19:56:38 -07:00
|
|
|
}
|
|
|
|
|
if (storageResponse.ok) {
|
|
|
|
|
const data = await storageResponse.json();
|
2026-11-16 23:10:44 +06:00
|
|
|
setFiles(setCached('vcon:storage-files', data.files ?? []));
|
|
|
|
|
if (data.stats) setCached('vcon:storage-stats', data.stats);
|
2026-10-23 19:56:38 -07:00
|
|
|
}
|
|
|
|
|
if (clientsResponse.ok) {
|
|
|
|
|
const data = await clientsResponse.json();
|
2026-11-16 23:10:44 +06:00
|
|
|
setClients(setCached('vcon:clients', data.clients ?? []));
|
2026-10-23 19:56:38 -07:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep the last successful dashboard snapshot when one request fails.
|
|
|
|
|
} finally {
|
|
|
|
|
setIsRefreshing(false);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void refresh();
|
|
|
|
|
const interval = window.setInterval(() => void refresh(), 5000);
|
|
|
|
|
return () => window.clearInterval(interval);
|
|
|
|
|
}, [refresh]);
|
|
|
|
|
|
|
|
|
|
const today = new Date().toISOString().slice(0, 10);
|
|
|
|
|
const storedRecordsToday = files
|
|
|
|
|
.filter((file) => file.date === today)
|
|
|
|
|
.reduce((total, file) => total + file.linesCount, 0);
|
|
|
|
|
const eventsToday = storedRecordsToday + (metrics?.bufferStats.currentBufferSize ?? 0);
|
|
|
|
|
const activeClients = metrics?.activeClients ?? clients.filter((client) => client.status === 'active').length;
|
|
|
|
|
const totalClients = metrics?.totalClients ?? clients.length;
|
|
|
|
|
const totalStorage = metrics?.storageStats.totalMB ?? '0.00';
|
|
|
|
|
const totalStorageBytes = metrics?.storageStats.totalBytes ?? 0;
|
|
|
|
|
const storageValue = totalStorageBytes < 1024 * 1024
|
|
|
|
|
? String(Math.max(1, Math.round(totalStorageBytes / 1024)))
|
|
|
|
|
: totalStorage;
|
|
|
|
|
const storageUnit = totalStorageBytes < 1024 * 1024 ? 'KB' : 'MB';
|
|
|
|
|
const totalFiles = metrics?.storageStats.totalFiles ?? files.length;
|
|
|
|
|
const totalRows = metrics?.storageStats.totalRows ?? files.reduce((sum, file) => sum + file.linesCount, 0);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-16 21:58:18 +06:00
|
|
|
<section className="w-full">
|
|
|
|
|
<AdminPageHeader
|
|
|
|
|
title="Dashboard"
|
|
|
|
|
actions={
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
size="icon"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
icon="RefreshCw"
|
|
|
|
|
aria-label="Refresh dashboard"
|
|
|
|
|
title="Refresh dashboard"
|
|
|
|
|
loading={isRefreshing}
|
|
|
|
|
onClick={() => void refresh()}
|
|
|
|
|
/>
|
|
|
|
|
<a href="/api/export?clientId=all" download>
|
|
|
|
|
<Button size="xs" variant="secondary" icon="Download">Export</Button>
|
|
|
|
|
</a>
|
|
|
|
|
<Link href="/vcon/clients">
|
|
|
|
|
<Button size="xs" variant="primary" icon="Plus">New client</Button>
|
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-10-23 19:56:38 -07:00
|
|
|
|
2026-11-16 23:10:44 +06:00
|
|
|
<StatStrip
|
|
|
|
|
items={[
|
|
|
|
|
{ label: 'Events today', value: eventsToday.toLocaleString() },
|
|
|
|
|
{ label: 'Active clients', value: `${activeClients} / ${totalClients}`, dot: activeClients > 0 ? 'ok' : 'off' },
|
|
|
|
|
{ label: 'Storage used', value: `${storageValue} ${storageUnit}` },
|
|
|
|
|
{ label: 'Stored rows', value: totalRows.toLocaleString() },
|
|
|
|
|
]}
|
|
|
|
|
/>
|
2026-10-23 19:56:38 -07:00
|
|
|
|
2026-11-16 23:10:44 +06:00
|
|
|
<div className="grid min-w-0 grid-cols-1 gap-2.5 lg:grid-cols-[minmax(0,1.85fr)_minmax(0,1fr)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
<Panel
|
2026-11-16 21:58:18 +06:00
|
|
|
title="Recent events"
|
|
|
|
|
meta={`${events.length}`}
|
2026-10-23 19:56:38 -07:00
|
|
|
action={<Link href="/vcon/data" className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">View all →</Link>}
|
|
|
|
|
>
|
|
|
|
|
<TableWrap className="border-0 bg-transparent">
|
2026-11-16 21:58:18 +06:00
|
|
|
<Table className="min-w-[560px] text-[11px]">
|
2026-10-23 19:56:38 -07:00
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHead>Time</TableHead>
|
|
|
|
|
<TableHead>Client</TableHead>
|
|
|
|
|
<TableHead>Payload</TableHead>
|
|
|
|
|
<TableHead className="text-right">Size</TableHead>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{events.map((event) => (
|
|
|
|
|
<TableRow key={event.eventId}>
|
2026-11-16 21:58:18 +06:00
|
|
|
<TableCell className="font-mono text-[10.5px] tabular-nums text-[var(--text-faint)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
{event.timestamp.includes('T') ? new Date(event.timestamp).toLocaleTimeString() : event.timestamp}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className="font-mono text-[11px] text-[var(--accent)]">
|
|
|
|
|
{event.clientId}
|
|
|
|
|
</TableCell>
|
2026-11-16 21:58:18 +06:00
|
|
|
<TableCell className="max-w-[380px] truncate font-mono text-[11px] text-[var(--text)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
{event.payload}
|
|
|
|
|
</TableCell>
|
2026-11-16 21:58:18 +06:00
|
|
|
<TableCell className="text-right font-mono text-[10.5px] tabular-nums text-[var(--text-faint)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
{new TextEncoder().encode(event.payload).length} B
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableWrap>
|
|
|
|
|
</Panel>
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<div className="flex min-w-0 flex-col gap-2.5">
|
2026-10-23 19:56:38 -07:00
|
|
|
<Panel
|
2026-11-16 21:58:18 +06:00
|
|
|
title="Clients"
|
2026-10-23 19:56:38 -07:00
|
|
|
action={<Link href="/vcon/clients" className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">All →</Link>}
|
|
|
|
|
>
|
|
|
|
|
<ClientList clients={clients} limit={4} />
|
|
|
|
|
</Panel>
|
|
|
|
|
|
|
|
|
|
<Panel
|
|
|
|
|
title="Storage"
|
2026-11-16 21:58:18 +06:00
|
|
|
meta={`${totalFiles}`}
|
|
|
|
|
action={<a href="/api/export?clientId=all" download className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">Export ↓</a>}
|
2026-10-23 19:56:38 -07:00
|
|
|
>
|
|
|
|
|
<div className="divide-y divide-[var(--border)]">
|
|
|
|
|
{files.slice(0, 3).map((file) => (
|
2026-11-16 21:58:18 +06:00
|
|
|
<div key={`${file.clientId}/${file.filename}`} className="flex items-center justify-between gap-2.5 px-3 py-1.5">
|
2026-10-23 19:56:38 -07:00
|
|
|
<div className="min-w-0">
|
2026-11-16 21:58:18 +06:00
|
|
|
<p className="truncate text-[11.5px] text-[var(--text)]">{file.filename}</p>
|
2026-10-23 19:56:38 -07:00
|
|
|
<p className="truncate font-mono text-[10px] text-[var(--text-faint)]">{file.clientId}</p>
|
|
|
|
|
</div>
|
2026-11-16 21:58:18 +06:00
|
|
|
<span className="shrink-0 font-mono text-[10.5px] tabular-nums text-[var(--text-muted)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
{(file.sizeBytes / 1024).toFixed(1)} KB
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
{files.length === 0 && (
|
2026-11-16 21:58:18 +06:00
|
|
|
<p className="px-3 py-4 text-[11px] text-[var(--text-faint)]">No CSV files</p>
|
2026-10-23 19:56:38 -07:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</Panel>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DashboardOverview;
|