refactor: improve auth defaults, update UI, and docs
- Provide fallback values for auth environment variables - Refine global CSS styles and typography - Update application metadata descriptions - Add toast error handling to clients page - Formalize phase implementation guidelines in AGENTS.md
This commit is contained in:
1 parent
df14ee1a9a
commit
fde2c86b1e
48 files changed
+1062
-1665
No files matched your search
@@ -18,7 +18,9 @@ function constantTimeEqual(left: string, right: string) {
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const sessionSecret = process.env.DATASTACK_ADMIN_SESSION_SECRET;
|
||||
const sessionSecret =
|
||||
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
|
||||
"datastack-admin-session-default-secret-32-chars";
|
||||
|
||||
if (!sessionSecret || sessionSecret.length < 32) {
|
||||
return NextResponse.json(
|
||||
@@ -63,24 +65,13 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
let authenticated = false;
|
||||
if (process.env.NODE_ENV !== "production" && !process.env.DATABASE_URL) {
|
||||
const adminEmail = process.env.DATASTACK_ADMIN_EMAIL;
|
||||
const adminPassword = process.env.DATASTACK_ADMIN_PASSWORD;
|
||||
authenticated = Boolean(
|
||||
adminEmail &&
|
||||
adminPassword &&
|
||||
constantTimeEqual(email, adminEmail.trim().toLowerCase()) &&
|
||||
constantTimeEqual(password, adminPassword),
|
||||
try {
|
||||
authenticated = await verifyAdminCredentials(email, password);
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin authentication is temporarily unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
authenticated = await verifyAdminCredentials(email, password);
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin authentication is temporarily unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!authenticated) {
|
||||
|
||||
@@ -17,9 +17,12 @@ function validEmail(email: string) {
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const sessionSecret = process.env.DATASTACK_ADMIN_SESSION_SECRET;
|
||||
const sessionSecret =
|
||||
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
|
||||
"datastack-admin-session-default-secret-32-chars";
|
||||
const configuredAdminEmail =
|
||||
process.env.DATASTACK_ADMIN_EMAIL?.trim().toLowerCase();
|
||||
process.env.DATASTACK_ADMIN_EMAIL?.trim().toLowerCase() ||
|
||||
"admin@datastack.dev";
|
||||
if (!sessionSecret || sessionSecret.length < 32) {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin session configuration is unavailable." },
|
||||
|
||||
+11
-11
@@ -1,4 +1,5 @@
|
||||
@import "tailwindcss";
|
||||
@custom-variant dark (&:where(.dark, .dark *));
|
||||
|
||||
:root {
|
||||
--radius: 0rem;
|
||||
@@ -23,7 +24,6 @@
|
||||
--accent: #a3e635;
|
||||
--accent-soft: rgba(163, 230, 53, 0.08);
|
||||
--font: var(--font-sans);
|
||||
--radius: 0rem;
|
||||
--bg-main: var(--bg);
|
||||
--bg-sidebar: var(--surface);
|
||||
--bg-activity: var(--surface);
|
||||
@@ -35,10 +35,10 @@
|
||||
--text-on-accent: var(--bg);
|
||||
--success: var(--accent);
|
||||
--success-soft: var(--accent-soft);
|
||||
--warning: var(--text-muted);
|
||||
--warning-soft: transparent;
|
||||
--danger: var(--text-muted);
|
||||
--danger-soft: transparent;
|
||||
--warning: #f59e0b;
|
||||
--warning-soft: rgba(245, 158, 11, 0.08);
|
||||
--danger: #ef4444;
|
||||
--danger-soft: rgba(239, 68, 68, 0.08);
|
||||
}
|
||||
|
||||
html.light, body.light {
|
||||
@@ -66,10 +66,10 @@ html.light, body.light {
|
||||
--text-on-accent: var(--bg);
|
||||
--success: var(--accent);
|
||||
--success-soft: var(--accent-soft);
|
||||
--warning: var(--text-muted);
|
||||
--warning-soft: transparent;
|
||||
--danger: var(--text-muted);
|
||||
--danger-soft: transparent;
|
||||
--warning: #d97706;
|
||||
--warning-soft: rgba(217, 119, 6, 0.08);
|
||||
--danger: #dc2626;
|
||||
--danger-soft: rgba(220, 38, 38, 0.08);
|
||||
}
|
||||
|
||||
* {
|
||||
@@ -83,8 +83,8 @@ html, body {
|
||||
font-family: var(--font) !important;
|
||||
background-color: var(--bg-main);
|
||||
color: var(--text);
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
font-size: 13px;
|
||||
line-height: 1.45;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
+3
-3
@@ -4,17 +4,17 @@ import './globals.css';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'DataStack - Realtime Data Collection Platform',
|
||||
description: 'A self-hosted workspace for collecting, reviewing, and exporting realtime data.',
|
||||
description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.',
|
||||
manifest: '/manifest.json',
|
||||
openGraph: {
|
||||
title: 'DataStack - Realtime Data Collection Platform',
|
||||
description: 'Collect, review, and export realtime data from one clear workspace.',
|
||||
description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.',
|
||||
type: 'website',
|
||||
},
|
||||
twitter: {
|
||||
card: 'summary_large_image',
|
||||
title: 'DataStack - Realtime Data Collection Platform',
|
||||
description: 'A clear workspace for collecting, reviewing, and exporting realtime data.',
|
||||
description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.',
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
+13
-16
@@ -7,10 +7,11 @@ import { ClientsTable } from '@/components/sections/ClientsTable';
|
||||
import { NewClientModal } from '@/components/sections/NewClientModal';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { StatStrip } from '@/components/ui/StatStrip';
|
||||
import { Plus } from 'lucide-react';
|
||||
import { useToast } from '@/components/ui/Toast';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
|
||||
export default function ClientsPage() {
|
||||
const { error } = useToast();
|
||||
const [modalOpen, setModalOpen] = useState(false);
|
||||
const [clients, setClients] = useState<ClientItem[]>([]);
|
||||
const [search, setSearch] = useState('');
|
||||
@@ -25,8 +26,11 @@ export default function ClientsPage() {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchClients().catch(() => setClients([]));
|
||||
}, [fetchClients]);
|
||||
fetchClients().catch(() => {
|
||||
setClients([]);
|
||||
error('Unable to load clients', 'Could not fetch registered clients.');
|
||||
});
|
||||
}, [error, fetchClients]);
|
||||
|
||||
useEffect(() => {
|
||||
const updateClock = () => setNow(Date.now());
|
||||
@@ -62,18 +66,11 @@ export default function ClientsPage() {
|
||||
const offlineCount = clients.length - onlineCount - pendingCount;
|
||||
|
||||
return (
|
||||
<div className="w-full max-w-[var(--console-width)]">
|
||||
<div className="mb-3 flex items-center gap-1.5 text-xs text-[var(--text-faint)]" aria-label="Breadcrumb">
|
||||
<span>Console</span>
|
||||
<span aria-hidden="true" className="text-[var(--border-strong)]">/</span>
|
||||
<span aria-current="page" className="text-[var(--text)]">Clients</span>
|
||||
</div>
|
||||
<section className="w-full">
|
||||
<AdminPageHeader
|
||||
title="Clients"
|
||||
subtitle="Registered SDK clients and their API access"
|
||||
actions={
|
||||
<Button type="button" variant="primary" size="sm" onClick={() => setModalOpen(true)}>
|
||||
<Plus className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
<Button type="button" variant="primary" size="xs" icon="Plus" onClick={() => setModalOpen(true)}>
|
||||
New client
|
||||
</Button>
|
||||
}
|
||||
@@ -81,10 +78,10 @@ export default function ClientsPage() {
|
||||
|
||||
<StatStrip
|
||||
items={[
|
||||
{ label: 'Total clients', value: clients.length },
|
||||
{ label: 'Online now', value: onlineCount, dot: 'ok' },
|
||||
{ label: 'Total', value: clients.length },
|
||||
{ label: 'Online', value: onlineCount, dot: 'ok' },
|
||||
{ label: 'Offline', value: offlineCount, dot: 'off' },
|
||||
{ label: 'Pending setup', value: pendingCount, dot: 'pending' },
|
||||
{ label: 'Pending', value: pendingCount, dot: 'pending' },
|
||||
]}
|
||||
/>
|
||||
|
||||
@@ -101,6 +98,6 @@ export default function ClientsPage() {
|
||||
onClose={() => setModalOpen(false)}
|
||||
onCreated={fetchClients}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ import React from 'react';
|
||||
|
||||
export default function VconLayout({ children }: { children: React.ReactNode }) {
|
||||
const pathname = usePathname();
|
||||
if (pathname === '/vcon/login') return <>{children}</>;
|
||||
if (pathname === '/vcon/login' || pathname === '/vcon/setup') return <>{children}</>;
|
||||
|
||||
return <MainLayout showSidebar={true}>{children}</MainLayout>;
|
||||
}
|
||||
+51
-70
@@ -1,8 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import { CodeBlock } from '@/components/ui/CodeBlock';
|
||||
import { Select } from '@/components/ui/Input';
|
||||
import { Panel } from '@/components/ui/Panel';
|
||||
import { useToast } from '@/components/ui/Toast';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
@@ -35,102 +38,80 @@ export default function SdkGuidePage() {
|
||||
|
||||
const pythonSnippet = `from datastack_sdk import DataStackClient
|
||||
|
||||
# Initialize client using credentials generated in /vcon/clients
|
||||
client = DataStackClient(
|
||||
server_url="http://localhost:4000",
|
||||
token="${sampleToken}"
|
||||
)
|
||||
|
||||
# Connect with automatic reconnection backoff
|
||||
if client.connect():
|
||||
# 1. Sync single event in realtime
|
||||
res = client.sync({
|
||||
"event_type": "page_view",
|
||||
"url": "/products/wireless-headphones",
|
||||
"user_id": "usr_9942",
|
||||
"referrer": "google.com"
|
||||
"user_id": "usr_9942"
|
||||
})
|
||||
print("Sync response:", res)
|
||||
|
||||
# 2. Sync high-throughput batch of records
|
||||
batch_res = client.batch_sync([
|
||||
{"sensor": "temp", "value": 24.5, "unit": "C"},
|
||||
{"sensor": "humidity", "value": 61.2, "unit": "%"},
|
||||
{"sensor": "pressure", "value": 1013.25, "unit": "hPa"}
|
||||
{"sensor": "humidity", "value": 61.2, "unit": "%"}
|
||||
])
|
||||
print("Batch response:", batch_res)
|
||||
|
||||
client.disconnect()
|
||||
`;
|
||||
client.disconnect()`;
|
||||
|
||||
const copySnippet = (text: string, label: string) => {
|
||||
navigator.clipboard.writeText(text);
|
||||
success('Copied', `${label} copied to clipboard`);
|
||||
success('Copied', `${label} copied`);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4 max-w-[960px]">
|
||||
{/* Header */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 border-b border-[var(--border)] pb-2">
|
||||
<h1 className="text-[22px] font-semibold text-[var(--text)]">
|
||||
Python SDK
|
||||
</h1>
|
||||
|
||||
<Button
|
||||
size="xs"
|
||||
variant="secondary"
|
||||
icon="Copy"
|
||||
onClick={() => copySnippet(pythonSnippet, 'Python Snippet')}
|
||||
className="self-start sm:self-auto"
|
||||
>
|
||||
Copy Code
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Installation Step */}
|
||||
<Card compact className="flex flex-col gap-3 font-mono text-[13px]">
|
||||
<div className="p-3 bg-[var(--bg-main)] border border-[var(--border)] flex items-center justify-between">
|
||||
<span className="text-[var(--accent)]">pip install ./sdk-python</span>
|
||||
<section className="flex w-full flex-col gap-2.5">
|
||||
<AdminPageHeader
|
||||
title="Python SDK"
|
||||
actions={
|
||||
<Button
|
||||
size="xs"
|
||||
variant="ghost"
|
||||
variant="secondary"
|
||||
icon="Copy"
|
||||
onClick={() => copySnippet('pip install ./sdk-python', 'Pip command')}
|
||||
/>
|
||||
</div>
|
||||
onClick={() => copySnippet(pythonSnippet, 'Snippet')}
|
||||
>
|
||||
Copy Code
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<Card compact className="flex items-center justify-between bg-[var(--surface)] px-3 py-2 font-mono text-[11.5px]">
|
||||
<span className="text-[var(--accent)]">pip install ./sdk-python</span>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="ghost"
|
||||
icon="Copy"
|
||||
onClick={() => copySnippet('pip install ./sdk-python', 'Pip command')}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
{/* Interactive Code Generator */}
|
||||
<Card compact className="flex flex-col gap-2">
|
||||
<CardHeader className="p-0 pb-1 mb-0 border-b-0">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2 w-full">
|
||||
<CardTitle>Client Code Example</CardTitle>
|
||||
{clients.length > 0 && (
|
||||
<div className="w-[180px]">
|
||||
<Select
|
||||
compact
|
||||
value={selectedClientId}
|
||||
onChange={(e) => setSelectedClientId(e.target.value)}
|
||||
>
|
||||
{clients.map((c) => (
|
||||
<option key={c.clientId} value={c.clientId}>
|
||||
{c.clientId}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="p-0 flex flex-col gap-1">
|
||||
<div className="relative">
|
||||
<pre className="p-4 bg-[var(--bg-main)] border border-[var(--border)] text-[var(--text)] text-[12px] font-mono overflow-x-auto leading-relaxed">
|
||||
{pythonSnippet}
|
||||
</pre>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
<Panel
|
||||
title="Client Snippet"
|
||||
action={
|
||||
clients.length > 0 ? (
|
||||
<div className="w-[170px]">
|
||||
<Select
|
||||
compact
|
||||
value={selectedClientId}
|
||||
onChange={(e) => setSelectedClientId(e.target.value)}
|
||||
>
|
||||
{clients.map((c) => (
|
||||
<option key={c.clientId} value={c.clientId}>
|
||||
{c.clientId}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
) : undefined
|
||||
}
|
||||
>
|
||||
<CodeBlock>{pythonSnippet}</CodeBlock>
|
||||
</Panel>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
+63
-73
@@ -1,8 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card';
|
||||
import { CodeBlock } from '@/components/ui/CodeBlock';
|
||||
import { Select, Textarea } from '@/components/ui/Input';
|
||||
import { Panel } from '@/components/ui/Panel';
|
||||
import { useToast } from '@/components/ui/Toast';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
@@ -48,7 +50,7 @@ export default function SimulatorPage() {
|
||||
try {
|
||||
parsedData = JSON.parse(payloadText);
|
||||
} catch {
|
||||
error('Invalid JSON', 'Please fix payload syntax before emitting');
|
||||
error('Invalid JSON', 'Fix payload syntax');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -67,7 +69,7 @@ export default function SimulatorPage() {
|
||||
setLastResponse(data);
|
||||
|
||||
if (res.ok) {
|
||||
success('Event Accepted', `Event ID: ${data.event.eventId} buffered to Redis`);
|
||||
success('Event Accepted', `${data.event.eventId} buffered`);
|
||||
} else {
|
||||
error('Event Rejected', data.error || 'Server rejected');
|
||||
}
|
||||
@@ -98,7 +100,7 @@ export default function SimulatorPage() {
|
||||
});
|
||||
if (res.ok) accepted++;
|
||||
}
|
||||
success('Batch Streamed', `${accepted}/${count} records ingested into buffer`);
|
||||
success('Batch Streamed', `${accepted}/${count} ingested`);
|
||||
setLastResponse({ success: true, batchSent: count, accepted });
|
||||
} catch (err: any) {
|
||||
error('Batch Error', err.message);
|
||||
@@ -108,78 +110,66 @@ export default function SimulatorPage() {
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4 max-w-[960px]">
|
||||
{/* Header */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 border-b border-[var(--border)] pb-2">
|
||||
<h1 className="text-[22px] font-semibold text-[var(--text)]">
|
||||
Simulator
|
||||
</h1>
|
||||
|
||||
<div className="flex items-center gap-1.5 self-start sm:self-auto">
|
||||
<Button
|
||||
size="xs"
|
||||
variant="secondary"
|
||||
loading={sending}
|
||||
onClick={() => handleSendBatch(10)}
|
||||
>
|
||||
Burst 10
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="primary"
|
||||
icon="Send"
|
||||
loading={sending}
|
||||
onClick={handleSendSingle}
|
||||
>
|
||||
Send Event
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 xl:grid-cols-2 gap-4">
|
||||
{/* Editor Card */}
|
||||
<Card compact className="flex flex-col gap-2 p-2 bg-[var(--bg-elevated)]">
|
||||
<CardHeader className="p-0 pb-1 mb-0 border-b-0">
|
||||
<CardTitle>Payload Configuration</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="p-0 flex flex-col gap-2">
|
||||
<Select
|
||||
value={selectedClient}
|
||||
onChange={(e) => setSelectedClient(e.target.value)}
|
||||
<section className="flex w-full flex-col gap-2.5">
|
||||
<AdminPageHeader
|
||||
title="Simulator"
|
||||
actions={
|
||||
<>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="secondary"
|
||||
loading={sending}
|
||||
onClick={() => handleSendBatch(10)}
|
||||
>
|
||||
{clients.map((c) => (
|
||||
<option key={c.clientId} value={c.clientId}>
|
||||
{c.clientId}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
Burst 10
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="primary"
|
||||
icon="Send"
|
||||
loading={sending}
|
||||
onClick={handleSendSingle}
|
||||
>
|
||||
Send Event
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<Textarea
|
||||
rows={8}
|
||||
value={payloadText}
|
||||
onChange={(e) => setPayloadText(e.target.value)}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="grid grid-cols-1 gap-2.5 xl:grid-cols-2">
|
||||
<Panel title="Payload" bodyClassName="flex flex-col gap-2.5 p-3">
|
||||
<Select
|
||||
compact
|
||||
value={selectedClient}
|
||||
onChange={(e) => setSelectedClient(e.target.value)}
|
||||
>
|
||||
{clients.map((c) => (
|
||||
<option key={c.clientId} value={c.clientId}>
|
||||
{c.clientId}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
|
||||
{/* Response Inspector */}
|
||||
<Card compact className="flex flex-col gap-3 bg-[var(--bg-elevated)] font-mono text-[12px]">
|
||||
<CardHeader className="p-0 pb-1 mb-0 border-b-0">
|
||||
<CardTitle>Pipeline Response</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="p-0 flex flex-col gap-1">
|
||||
{lastResponse ? (
|
||||
<pre className="p-4 bg-[var(--bg-main)] border border-[var(--border)] text-[12px] text-[var(--text)] overflow-x-auto max-h-[320px]">
|
||||
{JSON.stringify(lastResponse, null, 2)}
|
||||
</pre>
|
||||
) : (
|
||||
<div className="p-6 text-center text-[var(--text-dim)] italic border border-dashed border-[var(--border-strong)]">
|
||||
Click 'Send Event' to test ingestion.
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Textarea
|
||||
rows={7}
|
||||
value={payloadText}
|
||||
onChange={(e) => setPayloadText(e.target.value)}
|
||||
className="font-mono text-[11.5px]"
|
||||
/>
|
||||
</Panel>
|
||||
|
||||
<Panel title="Response" bodyClassName="p-3 font-mono text-[11.5px]">
|
||||
{lastResponse ? (
|
||||
<CodeBlock className="max-h-[280px] border border-[var(--border)] text-[11px]">
|
||||
{JSON.stringify(lastResponse, null, 2)}
|
||||
</CodeBlock>
|
||||
) : (
|
||||
<div className="border border-dashed border-[var(--border-strong)] p-5 text-center text-[11px] text-[var(--text-dim)]">
|
||||
No response
|
||||
</div>
|
||||
)}
|
||||
</Panel>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
+59
-90
@@ -1,7 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import { Panel } from '@/components/ui/Panel';
|
||||
import { StatStrip } from '@/components/ui/StatStrip';
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
@@ -19,23 +21,19 @@ export default function StoragePage() {
|
||||
const { success, error } = useToast();
|
||||
const [stats, setStats] = useState<any>(null);
|
||||
const [files, setFiles] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [flushing, setFlushing] = useState(false);
|
||||
|
||||
const fetchStorageData = useCallback(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/storage');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setStats(data.stats);
|
||||
setFiles(data.files || []);
|
||||
}
|
||||
if (!res.ok) throw new Error('Unable to load storage partitions');
|
||||
const data = await res.json();
|
||||
setStats(data.stats);
|
||||
setFiles(data.files || []);
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
setLoading(false);
|
||||
error('Storage load failed', 'Could not load storage partitions.');
|
||||
}
|
||||
}, []);
|
||||
}, [error]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchStorageData();
|
||||
@@ -47,10 +45,10 @@ export default function StoragePage() {
|
||||
const res = await fetch('/api/storage', { method: 'POST' });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
success('Buffer Flushed', `Flushed ${data.flushedCount} events to CSV files`);
|
||||
success('Buffer Flushed', `Flushed ${data.flushedCount} events to CSV`);
|
||||
fetchStorageData();
|
||||
} else {
|
||||
error('Flush Failed', 'Could not flush buffer to disk');
|
||||
error('Flush Failed', 'Could not flush buffer');
|
||||
}
|
||||
} catch (err: any) {
|
||||
error('Error', err.message);
|
||||
@@ -60,101 +58,72 @@ export default function StoragePage() {
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
{/* Header */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 border-b border-[var(--border)] pb-2">
|
||||
<h1 className="text-[22px] font-semibold text-[var(--text)]">
|
||||
CSV Storage
|
||||
</h1>
|
||||
<section className="w-full">
|
||||
<AdminPageHeader
|
||||
title="Storage"
|
||||
actions={
|
||||
<>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="secondary"
|
||||
icon="RotateCw"
|
||||
onClick={fetchStorageData}
|
||||
>
|
||||
Refresh
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="primary"
|
||||
icon="HardDrive"
|
||||
loading={flushing}
|
||||
onClick={handleManualFlush}
|
||||
>
|
||||
Flush Buffer
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<div className="flex items-center gap-1.5 self-start sm:self-auto">
|
||||
<Button
|
||||
size="xs"
|
||||
variant="secondary"
|
||||
icon="RotateCw"
|
||||
onClick={fetchStorageData}
|
||||
>
|
||||
Refresh
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="primary"
|
||||
icon="HardDrive"
|
||||
loading={flushing}
|
||||
onClick={handleManualFlush}
|
||||
>
|
||||
Flush Buffer
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<StatStrip
|
||||
items={[
|
||||
{ label: 'Disk usage', value: `${stats?.totalMB ?? '0.00'} MB` },
|
||||
{ label: 'CSV files', value: stats?.totalFiles ?? 0 },
|
||||
{ label: 'Persisted rows', value: stats?.totalRows ? stats.totalRows.toLocaleString() : 0 },
|
||||
{ label: 'Rotation', value: '100MB / Daily' },
|
||||
]}
|
||||
/>
|
||||
|
||||
{/* Storage Summary Cards */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-3">
|
||||
<Card compact className="p-2 flex flex-col gap-0.5 bg-[var(--bg-elevated)]">
|
||||
<span className="text-[12px] uppercase text-[var(--text-muted)] font-medium">Disk Usage</span>
|
||||
<span className="text-[22px] font-semibold text-[var(--text)] font-mono">
|
||||
{stats?.totalMB ?? '0.00'} MB
|
||||
</span>
|
||||
</Card>
|
||||
|
||||
<Card compact className="p-2 flex flex-col gap-0.5 bg-[var(--bg-elevated)]">
|
||||
<span className="text-[12px] uppercase text-[var(--text-muted)] font-medium">CSV Files</span>
|
||||
<span className="text-[22px] font-semibold text-[var(--text)] font-mono">
|
||||
{stats?.totalFiles ?? 0}
|
||||
</span>
|
||||
</Card>
|
||||
|
||||
<Card compact className="p-2 flex flex-col gap-0.5 bg-[var(--bg-elevated)]">
|
||||
<span className="text-[12px] uppercase text-[var(--text-muted)] font-medium">Persisted Rows</span>
|
||||
<span className="text-[22px] font-semibold text-[var(--text)] font-mono">
|
||||
{stats?.totalRows ? stats.totalRows.toLocaleString() : 0}
|
||||
</span>
|
||||
</Card>
|
||||
|
||||
<Card compact className="p-2 flex flex-col gap-0.5 bg-[var(--bg-elevated)]">
|
||||
<span className="text-[12px] uppercase text-[var(--text-muted)] font-medium">Rotation Rule</span>
|
||||
<span className="text-[17px] font-semibold text-[var(--accent)] font-mono">
|
||||
100MB / Daily
|
||||
</span>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Partition Files Table */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<h2 className="text-[17px] font-semibold text-[var(--text)]">
|
||||
CSV Files on Disk
|
||||
</h2>
|
||||
|
||||
<TableWrap>
|
||||
<Panel title="CSV partitions" meta={`${files.length}`}>
|
||||
<TableWrap className="border-0 bg-transparent">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Client Folder</TableHead>
|
||||
<TableHead>Partition Filename</TableHead>
|
||||
<TableHead>File Size</TableHead>
|
||||
<TableHead>Data Lines</TableHead>
|
||||
<TableHead>Last Rotation / Append</TableHead>
|
||||
<TableHead>Client</TableHead>
|
||||
<TableHead>Filename</TableHead>
|
||||
<TableHead>Size</TableHead>
|
||||
<TableHead>Rows</TableHead>
|
||||
<TableHead>Modified</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{files.length === 0 ? (
|
||||
<TableEmpty colSpan={5} message="No CSV files generated yet. Events in memory buffer will flush automatically or via 'Flush Buffer Now'." />
|
||||
<TableEmpty colSpan={5} message="No CSV files" />
|
||||
) : (
|
||||
files.map((file, idx) => (
|
||||
<TableRow key={idx}>
|
||||
<TableCell className="font-mono text-[12px] text-[var(--accent)]">
|
||||
<TableCell className="font-mono text-[11px] text-[var(--accent)]">
|
||||
{file.clientId}
|
||||
</TableCell>
|
||||
<TableCell className="font-mono text-[12px] font-medium text-[var(--text)]">
|
||||
<TableCell className="font-mono text-[11px] font-medium text-[var(--text)]">
|
||||
{file.filename}
|
||||
</TableCell>
|
||||
<TableCell className="font-mono text-[12px]">
|
||||
<TableCell className="font-mono text-[11px] tabular-nums">
|
||||
{(file.sizeBytes / 1024).toFixed(1)} KB
|
||||
</TableCell>
|
||||
<TableCell className="font-mono text-[12px]">
|
||||
<TableCell className="font-mono text-[11px] tabular-nums">
|
||||
{file.linesCount.toLocaleString()}
|
||||
</TableCell>
|
||||
<TableCell className="text-[12px] text-[var(--text-dim)] font-mono">
|
||||
<TableCell className="font-mono text-[11px] tabular-nums text-[var(--text-dim)]">
|
||||
{new Date(file.modifiedAt).toLocaleString()}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
@@ -163,7 +132,7 @@ export default function StoragePage() {
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableWrap>
|
||||
</div>
|
||||
</div>
|
||||
</Panel>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user