With a "Count from" month set, that month's cohort is everyone already paying then, not only those new that month. Both cohort tables now say "existing customers" under it, the help text explains, and both cohort CSVs add an existing_customers column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
275 lines
8.3 KiB
Go
275 lines
8.3 KiB
Go
package metrics
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"io"
|
|
"slices"
|
|
"strconv"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// Exports are the tables that can be downloaded as CSV, by name.
|
|
var Exports = []struct{ Name, Title string }{
|
|
{"mrr", "MRR month by month"},
|
|
{"nrr-cohorts", "NRR by cohort"},
|
|
{"cohort-revenue", "Revenue by cohort"},
|
|
{"cac", "CAC by month"},
|
|
{"cac-channels", "CAC by channel (12 months)"},
|
|
{"spend", "Spend log"},
|
|
{"products", "Products and margins"},
|
|
{"customers", "Customers: MRR and CRM match"},
|
|
}
|
|
|
|
func dollars(cents float64) string { return strconv.FormatFloat(cents/100, 'f', 2, 64) }
|
|
|
|
func pct(f float64) string {
|
|
if f < 0 {
|
|
return ""
|
|
}
|
|
return strconv.FormatFloat(f, 'f', 1, 64)
|
|
}
|
|
|
|
// Export writes one table as CSV, amounts in dollars.
|
|
func Export(w io.Writer, d *db.DB, name string, now time.Time) error {
|
|
model, err := Build(d, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cw := csv.NewWriter(w)
|
|
defer cw.Flush()
|
|
write := func(rows ...[]string) error {
|
|
for _, r := range rows {
|
|
if err := cw.Write(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
switch name {
|
|
case "mrr":
|
|
write([]string{"month", "partial", "mrr", "customers", "arpa", "new", "new_customers", "expansion", "contraction", "churn",
|
|
"churned_customers", "reactivation", "net_new", "nrr_pct", "nrr_12m_pct"})
|
|
for _, m := range model.Months {
|
|
if err := write([]string{m.Month, strconv.FormatBool(m.Partial), dollars(m.MRR), strconv.Itoa(m.Customers), dollars(m.ARPA()),
|
|
dollars(m.New), strconv.Itoa(m.NewCount), dollars(m.Expansion), dollars(m.Contraction), dollars(m.Churn),
|
|
strconv.Itoa(m.ChurnCount), dollars(m.Reactivation), dollars(m.NetNew()), pct(m.NRR), pct(m.NRR12)}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "nrr-cohorts":
|
|
head := []string{"first_paid", "customers", "starting_mrr"}
|
|
span := max(len(model.Months)-1, 0) // every cohort, to the latest month
|
|
for k := 0; k <= span; k++ {
|
|
head = append(head, fmt.Sprintf("month_%d_pct", k))
|
|
}
|
|
for k := 0; k <= span; k++ {
|
|
head = append(head, fmt.Sprintf("month_%d_mrr", k))
|
|
}
|
|
head = append(head, "existing_customers")
|
|
write(head)
|
|
for _, c := range model.Cohorts(len(model.Months), span) {
|
|
row := []string{c.Month, strconv.Itoa(c.Customers), dollars(c.Start)}
|
|
for _, r := range c.Retained {
|
|
row = append(row, pct(r))
|
|
}
|
|
for _, v := range c.MRR {
|
|
if v < 0 {
|
|
row = append(row, "")
|
|
} else {
|
|
row = append(row, dollars(v))
|
|
}
|
|
}
|
|
row = append(row, strconv.FormatBool(c.Existing))
|
|
if err := write(row); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "cohort-revenue":
|
|
span := max(len(model.Months)-1, 0) // every cohort, to the latest month
|
|
cs, err := RevenueByCohort(d, model, len(model.Months), span, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
head := []string{"first_paid", "customers"}
|
|
for k := 0; k <= span; k++ {
|
|
head = append(head, fmt.Sprintf("month_%d_revenue_per_customer", k))
|
|
}
|
|
head = append(head, "existing_customers")
|
|
write(head)
|
|
for _, c := range cs {
|
|
row := []string{c.Month, strconv.Itoa(c.Customers)}
|
|
for _, v := range c.PerCustomer {
|
|
if v.Known {
|
|
row = append(row, dollars(v.V))
|
|
} else {
|
|
row = append(row, "")
|
|
}
|
|
}
|
|
row = append(row, strconv.FormatBool(c.Existing))
|
|
if err := write(row); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "cac", "cac-channels":
|
|
names, err := CustomerNames(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c, err := BuildCAC(d, model, names)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
amount := func(f float64) string {
|
|
if f < 0 {
|
|
return ""
|
|
}
|
|
return dollars(f)
|
|
}
|
|
if name == "cac" {
|
|
write([]string{"month", "spend", "new_customers", "cac", "cac_3_months"})
|
|
months := slices.Clone(c.Months)
|
|
slices.Reverse(months)
|
|
for _, m := range months {
|
|
if err := write([]string{m.Month, dollars(float64(m.Spend)), strconv.Itoa(m.New), amount(m.CAC), amount(m.Trailing3)}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
write([]string{"channel", "spend", "new_customers", "cac"})
|
|
for _, ch := range c.Channels {
|
|
if err := write([]string{ChannelName(ch.Channel), dollars(float64(ch.Spend)), strconv.Itoa(ch.New), amount(ch.CAC)}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "spend":
|
|
log, err := SpendLog(d, 100000)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
write([]string{"month", "channel", "amount", "note", "source"})
|
|
for _, s := range log {
|
|
if err := write([]string{s.Month, s.Channel, dollars(float64(s.AmountCents)), s.Note, s.Source}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "products":
|
|
ps, err := Products(d, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
write([]string{"product_id", "name", "billed_12_months", "invoices", "margin_pct", "own_margin"})
|
|
for _, p := range ps {
|
|
if err := write([]string{p.ID, p.Name, dollars(float64(p.Revenue)), strconv.Itoa(p.Invoices), pct(p.Margin), strconv.FormatBool(p.Own)}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "customers":
|
|
ms, err := Matches(d, model)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
write([]string{"customer_id", "name", "email", "mrr_now", "first_paid", "crm_account_id", "crm_account", "matched_by"})
|
|
for _, m := range ms {
|
|
if err := write([]string{m.Customer.ID, m.Customer.Name, m.Customer.Email, dollars(m.MRR), firstPaid(model, m.Customer.ID),
|
|
m.Account.ID, m.Account.Name, m.How}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
default:
|
|
return fmt.Errorf("no table %q", name)
|
|
}
|
|
return cw.Error()
|
|
}
|
|
|
|
// PublicParts are what an open metrics page can show; nothing about any
|
|
// one customer is ever published.
|
|
var PublicParts = []struct{ ID, Label string }{
|
|
{"mrr", "MRR and ARR"},
|
|
{"customers", "Paying customers and the average per customer"},
|
|
{"growth", "Growth over 12 months"},
|
|
{"nrr", "Net revenue retention (12 months)"},
|
|
{"churn", "Monthly customer churn"},
|
|
{"table", "A month-by-month table (MRR, customers, net change), last 12 months"},
|
|
}
|
|
|
|
// PublicPage is the Markdown body of an open metrics page: totals only.
|
|
func PublicPage(m *Model, clv *CLV, parts map[string]bool, now time.Time) string {
|
|
months := m.Months
|
|
if len(months) > 0 && months[len(months)-1].Partial {
|
|
months = months[:len(months)-1]
|
|
}
|
|
if len(months) == 0 {
|
|
return ""
|
|
}
|
|
last := months[len(months)-1]
|
|
money := func(cents float64) string {
|
|
s := strconv.FormatFloat(cents/100, 'f', 0, 64)
|
|
for i := len(s) - 3; i > 0; i -= 3 {
|
|
s = s[:i] + "," + s[i:]
|
|
}
|
|
return "$" + s
|
|
}
|
|
var b []string
|
|
b = append(b, fmt.Sprintf("Our numbers at the end of %s, straight from our billing.", monthName(last.Month)), "")
|
|
if parts["mrr"] {
|
|
b = append(b, fmt.Sprintf("- **Monthly recurring revenue:** %s (%s a year)", money(last.MRR), money(12*last.MRR)))
|
|
}
|
|
if parts["customers"] {
|
|
b = append(b, fmt.Sprintf("- **Paying customers:** %d, at %s a month on average", last.Customers, money(last.ARPA())))
|
|
}
|
|
if parts["growth"] && len(months) > 12 {
|
|
if base := months[len(months)-13].MRR; base > 0 {
|
|
b = append(b, fmt.Sprintf("- **Growth over 12 months:** %+.0f%%", 100*(last.MRR-base)/base))
|
|
}
|
|
}
|
|
if parts["nrr"] && last.NRR12 >= 0 {
|
|
b = append(b, fmt.Sprintf("- **Net revenue retention, 12 months:** %.0f%% (what customers of a year ago pay now)", last.NRR12))
|
|
}
|
|
if parts["churn"] && clv != nil && clv.ChurnMonths > 0 {
|
|
b = append(b, fmt.Sprintf("- **Monthly customer churn:** %.1f%%, averaged over %d months", clv.Churn, clv.ChurnMonths))
|
|
}
|
|
if parts["table"] {
|
|
b = append(b, "", "| Month | MRR | Customers | Net change |", "|---|---:|---:|---:|")
|
|
from := max(0, len(months)-12)
|
|
for i := len(months) - 1; i >= from; i-- {
|
|
mo := months[i]
|
|
sign := ""
|
|
if mo.NetNew() > 0 {
|
|
sign = "+"
|
|
}
|
|
b = append(b, fmt.Sprintf("| %s | %s | %d | %s%s |", monthName(mo.Month), money(mo.MRR), mo.Customers, sign, money(mo.NetNew())))
|
|
}
|
|
}
|
|
b = append(b, "", fmt.Sprintf("*Updated %s. MRR counts recurring billing after discounts; trials, one-off charges and unpaid invoices don't count.*", now.Format("2 January 2006")))
|
|
return joinLines(b)
|
|
}
|
|
|
|
func monthName(ym string) string {
|
|
t, err := time.Parse("2006-01", ym)
|
|
if err != nil {
|
|
return ym
|
|
}
|
|
return t.Format("January 2006")
|
|
}
|
|
|
|
func joinLines(lines []string) string {
|
|
out := ""
|
|
for _, l := range lines {
|
|
out += l + "\n"
|
|
}
|
|
return out
|
|
}
|
|
|
|
// firstPaid is the month a customer first paid: their cohort's, or, for one
|
|
// who was already a customer, the month first seen paying.
|
|
func firstPaid(m *Model, customer string) string {
|
|
if f := m.First[customer]; f != "" {
|
|
return f
|
|
}
|
|
return m.Existing[customer]
|
|
}
|