carlos / platform Public

console: sum protection counts over whole UTC days

protectionDates gives the last 7 or 30 UTC dates including today.
sumProtection totals one app's accepted guard tallies per day and over the
window, and sumProtectionAll does it for every app, sorted by caught
requests. Tallies count only where tallyCounts accepts them, so a fleet
vouches only for its owner and a re-owned host keeps each owner's counts
apart. No coverage is derived.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7857982ce3aa71d3d254cbd2f5e869fece4c7039 parent aa0d943
2 files changed, +399 −0
  • internal/console/protection.go +177 −0
  • internal/console/protection_test.go +222 −0
diff --git a/internal/console/protection.go b/internal/console/protection.go
new file mode 100644
index 000000000..1d8b1485b
--- /dev/null
+++ b/internal/console/protection.go
@@ -0,0 +1,177 @@
+package console
+
+import (
+ "sort"
+ "time"
+
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/usage"
+)
+
+// The Protection tab and the operator's protection page count what the edge
+// guard looked at, noted and refused over whole UTC days. These are the sums
+// they render, over the sources readEdgeDays returns. A sum says only what
+// the tallies held: it makes no claim that the window was fully counted,
+// partly counted or when counting began, so none of its fields can be read
+// as one.
+
+// protectionDay is one UTC date's counts.
+type protectionDay struct {
+ Date string `json:"date"`
+ Checked int64 `json:"checked"`
+ Watch int64 `json:"watch"`
+ Block int64 `json:"block"`
+}
+
+// protectionSummary is a window's counts. From and To are its first and last
+// dates, and Days lists every date between, oldest first, zeros included.
+// Groups is never nil: a window with nothing counted holds the empty map, so
+// the JSON is always {} and a reader needs no nil case.
+type protectionSummary struct {
+ From string `json:"from"`
+ To string `json:"to"`
+ Days []protectionDay `json:"days"`
+ Checked int64 `json:"checked"`
+ Watch int64 `json:"watch"`
+ Block int64 `json:"block"`
+ Groups map[string]int64 `json:"groups"`
+}
+
+// protectionAppRow is one app's totals on the operator's page.
+type protectionAppRow struct {
+ Account string
+ App string
+ Checked int64
+ Watch int64
+ Block int64
+}
+
+// protectionDates is the last days UTC dates up to and including now's,
+// oldest first. The window is 7 or 30 whole days; any other value reads as 7.
+func protectionDates(days int, now time.Time) []string {
+ if days != 7 && days != 30 {
+ days = 7
+ }
+ today := now.UTC()
+ out := make([]string, days)
+ for i := range out {
+ out[i] = today.AddDate(0, 0, i-(days-1)).Format("2006-01-02")
+ }
+ return out
+}
+
+// protectionWindow builds summaries over one set of dates: it holds the
+// date-to-slot index so each tally lands in its day.
+type protectionWindow struct {
+ sum protectionSummary
+ slot map[string]int
+}
+
+func newProtectionWindow(dates []string) *protectionWindow {
+ w := &protectionWindow{
+ sum: protectionSummary{Days: make([]protectionDay, len(dates)), Groups: map[string]int64{}},
+ slot: make(map[string]int, len(dates)),
+ }
+ for i, d := range dates {
+ w.sum.Days[i].Date = d
+ w.slot[d] = i
+ }
+ if len(dates) > 0 {
+ w.sum.From, w.sum.To = dates[0], dates[len(dates)-1]
+ }
+ return w
+}
+
+// add folds one accepted tally from the day date into the window and returns
+// the checked, watch and block counts it added there. A tally for a date
+// outside the window adds nothing.
+func (w *protectionWindow) add(date string, t usage.GuardTally) (checked, watch, block int64) {
+ i, ok := w.slot[date]
+ if !ok {
+ return 0, 0, 0
+ }
+ for _, h := range t.Hours {
+ checked += h.Checked
+ watch += h.Watch
+ block += h.Block
+ }
+ d := &w.sum.Days[i]
+ d.Checked += checked
+ d.Watch += watch
+ d.Block += block
+ w.sum.Checked += checked
+ w.sum.Watch += watch
+ w.sum.Block += block
+ for g, n := range t.Groups {
+ w.sum.Groups[g] += n
+ }
+ return checked, watch, block
+}
+
+// sumProtection is acct's app's counts over dates. Only tallies tallyCounts
+// accepts for (acct, app) count: a fleet's tally counts for its fleet's
+// owner and no one else, and a host that changed hands keeps each owner's
+// tally apart, so neither sees the other's.
+func sumProtection(srcs []edgeDaySource, dates []string, acct, app string) protectionSummary {
+ w := newProtectionWindow(dates)
+ for _, src := range srcs {
+ for _, t := range src.Day.Guard {
+ if tallyCounts(src, t, acct, app) {
+ w.add(src.Day.Date, t)
+ }
+ }
+ }
+ return w.sum
+}
+
+// sumProtectionAll is every app's counts over dates: the window's total, and
+// a row per app sorted by watch plus block, highest first, then account,
+// then app. A tally is taken at its own account and app, and only where
+// tallyCounts accepts it there, so a fleet's tally for an account that does
+// not own the fleet is left out of the total as well as the rows. A tally
+// naming no account is the ops account's, as on a route; one naming no app
+// belongs to no app and is left out.
+func sumProtectionAll(srcs []edgeDaySource, dates []string) (protectionSummary, []protectionAppRow) {
+ w := newProtectionWindow(dates)
+ type appKey struct{ account, app string }
+ byApp := map[appKey]*protectionAppRow{}
+ for _, src := range srcs {
+ for _, t := range src.Day.Guard {
+ acct := t.Account
+ if acct == "" {
+ acct = release.OpsAccount
+ }
+ if t.App == "" || !tallyCounts(src, t, acct, t.App) {
+ continue
+ }
+ checked, watch, block := w.add(src.Day.Date, t)
+ if _, in := w.slot[src.Day.Date]; !in {
+ continue
+ }
+ k := appKey{acct, t.App}
+ row := byApp[k]
+ if row == nil {
+ row = &protectionAppRow{Account: acct, App: t.App}
+ byApp[k] = row
+ }
+ row.Checked += checked
+ row.Watch += watch
+ row.Block += block
+ }
+ }
+ rows := make([]protectionAppRow, 0, len(byApp))
+ for _, r := range byApp {
+ rows = append(rows, *r)
+ }
+ sort.Slice(rows, func(i, j int) bool {
+ a, b := rows[i], rows[j]
+ if ca, cb := a.Watch+a.Block, b.Watch+b.Block; ca != cb {
+ return ca > cb
+ }
+ if a.Account != b.Account {
+ return a.Account < b.Account
+ }
+ return a.App < b.App
+ })
+ return w.sum, rows
+}
diff --git a/internal/console/protection_test.go b/internal/console/protection_test.go
new file mode 100644
index 000000000..866201d7f
--- /dev/null
+++ b/internal/console/protection_test.go
@@ -0,0 +1,222 @@
+package console
+
+import (
+ "reflect"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/usage"
+)
+
+const (
+ prHost = "shop.example.com"
+ prDay = "2026-11-05"
+)
+
+var prNow = time.Date(2026, 11, 5, 13, 0, 0, 0, time.UTC)
+
+// prSrc is a root box's day with the given tallies, keyed as the edge keys them.
+func prSrc(box, date string, tallies ...usage.GuardTally) edgeDaySource {
+ g := map[string]usage.GuardTally{}
+ for _, t := range tallies {
+ g[usage.GuardKey(t.Host, t.Account, t.App)] = t
+ }
+ return edgeDaySource{Day: usage.EdgeDay{V: 1, Box: box, Date: date, Guard: g}}
+}
+
+func prTally(acct, app string, hours map[string]usage.GuardHour, groups map[string]int64) usage.GuardTally {
+ return usage.GuardTally{Host: prHost, Account: acct, App: app, Hours: hours, Groups: groups}
+}
+
+func TestProtectionDatesWholeDays(t *testing.T) {
+ got := protectionDates(7, prNow)
+ if len(got) != 7 || got[0] != "2026-10-30" || got[6] != "2026-11-05" {
+ t.Fatalf("7 days = %v", got)
+ }
+ if odd := protectionDates(9, prNow); !reflect.DeepEqual(odd, got) {
+ t.Fatalf("9 days = %v, want the 7-day window", odd)
+ }
+ if thirty := protectionDates(30, prNow); len(thirty) != 30 || thirty[0] != "2026-10-07" || thirty[29] != prDay {
+ t.Fatalf("30 days = %v", thirty)
+ }
+ // A zone offset does not move the window: it is UTC dates.
+ late := time.Date(2026, 11, 5, 23, 30, 0, 0, time.FixedZone("west", -5*3600))
+ if got := protectionDates(7, late); got[6] != "2026-11-06" {
+ t.Fatalf("last date = %s, want 2026-11-06", got[6])
+ }
+}
+
+func TestSumProtectionTotals(t *testing.T) {
+ srcs := []edgeDaySource{
+ prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 100, Watch: 6}}, map[string]int64{"probe/wordpress": 5})),
+ prSrc("b", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"10": {Checked: 50, Watch: 2, Block: 1}}, map[string]int64{"hard/secrets": 4})),
+ }
+ got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop")
+ if got.Checked != 150 || got.Watch != 8 || got.Block != 1 {
+ t.Fatalf("totals = %d/%d/%d, want 150/8/1", got.Checked, got.Watch, got.Block)
+ }
+ if want := map[string]int64{"probe/wordpress": 5, "hard/secrets": 4}; !reflect.DeepEqual(got.Groups, want) {
+ t.Fatalf("groups = %v, want %v", got.Groups, want)
+ }
+ if got.From != "2026-10-30" || got.To != prDay || len(got.Days) != 7 {
+ t.Fatalf("window = %s..%s, %d days", got.From, got.To, len(got.Days))
+ }
+ if d := got.Days[6]; d.Date != prDay || d.Checked != 150 || d.Watch != 8 || d.Block != 1 {
+ t.Fatalf("last day = %+v", d)
+ }
+ for _, d := range got.Days[:6] {
+ if d.Checked != 0 || d.Watch != 0 || d.Block != 0 {
+ t.Fatalf("day %s = %+v, want zeros", d.Date, d)
+ }
+ }
+}
+
+func TestSumProtectionIgnoresDaysOutsideWindow(t *testing.T) {
+ srcs := []edgeDaySource{
+ prSrc("a", "2026-10-29", prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 9, Block: 9}}, nil)),
+ prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 3}}, nil)),
+ }
+ if got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop"); got.Checked != 3 || got.Block != 0 {
+ t.Fatalf("totals = %d/%d, want 3/0", got.Checked, got.Block)
+ }
+}
+
+func TestSumProtectionNothingCaught(t *testing.T) {
+ srcs := []edgeDaySource{prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 40}, "10": {Checked: 2}}, nil))}
+ got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop")
+ if got.Checked != 42 || got.Watch != 0 || got.Block != 0 {
+ t.Fatalf("totals = %d/%d/%d, want 42/0/0", got.Checked, got.Watch, got.Block)
+ }
+ if got.Groups == nil || len(got.Groups) != 0 {
+ t.Fatalf("groups = %#v, want the empty map", got.Groups)
+ }
+}
+
+func TestSumProtectionNoTallies(t *testing.T) {
+ src := edgeDaySource{Day: usage.EdgeDay{V: 1, Box: "a", Date: prDay, Hours: map[string]map[string]usage.EdgeRow{
+ "09": {prHost: {Account: "bab", App: "shop", Requests: 500}},
+ }}}
+ got := sumProtection([]edgeDaySource{src}, protectionDates(7, prNow), "bab", "shop")
+ if got.Checked != 0 || got.Watch != 0 || got.Block != 0 {
+ t.Fatalf("totals = %d/%d/%d, want zeros", got.Checked, got.Watch, got.Block)
+ }
+ if len(got.Days) != 7 {
+ t.Fatalf("days = %d, want 7", len(got.Days))
+ }
+ for _, d := range got.Days {
+ if d.Checked != 0 || d.Watch != 0 || d.Block != 0 {
+ t.Fatalf("day %s = %+v, want zeros", d.Date, d)
+ }
+ }
+ if got.Groups == nil || len(got.Groups) != 0 {
+ t.Fatalf("groups = %#v, want the empty map", got.Groups)
+ }
+}
+
+func TestSumProtectionOwnerChange(t *testing.T) {
+ // The host changed hands in hour 12: both owners' tallies hold that hour.
+ srcs := []edgeDaySource{prSrc("a", prDay,
+ prTally("bab", "shop", map[string]usage.GuardHour{"12": {Checked: 10, Watch: 3, Block: 1}}, map[string]int64{"probe/wordpress": 4}),
+ prTally("bdk", "shop", map[string]usage.GuardHour{"12": {Checked: 20, Watch: 7, Block: 2}}, map[string]int64{"hard/secrets": 9}),
+ )}
+ dates := protectionDates(7, prNow)
+ for _, c := range []struct {
+ acct string
+ checked, watch, block int64
+ groups map[string]int64
+ }{
+ {"bab", 10, 3, 1, map[string]int64{"probe/wordpress": 4}},
+ {"bdk", 20, 7, 2, map[string]int64{"hard/secrets": 9}},
+ } {
+ got := sumProtection(srcs, dates, c.acct, "shop")
+ if got.Checked != c.checked || got.Watch != c.watch || got.Block != c.block {
+ t.Fatalf("%s totals = %d/%d/%d", c.acct, got.Checked, got.Watch, got.Block)
+ }
+ if !reflect.DeepEqual(got.Groups, c.groups) {
+ t.Fatalf("%s groups = %v, want %v", c.acct, got.Groups, c.groups)
+ }
+ var groupTotal int64
+ for _, n := range got.Groups {
+ groupTotal += n
+ }
+ if caught := got.Watch + got.Block; caught != groupTotal {
+ t.Fatalf("%s caught %d, group total %d", c.acct, caught, groupTotal)
+ }
+ }
+}
+
+func TestSumProtectionFleetVouchesOnlyForOwner(t *testing.T) {
+ tally := prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 5, Block: 1}}, map[string]int64{"scanner": 1})
+ dates := protectionDates(7, prNow)
+ for _, c := range []struct {
+ name string
+ owner string
+ want int64
+ }{{"owner", "bab", 5}, {"other owner", "bdk", 0}, {"ownerless", "", 0}} {
+ src := prSrc("a", prDay, tally)
+ src.Fleet, src.FleetAccount = "f1", c.owner
+ if got := sumProtection([]edgeDaySource{src}, dates, "bab", "shop"); got.Checked != c.want {
+ t.Fatalf("%s: checked = %d, want %d", c.name, got.Checked, c.want)
+ }
+ all, rows := sumProtectionAll([]edgeDaySource{src}, dates)
+ if all.Checked != c.want || (c.want == 0) != (len(rows) == 0) {
+ t.Fatalf("%s: all checked = %d, %d rows", c.name, all.Checked, len(rows))
+ }
+ }
+}
+
+func TestSumProtectionAllSortsApps(t *testing.T) {
+ mk := func(acct, app string, watch, block int64) usage.GuardTally {
+ return usage.GuardTally{Host: app + ".example.com", Account: acct, App: app,
+ Hours: map[string]usage.GuardHour{"09": {Checked: 100, Watch: watch, Block: block}}, Groups: map[string]int64{"scanner": watch + block}}
+ }
+ srcs := []edgeDaySource{
+ prSrc("a", prDay, mk("bab", "quiet", 0, 0), mk("bab", "mid", 3, 2), mk("bdk", "loud", 8, 4)),
+ prSrc("b", prDay, mk("bab", "mid", 0, 0)),
+ }
+ sum, rows := sumProtectionAll(srcs, protectionDates(7, prNow))
+ var got []string
+ for _, r := range rows {
+ got = append(got, r.Account+"/"+r.App)
+ }
+ if want := []string{"bdk/loud", "bab/mid", "bab/quiet"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("order = %v, want %v", got, want)
+ }
+ if rows[0].Watch != 8 || rows[0].Block != 4 || rows[0].Checked != 100 {
+ t.Fatalf("top row = %+v", rows[0])
+ }
+ if rows[1].Checked != 200 {
+ t.Fatalf("mid row sums both boxes: %+v", rows[1])
+ }
+ if sum.Checked != 400 || sum.Watch != 11 || sum.Block != 6 {
+ t.Fatalf("summary = %d/%d/%d, want 400/11/6", sum.Checked, sum.Watch, sum.Block)
+ }
+ if sum.Groups["scanner"] != 17 || len(sum.Days) != 7 || sum.Days[6].Checked != 400 {
+ t.Fatalf("summary groups/days = %v / %+v", sum.Groups, sum.Days)
+ }
+}
+
+func TestSumProtectionAllTiesBreakByAccountThenApp(t *testing.T) {
+ mk := func(acct, app string) usage.GuardTally {
+ return usage.GuardTally{Host: app + ".example.com", Account: acct, App: app,
+ Hours: map[string]usage.GuardHour{"09": {Watch: 2}}}
+ }
+ srcs := []edgeDaySource{prSrc("a", prDay, mk("bdk", "aaa"), mk("bab", "zzz"), mk("bab", "mmm"))}
+ _, rows := sumProtectionAll(srcs, protectionDates(7, prNow))
+ var got []string
+ for _, r := range rows {
+ got = append(got, r.Account+"/"+r.App)
+ }
+ if want := []string{"bab/mmm", "bab/zzz", "bdk/aaa"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("order = %v, want %v", got, want)
+ }
+}
+
+func TestSumProtectionAllEmptyAccountIsOps(t *testing.T) {
+ // A tally with no account is the ops account's, as a route's is.
+ srcs := []edgeDaySource{prSrc("a", prDay, prTally("", "shop", map[string]usage.GuardHour{"09": {Checked: 4}}, nil))}
+ sum, rows := sumProtectionAll(srcs, protectionDates(7, prNow))
+ if sum.Checked != 4 || len(rows) != 1 || rows[0].App != "shop" {
+ t.Fatalf("summary %d, rows %+v", sum.Checked, rows)
+ }
+}