import { For, Show } from "solid-js"; import { STREAM_CONFIGS, StreamType } from "./config"; export type StreamStats = { connectedAt?: number; totalEvents: number; eventsPerSecond: number; eventTypes: Record; collections: Record; }; const formatUptime = (ms: number) => { const seconds = Math.floor(ms / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); if (hours > 0) { return `${hours}h ${minutes % 60}m ${seconds % 60}s`; } else if (minutes > 0) { return `${minutes}m ${seconds % 60}s`; } else { return `${seconds}s`; } }; export const StreamStatsPanel = (props: { stats: StreamStats; currentTime: number; streamType: StreamType; showAllEvents?: boolean; }) => { const config = () => STREAM_CONFIGS[props.streamType]; const uptime = () => (props.stats.connectedAt ? props.currentTime - props.stats.connectedAt : 0); const shouldShowEventTypes = () => { if (!config().showEventTypes) return false; if (props.streamType === "jetstream") return props.showAllEvents === true; return true; }; const topCollections = () => Object.entries(props.stats.collections) .sort(([, a], [, b]) => b - a) .slice(0, 5); const topEventTypes = () => Object.entries(props.stats.eventTypes) .sort(([, a], [, b]) => b - a) .slice(0, 5); return (
Statistics
Uptime
{formatUptime(uptime())}
Total Events
{props.stats.totalEvents.toLocaleString()}
Events/sec
{props.stats.eventsPerSecond.toFixed(1)}
Avg/sec
{uptime() > 0 ? ((props.stats.totalEvents / uptime()) * 1000).toFixed(1) : "0.0"}
0 && shouldShowEventTypes()}>
Event Types
{([type, count]) => { const percentage = ((count / props.stats.totalEvents) * 100).toFixed(1); return ( <> {type} {count.toLocaleString()} {percentage}% ); }}
0}>
{config().collectionsLabel}
{([collection, count]) => { const percentage = ((count / props.stats.totalEvents) * 100).toFixed(1); return ( <> {collection} {count.toLocaleString()} {percentage}% ); }}
); };