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:
@@ -15,6 +15,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/gitea"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/hugorepo"
|
||||
)
|
||||
@@ -265,3 +266,48 @@ func (ap *Applier) setupSite(ctx context.Context, site, key string, st *Setup) e
|
||||
st.PRNumber, st.PRURL, st.Note = pr.Number, pr.HTMLURL, note
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishPage opens a pull request that adds a page to the site, or
|
||||
// updates it if it's there: used for the open metrics page. The build
|
||||
// checks the page comes out.
|
||||
func (ap *Applier) PublishPage(ctx context.Context, pagePath, title, description, body, why string) (*gitea.Pull, string, error) {
|
||||
wc, full, branch, err := ap.workingCopy(ctx)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
canBuild, note := buildCheck(ctx, wc)
|
||||
pagePath = "/" + strings.Trim(pagePath, "/") + "/"
|
||||
work := "supermarket/page" + strings.TrimSuffix(strings.ReplaceAll(pagePath, "/", "-"), "-")
|
||||
if err := wc.NewBranch(ctx, work); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
file, err := wc.FindContent(pagePath)
|
||||
if err != nil {
|
||||
file = "content" + strings.TrimSuffix(pagePath, "/") + ".md"
|
||||
}
|
||||
fields := []hugorepo.Field{{Key: "title", Value: title}, {Key: "description", Value: description}, {Key: "date", Value: time.Now().Format("2006-01-02")}}
|
||||
if err := wc.UpsertPage(file, fields, body); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if canBuild {
|
||||
out, err := wc.Build(ctx)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("after the change, %w", err)
|
||||
}
|
||||
_, _, err = hugorepo.BuiltHead(out, pagePath)
|
||||
os.RemoveAll(out)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("the site builds, but %w", err)
|
||||
}
|
||||
note = "Checked with a build: the page is at " + pagePath + "."
|
||||
}
|
||||
subject := "Update " + pagePath + ": " + title
|
||||
if err := wc.CommitAndPush(ctx, work, subject); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
pr, err := ap.Gitea.OpenPull(ctx, full, work, branch, subject, why+"\n\n"+note+"\n\nMerging publishes it.")
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return pr, file, nil
|
||||
}
|
||||
|
||||
@@ -111,3 +111,31 @@ func TestSetupSite(t *testing.T) {
|
||||
t.Error("the setup isn't kept")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishPage(t *testing.T) {
|
||||
if hugorepo.HugoVersion(context.Background()) == "" {
|
||||
t.Skip("no hugo")
|
||||
}
|
||||
clone := hugotest.Site(t)
|
||||
n := 0
|
||||
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/o/site":
|
||||
json.NewEncoder(w).Encode(map[string]any{"full_name": "o/site", "default_branch": "main", "clone_url": clone, "permissions": map[string]any{"push": true}})
|
||||
case r.URL.Path == "/api/v1/repos/o/site/pulls":
|
||||
n++
|
||||
json.NewEncoder(w).Encode(map[string]any{"number": n, "html_url": fmt.Sprintf("https://gitea.example/pr/%d", n)})
|
||||
}
|
||||
}))
|
||||
defer g.Close()
|
||||
ap := &Applier{Gitea: gitea.New(g.URL, "tok"), Repo: "o/site", ReposDir: filepath.Join(t.TempDir(), "sites"), Author: "P <p@example.com>"}
|
||||
pr, file, err := ap.PublishPage(context.Background(), "/metrics/", "Open metrics", "Our numbers.", "MRR: $150\n", "why")
|
||||
if err != nil || pr.Number != 1 || file != "content/metrics.md" {
|
||||
t.Fatalf("publish: %+v %q %v", pr, file, err)
|
||||
}
|
||||
repo := strings.TrimPrefix(clone, "file://")
|
||||
out, _ := exec.Command("git", "-C", repo, "show", "supermarket/page-metrics:content/metrics.md").CombinedOutput()
|
||||
if !strings.Contains(string(out), `title: "Open metrics"`) || !strings.Contains(string(out), "MRR: $150") {
|
||||
t.Errorf("page: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user