refactor: improve storage handling, UI feedback, and defaults

- Update default admin email to admin@datastack.dev
- Improve storage flush logic with partial success handling
- Enhance CSV download reliability using fetch and Blob
- Adjust simulator protocol resolution and timing
- Fix date range filtering in data API query
This commit is contained in:
Team Dark Devil committed 2026-11-19 00:42:38 +06:00
1 parent 40a6855724
commit 3ad9b1d4d2
17 files changed
+281 -78

No files matched your search

+2 -3
View File
@@ -33,7 +33,7 @@ export async function GET(request: NextRequest) {
let total = 0;
const addIfMatching = (event: DataEvent) => {
const eventDate = CSVStorageManager.extractSafePartitionDate(event.timestamp);
const eventDate = CSVStorageManager.extractSafePartitionDate(event.timestamp, null);
const dedupeKey = `${event.clientId}:${event.eventId}`;
const matchesSearch =
!search ||
@@ -43,8 +43,7 @@ export async function GET(request: NextRequest) {
if (
(clientId !== "all" && event.clientId !== clientId) ||
(from && eventDate < from) ||
(to && eventDate > to) ||
((from || to) && (!eventDate || (from && eventDate < from) || (to && eventDate > to))) ||
!matchesSearch ||
eventIds.has(dedupeKey)
) {
+41 -44
View File
@@ -24,9 +24,9 @@ type DispatchStatus = 'Ready' | 'Accepted' | 'Rejected' | 'Error';
function getSocketOrigin(): string {
const isLocalDev = window.location.port === '3000';
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
const host = isLocalDev ? `${window.location.hostname}:4000` : window.location.host;
return `${protocol}://${host}`;
return `${protocol}//${host}`;
}
export default function SimulatorPage() {
@@ -112,8 +112,8 @@ export default function SimulatorPage() {
const timer = window.setTimeout(() => {
socket.disconnect();
reject(new Error('WebSocket handshake timed out (is Port 4000 ingestion server running?)'));
}, 4500);
reject(new Error('WebSocket timeout (is Port 4000 ingestion server running?)'));
}, 5000);
socket.on('connect_error', (connectErr) => {
window.clearTimeout(timer);
@@ -122,49 +122,46 @@ export default function SimulatorPage() {
});
socket.on('connect', () => {
const responses: Record<string, unknown>[] = [];
let accepted = 0;
let rejected = 0;
let settled = 0;
const finishIfDone = () => {
if (settled >= items.length) {
window.clearTimeout(timer);
socket.disconnect();
resolve({ accepted, rejected, responses });
}
};
const onAck = (ack: Record<string, unknown>) => {
responses.push(ack);
if (ack?.status === 'ok' || ack?.status === 'duplicate') {
accepted++;
} else {
rejected++;
}
settled++;
finishIfDone();
};
const onErrorEvent = (errPayload: Record<string, unknown>) => {
responses.push(errPayload);
rejected++;
settled++;
finishIfDone();
};
socket.on('ack', onAck);
socket.on('error_event', onErrorEvent);
items.forEach((data, index) => {
const eventId = `sim_ws_${Date.now()}_${index}`;
socket.emit('telemetry_event', {
clientId,
eventId,
if (items.length === 1) {
const eventId = `sim_ws_${Date.now()}_0`;
socket.emit(
'sync_data',
{
clientId,
eventId,
timestamp: new Date().toISOString(),
data: items[0],
},
(response: Record<string, unknown>) => {
window.clearTimeout(timer);
socket.disconnect();
const ok = Boolean(response?.success);
resolve({
accepted: ok ? 1 : 0,
rejected: ok ? 0 : 1,
responses: [response || { success: false, error: 'Empty response' }],
});
}
);
} else {
const batchPayload = items.map((data, idx) => ({
eventId: `sim_ws_${Date.now()}_${idx}`,
timestamp: new Date().toISOString(),
data,
}));
socket.emit('batch_sync', batchPayload, (response: Record<string, unknown>) => {
window.clearTimeout(timer);
socket.disconnect();
const processed = Number(response?.processed) || 0;
const rejected = Number(response?.rejected) || 0;
const ok = Boolean(response?.success);
resolve({
accepted: processed,
rejected: ok && rejected === 0 ? 0 : (rejected || (ok ? 0 : items.length)),
responses: [response || { success: false, error: 'Empty response' }],
});
});
});
}
});
});
};
+20 -7
View File
@@ -19,7 +19,7 @@ import { getCached, setCached } from '@/lib/vconCache';
import { useCallback, useEffect, useState } from 'react';
export default function StoragePage() {
const { success, error } = useToast();
const { success, warning, error } = useToast();
const [stats, setStats] = useState<any>(() =>
getCached<any>('vcon:storage-stats', null)
);
@@ -48,15 +48,28 @@ export default function StoragePage() {
setFlushing(true);
try {
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`);
const data = await res.json().catch(() => ({}));
if (res.ok && data.success) {
const count = Number(data.flushedCount) || 0;
success(
'Buffer Flushed',
`Flushed ${count} event${count === 1 ? '' : 's'} to CSV${data.warning ? ` (${data.warning})` : ''}`
);
fetchStorageData();
} else if (data.flushedCount !== undefined && data.flushedCount > 0) {
// Partial flush: local events persisted, remote Port 4000 flush returned error
warning(
'Partial Flush',
`Flushed ${data.flushedCount} local events. Ingestion server: ${data.error || 'flush failed'}`
);
fetchStorageData();
} else {
error('Flush Failed', 'Could not flush buffer');
error('Flush Failed', data.error || 'Could not flush buffer');
}
} catch (err: any) {
error('Error', err.message);
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : 'Unknown network error';
error('Flush Error', msg);
} finally {
setFlushing(false);
}