2026-10-23 11:43:49 -05:00
|
|
|
'use client';
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { Button } from '@/components/ui/Button';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { CodeBlock } from '@/components/ui/CodeBlock';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { Select, Textarea } from '@/components/ui/Input';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { Panel } from '@/components/ui/Panel';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { useToast } from '@/components/ui/Toast';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { useEffect, useState } from 'react';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
export default function SimulatorPage() {
|
|
|
|
|
const { success, error } = useToast();
|
|
|
|
|
const [clients, setClients] = useState<any[]>([]);
|
|
|
|
|
const [selectedClient, setSelectedClient] = useState<string>('');
|
|
|
|
|
const [payloadText, setPayloadText] = useState<string>(
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
{
|
|
|
|
|
event_name: 'sensor_telemetry',
|
|
|
|
|
temperature: 24.8,
|
|
|
|
|
humidity: 58.2,
|
|
|
|
|
voltage: 3.31,
|
|
|
|
|
status: 'NOMINAL',
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
2
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
const [sending, setSending] = useState(false);
|
|
|
|
|
const [lastResponse, setLastResponse] = useState<any>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async function loadClients() {
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/clients');
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
const list = data.clients || [];
|
|
|
|
|
setClients(list);
|
|
|
|
|
if (list.length > 0) setSelectedClient(list[0].clientId);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadClients();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleSendSingle = async () => {
|
|
|
|
|
let parsedData: any;
|
|
|
|
|
try {
|
|
|
|
|
parsedData = JSON.parse(payloadText);
|
|
|
|
|
} catch {
|
2026-11-16 21:58:18 +06:00
|
|
|
error('Invalid JSON', 'Fix payload syntax');
|
2026-10-23 11:43:49 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSending(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/events', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
clientId: selectedClient || 'py_telemetry_agent_01',
|
|
|
|
|
data: parsedData,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
setLastResponse(data);
|
|
|
|
|
|
|
|
|
|
if (res.ok) {
|
2026-11-16 21:58:18 +06:00
|
|
|
success('Event Accepted', `${data.event.eventId} buffered`);
|
2026-10-23 11:43:49 -05:00
|
|
|
} else {
|
|
|
|
|
error('Event Rejected', data.error || 'Server rejected');
|
|
|
|
|
}
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
error('Error', err.message);
|
|
|
|
|
} finally {
|
|
|
|
|
setSending(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSendBatch = async (count = 10) => {
|
|
|
|
|
setSending(true);
|
|
|
|
|
try {
|
|
|
|
|
let accepted = 0;
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
|
const res = await fetch('/api/events', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
clientId: selectedClient || 'py_telemetry_agent_01',
|
|
|
|
|
data: {
|
|
|
|
|
batch_index: i + 1,
|
|
|
|
|
sensor: `sensor_${(i % 3) + 1}`,
|
|
|
|
|
reading: +(Math.random() * 100).toFixed(2),
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
if (res.ok) accepted++;
|
|
|
|
|
}
|
2026-11-16 21:58:18 +06:00
|
|
|
success('Batch Streamed', `${accepted}/${count} ingested`);
|
2026-10-23 11:43:49 -05:00
|
|
|
setLastResponse({ success: true, batchSent: count, accepted });
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
error('Batch Error', err.message);
|
|
|
|
|
} finally {
|
|
|
|
|
setSending(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-16 21:58:18 +06:00
|
|
|
<section className="flex w-full flex-col gap-2.5">
|
|
|
|
|
<AdminPageHeader
|
|
|
|
|
title="Simulator"
|
|
|
|
|
actions={
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
size="xs"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
loading={sending}
|
|
|
|
|
onClick={() => handleSendBatch(10)}
|
2026-10-23 11:43:49 -05:00
|
|
|
>
|
2026-11-16 21:58:18 +06:00
|
|
|
Burst 10
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="xs"
|
|
|
|
|
variant="primary"
|
|
|
|
|
icon="Send"
|
|
|
|
|
loading={sending}
|
|
|
|
|
onClick={handleSendSingle}
|
|
|
|
|
>
|
|
|
|
|
Send Event
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<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>
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<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>
|
2026-10-23 11:43:49 -05:00
|
|
|
</div>
|
2026-11-16 21:58:18 +06:00
|
|
|
</section>
|
2026-10-23 11:43:49 -05:00
|
|
|
);
|
|
|
|
|
}
|