carlos / platform Public

console: the settled-day cache keeps an edge day with guard tallies

TestUsageDayCacheKeepsGuardDay reads a settled edge day carrying a full
day of guard tallies twice through usageDayReader; the second read is
answered from the cache. TestAppTabBarIsOneLevel now expects the
Protection tab between Analytics and Activity.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 1cd4f0a3b31be341390d1b2b5a84d2f6771391d4 parent 14791cf
2 files changed, +88 −3
  • internal/console/appnav_test.go +2 −2
  • internal/console/usagedays_test.go +86 −1

CI cancelled — superseded by a newer push — run details

diff --git a/internal/console/appnav_test.go b/internal/console/appnav_test.go
index b98a7f786..dff7afa51 100644
--- a/internal/console/appnav_test.go
+++ b/internal/console/appnav_test.go
@@ -45,11 +45,11 @@ func appTabLabels(t *testing.T, body string) (labels []string, current string) {
func TestAppTabBarIsOneLevel(t *testing.T) {
fx := newConsoleFixture(t)
c := signInPaul(t, fx)
- want := []string{"Overview", "Running", "Releases", "Logs", "Errors", "Availability", "Analytics", "Activity", "Settings"}
+ want := []string{"Overview", "Running", "Releases", "Logs", "Errors", "Availability", "Analytics", "Protection", "Activity", "Settings"}
for path, current := range map[string]string{
"/apps/kass": "Overview", "/apps/kass/instances": "Running", "/apps/kass/releases": "Releases",
"/apps/kass/logs": "Logs", "/apps/kass/errors": "Errors", "/apps/kass/availability": "Availability",
- "/apps/kass/analytics": "Analytics", "/apps/kass/activity": "Activity",
+ "/apps/kass/analytics": "Analytics", "/apps/kass/protection": "Protection", "/apps/kass/activity": "Activity",
"/apps/kass/settings": "Settings", "/apps/kass/settings/domains": "Settings",
} {
status, _, body := get(t, c, fx.u(path))
diff --git a/internal/console/usagedays_test.go b/internal/console/usagedays_test.go
index 88ebe2722..8dd257801 100644
--- a/internal/console/usagedays_test.go
+++ b/internal/console/usagedays_test.go
@@ -1,6 +1,14 @@
package console
-import "testing"
+import (
+ "context"
+ "encoding/json"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/usage"
+)
// TestUsageDayDateFleetEdgeDay: a fleet box's edge day, written through its
// store rebased under fleets/<fleet>/, is a day object the cache knows, so a
@@ -25,3 +33,80 @@ func TestUsageDayDateFleetEdgeDay(t *testing.T) {
}
}
}
+
+// TestUsageDayCacheKeepsGuardDay: an edge day carrying guard tallies is
+// still a day the settled-day cache keeps. The Protection tab reads its
+// days through usageDayReader, so a settled day read once is answered from
+// the cache the next time, tallies and all, without reaching the bucket.
+func TestUsageDayCacheKeepsGuardDay(t *testing.T) {
+ fx := newConsoleFixture(t)
+ ctx := context.Background()
+ now := time.Now().UTC()
+ at := now.AddDate(0, 0, -3)
+ date := usage.DayOf(at)
+ if !usageDaySettled(date, now) {
+ t.Fatalf("%s is not settled at %s; the test needs a settled day", date, now)
+ }
+ host := "shop.example.com"
+ key := usage.EdgeDayKey("box-a", date)
+ for h := 0; h < 24; h++ {
+ hourAt := time.Date(at.Year(), at.Month(), at.Day(), h, 5, 0, 0, time.UTC)
+ groups := map[string]int64{}
+ for g := 0; g < 16; g++ {
+ groups["probe/group"+string(rune('a'+g))] = 1
+ }
+ f := usage.EdgeFlush{
+ Rows: map[string]usage.EdgeRow{host: {Account: "bab", App: "shop", Requests: 40}},
+ Guard: map[string]usage.GuardDelta{usage.GuardKey(host, "bab", "shop"): {
+ Host: host, Account: "bab", App: "shop", Checked: 40, Watch: 16, Block: 2, Groups: groups,
+ }},
+ }
+ if err := usage.UpsertEdgeDay(ctx, fx.rel, "box-a", hourAt, f); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ bucket := fx.srv.cfg.Releases
+ logged := &edKeyLog{Store: bucket}
+ fx.srv.cfg.Releases = logged
+ t.Cleanup(func() { fx.srv.cfg.Releases = bucket })
+
+ read := func(label string) usage.EdgeDay {
+ t.Helper()
+ body, _, err := fx.srv.usageDayReader(now).Get(ctx, key)
+ if err != nil {
+ t.Fatalf("%s: %v", label, err)
+ }
+ var day usage.EdgeDay
+ if err := json.Unmarshal(body, &day); err != nil {
+ t.Fatalf("%s: %v", label, err)
+ }
+ tally := day.Guard[usage.GuardKey(host, "bab", "shop")]
+ if len(tally.Hours) != 24 || len(tally.Groups) != 16 || tally.Hours["09"].Checked != 40 {
+ t.Fatalf("%s: guard tally has %d hours, %d groups, hour 09 %+v; want 24, 16, 40 checked",
+ label, len(tally.Hours), len(tally.Groups), tally.Hours["09"])
+ }
+ return day
+ }
+ read("first read")
+ if n, _ := fx.srv.usageDays.held(); n != 1 {
+ t.Fatalf("the cache holds %d entries after the first read, want the guard day", n)
+ }
+ reached := func() int {
+ n := 0
+ for _, s := range logged.seen {
+ if strings.HasSuffix(s, " "+key) {
+ n++
+ }
+ }
+ return n
+ }
+ first := reached()
+ if first != 1 {
+ t.Fatalf("the first read reached the bucket %d times for %s (%v), want once", first, key, logged.seen)
+ }
+ read("second read")
+ if got := reached(); got != first {
+ t.Errorf("the second read reached the bucket for %s (%v); want it answered from the cache", key, logged.seen)
+ }
+}