region labels: stored region and fleet facts from SQLite and rows in hand (plan 1 task 5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+386
−9
internal/console/fleets.go+1 −9internal/console/regionfacts.go+225 −0internal/console/regionfacts_test.go+149 −0internal/console/server.go+4 −0internal/console/view.go+7 −0
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 6eff34aff..ac69815f2 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -501,15 +501,7 @@ func (s *Server) appPlacement(reg consoleRegistry, acct *Account, app string) (s |
| if err != nil { |
| return "", err |
| } |
| - for _, rt := range routes { |
| - if rt.Account != acct.Sqid || rt.App != app { |
| - continue |
| - } |
| - if _, ok := registry.FleetPlacement(rt.Region); ok { |
| - return rt.Region, nil |
| - } |
| - } |
| - return "", nil |
| + return fleetFromRoutes(routes, acct.Sqid, app), nil |
| } |
| // placeInstancesFailed is placeAppTarget's answer when it cannot read the |
diff --git a/internal/console/regionfacts.go b/internal/console/regionfacts.go| new file mode 100644 |
| index 000000000..da4f2e642 |
| --- /dev/null |
| +++ b/internal/console/regionfacts.go |
| @@ -0,0 +1,225 @@ |
| +package console |
| + |
| +import ( |
| + "fmt" |
| + "log" |
| + "sort" |
| + |
| + "github.com/carlosframework/platform/internal/regionlabel" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// regionLabelCopy is every console string the region labels add (copy rows |
| +// RC-14, RC-15, RC-18 (a, c, d, e), RC-19, RC-20, RL-S1; the labels themselves, RC-1 to |
| +// RC-4b, live in internal/regionlabel). Change none without a copy review. |
| +type regionLabelCopyStrings struct { |
| + StoredIs string // RC-18a: label |
| + OnFleet string // RC-18d: fleet name (the Running tab's existing sentence) |
| + PooledStart string // RC-18c: label |
| + PooledCant string // RC-18e: label |
| + SourceChosen string // RC-19 |
| + SourceTZ string // RC-19 |
| + SourceIP string // RC-19 |
| + SourceDefault string // RC-19 |
| + ElsewhereOne string // RC-15: label, count |
| + ElsewhereMany string // RC-15: label, count |
| + CountIn string // RC-20: count, label |
| + CountJoin string // RC-20's joiner |
| + ChangeFleet string // RC-14 |
| + SeeRunning string // RL-S1 |
| +} |
| + |
| +var regionLabelCopy = regionLabelCopyStrings{ |
| + StoredIs: "This app's region is %s.", |
| + OnFleet: "This app runs on your fleet %s.", |
| + PooledStart: "New pooled instances start in %s.", |
| + PooledCant: "New pooled instances can't start in %s right now. Pass a region to place one elsewhere.", |
| + SourceChosen: "Chosen when the app was created.", |
| + SourceTZ: "From your timezone when the app was created.", |
| + SourceIP: "From your network location when the app was created.", |
| + SourceDefault: "The platform default.", |
| + ElsewhereOne: "also in %s: %d instance", |
| + ElsewhereMany: "also in %s: %d instances", |
| + CountIn: "%d in %s", |
| + CountJoin: ", ", |
| + ChangeFleet: "Change fleet", |
| + SeeRunning: "See Running for where new instances start.", |
| +} |
| + |
| +func (c regionLabelCopyStrings) all() []string { |
| + return []string{c.StoredIs, c.OnFleet, c.PooledStart, c.PooledCant, c.SourceChosen, c.SourceTZ, c.SourceIP, |
| + c.SourceDefault, c.ElsewhereOne, c.ElsewhereMany, c.CountIn, c.CountJoin, c.ChangeFleet, c.SeeRunning} |
| +} |
| + |
| +func (p page) RegionLabelCopy() regionLabelCopyStrings { return regionLabelCopy } |
| + |
| +// appRegionFacts is an app's stored region or fleet as the pages, /list and |
| +// the account app list show it (spec §3.4, labels 2 and R3-3): SQLite and |
| +// the registry rows a request already listed. Never appDefault, never a |
| +// store request. |
| +type appRegionFacts struct { |
| + Known bool |
| + Fleet string // the fleet's name; Label is then the fleet's ("fleet <name>") |
| + Label regionlabel.Label // the stored region's label, or the fleet's |
| + Source string |
| +} |
| + |
| +func (f appRegionFacts) Text() string { |
| + if !f.Known { |
| + return "" |
| + } |
| + return f.Label.Text |
| +} |
| + |
| +func (f appRegionFacts) Code() string { |
| + if !f.Known { |
| + return "" |
| + } |
| + return f.Label.Code |
| +} |
| + |
| +func (f appRegionFacts) Sentence() string { |
| + switch { |
| + case !f.Known: |
| + return "" |
| + case f.Fleet != "": |
| + return fmt.Sprintf(regionLabelCopy.OnFleet, f.Fleet) |
| + } |
| + return fmt.Sprintf(regionLabelCopy.StoredIs, f.Label.Text) |
| +} |
| + |
| +func (f appRegionFacts) SourceSentence() string { |
| + if !f.Known || f.Fleet != "" { |
| + return "" |
| + } |
| + switch f.Source { |
| + case "chosen": |
| + return regionLabelCopy.SourceChosen |
| + case "timezone": |
| + return regionLabelCopy.SourceTZ |
| + case "ip": |
| + return regionLabelCopy.SourceIP |
| + case "default": |
| + return regionLabelCopy.SourceDefault |
| + } |
| + return "" |
| +} |
| + |
| +// fleetFromRoutes is appPlacement's legacy fallback over rows already |
| +// listed: the first fleet placement among sqid/app's rows, compared on the |
| +// raw account column exactly as appPlacement always has (fleets.go). |
| +func fleetFromRoutes(routes []registry.Route, sqid, app string) string { |
| + for _, rt := range routes { |
| + if rt.Account != sqid || rt.App != app { |
| + continue |
| + } |
| + if _, ok := registry.FleetPlacement(rt.Region); ok { |
| + return rt.Region |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +// registryRoutes lists the console box's registry once for a request that |
| +// folds many apps (/list, the platform fleet page). ok is false when it |
| +// cannot be read, or when this console serves no registry (opening "" would |
| +// make a throwaway database and answer "no routes"). |
| +func (s *Server) registryRoutes() ([]registry.Route, bool) { |
| + if s.listRoutes != nil { |
| + routes, err := s.listRoutes() |
| + if err != nil { |
| + log.Printf("console: region labels: registry: %v", err) |
| + } |
| + return routes, err == nil |
| + } |
| + if s.cfg.Registry == "" { |
| + return nil, false |
| + } |
| + reg, err := openRegistry(s.cfg.Registry) |
| + if err != nil { |
| + log.Printf("console: region labels: open registry: %v", err) |
| + return nil, false |
| + } |
| + routes, err := reg.List() |
| + if err != nil { |
| + log.Printf("console: region labels: list registry: %v", err) |
| + return nil, false |
| + } |
| + return routes, true |
| +} |
| + |
| +// regionFactsFor is appPlacement's answer and the stored region, from the |
| +// store's columns and a route fleet the caller already folded from rows it |
| +// listed (routeFleet, with routesOK false when that listing failed). An |
| +// unread fact is unknown: never the home by default. |
| +func (s *Server) regionFactsFor(acct *Account, app, routeFleet string, routesOK bool) appRegionFacts { |
| + placed, err := s.store.AppPlacement(acct.ID, app) |
| + if err != nil { |
| + log.Printf("console: region labels %s/%s: placement: %v", acct.Sqid, app, err) |
| + return appRegionFacts{} |
| + } |
| + if placed == "" { |
| + if !routesOK { |
| + return appRegionFacts{} |
| + } |
| + placed = routeFleet |
| + } |
| + if name, ok := registry.FleetPlacement(placed); ok { |
| + return appRegionFacts{Known: true, Fleet: name, Label: regionlabel.ForCode(placed, s.regionHome(), s.regionNames())} |
| + } |
| + region, source, err := s.appRegion(acct.ID, app) |
| + if err != nil { |
| + log.Printf("console: region labels %s/%s: region: %v", acct.Sqid, app, err) |
| + return appRegionFacts{} |
| + } |
| + return appRegionFacts{Known: true, Label: regionlabel.ForCode(region, s.regionHome(), s.regionNames()), Source: source} |
| +} |
| + |
| +// regionCount is how many of an app's instances one label covers. |
| +type regionCount struct { |
| + Text string |
| + N int |
| +} |
| + |
| +// Line is RC-15. |
| +func (c regionCount) Line() string { |
| + if c.N == 1 { |
| + return fmt.Sprintf(regionLabelCopy.ElsewhereOne, c.Text, c.N) |
| + } |
| + return fmt.Sprintf(regionLabelCopy.ElsewhereMany, c.Text, c.N) |
| +} |
| + |
| +// InLine is one element of RC-20. |
| +func (c regionCount) InLine() string { return fmt.Sprintf(regionLabelCopy.CountIn, c.N, c.Text) } |
| + |
| +// elsewhereCounts is RC-15's counts (spec §3.4, labels 3): the viewed |
| +// account's non-platform rows whose label is not the stored region's, |
| +// grouped by label, in label order. A row served everywhere is not |
| +// elsewhere. Nothing is said when the stored region is unknown. |
| +func elsewhereCounts(routes []RouteView, sqid string, stored appRegionFacts) []regionCount { |
| + if !stored.Known { |
| + return nil |
| + } |
| + home := stored.Text() |
| + by := map[string]int{} |
| + for _, rt := range routes { |
| + if rt.Account != sqid || rt.Platform || rt.Region.Everywhere || rt.Region.Text == "" || rt.Region.Text == home { |
| + continue |
| + } |
| + by[rt.Region.Text]++ |
| + } |
| + return sortedCounts(by) |
| +} |
| + |
| +// sortedCounts orders label counts by label. |
| +func sortedCounts(by map[string]int) []regionCount { |
| + if len(by) == 0 { |
| + return nil |
| + } |
| + out := make([]regionCount, 0, len(by)) |
| + for text, n := range by { |
| + out = append(out, regionCount{Text: text, N: n}) |
| + } |
| + sort.Slice(out, func(i, j int) bool { return out[i].Text < out[j].Text }) |
| + return out |
| +} |
diff --git a/internal/console/regionfacts_test.go b/internal/console/regionfacts_test.go| new file mode 100644 |
| index 000000000..bd3cd14f1 |
| --- /dev/null |
| +++ b/internal/console/regionfacts_test.go |
| @@ -0,0 +1,149 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/regionlabel" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestRegionFactsByCase is §10's §3.4 list: an unpooled app, a pooled app |
| +// storing uk, a fleet app, a legacy route-only fleet app, an unread |
| +// registry. All of it from SQLite and rows in hand: zero store calls. |
| +func TestRegionFactsByCase(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.HomeRegion = "ie" |
| + putRegionEdges(t, fx.rel, "box-ie", "", "eu-west-1") |
| + putRegionEdges(t, fx.rel, "box-uk", "uk", "eu-west-2") |
| + fx.srv.refreshRegionNames(context.Background(), nil, time.Now()) |
| + cs := &opCountStore{Store: fx.rel} // Task 4: every Store method counted |
| + fx.srv.cfg.Releases = cs |
| + now := time.Now() |
| + acct := &Account{ID: OpsAccountID, Sqid: fx.sqid} |
| + mustCreate := func(app, region, source string) { |
| + if _, err := fx.st.CreateAppInRegion(OpsAccountID, app, "paul@keymail.dev", region, source, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + mustCreate("shop", "", "default") |
| + mustCreate("blog", "uk", "timezone") // a pooled manifest would not change this: the manifest is never read |
| + mustCreate("cart", "", "chosen") |
| + if err := fx.st.SetAppPlacement(OpsAccountID, "cart", "fleet/private"); err != nil { |
| + t.Fatal(err) |
| + } |
| + mustCreate("legacy", "", "") |
| + legacyRows := []registry.Route{{Host: "legacy.shop.example.com", Kind: "instance", App: "legacy", Account: fx.sqid, Region: "fleet/private"}} |
| + |
| + for _, c := range []struct { |
| + app string |
| + routes []registry.Route |
| + routesOK bool |
| + text, sentence string |
| + source string |
| + }{ |
| + {"shop", nil, true, "Dublin (ie)", "This app's region is Dublin (ie).", "The platform default."}, |
| + {"blog", nil, true, "London (uk)", "This app's region is London (uk).", "From your timezone when the app was created."}, |
| + {"cart", nil, true, "fleet private", "This app runs on your fleet private.", ""}, |
| + {"legacy", legacyRows, true, "fleet private", "This app runs on your fleet private.", ""}, |
| + {"legacy", nil, false, "", "", ""}, |
| + {"cart", nil, false, "fleet private", "This app runs on your fleet private.", ""}, // the store's placement needs no registry |
| + } { |
| + f := fx.srv.regionFactsFor(acct, c.app, fleetFromRoutes(c.routes, fx.sqid, c.app), c.routesOK) |
| + if f.Text() != c.text || f.Sentence() != c.sentence || f.SourceSentence() != c.source { |
| + t.Errorf("%s (registry read %v): %q / %q / %q, want %q / %q / %q", c.app, c.routesOK, |
| + f.Text(), f.Sentence(), f.SourceSentence(), c.text, c.sentence, c.source) |
| + } |
| + } |
| + if n := cs.total(); n != 0 { |
| + t.Fatalf("the stored-region facts made %d store calls, want 0", n) |
| + } |
| +} |
| + |
| +// TestFleetFromRoutesIsAppPlacementsFallback pins appPlacement's legacy |
| +// route fallback (TestAppPlacementFallsBackToRouteWhenStoreEmpty's shape, |
| +// with neutral names) to the shared function, and its account and app scope. |
| +func TestFleetFromRoutesIsAppPlacementsFallback(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + if _, err := fx.st.CreateAppIn(OpsAccountID, "legacy", "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openTestReg(t, fx) |
| + for _, rt := range []registry.Route{ |
| + {Host: "other.shop.example.com", Kind: "instance", App: "legacy", Account: "bac", Region: "fleet/theirs"}, |
| + {Host: "sibling.shop.example.com", Kind: "instance", App: "shop", Account: fx.sqid, Region: "fleet/wrong"}, |
| + {Host: "legacy.shop.example.com", Kind: "instance", App: "legacy", Account: fx.sqid, Region: "fleet/private"}, |
| + } { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + got, err := fx.srv.appPlacement(reg, &Account{ID: OpsAccountID, Sqid: fx.sqid}, "legacy") |
| + if err != nil || got != "fleet/private" { |
| + t.Fatalf("appPlacement = %q %v, want fleet/private", got, err) |
| + } |
| + routes, _ := reg.List() |
| + if got := fleetFromRoutes(routes, fx.sqid, "legacy"); got != "fleet/private" { |
| + t.Fatalf("fleetFromRoutes = %q", got) |
| + } |
| +} |
| + |
| +func TestRegistryRoutesSeamAndFailure(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.listRoutes = func() ([]registry.Route, error) { return nil, errors.New("registry unreadable") } |
| + if _, ok := fx.srv.registryRoutes(); ok { |
| + t.Fatal("an unreadable registry reads as read") |
| + } |
| + fx.srv.listRoutes = nil |
| + fx.srv.cfg.Registry = "" |
| + if _, ok := fx.srv.registryRoutes(); ok { |
| + t.Fatal("a console with no registry reads as read (and would have made a temp database)") |
| + } |
| +} |
| + |
| +func TestElsewhereCounts(t *testing.T) { |
| + n := regionlabel.NamesFrom(edgesSnapOf("ie:eu-west-1", "uk:eu-west-2", "au:ap-southeast-2"), "ie") |
| + row := func(acct, code string, everywhere bool) RouteView { |
| + l := regionlabel.ForCode(code, "ie", n) |
| + if everywhere { |
| + l.Everywhere, l.Text = true, "every region" |
| + } |
| + return RouteView{Account: acct, Region: l} |
| + } |
| + stored := appRegionFacts{Known: true, Label: regionlabel.ForCode("", "ie", n)} |
| + routes := []RouteView{ |
| + row("bab", "", false), row("bab", "uk", false), row("bab", "uk", false), row("bab", "au", false), |
| + row("bab", "uk", true), // served everywhere: not "elsewhere" |
| + row("bac", "uk", false), // another account's row: never counted |
| + } |
| + got := elsewhereCounts(routes, "bab", stored) |
| + if len(got) != 2 || got[0].Line() != "also in London (uk): 2 instances" || got[1].Line() != "also in Sydney (au): 1 instance" { |
| + t.Fatalf("elsewhere = %+v", got) |
| + } |
| + if elsewhereCounts(routes, "bab", appRegionFacts{}) != nil { |
| + t.Fatal("an unknown stored region has no elsewhere") |
| + } |
| +} |
| + |
| +// edgesSnapOf builds a snapshot from "code:aws" pairs, the home's code |
| +// written as its name. |
| +func edgesSnapOf(pairs ...string) edgesSnapshot { |
| + var s edgesSnapshot |
| + s.Complete = true |
| + for i, p := range pairs { |
| + code, aws, _ := strings.Cut(p, ":") |
| + s.Files = append(s.Files, edgesFilePlacementView{V: 1, Box: "box-" + string(rune('a'+i)), Placement: code, Region: aws}) |
| + } |
| + return s |
| +} |
| + |
| +func TestRegionLabelCopyHasNoEmDash(t *testing.T) { |
| + for _, s := range regionLabelCopy.all() { |
| + if strings.ContainsRune(s, '—') { |
| + t.Errorf("em dash in %q", s) |
| + } |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index 9f70f6dee..b82e24a37 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -954,6 +954,10 @@ type Server struct { |
| // competing insert landing after the region was resolved. |
| appRegionOf func(accountID int64, app string) (string, string, error) |
| beforeClaimInsert func() |
| + // listRoutes is registryRoutes' TEST SEAM (nil in production): a |
| + // test counts how often a request lists the registry, or makes the |
| + // listing fail. |
| + listRoutes func() ([]registry.Route, error) |
| // claims folds the deployment's domain claims for the read paths — the |
| // Settings render and the domains list door — reading only the claims |
| // whose bucket stamp moved since the last fold (#173). Process-lifetime |
diff --git a/internal/console/view.go b/internal/console/view.go| index 90544da62..1258ac62b 100644 |
| --- a/internal/console/view.go |
| +++ b/internal/console/view.go |
| @@ -20,6 +20,7 @@ import ( |
| "golang.org/x/net/publicsuffix" |
| "github.com/carlosframework/platform/internal/perf" |
| + "github.com/carlosframework/platform/internal/regionlabel" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/status" |
| @@ -365,6 +366,12 @@ type RouteView struct { |
| // Provisioned mirrors registry.Route.Provisioned: minted from a bucket |
| // instance record, so the activator runs its process by construction. |
| Provisioned bool |
| + // Region is where this row runs, as the labels show it (region labels |
| + // spec §3.1): from the instance record when the row has one, else from |
| + // the registry row. Moving is the record's move phrase (§3.3), "" with |
| + // no move. Both are zero on a view assembled without a remote source. |
| + Region regionlabel.Label |
| + Moving string |
| // remote marks a row built from a bucket instance record whose route is |
| // on another box (remoteinstances.go), not from the console box's own |