From 61b8090c6482094e393eb3d7e7d0c5fc1958b437 Mon Sep 17 00:00:00 2001 From: shay Date: Wed, 27 May 2026 10:28:51 -0700 Subject: [PATCH] feat(api): add endpoint for retrieving growth data for the dashboard --- .../frontend/src/components/GrowthChart.tsx | 91 +++++++++++++++---- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/generator/frontend/src/components/GrowthChart.tsx b/generator/frontend/src/components/GrowthChart.tsx index 03c3ced..4eb0d2e 100644 --- a/generator/frontend/src/components/GrowthChart.tsx +++ b/generator/frontend/src/components/GrowthChart.tsx @@ -8,7 +8,12 @@ export function GrowthChart() { const [mode, setMode] = useState('daily') useEffect(() => { fetchGrowth().then(setData).catch(() => {}) }, []) - const points = data ? (mode === 'daily' ? data.daily : data.weekly) : [] + // Trim leading empty buckets so the chart starts at the first day/week + // with traffic instead of e.g. four weeks of zeros from before the + // service went live. + const rawPoints = data ? (mode === 'daily' ? data.daily : data.weekly) : [] + const firstActive = rawPoints.findIndex(p => p.requests > 0 || p.users > 0) + const points = firstActive >= 0 ? rawPoints.slice(firstActive) : [] const labels = points.map(p => 'day' in p ? p.day : p.week) const requests = points.map(p => p.requests) const users = points.map(p => p.users) @@ -22,12 +27,14 @@ export function GrowthChart() { setMode('weekly')}>week · 12 - {points.length === 0 ? ( + {!data ? (
loading…
+ ) : points.length === 0 ? ( +
no traffic yet
) : (
- - + +
)} @@ -47,21 +54,26 @@ function Tab({ active, onClick, children }: { active: boolean; onClick: () => vo ) } -function Line({ label, values, labels, stroke }: { - label: string; values: number[]; labels: string[]; stroke: string +function Line({ label, values, labels, mode, stroke }: { + label: string; values: number[]; labels: string[]; mode: Mode; stroke: string }) { const width = 600 const height = 70 const max = Math.max(1, ...values) - const stepX = width / Math.max(1, values.length - 1) + const n = values.length + const stepX = n > 1 ? width / (n - 1) : 0 const total = values.reduce((a, b) => a + b, 0) - const latest = values[values.length - 1] ?? 0 - const pts = values.map((v, i) => { - const x = i * stepX + const latest = values[n - 1] ?? 0 + const xy = values.map((v, i) => { + const x = n > 1 ? i * stepX : width / 2 const y = height - (v / max) * (height - 4) - 2 - return `${x.toFixed(1)},${y.toFixed(1)}` - }).join(' ') - const area = `0,${height} ${pts} ${width},${height}` + return { x, y } + }) + const pts = xy.map(p => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(' ') + // Sealed area polygon: only valid when we have a real line (n >= 2). + const area = n > 1 ? `0,${height} ${pts} ${width},${height}` : '' + const ticks = pickTicks(labels, Math.min(6, n)) + return (
@@ -71,14 +83,55 @@ function Line({ label, values, labels, stroke }: { total {total.toLocaleString()}
- - - + + {area && } + {n > 1 && ( + + )} + {n === 1 && ( + + )} + {ticks.map(t => { + const x = n > 1 ? t.idx * stepX : width / 2 + return ( + + ) + })} -
- {labels[0]} - {labels[labels.length - 1]} +
+ {ticks.map(t => ( + 1 ? `${(t.idx / (n - 1)) * 100}%` : '50%' }}> + {formatTick(t.label, mode)} + + ))}
) } + +function pickTicks(labels: string[], n: number): { idx: number; label: string }[] { + if (labels.length === 0 || n <= 0) return [] + if (labels.length <= n) return labels.map((label, idx) => ({ idx, label })) + const out: { idx: number; label: string }[] = [] + for (let i = 0; i < n; i++) { + const idx = Math.round((i * (labels.length - 1)) / (n - 1)) + out.push({ idx, label: labels[idx] }) + } + return out +} + +function formatTick(label: string, mode: Mode): string { + if (mode === 'daily') { + // "2026-05-27" -> "05-27" + return label.length >= 10 ? label.slice(5) : label + } + // "2026-W21" -> "W21" + const parts = label.split('-') + return parts[1] || label +}