console: paying accounts tile on the dashboard
With billing on, the Dashboard's band gains a sixth tile, Paying accounts: the paying count and ARR in euros a year, both through opMoney (billingDashStat), the ECB rate day after a converted ARR, the test-mode word in test mode, and the whole tile one link to /fleet/billing with the Billing word after the note. A failed revenue read is logged and the tile left out; billing off renders exactly as before. opStat gains Href, Link and Flag; op_stat renders an <a rst-stat> when Href is set. ops.css styles the linked cell, narrows the band's bases only when a linked cell is present so six fit on one row, and keeps the divider on a wrapped (co-statna) cell that is not the band's first.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+230
−5
internal/console/opdashboard.go+48 −0internal/console/opdashboard_test.go+152 −0internal/console/opparts.go+5 −1internal/console/static/ops.css+19 −0internal/console/templates/opparts.html+4 −2internal/console/templates/ops_dashboard.html+2 −2
diff --git a/internal/console/opdashboard.go b/internal/console/opdashboard.go| index f6151f6d2..9e91daec8 100644 |
| --- a/internal/console/opdashboard.go |
| +++ b/internal/console/opdashboard.go |
| @@ -3,8 +3,10 @@ package console |
| import ( |
| "fmt" |
| "log" |
| + "maps" |
| "math" |
| "net/http" |
| + "slices" |
| "sort" |
| "strings" |
| "time" |
| @@ -214,6 +216,37 @@ func dashStats(c OperatorCounts) []opStat { |
| return []opStat{people, newPeople, newAccounts, apps, deploys} |
| } |
| +// billingDashStat is the Dashboard's billing tile (billing dashboard plan 2, |
| +// Task 7): Paying accounts, the paying count, and under it ARR in euros a |
| +// year, with the rate day when any of it converted (the Billing overview |
| +// carries the breakdown). Both figures go through opMoney: the count |
| +// declares the facts and payments, ARR those, the amounts and its |
| +// currencies' rates. A refused count reads Not available yet; a refused |
| +// ARR puts its reason in place of the note. The whole tile links to |
| +// Billing, and says so in test mode. |
| +func billingDashStat(rev billingRevenue, in moneyInputs) (opStat, []opMoneyFigure) { |
| + var figs []opMoneyFigure |
| + st := opStat{Label: billCopyDashTile, Href: "/fleet/billing", Link: billCopyDashTileLink} |
| + if rev.TestMode { |
| + st.Flag = billCopyDashTileTestMode |
| + } |
| + paying := opMoney(&figs, "dash.paying", in, moneyUse{Inputs: inFacts | inPayments}, nil, |
| + func() string { return opNumber(rev.Paying) }) |
| + st.Value = paying.Text |
| + cur := slices.Sorted(maps.Keys(rev.ByCurrency)) |
| + arrUse := opBillingUse(inFacts|inPayments|inAmounts, cur, "") |
| + arr := opMoney(&figs, "dash.arr", in, arrUse, nil, func() string { return eurText(rev.EUR) }) |
| + switch { |
| + case !arr.Shown: |
| + st.Note = arr.Reason |
| + case ratesUse(cur...) != 0: |
| + st.Note = fmt.Sprintf(billCopyDashTileNote, arr.Text) + ", " + fmt.Sprintf(billCopyRateNote, opBillingRateDay(in.Rates.Day)) |
| + default: |
| + st.Note = fmt.Sprintf(billCopyDashTileNote, arr.Text) |
| + } |
| + return st, figs |
| +} |
| + |
| // dashBox is one row of the dashboard's Boxes table. Meter is nil for a |
| // silent box or one with no metrics block; LastSeen is zero for a box no |
| // report was read for. |
| @@ -329,6 +362,11 @@ type dashView struct { |
| Jobs []opJobRow |
| } |
| +// NotAvailable is the billing tile's word for a figure it cannot stand |
| +// behind (BDP-7): the template mutes a tile showing it, as the Billing |
| +// overview does. |
| +func (d *dashView) NotAvailable() string { return billCopyNotAvailable } |
| + |
| // ActorsHead is Latest activity's head row. |
| func (d *dashView) ActorsHead() []string { return activityDashHead } |
| @@ -376,6 +414,16 @@ func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) { |
| } else { |
| d.Stats = dashStats(c) |
| } |
| + // The billing tile, only on a console with billing on; a failed read |
| + // leaves it out. |
| + if s.billing != nil { |
| + if rev, _, err := s.billingRevenueNow(now); err != nil { |
| + log.Printf("console: dashboard billing: %v", err) |
| + } else { |
| + st, _ := billingDashStat(rev, rev.Inputs) |
| + d.Stats = append(d.Stats, st) |
| + } |
| + } |
| if days, err := s.store.NewPeoplePerDay(now, 30); err != nil { |
| log.Printf("console: dashboard new people per day: %v", err) |
| } else { |
diff --git a/internal/console/opdashboard_test.go b/internal/console/opdashboard_test.go| index 7700cd9b4..a8c1ee766 100644 |
| --- a/internal/console/opdashboard_test.go |
| +++ b/internal/console/opdashboard_test.go |
| @@ -968,3 +968,155 @@ func seedOperatorScale(t *testing.T, fx *consoleFixture) { |
| t.Fatal(err) |
| } |
| } |
| + |
| +// dashStatCell is one cell of the headline band: a <div rst-stat>, or an |
| +// <a rst-stat href> when the whole cell is a link (its Href captured). |
| +var dashStatCell = regexp.MustCompile(`<(?:div|a) rst-stat(?:="lead")?(?: href="([^"]*)")?>`) |
| + |
| +// dashBand is the headline band's cells' hrefs, in order ("" for a cell |
| +// that is not a link), and the band itself. |
| +func dashBand(t *testing.T, body string) ([]string, string) { |
| + t.Helper() |
| + band := dashCut(t, body, "<div rst-stats", "<div rst-box-head") |
| + var hrefs []string |
| + for _, m := range dashStatCell.FindAllStringSubmatch(band, -1) { |
| + hrefs = append(hrefs, m[1]) |
| + } |
| + return hrefs, band |
| +} |
| + |
| +// TestDashboardTileOnlyWithBilling: with billing off the band is the five |
| +// stats it always was and nothing links to Billing; with billing on a |
| +// sixth, Paying accounts, is one link to /fleet/billing carrying the count, |
| +// the ARR a year, the link word and, in test mode only, the test-mode word. |
| +func TestDashboardTileOnlyWithBilling(t *testing.T) { |
| + t.Run("off", func(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + body := dashGet(t, fx, signInOperator(t, fx)) |
| + hrefs, _ := dashBand(t, body) |
| + if want := []string{"", "", "", "", ""}; !slices.Equal(hrefs, want) { |
| + t.Errorf("band cells = %q, want five plain stats", hrefs) |
| + } |
| + if strings.Contains(body, `"/fleet/billing`) { |
| + t.Error("billing off: the dashboard links to /fleet/billing") |
| + } |
| + if strings.Contains(body, billCopyDashTile) { |
| + t.Errorf("billing off: the dashboard says %q", billCopyDashTile) |
| + } |
| + }) |
| + fx, _ := overviewFixture(t) |
| + c := signInOperator(t, fx) |
| + for _, live := range []bool{false, true} { |
| + fx.srv.billing.cfg.LiveMode = live |
| + hrefs, band := dashBand(t, dashGet(t, fx, c)) |
| + if want := []string{"", "", "", "", "", "/fleet/billing"}; !slices.Equal(hrefs, want) { |
| + t.Errorf("live %v: band cells = %q, want the sixth linking to Billing", live, hrefs) |
| + continue |
| + } |
| + tile := html.UnescapeString(band[strings.Index(band, `<a rst-stat href="/fleet/billing">`):]) |
| + tile = tile[:strings.Index(tile, "</a>")] |
| + for _, want := range []string{ |
| + "<span rst-stat-label>" + billCopyDashTile + "</span>", |
| + "<span rst-stat-num>1</span>", |
| + "<span rst-stat-note>" + fmt.Sprintf(billCopyDashTileNote, "€24.00") + "</span>", |
| + ">" + billCopyDashTileLink + "</span>", |
| + } { |
| + if !strings.Contains(tile, want) { |
| + t.Errorf("live %v: tile lacks %s:\n%s", live, want, tile) |
| + } |
| + } |
| + if got := strings.Contains(tile, billCopyDashTileTestMode); got == live { |
| + t.Errorf("live %v: tile shows the test-mode word = %v:\n%s", live, got, tile) |
| + } |
| + if strings.Contains(tile, "ECB") { |
| + t.Errorf("live %v: an all-eur ARR carries a rate note:\n%s", live, tile) |
| + } |
| + } |
| +} |
| + |
| +// dashStatFacts is the ops account and two paying accounts, one in eur |
| +// (2400 a year) and one in usd (2800 a year, 2500 at 1.12). |
| +func dashStatFacts() []billingFact { |
| + return []billingFact{{AccountID: 1, Sqid: "ops", Name: "ops"}, payingFact(2, "eur", 2400), payingFact(3, "usd", 2800)} |
| +} |
| + |
| +// dashStatRev is billingRevenueNow's totals over facts at in's rates. |
| +func dashStatRev(facts []billingFact, in moneyInputs, test bool) billingRevenue { |
| + return billingRevenue{arrTotals: arrOf(facts, 1, in.Rates), Accounts: len(facts), TestMode: test, Inputs: in} |
| +} |
| + |
| +func TestDashStatGateInvariant(t *testing.T) { |
| + good := goodMoneyInputs(overviewNow) |
| + _, figs := billingDashStat(dashStatRev(dashStatFacts(), good, false), good) |
| + var names []string |
| + for _, f := range figs { |
| + names = append(names, f.Name) |
| + } |
| + if want := []string{"dash.paying", "dash.arr"}; !slices.Equal(names, want) { |
| + t.Errorf("figures = %q, want %q", names, want) |
| + } |
| + checkGateInvariant(t, good, func(in moneyInputs) []moneyFigure { |
| + _, figs := billingDashStat(dashStatRev(dashStatFacts(), in, false), in) |
| + var out []moneyFigure |
| + for _, f := range figs { |
| + out = append(out, f.moneyFigure) |
| + } |
| + return out |
| + }) |
| +} |
| + |
| +// TestDashStatNotes: the tile's words in each case: an all-eur ARR with no |
| +// rate note, a converted one with the rate day, test mode, and a refused |
| +// gate (Not available yet with the reason, never a partial number). |
| +func TestDashStatNotes(t *testing.T) { |
| + good := goodMoneyInputs(overviewNow) |
| + base := opStat{Label: billCopyDashTile, Href: "/fleet/billing", Link: billCopyDashTileLink} |
| + eurOnly := []billingFact{{AccountID: 1}, payingFact(2, "eur", 2400)} |
| + stale := good |
| + stale.Reconciled = time.Time{} |
| + for _, tc := range []struct { |
| + name string |
| + facts []billingFact |
| + in moneyInputs |
| + test bool |
| + want opStat |
| + }{ |
| + {"eur", eurOnly, good, false, opStat{Value: "1", Note: fmt.Sprintf(billCopyDashTileNote, "€24.00")}}, |
| + {"converted", dashStatFacts(), good, false, opStat{Value: "2", |
| + Note: fmt.Sprintf(billCopyDashTileNote, "€49.00") + ", " + fmt.Sprintf(billCopyRateNote, "6 Nov 2026")}}, |
| + {"test mode", eurOnly, good, true, opStat{Value: "1", Note: fmt.Sprintf(billCopyDashTileNote, "€24.00"), Flag: billCopyDashTileTestMode}}, |
| + {"refused", dashStatFacts(), stale, false, opStat{Value: billCopyNotAvailable, Note: billCopyWhyPayments}}, |
| + } { |
| + want := base |
| + want.Value, want.Note, want.Flag = tc.want.Value, tc.want.Note, tc.want.Flag |
| + got, _ := billingDashStat(dashStatRev(tc.facts, tc.in, tc.test), tc.in) |
| + if got != want { |
| + t.Errorf("%s: stat = %+v\nwant %+v", tc.name, got, want) |
| + } |
| + } |
| +} |
| + |
| +// TestOpStatLinks: an opStat with Href renders as one link holding the |
| +// whole cell, its flag and link word after the note; without, the markup |
| +// is what it always was. |
| +func TestOpStatLinks(t *testing.T) { |
| + render := func(s opStat) string { |
| + var out strings.Builder |
| + if err := templates.ExecuteTemplate(&out, "op_stat", s); err != nil { |
| + t.Fatal(err) |
| + } |
| + return out.String() |
| + } |
| + plain := opStat{Label: "People", Value: "9", Note: "4 active in the last 30 days", Lead: true} |
| + if got, want := render(plain), `<div rst-stat="lead"><span rst-stat-label>People</span><span rst-stat-num>9</span><span rst-stat-note>4 active in the last 30 days</span></div>`; got != want { |
| + t.Errorf("plain stat:\n got %s\nwant %s", got, want) |
| + } |
| + linked := opStat{Label: "Paying accounts", Value: "1", Note: "€24.00 a year", Href: "/fleet/billing", Link: "Billing", Flag: "Stripe test mode"} |
| + got := render(linked) |
| + if want := `<a rst-stat href="/fleet/billing"><span rst-stat-label>Paying accounts</span><span rst-stat-num>1</span><span rst-stat-note>€24.00 a year</span><span rst-status rst-tone="neutral" co-statflag>Stripe test mode</span><span co-statlink>Billing</span></a>`; got != want { |
| + t.Errorf("linked stat:\n got %s\nwant %s", got, want) |
| + } |
| + if strings.Count(got, "<a ") != 1 || strings.Count(got, "href=") != 1 { |
| + t.Errorf("linked stat is not exactly one link: %s", got) |
| + } |
| +} |
diff --git a/internal/console/opparts.go b/internal/console/opparts.go| index dce16c4ed..3324e5f65 100644 |
| --- a/internal/console/opparts.go |
| +++ b/internal/console/opparts.go |
| @@ -19,10 +19,14 @@ type opPill struct{ Label, Tone string } |
| // opStat is op_stat's data: one reading in an rst-stats band. Value is |
| // preformatted; Delta carries its own sign; Tone colours the delta and is |
| -// the caller's call, never derived from the sign. |
| +// the caller's call, never derived from the sign. Href, when set, makes |
| +// the whole cell one link there, with Link its word after the note; Flag |
| +// is a word shown as a status after the note (the Dashboard's billing |
| +// tile in test mode). |
| type opStat struct { |
| Label, Value, Delta, Tone, Note string |
| Lead bool |
| + Href, Link, Flag string |
| } |
| // opEmpty is op_empty's data: an optional <h2> and one sentence. The |
diff --git a/internal/console/static/ops.css b/internal/console/static/ops.css| index 8eed4e183..0c317ccca 100644 |
| --- a/internal/console/static/ops.css |
| +++ b/internal/console/static/ops.css |
| @@ -264,6 +264,9 @@ |
| [co-total] { font-weight: 650; } |
| [co-statna] { display: contents; } |
| [co-statna] [rst-stat-num] { color: var(--rst-text-muted); font-size: var(--rst-fs-lg); } |
| +/* The wrapper makes its stat a first child; only the band's first cell |
| + goes without the dividing line. */ |
| +[rst-stats] > [co-statna]:not(:first-child) > [rst-stat] { border-inline-start: 1px solid var(--rst-line); } |
| [co-cols^="bill"] > [rst-lrow="head"] > * { min-inline-size: 0; white-space: normal; } |
| /* A billing row that opens a page: the whole row is the link. Its first |
| @@ -272,6 +275,22 @@ |
| [co-rowlink] > a.rst-nm::after { content: ""; inset: 0; position: absolute; } |
| [co-rowlink]:hover { background-color: var(--rst-accent-soft); } |
| +/* A stat cell that is a link (the Dashboard's billing tile): the whole |
| + cell, in the text colour, lit on hover like a billing row; its link |
| + word after the note in the accent, its test-mode flag sized to its |
| + text rather than stretched across the cell. */ |
| +a[rst-stat] { color: inherit; text-decoration: none; } |
| +a[rst-stat]:hover { background-color: var(--rst-accent-soft); } |
| +a[rst-stat]:focus-visible { outline: 2px solid var(--rst-accent); outline-offset: -2px; } |
| +[rst-stat] > [co-statflag] { align-self: flex-start; margin-block-start: var(--rst-sp-1); } |
| +[co-statlink] { color: var(--rst-accent); font-size: var(--rst-fs-sm); font-weight: 600; margin-block-start: var(--rst-sp-1); } |
| +a[rst-stat]:hover [co-statlink] { text-decoration: underline; } |
| +/* With that tile the Dashboard's band holds six cells: narrower bases |
| + keep them on one row at desktop widths instead of leaving the sixth |
| + alone on a second. A band without a linked cell is untouched. */ |
| +[rst-stats]:has(a[rst-stat]) [rst-stat] { flex-basis: 7rem; } |
| +[rst-stats]:has(a[rst-stat]) [rst-stat~="lead"] { flex-basis: 12rem; } |
| + |
| /* Earned this month beside the funnel; one above the other on a phone. |
| The ARR chart has up to 90 bars, so they sit closer. */ |
| [co-billtwo] { display: grid; gap: 1.5rem var(--rst-sp-5); grid-template-columns: repeat(2, minmax(0, 1fr)); margin-block-start: 1.5rem; } |
diff --git a/internal/console/templates/opparts.html b/internal/console/templates/opparts.html| index 6246faa9d..e304c88d0 100644 |
| --- a/internal/console/templates/opparts.html |
| +++ b/internal/console/templates/opparts.html |
| @@ -18,8 +18,10 @@ |
| text; the tone is a second signal, never the only one. */}} |
| {{define "op_status"}}<span rst-status rst-tone="{{if .Tone}}{{.Tone}}{{else}}neutral{{end}}">{{.Label}}</span>{{end}} |
| -{{/* stat: one reading in an rst-stats band. Delta carries its own sign. */}} |
| -{{define "op_stat"}}<div rst-stat{{if .Lead}}="lead"{{end}}><span rst-stat-label>{{.Label}}</span><span rst-stat-num>{{.Value}}</span>{{with .Delta}}<span rst-stat-delta rst-tone="{{if $.Tone}}{{$.Tone}}{{else}}neutral{{end}}">{{.}}</span>{{end}}{{with .Note}}<span rst-stat-note>{{.}}</span>{{end}}</div>{{end}} |
| +{{/* stat: one reading in an rst-stats band. Delta carries its own sign. |
| + With an Href the whole cell is one link (one focus stop), its Flag |
| + and Link word after the note. */}} |
| +{{define "op_stat"}}{{if .Href}}<a rst-stat{{if .Lead}}="lead"{{end}} href="{{.Href}}">{{else}}<div rst-stat{{if .Lead}}="lead"{{end}}>{{end}}<span rst-stat-label>{{.Label}}</span><span rst-stat-num>{{.Value}}</span>{{with .Delta}}<span rst-stat-delta rst-tone="{{if $.Tone}}{{$.Tone}}{{else}}neutral{{end}}">{{.}}</span>{{end}}{{with .Note}}<span rst-stat-note>{{.}}</span>{{end}}{{with .Flag}}<span rst-status rst-tone="neutral" co-statflag>{{.}}</span>{{end}}{{if .Href}}{{with .Link}}<span co-statlink>{{.}}</span>{{end}}</a>{{else}}</div>{{end}}{{end}} |
| {{/* empty-state: an optional <h2> and one sentence, with no call to |
| action on the operator pages. */}} |
diff --git a/internal/console/templates/ops_dashboard.html b/internal/console/templates/ops_dashboard.html| index 5652573a6..ffb3b36bf 100644 |
| --- a/internal/console/templates/ops_dashboard.html |
| +++ b/internal/console/templates/ops_dashboard.html |
| @@ -19,10 +19,10 @@ |
| {{- end}} |
| </div> |
| </div> |
| -{{with .Stats}} |
| +{{$na := .NotAvailable}}{{with .Stats}} |
| <div rst-stats rst-card> |
| {{- range .}} |
| - {{template "op_stat" .}} |
| + {{if eq .Value $na}}<div co-statna>{{template "op_stat" .}}</div>{{else}}{{template "op_stat" .}}{{end}} |
| {{- end}} |
| </div> |
| {{end}} |