carlos / platform Public

console: analytics on-ramp review fixes: range picker stays when off; new-account analytics start is not best-effort

The Analytics tab keeps its range picker when analytics is off, so data
counted before it was switched off stays reachable. A new account's
default-on analytics setting is written after the manifest and before the
membership, and a failure fails the creation like any other post-manifest
failure instead of leaving the account on with analytics off forever.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 820e139641a474207f6d643769dc1801e947c3ba parent 9bb1127
4 files changed, +90 −17
  • internal/console/analytics_onramp_test.go +70 −2
  • internal/console/analyticsonramp.go +8 −9
  • internal/console/server.go +9 −1
  • internal/console/templates/app_analytics.html +3 −5

CI passed — run details

diff --git a/internal/console/analytics_onramp_test.go b/internal/console/analytics_onramp_test.go
index cdd450bcb..523e17858 100644
--- a/internal/console/analytics_onramp_test.go
+++ b/internal/console/analytics_onramp_test.go
@@ -7,11 +7,14 @@ package console
import (
"context"
"encoding/json"
+ "errors"
"net/http"
"net/url"
"strings"
"testing"
+ "time"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/telemetry"
)
@@ -45,9 +48,9 @@ func TestAnalyticsTabOffStateOwner(t *testing.T) {
}
}
// It no longer sends the owner to Settings (AN-C05), and an app that
- // never counted shows no empty tables, chart or range picker.
+ // never counted shows no empty tables or chart (the range picker stays).
for _, gone := range []string{"settings/errors-analytics", "Turn it on for this app in", onrampMemberOff,
- `role="img" aria-label="Page views"`, `<form method="get" class="toolbar">`} {
+ `role="img" aria-label="Page views"`} {
if strings.Contains(body, gone) {
t.Errorf("off state (owner) still shows %q", gone)
}
@@ -132,6 +135,34 @@ func TestAnalyticsTabOffStateMember(t *testing.T) {
}
// Analytics off but counted earlier: the numbers stay, under the off state.
+// Counted yesterday, switched off, nothing today: the default tab still has
+// the range picker, and the wider window shows the counts.
+func TestAnalyticsTabOffStateKeepsRangePickerForOlderData(t *testing.T) {
+ fx, _ := instanceFixture(t, "code-onramp-tab-older")
+ seedAnalytics(t, fx, seedTime().Add(-24*time.Hour))
+ fx.setUser("paul@keymail.dev")
+ c := newBrowser(t)
+ signUp(t, fx, c, "code-onramp-tab-older-browser")
+ _, _, body := get(t, c, fx.u("/apps/titogo/analytics"))
+ for _, want := range []string{onrampOffLead, onrampTurnOn, `<form method="get" class="toolbar">`, `<option value="7d">`} {
+ if !strings.Contains(body, want) {
+ t.Errorf("off, data only before today: default tab missing %q", want)
+ }
+ }
+ for _, gone := range onrampDataMarkers {
+ if strings.Contains(body, gone) {
+ t.Errorf("off, nothing today: default tab shows the data section %q", gone)
+ }
+ }
+ _, _, body = get(t, c, fx.u("/apps/titogo/analytics?range=7d"))
+ for _, want := range []string{onrampOffLead, `<form method="get" class="toolbar">`, `<option value="7d" selected>`,
+ "<th>Page views</th>", "<td>5</td>", "/pricing"} {
+ if !strings.Contains(body, want) {
+ t.Errorf("?range=7d with older data: missing %q", want)
+ }
+ }
+}
+
func TestAnalyticsTabOffStateKeepsCountedData(t *testing.T) {
fx, _ := instanceFixture(t, "code-onramp-tab-data")
seedAnalytics(t, fx, seedTime())
@@ -393,6 +424,43 @@ func TestNewAccountStartsWithAnalyticsOn(t *testing.T) {
}
}
+// failTelemetryStore refuses every write of an account's telemetry object.
+type failTelemetryStore struct{ release.Store }
+
+func (f failTelemetryStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) {
+ if strings.HasPrefix(key, "accounts/") && strings.HasSuffix(key, "/telemetry.json") {
+ return "", errors.New("telemetry put refused")
+ }
+ return f.Store.Put(ctx, key, body, ifMatch)
+}
+
+// A new account whose analytics setting cannot be written is not reported as
+// created: the same failure answer as any other post-manifest failure, and no
+// membership is published. Existing accounts are untouched.
+func TestNewAccountFailsWhenAnalyticsCannotBeStarted(t *testing.T) {
+ fx, _ := instanceFixture(t, "code-onramp-account-fail")
+ fx.setUser("paul@keymail.dev")
+ c := newBrowser(t)
+ signUp(t, fx, c, "code-onramp-account-fail-owner")
+ before, err := fx.st.AccountsFor("paul@keymail.dev")
+ if err != nil {
+ t.Fatal(err)
+ }
+ fx.srv.cfg.Releases = failTelemetryStore{fx.rel}
+ form := url.Values{"csrf": {sessionToken(t, fx, c)}, "name": {"second"}}
+ status, loc, _ := postForm(t, c, fx.ts.URL+"/accounts/new", form)
+ if status != http.StatusInternalServerError || strings.Contains(loc, "/a/") {
+ t.Fatalf("create account = %d -> %q, want the failure answer", status, loc)
+ }
+ after, err := fx.st.AccountsFor("paul@keymail.dev")
+ if err != nil || len(after) != len(before) {
+ t.Fatalf("accounts after the failed create = %d, want %d (%v)", len(after), len(before), err)
+ }
+ if _, etag, _ := telemetry.Read(context.Background(), fx.rel, telemetry.AccountKey(fx.sqid)); etag != "" {
+ t.Error("the existing account gained a telemetry object")
+ }
+}
+
// The CLI door: the same rules, with the choice echoed back.
func TestAPICreateAppAnalytics(t *testing.T) {
fx, member := instanceFixture(t, "code-onramp-api")
diff --git a/internal/console/analyticsonramp.go b/internal/console/analyticsonramp.go
index 871fc4b92..42073a436 100644
--- a/internal/console/analyticsonramp.go
+++ b/internal/console/analyticsonramp.go
@@ -117,16 +117,15 @@ func (s *Server) newAppAnalytics(ctx context.Context, sqid string) *newAppAnalyt
// startNewAccountAnalytics writes analytics on as a new account's own
// setting (plan item 3): new accounts count from the start, and an owner can
// turn it off in the account's Settings. Accounts that already exist have no
-// such object written, so they keep the built-in default. Best-effort: an
-// account that could not take the setting still exists and simply starts
-// with analytics off, which is how every account began before.
-func (s *Server) startNewAccountAnalytics(ctx context.Context, sqid string) {
+// such object written, so they keep the built-in default. A failure is
+// returned: mintAccount runs this before the account's membership exists and
+// fails the creation, so no account is left looking created with analytics
+// silently off.
+func (s *Server) startNewAccountAnalytics(ctx context.Context, sqid string) error {
if s.cfg.Releases == nil {
- return
+ return nil
}
- if err := telemetry.Update(ctx, s.cfg.Releases, telemetry.AccountKey(sqid), func(st *telemetry.Settings) {
+ return telemetry.Update(ctx, s.cfg.Releases, telemetry.AccountKey(sqid), func(st *telemetry.Settings) {
mergeAnalytics(st, telemetry.CountEdge, "")
- }); err != nil {
- log.Printf("console: new account %s: could not turn analytics on: %v", sqid, err)
- }
+ })
}
diff --git a/internal/console/server.go b/internal/console/server.go
index dcb850d60..8fb24ff7b 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -2676,6 +2676,15 @@ func (s *Server) mintAccount(ctx context.Context, name, ownerAddr string) (*Acco
if err != nil {
return nil, err
}
+ // The default-on analytics setting is written after the manifest and
+ // before the membership is published, so an account that exists for its
+ // owner always has it. A failure is handled like a failed CreateAccount
+ // below: the account is stranded and the caller reports the outage.
+ if err := s.startNewAccountAnalytics(ctx, sqid); err != nil {
+ log.Printf("console: minted account %s (id %d, %q) into the bucket but could not turn analytics on: "+
+ "%v — accounts/%s/account.json is stranded with no owner", sqid, id, name, err, sqid)
+ return nil, err
+ }
if err := s.store.CreateAccount(id, sqid, name, ownerAddr, now); err != nil {
// The manifest already landed, so the id is claimed and there is now an
// account in the bucket with no console row and no owner. It is
@@ -2692,7 +2701,6 @@ func (s *Server) mintAccount(ctx context.Context, name, ownerAddr string) (*Acco
if err := s.store.SetLastAccount(ownerAddr, id); err != nil {
log.Printf("console: set last account for %s: %v", ownerAddr, err)
}
- s.startNewAccountAnalytics(ctx, sqid)
return &Account{ID: id, Sqid: sqid, Name: name, Role: RoleOwner, CreatedAt: now}, nil
}
diff --git a/internal/console/templates/app_analytics.html b/internal/console/templates/app_analytics.html
index 54eaeea12..712ee6623 100644
--- a/internal/console/templates/app_analytics.html
+++ b/internal/console/templates/app_analytics.html
@@ -21,10 +21,9 @@ When analytics is on, the edge counts HTML page views and daily visitors. No Jav
<p class="small">An account owner can turn on analytics.</p>
{{end}}
{{end}}
-{{/* Off with nothing counted in the window: no empty tables, chart or
- "Nothing yet" rows. Numbers counted while it was on still show, and a
- range other than today keeps its picker so older days stay reachable. */}}
-{{if or .AnalyticsWin.On .AnalyticsWin.HasData (ne .AnalyticsRange "today")}}
+{{/* The range picker always shows: numbers counted while analytics was on
+ stay reachable after it is switched off. With nothing counted in the
+ window there are no empty tables, chart or "Nothing yet" rows. */}}
<form method="get" class="toolbar">
<label>range
<select name="range">
@@ -35,7 +34,6 @@ When analytics is on, the edge counts HTML page views and daily visitors. No Jav
</label>
<button type="submit" class="btn-sm">Update</button>
</form>
-{{end}}
{{if or .AnalyticsWin.On .AnalyticsWin.HasData}}
{{with .AnalyticsWin}}
<div class="tablewrap"><table>