import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip'; import { useTRPC } from '@/integrations/trpc/react'; import { cn } from '@/utils/cn'; import { useQuery } from '@tanstack/react-query'; import type { IChartProps } from '@openpanel/validation'; import { AnimatedNumber } from '../animated-number'; interface RealtimeLiveHistogramProps { projectId: string; } export const getReport = (projectId: string): IChartProps => { return { projectId, events: [ { segment: 'user', filters: [], name: '*', displayName: 'Active users', }, ], chartType: 'histogram', interval: 'minute', range: '30min', name: '', metric: 'sum', breakdowns: [], lineType: 'monotone', previous: false, }; }; export const getCountReport = (projectId: string): IChartProps => { return { name: '', projectId, events: [ { segment: 'user', filters: [], id: 'A', name: 'session_start', }, ], breakdowns: [], chartType: 'metric', lineType: 'monotone', interval: 'minute', range: '30min', previous: false, metric: 'sum', }; }; export function RealtimeLiveHistogram({ projectId, }: RealtimeLiveHistogramProps) { const report = getReport(projectId); const countReport = getCountReport(projectId); const trpc = useTRPC(); const res = useQuery(trpc.chart.chart.queryOptions(report)); const countRes = useQuery(trpc.chart.chart.queryOptions(countReport)); const metrics = res.data?.series[0]?.metrics; const minutes = (res.data?.series[0]?.data || []).slice(-30); const liveCount = countRes.data?.series[0]?.metrics?.sum ?? 0; if (res.isInitialLoading || countRes.isInitialLoading || liveCount === 0) { const staticArray = [ 10, 25, 30, 45, 20, 5, 55, 18, 40, 12, 50, 35, 8, 22, 38, 42, 15, 28, 52, 5, 48, 14, 32, 58, 7, 19, 33, 56, 24, 5, ]; return ( {staticArray.map((percent, i) => (
))} ); } if (!res.isSuccess && !countRes.isSuccess) { return null; } return ( {minutes.map((minute) => { return (
{minute.count} active users
@ {new Date(minute.date).toLocaleTimeString()}
); })} ); } interface WrapperProps { children: React.ReactNode; count: number; } function Wrapper({ children, count }: WrapperProps) { return (
Unique vistors last 30 minutes
{children}
); }