Phase 4 part 2: MRR, its movements, NRR and cohorts

Each customer's MRR at each month's end, from the recurring lines of paid
and open invoices: the amount after discounts over the billing interval,
prorations over their part-period. Moves are new, expansion, contraction,
churn and reactivation (sub-dollar noise ignored); NRR monthly and over
12 months; cohorts by first paid month. A start month, suggested when
most of the business appears at once (billing moved to Stripe), makes
those customers the base. Metrics page with a chart, the month table,
cohorts, and each month's customer moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 12:17:53 -07:00
co-authored by Claude Opus 5.5
parent 2c242d8c94
commit 5e799c7541
11 changed files with 860 additions and 2 deletions
+78
View File
@@ -26,12 +26,90 @@ func (s *Server) renderMetrics(w http.ResponseWriter, r *http.Request, errMsg st
s.serverError(w, err)
return
}
model, err := metrics.Build(s.DB, time.Now())
if err != nil {
s.serverError(w, err)
return
}
s.render(w, r, "metrics", page{Title: "Metrics", Nav: "metrics/overview", Error: errMsg, Data: map[string]any{
"Stripe": s.stripe() != nil,
"Last": metrics.LastSync(s.DB),
"Counts": counts,
"Products": products,
"Margins": metrics.LoadMargins(s.DB),
"Model": model,
"Now": model.Last(),
"Recent": model.Recent(13),
"Bars": bars(model, 24),
"Cohorts": model.Cohorts(12, 12),
"Span": make([]int, 13),
}})
}
// bar is one month on the MRR chart.
type bar struct {
Month string
MRR float64
X, W, H float64 // in the SVG's units
Y float64
Label bool
}
// bars lays out the last n months of MRR as an SVG bar chart, 600 by 160.
func bars(m *metrics.Model, n int) []bar {
ms := m.Months
if len(ms) > n {
ms = ms[len(ms)-n:]
}
top := 0.0
for _, mo := range ms {
top = max(top, mo.MRR)
}
if top == 0 || len(ms) == 0 {
return nil
}
w := 600.0 / float64(len(ms))
out := make([]bar, len(ms))
for i, mo := range ms {
h := 140 * mo.MRR / top
out[i] = bar{Month: mo.Month, MRR: mo.MRR, X: float64(i)*w + 1, W: w - 2, H: h, Y: 150 - h, Label: i%3 == 0 || i == len(ms)-1}
}
return out
}
// metricsStart sets the month the metrics count from.
func (s *Server) metricsStart(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := metrics.SetStartMonth(s.DB, strings.TrimSpace(r.PostFormValue("month"))); err != nil {
s.renderMetrics(w, r, err.Error())
return
}
redirect(w, r, "/metrics", "saved")
}
// metricsMonth shows one month's moves, customer by customer.
func (s *Server) metricsMonth(w http.ResponseWriter, r *http.Request) {
model, err := metrics.Build(s.DB, time.Now())
if err != nil {
s.serverError(w, err)
return
}
month := r.PathValue("month")
mo := model.Find(month)
if mo == nil {
http.NotFound(w, r)
return
}
names, err := metrics.CustomerNames(s.DB)
if err != nil {
s.serverError(w, err)
return
}
s.render(w, r, "metrics-month", page{Title: "MRR in " + month, Nav: "metrics/overview", Data: map[string]any{
"M": mo, "Changes": model.Changes(month, names),
}})
}