Phase 4 part 6: the dashboard, CSV exports and an open metrics page

The MRR page adds churn, CLV, CAC and CLV:CAC. Eight tables download as
CSV (MRR, NRR and revenue cohorts, CAC by month and channel, spend,
products, customers). Metrics → Publish builds an open metrics page from
chosen totals only (never a customer) and opens a pull request that adds
or updates it on the site, checked with a Hugo build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 13:28:50 -07:00
co-authored by Claude Opus 5.5
parent 23f765159f
commit 152c14b580
18 changed files with 637 additions and 5 deletions
+61
View File
@@ -0,0 +1,61 @@
package metrics
import (
"bytes"
"encoding/csv"
"strings"
"testing"
"time"
)
func TestExportsAndPublicPage(t *testing.T) {
d := openDB(t)
now := time.Date(2027, 2, 15, 0, 0, 0, 0, time.UTC)
addLines(t, d, monthly("cus_a", 10000, "2026-01", "2027-03")...)
addLines(t, d, monthly("cus_b", 5000, "2026-06", "2027-03")...)
d.Exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', 'cus_a', 'cus_a', '{"name":"Secret Co","email":"ceo@secret.example"}')`)
d.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', 'pa', 'cus_a', 'paid', 'usd', '{"amount_paid":10000}')`)
AddSpend(d, Spend{Month: "2026-06", Channel: "linkedin", AmountCents: 50000}, now)
for _, e := range Exports {
var buf bytes.Buffer
if err := Export(&buf, d, e.Name, now); err != nil {
t.Errorf("%s: %v", e.Name, err)
continue
}
rows, err := csv.NewReader(&buf).ReadAll()
if err != nil || len(rows) < 2 {
t.Errorf("%s: %d rows %v", e.Name, len(rows), err)
}
}
var buf bytes.Buffer
Export(&buf, d, "mrr", now)
if !strings.Contains(buf.String(), "2026-06,false,150.00,2,75.00,50.00,1,") {
t.Errorf("mrr csv:\n%s", buf.String())
}
buf.Reset()
Export(&buf, d, "customers", now)
if !strings.Contains(buf.String(), "cus_a,Secret Co,ceo@secret.example,100.00,2026-01") {
t.Errorf("customers csv:\n%s", buf.String())
}
if err := Export(&buf, d, "nope", now); err == nil {
t.Error("an unknown table exported")
}
m, _ := Build(d, now)
clv, _ := BuildCLV(d, m, now)
page := PublicPage(m, clv, map[string]bool{"mrr": true, "customers": true, "growth": true, "nrr": true, "churn": true, "table": true}, now)
for _, want := range []string{"end of January 2027", "**Monthly recurring revenue:** $150 ($1,800 a year)", "**Paying customers:** 2",
"**Growth over 12 months:** +50%", "| January 2027 | $150 | 2 |", "| June 2026 | $150 | 2 | +$50 |"} {
if !strings.Contains(page, want) {
t.Errorf("page lacks %q:\n%s", want, page)
}
}
for _, never := range []string{"Secret", "secret.example", "cus_"} {
if strings.Contains(page, never) {
t.Errorf("the public page names a customer: %q", never)
}
}
if p := PublicPage(m, clv, map[string]bool{"mrr": true}, now); strings.Contains(p, "|") || strings.Contains(p, "customers") {
t.Errorf("unchosen parts shown:\n%s", p)
}
}