carlos / platform Public

console: billing history note reads the first stored snapshot day

BDP-14 on the overview and the accounts page now comes from
Store.FirstSnapshotDay, not from whichever window of snapshots a page
read, so both pages agree. A day missing inside the history leaves the
month uncovered (BDP-13 in the cells) without saying the history is short.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev fb9c244e6dc73affeaf60e47b7d9f52930337d80 parent 578965c
3 files changed, +59 −11
  • internal/console/opbilling.go +6 −2
  • internal/console/opbilling_overview.go +12 −6
  • internal/console/opbilling_test.go +41 −3

CI passed — run details

diff --git a/internal/console/opbilling.go b/internal/console/opbilling.go
index 106429e16..6b0f78022 100644
--- a/internal/console/opbilling.go
+++ b/internal/console/opbilling.go
@@ -35,7 +35,8 @@ type opBillingAccountsView struct {
MonthName string // the last complete month, as the overview names it
MonthNote string // BDP-117
// HistoryNote is BDP-14 while the stored history is shorter than the
- // month (opBillingHistoryNote); the handler sets it from the snapshots.
+ // month (opBillingHistoryNote); the handler sets it from the first
+ // stored snapshot day.
HistoryNote string
Tabs opTabs
Sort opTabs
@@ -364,6 +365,9 @@ func (s *Server) opBillingAccountInputs(ctx context.Context, now, snapsFrom time
if in.Snaps, err = s.store.ARRSnapshots(snapsFrom.Format(snapshotDayFormat), today.Format(snapshotDayFormat)); err != nil {
return opBillingInput{}, err
}
+ if in.FirstDay, err = s.store.FirstSnapshotDay(); err != nil {
+ return opBillingInput{}, err
+ }
days, err := s.store.AccountDays(lastStart.Format(snapshotDayFormat), monthStart.AddDate(0, 0, -1).Format(snapshotDayFormat))
if err != nil {
return opBillingInput{}, err
@@ -406,7 +410,7 @@ func (s *Server) handleOpBillingAccounts(w http.ResponseWriter, r *http.Request)
v := buildOpBillingAccounts(rows, in.Covered, in.Gate, in.Rates, billingGroup(q.Get("tab")), q.Get("sort"), page, q)
v.Header = opHeader{Title: billCopyAccounts, Sub: billCopyAccountsSub}
v.TestMode = s.billingTestMode()
- v.HistoryNote = opBillingHistoryNote(in.Snaps, in.LastMonth)
+ v.HistoryNote = opBillingHistoryNote(in.FirstDay, in.LastMonth)
p := s.billingPage(addr, raw, billCopyAccounts)
p.OpBillingAccounts = &v
s.render(w, http.StatusOK, "ops_billing_accounts", p)
diff --git a/internal/console/opbilling_overview.go b/internal/console/opbilling_overview.go
index ec4d50c0e..8052e6b14 100644
--- a/internal/console/opbilling_overview.go
+++ b/internal/console/opbilling_overview.go
@@ -50,6 +50,9 @@ type opBillingInput struct {
LastMonth string
LastRows []paymentRow
Snaps []arrSnapshot // the last 90 days, oldest first
+ // FirstDay is the first stored snapshot's day (Store.FirstSnapshotDay),
+ // "" with none: where the history starts, whatever window was read.
+ FirstDay string
// Earned and Covered are accountEarnings over LastMonth.
Earned map[int64]int64
Covered bool
@@ -698,16 +701,19 @@ func opBillingPayingList(v *opBillingView, in opBillingInput, rows []accountMone
v.Paying = append(v.Paying, row)
}
v.PayingNote = fmt.Sprintf(billCopyPayingListNote, opBillingMonthLabel(in.LastMonth, in.Now))
- v.HistoryNote = opBillingHistoryNote(in.Snaps, in.LastMonth)
+ v.HistoryNote = opBillingHistoryNote(in.FirstDay, in.LastMonth)
v.PayingWhys = opBillingWhys(v.Figures[start:])
}
// opBillingHistoryNote is BDP-14 while the stored history is shorter than
-// month ("2006-01"): no snapshot, or the first (snaps oldest first) after
-// the month's first day; "" otherwise. It sits under every per-account
-// list.
-func opBillingHistoryNote(snaps []arrSnapshot, month string) string {
- if len(snaps) == 0 || snaps[0].Day > month+"-01" {
+// month ("2006-01"): no snapshot stored (firstDay ""), or the first stored
+// day after the month's first day; "" otherwise. firstDay is
+// Store.FirstSnapshotDay, so the note does not depend on the window a page
+// read. A day missing inside the history leaves the month uncovered (the
+// cells say BDP-13) but does not make the history short. It sits under
+// every per-account list.
+func opBillingHistoryNote(firstDay, month string) string {
+ if firstDay == "" || firstDay > month+"-01" {
return billCopyHistoryShort
}
return ""
diff --git a/internal/console/opbilling_test.go b/internal/console/opbilling_test.go
index 5a5611a62..c677cab3f 100644
--- a/internal/console/opbilling_test.go
+++ b/internal/console/opbilling_test.go
@@ -161,10 +161,14 @@ func (w overviewWorld) input(in moneyInputs) opBillingInput {
}
}
earned, covered := accountEarnings("2026-10", w.days, w.snaps)
+ var first string
+ if len(w.snaps) > 0 {
+ first = w.snaps[0].Day
+ }
return opBillingInput{
Now: in.Now, Rev: rev, Gate: gate, Facts: w.facts, ExemptID: exempt, Rates: r,
Months: moneyByMonth(w.payments, overviewMonths, r, platform), Payments: w.payments, LastMonth: "2026-10", LastRows: last,
- Snaps: w.snaps, Earned: earned, Covered: covered, Recent: recent, CostUSD: w.costUSD,
+ Snaps: w.snaps, FirstDay: first, Earned: earned, Covered: covered, Recent: recent, CostUSD: w.costUSD,
Platform: platform, Funnel: w.funnel, Near: w.near,
}
}
@@ -1205,10 +1209,44 @@ func TestBillingAccountsHistoryShort(t *testing.T) {
t.Errorf("page lacks %q", s)
}
}
- if got := opBillingHistoryNote(nil, "2026-10"); got != billCopyHistoryShort {
+ if got := opBillingHistoryNote("", "2026-10"); got != billCopyHistoryShort {
t.Errorf("no snapshots: %q", got)
}
- if got := opBillingHistoryNote([]arrSnapshot{{Day: "2026-10-01"}}, "2026-10"); got != "" {
+ if got := opBillingHistoryNote("2026-10-01", "2026-10"); got != "" {
t.Errorf("history from 1 October: %q", got)
}
}
+
+// History from 1 September, 1 October missing, every other day stored:
+// October is not covered (earns and margin say BDP-13 on both pages), but
+// the history is not short, so neither page shows BDP-14, whatever window
+// each page reads.
+func TestBillingAccountsHistoryGapNotShort(t *testing.T) {
+ fx, acct := overviewFixture(t)
+ for d := time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC); !d.After(overviewNow); d = d.AddDate(0, 0, 1) {
+ day := d.Format(snapshotDayFormat)
+ if day == "2026-10-01" {
+ continue
+ }
+ snap := arrSnapshot{Day: day, ARR: map[string]int64{"eur": 2400}, EUR: 2400, RateDay: "2026-11-06", Paying: 1, Complete: true}
+ rows := []accountDay{{Day: day, AccountID: acct.ID, Annual: 2400, Currency: "eur", AnnualEUR: 2400, Paying: true, InARR: true}}
+ if err := fx.st.PutDailySnapshot(snap, rows, day, d); err != nil {
+ t.Fatal(err)
+ }
+ }
+ history := html.EscapeString(fmt.Sprintf(billCopyWhyHistory, "October 2026"))
+ short := html.EscapeString(billCopyHistoryShort)
+ c := signInOperator(t, fx)
+ for _, path := range []string{"/fleet/billing", "/fleet/billing/accounts"} {
+ status, _, body := get(t, c, fx.ts.URL+path)
+ if status != http.StatusOK {
+ t.Fatalf("%s = %d", path, status)
+ }
+ if strings.Contains(body, short) {
+ t.Errorf("%s says the history is short with history from 1 September", path)
+ }
+ if !strings.Contains(body, history) {
+ t.Errorf("%s lacks the BDP-13 reason for uncovered October", path)
+ }
+ }
+}