{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "realtime-02",
  "description": "Requests per second over the last five minutes, with the peak, the error rate and errors on their own scale.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/realtime.json",
    "https://www.dashboardblocks.com/r/radix/stat-group.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/realtime/realtime-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipContent,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartReferenceLabel,\n  chartReferenceProps,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  LiveBadge,\n  LiveNumber,\n  RollingBars,\n  createRandom,\n} from '@/registry/components/dashboardblocks/realtime'\nimport {\n  Stat,\n  StatGroup,\n  StatLabel,\n  StatValue,\n} from '@/registry/components/dashboardblocks/stat-group'\nimport {\n  formatCompact,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\nimport {\n  Area,\n  AreaChart,\n  CartesianGrid,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface ThroughputSample {\n  errors: number\n  requests: number\n}\n\ninterface Realtime2Props extends BlockStateProps {\n  description: string\n  /** One sample per interval, oldest first. Leave out, or pass none, to show the empty state. */\n  samples?: ThroughputSample[]\n  /** Seconds between samples. */\n  interval?: number\n  title: string\n}\n\nconst CHART_RIGHT = 6\nconst CHART_HEIGHT = 160\nconst noSamples: ThroughputSample[] = []\n\nconst seed = createRandom(11)\nconst makeSample = (random: () => number, index: number): ThroughputSample => {\n  const wave = index % 28 < 14 ? index % 14 : 14 - (index % 14)\n  const requests = Math.round(780 + wave * 16 + random() * 90)\n  const spike = random() > 0.93\n  return {\n    errors: Math.round(requests * (spike ? 0.02 + random() * 0.02 : random() * 0.004)),\n    requests,\n  }\n}\n\nconst exampleProps: Realtime2Props = {\n  description: 'Requests per second across all regions over the last 5 minutes.',\n  samples: Array.from({ length: 60 }, (_, index) => makeSample(seed, index)),\n  title: 'Throughput',\n}\n\nconst Realtime2 = (props: Realtime2Props) => {\n  const {\n    description,\n    interval = 5,\n    samples = noSamples,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const id = useChartId()\n  const state = getBlockState(status, samples)\n\n  const latest = samples[samples.length - 1] ?? { errors: 0, requests: 0 }\n  const peak = Math.max(0, ...samples.map((sample) => sample.requests))\n  const totals = samples.reduce(\n    (sum, sample) => ({\n      errors: sum.errors + sample.errors,\n      requests: sum.requests + sample.requests,\n    }),\n    { errors: 0, requests: 0 },\n  )\n  const errorRate = totals.requests > 0 ? totals.errors / totals.requests : 0\n  const average = samples.length > 0 ? totals.requests / samples.length : 0\n  const labels = samples.map((_, index) => {\n    const seconds = (samples.length - 1 - index) * interval\n    return seconds === 0 ? 'Now' : `${seconds}s ago`\n  })\n  const rows = samples.map((sample, index) => ({ ...sample, label: labels[index] }))\n  const minutes = Math.round((samples.length * interval) / 60)\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex items-start justify-between gap-3'>\n              <div className='flex min-w-0 flex-col gap-0.5'>\n                <h2 className='font-semibold tracking-tight'>{title}</h2>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </div>\n              {!state && <LiveBadge />}\n            </header>\n            <StatGroup className='grid-cols-2 @md:grid-cols-[2fr_1fr_1fr]'>\n              <Stat className='col-span-2 @md:col-span-1'>\n                <StatLabel>Requests per second</StatLabel>\n                <StatValue>\n                  <LiveNumber value={latest.requests} />\n                </StatValue>\n              </Stat>\n              <Stat className='justify-end'>\n                <StatLabel>Peak, {minutes} min</StatLabel>\n                <StatValue className='text-2xl'>\n                  <LiveNumber value={peak} />\n                </StatValue>\n              </Stat>\n              <Stat className='justify-end'>\n                <StatLabel>Error rate</StatLabel>\n                <StatValue className='text-2xl'>\n                  <LiveNumber\n                    format={(value) => formatPercent(value, { maximumFractionDigits: 2 })}\n                    value={errorRate}\n                  />\n                </StatValue>\n              </Stat>\n            </StatGroup>\n            {state && <ChartPlaceholder height={CHART_HEIGHT} />}\n            {!state && (\n              <div\n                aria-hidden\n                className='w-full tabular-nums'\n                style={{ height: CHART_HEIGHT }}\n              >\n                <ResponsiveContainer width='100%' height='100%'>\n                  <AreaChart\n                    accessibilityLayer={false}\n                    data={rows}\n                    margin={{ top: 8, right: CHART_RIGHT, bottom: 0, left: 0 }}\n                  >\n                    <defs>\n                      <linearGradient id={`${id}-fill`} x1='0' x2='0' y1='0' y2='1'>\n                        <stop\n                          offset='0%'\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0.35}\n                        />\n                        <stop\n                          offset='100%'\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0.02}\n                        />\n                      </linearGradient>\n                    </defs>\n                    <CartesianGrid {...chartGridProps} />\n                    <YAxis\n                      {...chartAxisProps}\n                      domain={[0, 'auto']}\n                      tickFormatter={formatCompact}\n                      width={chartValueAxisWidth}\n                    />\n                    <ReferenceLine\n                      {...chartReferenceProps}\n                      y={average}\n                      label={{\n                        ...chartReferenceLabel,\n                        dy: -6,\n                        position: 'insideBottomLeft',\n                        value: `avg ${formatNumber(Math.round(average))}/s`,\n                      }}\n                    />\n                    <Tooltip\n                      content={({ active, payload }) => (\n                        <ChartTooltipContent\n                          active={active}\n                          label={\n                            (payload?.[0]?.payload as { label?: string } | undefined)\n                              ?.label\n                          }\n                          payload={payload}\n                          valueFormatter={(value) => `${formatNumber(value)}/s`}\n                        />\n                      )}\n                      cursor={chartLineCursor}\n                    />\n                    <Area\n                      activeDot={chartActiveDot}\n                      dataKey='requests'\n                      dot={chartLastDot(rows.length, 'var(--color-chart-2)')}\n                      fill={`url(#${id}-fill)`}\n                      isAnimationActive={false}\n                      name='Requests'\n                      stroke='var(--color-chart-2)'\n                      strokeWidth={2}\n                      type='monotone'\n                    />\n                  </AreaChart>\n                </ResponsiveContainer>\n              </div>\n            )}\n            <div\n              className='flex flex-col gap-1'\n              style={{ paddingLeft: chartValueAxisWidth, paddingRight: CHART_RIGHT }}\n            >\n              <span className='text-muted-foreground text-xs'>Errors per second</span>\n              <RollingBars\n                color='var(--color-destructive)'\n                formatter={(value) => `${formatNumber(value)}/s`}\n                height={32}\n                highlightLatest={false}\n                labels={labels}\n                values={samples.map((sample) => sample.errors)}\n              />\n              {!state && (\n                <div\n                  aria-hidden\n                  className='text-muted-foreground flex justify-between text-xs'\n                >\n                  <span>{minutes} min ago</span>\n                  <span>Now</span>\n                </div>\n              )}\n            </div>\n            {!state && (\n              <p className='sr-only'>\n                {`Latest: ${formatNumber(latest.requests)} requests and ${formatNumber(latest.errors)} errors per second. Peak ${formatNumber(peak)} and average ${formatNumber(Math.round(average))} requests per second in the last ${minutes} minutes.`}\n              </p>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Realtime2,\n  exampleProps as realtime2ExampleProps,\n  type Realtime2Props,\n  type ThroughputSample,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/realtime/realtime-02.tsx"
    }
  ],
  "meta": {
    "shows": "Requests per second over the last five minutes, with the peak, the error rate and errors on their own scale.",
    "size": "full",
    "usedWith": [
      "status-02",
      "alerts-01",
      "deployments-03"
    ],
    "component": "Realtime2",
    "props": "{ description: string; samples?: { errors: number; requests: number }[]; interval?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
