import { For, Show } from "solid-js"; import { STREAM_CONFIGS, StreamType } from "./config"; const TOP_COLLECTION_LIMIT = 5; export type StreamStats = { connectedAt?: number; totalEvents: number; eventsPerSecond: number; 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; showCollections?: boolean; }) => { const config = () => STREAM_CONFIGS[props.streamType]; const uptime = () => (props.stats.connectedAt ? props.currentTime - props.stats.connectedAt : 0); const topCollections = () => Object.entries(props.stats.collections) .sort(([, a], [, b]) => b - a) .slice(0, TOP_COLLECTION_LIMIT); return (
Statistics
Uptime
{formatUptime(uptime())}
Total Events
{props.stats.totalEvents.toLocaleString()}
Events/sec
{props.stats.eventsPerSecond.toFixed(1)}
{config().collectionsLabel}
{([collection, count]) => { const percentage = ((count / props.stats.totalEvents) * 100).toFixed(1); return ( <> {collection} {count.toLocaleString()} {percentage}% ); }}
); };