console: one CARLOS-only platform cost figure for Billing
carlosPlatformCostUSD is the seam the Billing pages call: a month's stored AWS bill less tax, shown only when the bill's effective basis is "account". effectiveBillBasis promotes only an unstated bill, and only under CONSOLE_COST_ACCOUNT_CARLOS_ONLY; any unknown stored value reads as "whole". A missing or unreadable bill reports costBasisMissing (read errors are logged). costBasisLabel and costBasisHint carry the approved copy C1-C4 and C7.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+189
−0
internal/console/platformcost.go+90 −0internal/console/platformcost_test.go+99 −0
diff --git a/internal/console/platformcost.go b/internal/console/platformcost.go| new file mode 100644 |
| index 000000000..72ad60eac |
| --- /dev/null |
| +++ b/internal/console/platformcost.go |
| @@ -0,0 +1,90 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "fmt" |
| + "log" |
| + |
| + "github.com/carlosframework/platform/internal/economics" |
| +) |
| + |
| +// costBasisMissing is the basis carlosPlatformCostUSD reports when there is |
| +// no readable bill for the month. It is never stored on a bill. |
| +const costBasisMissing = "missing" |
| + |
| +// billTaxService is the Services line Cost Explorer puts tax under. Tax is |
| +// not platform cost, so it comes off the bill total. |
| +const billTaxService = "Tax" |
| + |
| +// Approved copy (C1-C4, C7), verbatim. |
| +const ( |
| + costLabelAccount = "AWS, whole account: CARLOS only" |
| + costLabelOther = "AWS, whole account: not only CARLOS" |
| + costHintUnstated = "This bill covers the whole AWS account, which may hold more than CARLOS. If the account runs only CARLOS, set CONSOLE_COST_ACCOUNT_CARLOS_ONLY=true on the console." |
| + costHintNoBillFmt = "No AWS bill on file for %s yet." |
| + costHintWhole = "This bill was entered as covering the whole AWS account, CARLOS and other things, so it does not count as CARLOS cost." |
| +) |
| + |
| +// effectiveBillBasis is what a stored bill counts as under the declared |
| +// scope. Only an unstated bill is promoted to "account", and only when the |
| +// account is declared CARLOS-only; "account" and "whole" stand as stored. |
| +// Any other value is not something this console wrote, so it reads as |
| +// "whole", which the setting never promotes. |
| +func effectiveBillBasis(b economics.Bill, scope CostScope) string { |
| + switch b.Basis { |
| + case economics.BasisUnstated: |
| + if scope.AccountCarlosOnly { |
| + return economics.BasisAccount |
| + } |
| + return economics.BasisUnstated |
| + case economics.BasisAccount, economics.BasisWhole: |
| + return b.Basis |
| + default: |
| + return economics.BasisWhole |
| + } |
| +} |
| + |
| +// carlosPlatformCostUSD is month's ("2006-01") CARLOS-only AWS cost: the |
| +// stored bill's total less tax. ok is true only when the bill's effective |
| +// basis is "account". Otherwise usd is 0 and basis says why the figure is |
| +// hidden: the effective basis (unstated or whole), or costBasisMissing when |
| +// there is no readable bill. Callers own caching and currency conversion. |
| +func (s *Server) carlosPlatformCostUSD(ctx context.Context, month string) (usd float64, basis string, ok bool) { |
| + b, err := s.getBill(ctx, month) |
| + if err != nil { |
| + log.Printf("console: platform cost %s: read bill: %v", month, err) |
| + return 0, costBasisMissing, false |
| + } |
| + if b == nil { |
| + return 0, costBasisMissing, false |
| + } |
| + basis = effectiveBillBasis(*b, s.cfg.CostScope) |
| + if basis != economics.BasisAccount { |
| + return 0, basis, false |
| + } |
| + return b.TotalUSD - b.Services[billTaxService], basis, true |
| +} |
| + |
| +// costBasisLabel names what the cost figure covers. deployment is unused |
| +// for now; the tag follow-up will need it. |
| +func costBasisLabel(basis, deployment string) string { |
| + if basis == economics.BasisAccount { |
| + return costLabelAccount |
| + } |
| + return costLabelOther |
| +} |
| + |
| +// costBasisHint is the one-line explanation the Billing pages show in place |
| +// of a hidden cost, or "" when the cost is shown. month is "2006-01". |
| +func costBasisHint(scope CostScope, basis, month string) string { |
| + switch basis { |
| + case economics.BasisAccount: |
| + return "" |
| + case economics.BasisUnstated: |
| + return costHintUnstated |
| + case costBasisMissing: |
| + return fmt.Sprintf(costHintNoBillFmt, month) |
| + default: |
| + return costHintWhole |
| + } |
| +} |
diff --git a/internal/console/platformcost_test.go b/internal/console/platformcost_test.go| new file mode 100644 |
| index 000000000..ee53fcf16 |
| --- /dev/null |
| +++ b/internal/console/platformcost_test.go |
| @@ -0,0 +1,99 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/economics" |
| +) |
| + |
| +// TestCarlosPlatformCostBasis: the one figure Billing shows as CARLOS |
| +// platform cost exists only when the effective basis of the stored bill is |
| +// "account". Every other case hides the figure and says why. |
| +func TestCarlosPlatformCostBasis(t *testing.T) { |
| + const month = "2026-08" |
| + account := CostScope{AccountCarlosOnly: true} |
| + bill := func(basis, source string) economics.Bill { |
| + return economics.Bill{ |
| + Schema: 1, Month: month, TotalUSD: 100, |
| + Services: map[string]float64{"AmazonEC2": 90, "Tax": 10}, |
| + Source: source, Basis: basis, |
| + } |
| + } |
| + ce := economics.SourceCostExplorer |
| + for _, tc := range []struct { |
| + name string |
| + raw []byte // stored bytes, when set |
| + stored *economics.Bill // stored bill, when set |
| + scope CostScope |
| + wantUSD float64 |
| + wantBasis string |
| + wantOK bool |
| + }{ |
| + {name: "no object", scope: account, wantBasis: costBasisMissing}, |
| + {name: "unparseable", raw: []byte(`{"schema":1,"month":`), scope: account, wantBasis: costBasisMissing}, |
| + {name: "unstated bill zero scope", stored: ptrBill(bill("", ce)), wantBasis: economics.BasisUnstated}, |
| + {name: "whole bill account scope", stored: ptrBill(bill("whole", ce)), scope: account, wantBasis: economics.BasisWhole}, |
| + {name: "unstated bill account scope", stored: ptrBill(bill("", ce)), scope: account, wantUSD: 90, wantBasis: economics.BasisAccount, wantOK: true}, |
| + {name: "account bill zero scope", stored: ptrBill(bill("account", ce)), wantUSD: 90, wantBasis: economics.BasisAccount, wantOK: true}, |
| + {name: "tagged bill account scope", stored: ptrBill(bill("tagged", ce)), scope: account, wantBasis: economics.BasisWhole}, |
| + {name: "bogus bill account scope", stored: ptrBill(bill("bogus", ce)), scope: account, wantBasis: economics.BasisWhole}, |
| + {name: "manual unstated bill account scope", stored: ptrBill(bill("", economics.SourceManual)), scope: account, wantUSD: 90, wantBasis: economics.BasisAccount, wantOK: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.CostScope = tc.scope |
| + switch { |
| + case tc.raw != nil: |
| + if _, err := fx.rel.Put(context.Background(), economics.BillKey(month), tc.raw, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + case tc.stored != nil: |
| + putStoreJSON(t, fx.rel, economics.BillKey(month), *tc.stored) |
| + } |
| + usd, basis, ok := fx.srv.carlosPlatformCostUSD(context.Background(), month) |
| + if usd != tc.wantUSD || basis != tc.wantBasis || ok != tc.wantOK { |
| + t.Fatalf("carlosPlatformCostUSD = (%v, %q, %v), want (%v, %q, %v)", |
| + usd, basis, ok, tc.wantUSD, tc.wantBasis, tc.wantOK) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func ptrBill(b economics.Bill) *economics.Bill { return &b } |
| + |
| +// TestCostBasisLabelAndHint pins the approved copy (C1-C4, C7) byte for byte. |
| +func TestCostBasisLabelAndHint(t *testing.T) { |
| + const ( |
| + c1 = "AWS, whole account: CARLOS only" |
| + c2 = "AWS, whole account: not only CARLOS" |
| + c3 = "This bill covers the whole AWS account, which may hold more than CARLOS. If the account runs only CARLOS, set CONSOLE_COST_ACCOUNT_CARLOS_ONLY=true on the console." |
| + c4 = "No AWS bill on file for 2026-11 yet." |
| + c7 = "This bill was entered as covering the whole AWS account, CARLOS and other things, so it does not count as CARLOS cost." |
| + ) |
| + for _, tc := range []struct{ basis, want string }{ |
| + {economics.BasisAccount, c1}, |
| + {economics.BasisUnstated, c2}, |
| + {economics.BasisWhole, c2}, |
| + {costBasisMissing, c2}, |
| + } { |
| + if got := costBasisLabel(tc.basis, "flagship"); got != tc.want { |
| + t.Errorf("costBasisLabel(%q) = %q, want %q", tc.basis, got, tc.want) |
| + } |
| + } |
| + for _, scope := range []CostScope{{}, {AccountCarlosOnly: true}} { |
| + for _, tc := range []struct{ basis, want string }{ |
| + {economics.BasisUnstated, c3}, |
| + {economics.BasisWhole, c7}, |
| + {costBasisMissing, c4}, |
| + {economics.BasisAccount, ""}, |
| + } { |
| + if got := costBasisHint(scope, tc.basis, "2026-11"); got != tc.want { |
| + t.Errorf("costBasisHint(%+v, %q) = %q, want %q", scope, tc.basis, got, tc.want) |
| + } |
| + } |
| + } |
| + if billTaxService != "Tax" { |
| + t.Errorf("billTaxService = %q, want Tax", billTaxService) |
| + } |
| +} |