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:
@@ -32,6 +32,11 @@ func (s *Server) renderMetrics(w http.ResponseWriter, r *http.Request, errMsg st
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
clv, err := metrics.BuildCLV(s.DB, model, 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),
|
||||
@@ -39,6 +44,7 @@ func (s *Server) renderMetrics(w http.ResponseWriter, r *http.Request, errMsg st
|
||||
"Products": products,
|
||||
"Margins": metrics.LoadMargins(s.DB),
|
||||
"Model": model,
|
||||
"CLV": clv,
|
||||
"Now": model.Last(),
|
||||
"Recent": model.Recent(13),
|
||||
"Bars": bars(model, 24),
|
||||
@@ -425,3 +431,125 @@ func (s *Server) metricsCLV(w http.ResponseWriter, r *http.Request) {
|
||||
"Currency": model.Currency,
|
||||
}})
|
||||
}
|
||||
|
||||
// --- CSV export and the open metrics page ---
|
||||
|
||||
func (s *Server) metricsExport(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimSuffix(r.PathValue("name"), ".csv")
|
||||
known := false
|
||||
for _, e := range metrics.Exports {
|
||||
known = known || e.Name == name
|
||||
}
|
||||
if !known {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="supermarket-`+name+`-`+time.Now().Format("2006-01-02")+`.csv"`)
|
||||
if err := metrics.Export(w, s.DB, name, time.Now()); err != nil {
|
||||
s.Log.Error("metrics: export", "table", name, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// publishOptions are the open metrics page's settings.
|
||||
type publishOptions struct {
|
||||
Site int `json:"site"`
|
||||
Path string `json:"path"`
|
||||
Title string `json:"title"`
|
||||
Parts map[string]bool `json:"parts"`
|
||||
PRURL string `json:"pr_url"`
|
||||
PRAt time.Time `json:"pr_at"`
|
||||
}
|
||||
|
||||
const publishKey = "metrics:publish"
|
||||
|
||||
func (s *Server) publishOptions() publishOptions {
|
||||
o := publishOptions{Path: "/metrics/", Title: "Open metrics", Parts: map[string]bool{"mrr": true, "customers": true, "growth": true, "nrr": true}}
|
||||
_, _ = s.DB.GetJSON(publishKey, &o)
|
||||
return o
|
||||
}
|
||||
|
||||
func (s *Server) metricsPublishPage(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderPublish(w, r, s.publishOptions(), "")
|
||||
}
|
||||
|
||||
func (s *Server) renderPublish(w http.ResponseWriter, r *http.Request, o publishOptions, errMsg string) {
|
||||
now := time.Now()
|
||||
model, err := metrics.Build(s.DB, now)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
clv, err := metrics.BuildCLV(s.DB, model, now)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.render(w, r, "metrics-publish", page{Title: "Publish metrics", Nav: "metrics/publish", Error: errMsg, Data: map[string]any{
|
||||
"O": o, "Parts": metrics.PublicParts, "Sites": s.seoSites(), "Gitea": s.gitea() != nil,
|
||||
"Preview": metrics.PublicPage(model, clv, o.Parts, now),
|
||||
}})
|
||||
}
|
||||
|
||||
// metricsPublish saves the page's settings, and with "pr" opens the pull
|
||||
// request.
|
||||
func (s *Server) metricsPublish(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
o := s.publishOptions()
|
||||
o.Site, _ = strconv.Atoi(r.PostFormValue("site"))
|
||||
o.Path = strings.TrimSpace(r.PostFormValue("path"))
|
||||
o.Title = strings.TrimSpace(r.PostFormValue("title"))
|
||||
o.Parts = map[string]bool{}
|
||||
for _, p := range r.PostForm["part"] {
|
||||
o.Parts[p] = true
|
||||
}
|
||||
if o.Path == "" || o.Title == "" {
|
||||
s.renderPublish(w, r, o, "Give the page an address and a title.")
|
||||
return
|
||||
}
|
||||
if r.PostFormValue("do") == "pr" {
|
||||
var site *seoSite
|
||||
for _, st := range s.seoSites() {
|
||||
if st.Index == o.Site {
|
||||
site = &st
|
||||
}
|
||||
}
|
||||
if site == nil {
|
||||
s.renderPublish(w, r, o, "Choose a website.")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
model, err := metrics.Build(s.DB, now)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
clv, _ := metrics.BuildCLV(s.DB, model, now)
|
||||
body := metrics.PublicPage(model, clv, o.Parts, now)
|
||||
if body == "" {
|
||||
s.renderPublish(w, r, o, "There's no complete month of billing to publish yet.")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
||||
defer cancel()
|
||||
pr, _, err := s.applier(*site).PublishPage(ctx, o.Path, o.Title, "Our revenue numbers, straight from our billing.", body,
|
||||
"Proposed by Supermarket: the open metrics page, with totals only (nothing about any one customer).")
|
||||
if err != nil {
|
||||
s.renderPublish(w, r, o, "Couldn't open the pull request: "+err.Error())
|
||||
return
|
||||
}
|
||||
o.PRURL, o.PRAt = pr.HTMLURL, now
|
||||
}
|
||||
if err := s.DB.PutJSON(publishKey, o); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
msg := "saved"
|
||||
if r.PostFormValue("do") == "pr" {
|
||||
msg = "setup-opened"
|
||||
}
|
||||
redirect(w, r, "/metrics/publish", msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user