# Tally Stats

> A stats section for a GPU network: requests per hour as one dot cluster per region, each in its own color. Pick a scenario and the dots move: an outage sends a region's requests to its neighbours with retries showing as hollow rings, and a launch-day peak brings new traffic in from the edge. Three plain numbers sit underneath.

A Stats Section block from KeyframeUI: a whole landing-page section built around the Tally component, free to install as source with the shadcn CLI. Built with React and Tailwind CSS.

- Live preview: https://keyframeui.com/blocks/stats-sections/tally-stats
- Category: Stats Sections
- Built with: [Tally](https://keyframeui.com/components/tally.md)
- npm dependencies: clsx, tailwind-merge

## Installation

Register the @keyframeui registry in `components.json` once per project:

```json
{
  "registries": {
    "@keyframeui": "https://keyframeui.com/r/{name}.json"
  }
}
```

Then add the block. It lands in `components/tally-stats.tsx`, and the CLI also adds the components it's built with (`components/ui/tally.tsx`) and `lib/utils.ts`, and installs the npm dependencies:

```bash
npx shadcn@latest add @keyframeui/tally-stats
```

Or install by URL, without registering the namespace:

```bash
npx shadcn@latest add https://keyframeui.com/r/tally-stats.json
```

## Usage

Put it where the section goes. The copy, links and data sit in constants at the top of the file; edit them there.

```tsx
import { TallyStats } from "@/components/tally-stats";

export default function Page() {
  return <TallyStats />;
}
```

## Source

```tsx title="components/tally-stats.tsx"
"use client";

import * as React from "react";

import { Tally, type TallyItem } from "@/components/ui/tally";

/* ── Content ─────────────────────────────────────────────────
 * Everything you'd change to make the section yours: the
 * heading, the scenarios a visitor can switch between, the
 * regions in each, and the numbers under the chart. Each dot
 * is a share of the requests; switching scenarios moves the
 * dots that changed region, and new traffic flies in from the
 * edge. Hollow rings are requests that were retried.
 * ─────────────────────────────────────────────────────────── */

const EYEBROW = "Network";
const HEADING = "When a region goes dark, the work moves.";
const LEAD =
  "Every request runs on the nearest free GPU. When a region drops out, its requests move to the next nearest within seconds, and none of them are lost on the way.";

/** One color per region, bright on white and on night: `text-*` classes the chart reads. */
const REGION_COLORS: Record<string, string> = {
  "us-east": "text-[#ff5a1f] dark:text-[#ff7a45]",
  "eu-central": "text-[#ff2d87] dark:text-[#ff5ca8]",
  "ap-northeast": "text-[#7c3aed] dark:text-[#a78bfa]",
  "sa-east": "text-[#00b25d] dark:text-[#34d399]",
};

const region = (id: string, value: number, hollow: number): TallyItem => ({
  id,
  label: id,
  value,
  hollow,
  dotClassName: REGION_COLORS[id],
});

/** What a visitor can switch between. Requests per hour, in each region. */
const SCENARIOS = [
  {
    id: "normal",
    label: "Normal hour",
    note: "A normal hour. Requests run where they're made.",
    items: [region("us-east", 52000, 600), region("eu-central", 38000, 400), region("ap-northeast", 26000, 300), region("sa-east", 12000, 200)],
  },
  {
    id: "outage",
    label: "Outage",
    note: "eu-central drops out. Its 38,000 requests move west and east; retries rise, nothing is lost.",
    items: [region("us-east", 74000, 2600), region("eu-central", 0, 0), region("ap-northeast", 40000, 1400), region("sa-east", 14000, 400)],
  },
  {
    id: "peak",
    label: "Launch peak",
    note: "A customer launches. Traffic grows by half, and idle GPUs in every region pick it up.",
    items: [region("us-east", 76000, 900), region("eu-central", 58000, 700), region("ap-northeast", 39000, 500), region("sa-east", 19000, 300)],
  },
];

const FACTS = [
  { label: "Failover", value: "4.2 s", note: "median, region to region" },
  { label: "Requests lost", value: "0", note: "in the last 12 outages" },
  { label: "Regions", value: "12", note: "on four continents" },
];

/** Seconds the first scenario holds before the outage plays by itself, if nobody has picked one. */
const AUTOPLAY_HOLD = 1.6;

const thousands = (n: number) => (n >= 1000 ? `${Math.round(n / 1000)}k` : String(n));

/* ── Section ─────────────────────────────────────────────── */

export function TallyStats() {
  const [index, setIndex] = React.useState(0);
  const touched = React.useRef(false);
  const played = React.useRef(false);
  const timer = React.useRef(0);
  const scenario = SCENARIOS[index];
  React.useEffect(() => () => window.clearTimeout(timer.current), []);

  // After the first settle, play the outage once, unless the visitor has already picked.
  const onSettle = React.useCallback(() => {
    if (played.current || touched.current) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
    played.current = true;
    timer.current = window.setTimeout(() => {
      if (!touched.current) setIndex(1);
    }, AUTOPLAY_HOLD * 1000);
  }, []);

  const pick = (i: number) => {
    touched.current = true;
    window.clearTimeout(timer.current);
    setIndex(i);
  };

  // A radio group: arrow keys move the choice and the focus together.
  const radios = React.useRef<(HTMLButtonElement | null)[]>([]);
  const onKeyDown = (event: React.KeyboardEvent) => {
    const step = event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 0;
    if (!step) return;
    event.preventDefault();
    const next = (index + step + SCENARIOS.length) % SCENARIOS.length;
    pick(next);
    radios.current[next]?.focus();
  };

  return (
    <section className="bg-white text-zinc-950 dark:bg-[#09090b] dark:text-white">
      <div className="mx-auto max-w-6xl px-6 py-20 sm:py-24">
        <div className="grid grid-cols-1 items-end gap-x-16 gap-y-8 lg:grid-cols-[minmax(0,1fr)_auto]">
          <div className="min-w-0">
            <p className="font-mono text-[11px] tracking-wide text-[#d93f06] uppercase dark:text-[#ff7a45]">{EYEBROW}</p>
            <h2 className="mt-4 max-w-2xl text-4xl leading-[1.05] font-semibold tracking-[-0.035em] text-balance sm:text-5xl">{HEADING}</h2>
            <p className="mt-5 max-w-xl text-base leading-relaxed text-zinc-600 dark:text-zinc-400">{LEAD}</p>
          </div>
          <div role="radiogroup" aria-label="Scenario" onKeyDown={onKeyDown} className="flex flex-wrap gap-1 justify-self-start rounded-full border border-zinc-950/10 p-1 dark:border-white/10">
            {SCENARIOS.map((s, i) => (
              <button
                key={s.id}
                ref={(el) => {
                  radios.current[i] = el;
                }}
                type="button"
                role="radio"
                aria-checked={i === index}
                tabIndex={i === index ? 0 : -1}
                onClick={() => pick(i)}
                className="rounded-full px-3.5 py-1.5 font-mono text-xs text-zinc-600 transition-colors duration-150 hover:text-zinc-950 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-zinc-950 aria-checked:bg-zinc-950 aria-checked:text-white dark:text-zinc-400 dark:hover:text-white dark:focus-visible:outline-white dark:aria-checked:bg-white dark:aria-checked:text-zinc-950"
              >
                {s.label}
              </button>
            ))}
          </div>
        </div>

        <div className="mt-10 border border-zinc-950/10 dark:border-white/10">
          <Tally
            items={scenario.items}
            unit="requests"
            hollowLabel="retried"
            format={thousands}
            dotSize={8}
            gap={2}
            label="Requests per hour by region"
            onSettle={onSettle}
            className="mx-4 mt-4 h-[26rem] sm:mx-6 sm:h-80 lg:h-[21rem]"
          />
          <p aria-live="polite" className="border-t border-zinc-950/10 px-4 py-3.5 font-mono text-xs leading-relaxed text-zinc-600 sm:px-6 dark:border-white/10 dark:text-zinc-400">
            {scenario.note}
          </p>
        </div>

        <dl className="mt-10 grid grid-cols-1 gap-8 sm:grid-cols-3">
          {FACTS.map((f) => (
            <div key={f.label} className="border-t border-zinc-950/10 pt-4 dark:border-white/10">
              <dt className="font-mono text-[11px] tracking-wide text-zinc-500 uppercase dark:text-zinc-400">{f.label}</dt>
              <dd className="mt-2 text-3xl font-semibold tracking-tight tabular-nums">{f.value}</dd>
              <dd className="mt-1 text-sm text-zinc-600 dark:text-zinc-400">{f.note}</dd>
            </div>
          ))}
        </dl>
      </div>
    </section>
  );
}
```
