Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(analytics): serve /analytics with a growth chart and method note for every platform total

nirholasd0212823ae3a
diff --git a/pages/analytics.html b/pages/analytics.html
new file mode 100644
index 000000000..154795bf6
--- /dev/null
+++ b/pages/analytics.html
@@ -0,0 +1,204 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <style>/* three.ws hidden-guard: make the `hidden` attribute authoritative; see scripts/lib/hidden-guard.mjs */[hidden]{display:none!important}</style>
+ <script>/* three.ws theme boot: no-flash */(function(){try{var m=localStorage.getItem('twx_theme');var l=m==='auto'?(window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches):m==='light';document.documentElement.setAttribute('data-theme',l?'light':'dark');}catch(e){document.documentElement.setAttribute('data-theme','dark');}})();</script>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
+ <title>Platform Analytics · three.ws</title>
+ <meta name="description" content="Live three.ws platform totals and growth: agents, wallets, coins launched, 3D models generated, LLM tokens processed, x402 settlements, marketplace sales and agent-to-agent hire volume, each with a daily chart and a note on exactly how it is counted." />
+ <link rel="canonical" href="https://three.ws/analytics" />
+ <link rel="icon" href="/favicon.ico" sizes="any" />
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
+ <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
+ <link rel="manifest" href="/manifest.webmanifest" />
+ <link rel="stylesheet" href="/style.css" />
+ <link rel="stylesheet" href="/nav.css" />
+ <link rel="stylesheet" href="/mobile.css" />
+ <meta property="og:type" content="website" />
+ <meta property="og:site_name" content="three.ws" />
+ <meta property="og:title" content="three.ws Platform Analytics" />
+ <meta property="og:description" content="Every platform total in one place, read live from the ledgers, with a growth chart and a how-it's-counted note under each number." />
+ <meta property="og:url" content="https://three.ws/analytics" />
+ <meta property="og:image" content="https://three.ws/api/page-og?s=crypto&amp;t=Platform+Analytics&amp;d=Every+three.ws+platform+total+in+one+place%2C+read+live+from+the+ledgers%2C+with+a+growth+chart+and+a+method+note+under+each+number.&amp;p=%2Fanalytics" />
+ <meta property="og:image:alt" content="three.ws Platform Analytics" />
+ <meta property="og:locale" content="en_US" />
+ <meta name="twitter:card" content="summary_large_image" />
+ <meta name="twitter:site" content="@trythreews" />
+ <meta name="twitter:title" content="three.ws Platform Analytics" />
+ <meta name="twitter:description" content="Every platform total in one place, read live, with a growth chart and a how-it's-counted note under each number." />
+ <meta name="twitter:image" content="https://three.ws/api/page-og?s=crypto&amp;t=Platform+Analytics&amp;d=Every+three.ws+platform+total+in+one+place%2C+read+live+from+the+ledgers%2C+with+a+growth+chart+and+a+method+note+under+each+number.&amp;p=%2Fanalytics" />
+ <meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1" />
+ <script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"Platform Analytics","description":"Live three.ws platform totals and growth, each with a daily chart and a note on how it is counted.","url":"https://three.ws/analytics","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"}}</script>
+ <style>
+ :root { --pa-maxw: 1140px; --pa-mark: #8b5cf6; --pa-mark-soft: rgba(139, 92, 246, 0.16); }
+ :root[data-theme="light"] { --pa-mark: #5b21b6; --pa-mark-soft: rgba(91, 33, 182, 0.12); }
+ * { box-sizing: border-box; }
+ html, body { margin: 0; background: var(--bg-0); color: var(--ink); }
+ body { font-family: var(--font-body, 'Inter', system-ui, sans-serif); min-height: 100vh; }
+ .site-header { position: sticky; top: 0; z-index: 50; background: color-mix(in srgb, var(--bg-0) 82%, transparent); backdrop-filter: blur(10px); }
+ a { color: inherit; }
+
+ .pa-wrap { max-width: var(--pa-maxw); margin: 0 auto; padding: 0 18px 96px; }
+
+ /* hero */
+ .pa-hero { padding: 40px 0 8px; }
+ .pa-eyebrow { font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--pa-mark); font-weight: 650; margin: 0 0 10px; }
+ .pa-hero h1 { font-size: clamp(1.8rem, 4.6vw, 2.8rem); line-height: 1.1; margin: 0 0 12px; letter-spacing: -0.025em; color: var(--ink-bright, var(--ink)); }
+ .pa-hero p { margin: 0; max-width: 68ch; color: var(--ink-dim); line-height: 1.6; font-size: 1.02rem; }
+ .pa-hero p a { text-decoration: underline; text-underline-offset: 3px; }
+ .pa-hero p a:hover { color: var(--ink-bright, var(--ink)); }
+
+ /* toolbar */
+ .pa-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin: 26px 0 18px; }
+ .pa-seg { display: inline-flex; background: var(--surface-1); border: 1px solid var(--stroke); border-radius: 10px; padding: 3px; gap: 2px; }
+ .pa-seg button {
+ appearance: none; border: 0; background: none; color: var(--ink-dim); font: inherit; font-size: 0.82rem; font-weight: 600;
+ padding: 6px 12px; border-radius: 7px; cursor: pointer; min-height: 32px;
+ transition: background var(--duration-fast, 140ms) var(--ease-standard, ease), color var(--duration-fast, 140ms) var(--ease-standard, ease), transform 80ms var(--ease-standard, ease);
+ }
+ .pa-seg button:hover { color: var(--ink-bright, var(--ink)); background: var(--surface-2); }
+ .pa-seg button:active { transform: translateY(1px); }
+ .pa-seg button:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 1px; }
+ .pa-seg button[aria-pressed="true"] { background: var(--surface-3); color: var(--ink-bright, var(--ink)); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); }
+ .pa-updated { margin-left: auto; font-size: 0.8rem; color: var(--ink-dim); font-variant-numeric: tabular-nums; }
+ .pa-api { font-size: 0.8rem; color: var(--ink-dim); text-decoration: underline; text-underline-offset: 3px; }
+ .pa-api:hover { color: var(--ink-bright, var(--ink)); }
+ .pa-api:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 2px; border-radius: 4px; }
+
+ /* error + partial */
+ .pa-error { display: flex; align-items: center; justify-content: space-between; gap: 14px; flex-wrap: wrap; padding: 14px 16px; margin: 0 0 18px; border-radius: var(--radius-md, 10px); border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent); background: color-mix(in srgb, var(--danger) 9%, transparent); color: var(--ink); font-size: 0.9rem; line-height: 1.5; }
+ .pa-error strong { color: var(--ink-bright, var(--ink)); }
+ .pa-error button { appearance: none; border: 1px solid var(--stroke-strong); background: var(--surface-2); color: var(--ink-bright, var(--ink)); font: inherit; font-size: 0.85rem; font-weight: 600; padding: 7px 14px; border-radius: 8px; cursor: pointer; transition: background var(--duration-fast, 140ms) var(--ease-standard, ease), transform 80ms; }
+ .pa-error button:hover { background: var(--surface-3); }
+ .pa-error button:active { transform: translateY(1px); }
+ .pa-error button:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 2px; }
+ .pa-error button[disabled] { opacity: 0.6; cursor: progress; }
+ .pa-partial { font-size: 0.85rem; color: var(--ink-dim); margin: -6px 0 16px; }
+
+ /* tiles */
+ .pa-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
+ .pa-tile {
+ display: block; text-decoration: none; padding: 16px 16px 14px; border-radius: var(--radius-md, 10px);
+ background: var(--surface-1); border: 1px solid var(--stroke); min-height: 108px;
+ transition: border-color var(--duration-fast, 140ms) var(--ease-standard, ease), background var(--duration-fast, 140ms) var(--ease-standard, ease), transform 120ms var(--ease-standard, ease);
+ animation: pa-in 260ms var(--ease-standard, ease) both;
+ }
+ a.pa-tile:hover { border-color: var(--stroke-strong); background: var(--surface-2); transform: translateY(-1px); }
+ a.pa-tile:active { transform: translateY(0); }
+ a.pa-tile:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 2px; }
+ .pa-tile-label { font-size: 0.8rem; color: var(--ink-dim); font-weight: 550; }
+ .pa-tile-value { font-size: 1.7rem; font-weight: 750; letter-spacing: -0.03em; margin-top: 6px; color: var(--ink-bright, var(--ink)); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
+ .pa-tile-value.is-muted { font-size: 1.15rem; font-weight: 650; color: var(--ink-dim); letter-spacing: -0.01em; margin-top: 10px; }
+ .pa-tile-sub { font-size: 0.78rem; color: var(--ink-dim); margin-top: 6px; line-height: 1.45; }
+ .pa-tile.is-pending, .pa-tile.is-unavailable { border-style: dashed; }
+
+ /* charts */
+ .pa-section { margin-top: 40px; }
+ .pa-section > h2 { font-size: 1.2rem; letter-spacing: -0.01em; margin: 0 0 6px; color: var(--ink-bright, var(--ink)); }
+ .pa-section > p { margin: 0 0 16px; color: var(--ink-dim); font-size: 0.9rem; line-height: 1.55; max-width: 72ch; }
+ .pa-charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 12px; }
+ .pa-card { padding: 16px 16px 12px; border-radius: var(--radius-md, 10px); background: var(--surface-1); border: 1px solid var(--stroke); min-width: 0; scroll-margin-top: 90px; animation: pa-in 260ms var(--ease-standard, ease) both; }
+ .pa-card:target { border-color: var(--pa-mark); }
+ .pa-card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
+ .pa-card-head h3 { margin: 0; font-size: 0.92rem; font-weight: 650; color: var(--ink-bright, var(--ink)); }
+ .pa-card-total { font-size: 0.85rem; color: var(--ink-dim); font-variant-numeric: tabular-nums; white-space: nowrap; }
+ .pa-chart-wrap { position: relative; }
+ .pa-canvas { display: block; width: 100%; height: 170px; touch-action: pan-y; cursor: crosshair; }
+ .pa-chart-skel { height: 170px; margin-top: 12px; border-radius: 8px; }
+ .pa-chart-msg { height: 170px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; text-align: center; color: var(--ink-dim); font-size: 0.86rem; padding: 0 12px; }
+ .pa-chart-msg a, .pa-link-btn { color: var(--ink-bright, var(--ink)); text-decoration: underline; text-underline-offset: 3px; }
+ .pa-chart-msg a:hover, .pa-link-btn:hover { color: var(--pa-mark); }
+ .pa-chart-msg a:focus-visible, .pa-link-btn:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 2px; border-radius: 4px; }
+ .pa-link-btn { appearance: none; background: none; border: 0; font: inherit; cursor: pointer; padding: 0; }
+ .pa-tip { position: absolute; pointer-events: none; opacity: 0; transform: translate(-50%, -100%); background: var(--bg-1); border: 1px solid var(--stroke-strong); border-radius: 8px; padding: 6px 10px; font-size: 0.76rem; line-height: 1.4; white-space: nowrap; box-shadow: 0 8px 22px rgba(0, 0, 0, 0.3); transition: opacity 120ms ease; z-index: 2; left: 0; top: 0; }
+ .pa-tip[data-open="1"] { opacity: 1; }
+ .pa-tip-day { color: var(--ink-dim); font-size: 0.7rem; }
+ .pa-tip-val { font-weight: 700; color: var(--ink-bright, var(--ink)); font-variant-numeric: tabular-nums; }
+
+ /* methods */
+ .pa-methods { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; margin: 0; }
+ .pa-method { padding: 14px 16px; border-radius: var(--radius-md, 10px); border: 1px solid var(--stroke); background: var(--surface-1); }
+ .pa-method dt { font-weight: 650; font-size: 0.9rem; color: var(--ink-bright, var(--ink)); display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
+ .pa-method dd { margin: 6px 0 0; color: var(--ink-dim); font-size: 0.85rem; line-height: 1.55; }
+ .pa-badge { font-size: 0.68rem; font-weight: 650; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--stroke-strong); color: var(--ink-dim); }
+ .pa-badge.is-warn { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
+ .pa-empty { color: var(--ink-dim); font-size: 0.9rem; padding: 18px 0; margin: 0; }
+ .pa-foot { margin-top: 28px; font-size: 0.85rem; color: var(--ink-dim); line-height: 1.6; }
+ .pa-foot a { text-decoration: underline; text-underline-offset: 3px; }
+ .pa-foot a:hover { color: var(--ink-bright, var(--ink)); }
+ .pa-foot a:focus-visible { outline: 2px solid var(--pa-mark); outline-offset: 2px; border-radius: 4px; }
+
+ .skeleton { background: var(--surface-2); background-image: linear-gradient(90deg, var(--surface-1) 0%, var(--surface-3) 50%, var(--surface-1) 100%); background-size: 200% 100%; border-radius: 6px; animation: pa-shimmer 1.4s ease-in-out infinite; }
+ @keyframes pa-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
+ @keyframes pa-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
+ .visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0; }
+
+ @media (max-width: 720px) {
+ .pa-updated { margin-left: 0; width: 100%; }
+ .pa-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
+ .pa-tile-value { font-size: 1.35rem; }
+ .pa-charts, .pa-methods { grid-template-columns: minmax(0, 1fr); }
+ }
+ @media (prefers-reduced-motion: reduce) {
+ .skeleton, .pa-tile, .pa-card { animation: none; }
+ a.pa-tile:hover { transform: none; }
+ }
+ </style>
+</head>
+<body>
+ <header class="site-header"><div id="nav-container"></div></header>
+ <script src="/nav.js"></script>
+
+ <main class="pa-wrap" id="main">
+ <section class="pa-hero">
+ <p class="pa-eyebrow">Platform analytics</p>
+ <h1>three.ws, in numbers</h1>
+ <p>Every total the platform publishes about itself, on one page. Each figure is read live from the table that records the activity, charted per day, and explained in a note below, so you can check exactly what was counted. The same data is open at <a href="/api/platform/analytics">/api/platform/analytics</a>.</p>
+ </section>
+
+ <div class="pa-toolbar">
+ <div class="pa-seg" id="pa-window" role="group" aria-label="Time window">
+ <button type="button" data-window="30d" aria-pressed="true">30 days</button>
+ <button type="button" data-window="90d" aria-pressed="false">90 days</button>
+ <button type="button" data-window="all" aria-pressed="false">All time</button>
+ </div>
+ <div class="pa-seg" id="pa-view" role="group" aria-label="Chart view">
+ <button type="button" data-view="daily" aria-pressed="true">Per day</button>
+ <button type="button" data-view="cumulative" aria-pressed="false">Running total</button>
+ </div>
+ <span class="pa-updated" id="pa-updated" aria-live="polite"></span>
+ </div>
+
+ <div class="pa-error" id="pa-error" role="alert" hidden>
+ <span><strong>Could not load the platform numbers.</strong> <span id="pa-error-detail"></span></span>
+ <button type="button" id="pa-retry">Retry</button>
+ </div>
+ <p class="pa-partial" id="pa-partial" role="status" hidden></p>
+
+ <section aria-labelledby="pa-totals-h">
+ <h2 class="visually-hidden" id="pa-totals-h">Totals</h2>
+ <div class="pa-tiles" id="pa-tiles" aria-busy="true"></div>
+ </section>
+
+ <section class="pa-section" aria-labelledby="pa-growth-h">
+ <h2 id="pa-growth-h">Growth</h2>
+ <p>One chart per metric over the selected window. Hover or tap a chart to read a single day; switch to Running total to see the all-time curve.</p>
+ <div class="pa-charts" id="pa-charts" aria-busy="true"></div>
+ </section>
+
+ <section class="pa-section" id="methods" aria-labelledby="pa-methods-h">
+ <h2 id="pa-methods-h">How each number is calculated</h2>
+ <p>Written by the same code that computes the numbers, so a note can never describe a different query than the one that ran.</p>
+ <dl class="pa-methods" id="pa-methods"></dl>
+ </section>
+
+ <p class="pa-foot">Want a single area in depth? See <a href="/agent-economy-volume">agent-to-agent volume</a>, the <a href="/pulse">money pulse</a>, <a href="/launches">every launch</a> and <a href="/status">platform status</a>. Building on these numbers? The endpoint is documented in the <a href="/docs/api-reference#platform-analytics-api">API reference</a>.</p>
+ </main>
+
+ <div id="footer-container"></div>
+ <script src="/footer.js"></script>
+ <script type="module" src="/src/platform-analytics.js"></script>
+ <script type="module" src="/i18n.js"></script>
+</body>
+</html>
diff --git a/src/platform-analytics.js b/src/platform-analytics.js
index a3f75aff8..24ad6d6e5 100644
--- a/src/platform-analytics.js
+++ b/src/platform-analytics.js
@@ -246,12 +246,27 @@ function cssVar(name, fallback) {
return v || fallback;
}
-function niceMax(v) {
- if (v <= 0) return 1;
- const p = 10 ** Math.floor(Math.log10(v));
- const f = v / p;
- const step = f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10;
- return step * p;
+function niceStep(raw) {
+ const p = 10 ** Math.floor(Math.log10(raw));
+ const f = raw / p;
+ return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10) * p;
+}
+
+// Four even gridline intervals on round numbers. Counts never get a fractional
+// tick (a "0.7 coins" gridline reads as a bug). The running-total view starts
+// its axis near the window's first value so growth is visible, not flattened.
+function axisScale(values, unit, fromZero) {
+ const max = Math.max(...values, 0);
+ const min = fromZero ? 0 : Math.min(...values);
+ const whole = unit === 'count' || unit === 'tokens';
+ let step = niceStep(Math.max((max - min) / 4, whole ? 1 : 1e-6));
+ if (whole) step = Math.max(1, Math.ceil(step));
+ let lo = fromZero ? 0 : Math.floor(min / step) * step;
+ while (lo + step * 4 < max) {
+ step = niceStep(step * 1.01);
+ lo = fromZero ? 0 : Math.floor(min / step) * step;
+ }
+ return { lo, hi: lo + step * 4, ticks: 4 };
}
function drawChart(entry) {
@@ -272,9 +287,7 @@ function drawChart(entry) {
const cw = W - pad.left - pad.right;
const ch = H - pad.top - pad.bottom;
const values = series.map((d) => d.value);
- const minV = state.view === 'cumulative' ? Math.min(...values) : 0;
- const lo = state.view === 'cumulative' ? Math.max(0, minV - (Math.max(...values) - minV) * 0.08) : 0;
- const hi = niceMax(Math.max(...values, lo + 1e-9));
+ const { lo, hi, ticks } = axisScale(values, metric.unit, state.view === 'daily');
const yOf = (v) => pad.top + ch - ((v - lo) / (hi - lo || 1)) * ch;
const ink = cssVar('--ink-dim', '#888');
@@ -287,8 +300,8 @@ function drawChart(entry) {
ctx.font = '10px Inter, system-ui, sans-serif';
ctx.textBaseline = 'middle';
ctx.textAlign = 'right';
- for (let i = 0; i <= 3; i++) {
- const v = lo + ((hi - lo) * i) / 3;
+ for (let i = 0; i <= ticks; i++) {
+ const v = lo + ((hi - lo) * i) / ticks;
const y = yOf(v);
ctx.strokeStyle = grid;
ctx.lineWidth = 1;
@@ -315,7 +328,7 @@ function drawChart(entry) {
if (state.view === 'daily') {
// Thin bars, 2px surface gap, rounded data end anchored to the baseline.
- const gap = n > 120 ? 1 : 2;
+ const gap = slot >= 8 ? 2 : slot >= 3 ? 1 : 0;
const barW = Math.max(1, slot - gap);
ctx.fillStyle = mark;
series.forEach((d, i) => {
diff --git a/tests/platform-analytics.test.js b/tests/platform-analytics.test.js
new file mode 100644
index 000000000..5d66c4964
--- /dev/null
+++ b/tests/platform-analytics.test.js
@@ -0,0 +1,116 @@
+// /api/platform/analytics data layer: window math, zero-filled daily series,
+// per-metric `method` strings, and the failure contract (a failing source is
+// `null` plus a named error, never a zero and never a thrown 500).
+//
+// Sources are injected, so these tests exercise the real aggregation and shaping
+// code without a database. The SQL itself is exercised against production data
+// by `curl /api/platform/analytics` (see docs/api-reference.md).
+
+import { describe, it, expect, vi } from 'vitest';
+import {
+ readPlatformAnalytics,
+ resolveWindow,
+ windowStartDay,
+ dayRange,
+ METRICS,
+} from '../api/_lib/platform-analytics.js';
+
+const NOW = Date.parse('2026-09-30T12:00:00Z');
+
+function okSources(overrides = {}) {
+ return {
+ agents: async () => ({
+ total: { agents: 100, agents_with_wallet: 40 },
+ daily: [
+ { day: '2026-09-29', agents: 3, agents_with_wallet: 1 },
+ { day: '2026-09-30', agents: 2, agents_with_wallet: 2 },
+ ],
+ }),
+ launches: async () => ({ total: { coins_launched: 5 }, daily: [{ day: '2026-09-15', coins_launched: 1 }] }),
+ forge: async () => ({ total: { models_generated: 9 }, daily: [] }),
+ llm: async () => ({ total: { llm_tokens: 1000 }, daily: [{ day: '2026-09-30', llm_tokens: 250 }] }),
+ x402: async () => ({
+ total: { x402_settlements: 7, x402_volume_usd: 0.1 + 0.2 },
+ daily: [{ day: '2026-09-30', x402_settlements: 7, x402_volume_usd: 0.1 + 0.2 }],
+ }),
+ marketplace: async () => ({
+ total: { marketplace_sales: 0, marketplace_volume_three: 0, marketplace_volume_usd: 0 },
+ daily: [],
+ }),
+ hires: async () => ({ total: { hire_volume_usd: 0 }, daily: [] }),
+ ...overrides,
+ };
+}
+
+describe('window helpers', () => {
+ it('accepts only the documented windows and defaults to 30d', () => {
+ expect(resolveWindow('90d')).toBe('90d');
+ expect(resolveWindow('ALL')).toBe('all');
+ expect(resolveWindow('7d')).toBe('30d');
+ expect(resolveWindow(undefined)).toBe('30d');
+ expect(resolveWindow(['all'])).toBe('all');
+ });
+
+ it('counts today as day one of a bounded window', () => {
+ expect(windowStartDay('30d', NOW)).toBe('2026-09-01');
+ expect(windowStartDay('90d', NOW)).toBe('2026-07-03');
+ expect(windowStartDay('all', NOW)).toBeNull();
+ expect(dayRange('2026-09-01', NOW)).toHaveLength(30);
+ });
+});
+
+describe('readPlatformAnalytics', () => {
+ it('returns every metric with a method, totals and a zero-filled daily series', async () => {
+ const body = await readPlatformAnalytics({ window: '30d', now: NOW, sources: okSources() });
+ expect(body.window).toBe('30d');
+ expect(body.window_days).toBe(30);
+ expect(body.from).toBe('2026-09-01');
+ expect(body.to).toBe('2026-09-30');
+ expect(body.errors).toEqual([]);
+ expect(Object.keys(body.metrics)).toEqual(METRICS.map((m) => m.key));
+ for (const m of Object.values(body.metrics)) expect(typeof m.method).toBe('string');
+
+ const agents = body.metrics.agents;
+ expect(agents.total).toBe(100);
+ expect(agents.window_total).toBe(5);
+ expect(agents.daily).toHaveLength(30);
+ expect(agents.daily.at(-1)).toEqual({ day: '2026-09-30', value: 2 });
+ expect(agents.daily[0]).toEqual({ day: '2026-09-01', value: 0 });
+
+ // Float drift from summing decimal amounts is tidied.
+ expect(body.metrics.x402_volume_usd.total).toBe(0.3);
+ });
+
+ it('marks creator fees as pending until per-agent earnings exist', async () => {
+ const body = await readPlatformAnalytics({ window: '30d', now: NOW, sources: okSources() });
+ expect(body.metrics.creator_fees).toMatchObject({
+ available: false,
+ pending: true,
+ total: null,
+ method: 'coming with per-agent earnings',
+ });
+ });
+
+ it('turns a failing source into null metrics plus named errors, never zero', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ const body = await readPlatformAnalytics({
+ window: '30d',
+ now: NOW,
+ sources: okSources({ x402: async () => { throw new Error('connection reset'); } }),
+ });
+ warn.mockRestore();
+ expect(body.metrics.x402_settlements).toMatchObject({ available: false, total: null, window_total: null, daily: null });
+ expect(body.metrics.x402_volume_usd.total).toBeNull();
+ expect(body.errors.map((e) => e.metric)).toEqual(['x402_settlements', 'x402_volume_usd']);
+ expect(body.errors[0]).toMatchObject({ source: 'x402', error: 'query_failed' });
+ // Every other metric is still served.
+ expect(body.metrics.agents.total).toBe(100);
+ });
+
+ it('starts the all-time window on the first day any source recorded activity', async () => {
+ const body = await readPlatformAnalytics({ window: 'all', now: NOW, sources: okSources() });
+ expect(body.from).toBe('2026-09-15');
+ expect(body.metrics.coins_launched.daily[0]).toEqual({ day: '2026-09-15', value: 1 });
+ expect(body.metrics.coins_launched.window_total).toBe(1);
+ });
+});
diff --git a/vercel.json b/vercel.json
index 8b76261f2..7f872edc5 100644
--- a/vercel.json
+++ b/vercel.json
@@ -442,6 +442,10 @@
"src": "/agent-economy-volume/",
"dest": "/agent-economy-volume.html"
},
+ {
+ "src": "/analytics/?",
+ "dest": "/analytics.html"
+ },
{
"src": "/economy",
"dest": "/economy.html"
diff --git a/vite.config.js b/vite.config.js
index 4e4059610..dcbbeb848 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -865,6 +865,7 @@ const appConfig = {
live: resolve(__dirname, 'pages/live.html'),
'agent-economy': resolve(__dirname, 'pages/agent-economy.html'),
'agent-economy-volume': resolve(__dirname, 'pages/agent-economy-volume.html'),
+ analytics: resolve(__dirname, 'pages/analytics.html'),
economy: resolve(__dirname, 'pages/economy.html'),
'overlay-control': resolve(__dirname, 'pages/overlay-control.html'),
'mocap-studio': resolve(__dirname, 'pages/mocap-studio.html'),