carlos / platform Public

usage: omitempty on guard tally identity; storage-merge test for guard

GuardTally host/account/app are omitempty like every other new stored field.
A round-trip test through UpsertEdgeDay merges guard data into an existing
stored day: a delta with no groups, and a tally one name short of the group
cap receiving a new name, an overflow name and an incoming "other" count.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 007ea992c71929c540064ca280162e93ad049317 parent a2df86f
2 files changed, +80 −3
  • internal/usage/edge.go +3 −3
  • internal/usage/edge_test.go +77 −0
diff --git a/internal/usage/edge.go b/internal/usage/edge.go
index 7936172fc..e1104b6bc 100644
--- a/internal/usage/edge.go
+++ b/internal/usage/edge.go
@@ -63,9 +63,9 @@ type GuardHour struct {
// counters (SetOwner) and so would otherwise show one account the other's
// bad traffic.
type GuardTally struct {
- Host string `json:"host"`
- Account string `json:"account"`
- App string `json:"app"`
+ Host string `json:"host,omitempty"`
+ Account string `json:"account,omitempty"`
+ App string `json:"app,omitempty"`
Hours map[string]GuardHour `json:"hours,omitempty"`
Groups map[string]int64 `json:"groups,omitempty"`
}
diff --git a/internal/usage/edge_test.go b/internal/usage/edge_test.go
index 25bb16042..0aa9b743e 100644
--- a/internal/usage/edge_test.go
+++ b/internal/usage/edge_test.go
@@ -316,3 +316,80 @@ func TestEdgeDayOldShapeParses(t *testing.T) {
t.Fatalf("json = %s, want no guard key", out)
}
}
+
+// The storage merge, not just the in-memory fold: an existing day object
+// (rows plus a guard tally one name short of the group cap) is read back,
+// merged with a flush that has a delta with no groups and one carrying an
+// incoming "other" count, and written; the stored day is what's asserted.
+func TestUpsertEdgeDayMergesGuardIntoStoredDay(t *testing.T) {
+ const host = "shop.example.com"
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ at := time.Date(2026, 8, 11, 9, 10, 0, 0, time.UTC)
+ key := GuardKey(host, "bab", "shop")
+ quietKey := GuardKey("quiet.example.com", "bdk", "web")
+
+ // Seed: 30 named groups, so exactly one more name still fits.
+ seed := map[string]int64{}
+ for i := 0; i < GuardGroupCap-2; i++ {
+ seed[fmt.Sprintf("g%02d", i)] = 1
+ }
+ first := EdgeFlush{
+ Rows: map[string]EdgeRow{host: {Account: "bab", App: "shop", Requests: 7, BytesOut: 70}},
+ Guard: map[string]GuardDelta{key: guardDelta(host, "bab", "shop", 7, 1, 0, seed)},
+ }
+ if err := UpsertEdgeDay(ctx, s, "box1", at, first); err != nil {
+ t.Fatal(err)
+ }
+
+ // Second flush, next hour: a new name (fits), another new name (overflows
+ // to other), an incoming "other" count, and a second tally with no groups.
+ second := EdgeFlush{
+ Guard: map[string]GuardDelta{
+ key: guardDelta(host, "bab", "shop", 5, 3, 2, map[string]int64{
+ "new-a": 4, "new-b": 6, "g00": 2, GuardGroupOther: 5,
+ }),
+ quietKey: guardDelta("quiet.example.com", "bdk", "web", 9, 0, 0, nil),
+ },
+ }
+ if err := UpsertEdgeDay(ctx, s, "box1", at.Add(time.Hour), second); err != nil {
+ t.Fatal(err)
+ }
+
+ raw, _, err := s.Get(ctx, EdgeDayKey("box1", "2026-08-11"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var day EdgeDay
+ if err := json.Unmarshal(raw, &day); err != nil {
+ t.Fatal(err)
+ }
+ if row := day.Hours["09"][host]; row.Requests != 7 || row.BytesOut != 70 {
+ t.Fatalf("seeded row = %+v, want it kept", row)
+ }
+ g := day.Guard[key]
+ if g.Host != host || g.Account != "bab" || g.App != "shop" {
+ t.Fatalf("identity = %+v", g)
+ }
+ if got, want := g.Hours["09"], (GuardHour{Checked: 7, Watch: 1}); got != want {
+ t.Fatalf("hour 09 = %+v, want %+v", got, want)
+ }
+ if got, want := g.Hours["10"], (GuardHour{Checked: 5, Watch: 3, Block: 2}); got != want {
+ t.Fatalf("hour 10 = %+v, want %+v", got, want)
+ }
+ // 30 seeded + new-a fill the 31 named slots; new-b (6) and the incoming
+ // other (5) land in other; g00 sums.
+ if len(g.Groups) != GuardGroupCap {
+ t.Fatalf("groups = %d names, want %d: %v", len(g.Groups), GuardGroupCap, g.Groups)
+ }
+ if g.Groups["new-a"] != 4 || g.Groups["new-b"] != 0 || g.Groups[GuardGroupOther] != 11 || g.Groups["g00"] != 3 {
+ t.Fatalf("groups = %v", g.Groups)
+ }
+ q := day.Guard[quietKey]
+ if q.Hours["10"] != (GuardHour{Checked: 9}) || len(q.Groups) != 0 {
+ t.Fatalf("quiet tally = %+v, want checked 9 and no groups", q)
+ }
+ if strings.Contains(string(raw), `"groups":null`) || strings.Contains(string(raw), `"groups":{}`) {
+ t.Fatalf("stored day has an empty groups key: %s", raw)
+ }
+}