Repository navigation
fix(mentions): precompute the Perception tab's default view so it opens fast cold - #1385
Conversation
…ns fast cold On prod the Perception tab's first load took 8.7 s at 30 days (past the 8 s statement timeout) and 8.1 s at 90: 00237's rollup read is fast warm but not cold. 00241 keeps the default view (7, 30 or 90 days, no filters) per brand in mentions_perception_snapshots, refreshed after the brand's Mentions rollups (completed runs, the nightly sweep, the deleted-prompt rebuild, the backfills), and mentions_perception reads it in one lookup; filtered views read live as before. Competitor or brand renames and removals set the snapshots aside. The health e-mail also times the live read, and the tab retries a failed read once and says when a read is slow. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk Urgent, top of our queue: Perception's first load is 8.7 s → error at 30 days on prod again (details and my prod questions on #1325). This precomputes the default view (00241, independent of the open content stack, which uses 00240). Deploy order in the body: apply 00241, deploy the server, then please run |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. Two small follow-ups and one cosmetic fix, none of them blocking:
-
A failed refresh keeps serving the previous snapshot. It doesn't fall back to the live read.
server/src/lib/mentions-rollups.js:66says "the tab reads the rollups live meanwhile", andserver/src/lib/insights-rollups.js:189-190says a failed refresh "leaves the tab on the live read". Butmentions_perception(00241_mentions_perception_snapshots.sql:339-346) returns any snapshot whoseday_tois today or yesterday. So when a run's rollups land and the snapshot refresh after them fails, the default view keeps showing the pre-run figures until the next run or the 02:00 sweep. A filtered view reads the newer rollups during that time. The same happens after any rollup rewrite that doesn't go through the server (a manual backfill in SQL). Pick one:- Set the brand's snapshots aside wherever the rollups change.
refresh_mention_terms_daily(00231) could run the sameupdate … set day_to = date '1970-01-01', computed_at = clock_timestamp() where brand_id = p_brand_idasforget_mentions_perception_snapshots, in its own transaction. Then a snapshot is never older than the rollups. Thecomputed_atguard already keeps a refresh that started earlier from writing over it. Cheap to do now, while 00241 isn't applied. - Or keep the behaviour and correct the two comments (and the PR text) to say the last snapshot stays until the next refresh.
- Set the brand's snapshots aside wherever the rollups change.
-
The drain refreshes the snapshots even when nothing was rebuilt. At
server/src/lib/insights-rollups.js:135-136,refreshPerceptionSnapshotsruns even when the Insights refresh failed andrefreshMentionTermsDailynever ran. The entry stays queued, so a brand whose rebuild keeps failing recomputes its three windows every 5 minutes. On the largest brand that is about 16 s of cold reads each time. Only refresh after the terms refresh has run:if (ok) { ok = await refreshMentionTermsDaily(brandId, dayFrom, dayTo); await refreshPerceptionSnapshots(brandId); }
The "Perception rollups fail to rebuild" test (
insights-rollups.test.js:280) still holds. Add an assertion to "keeps the entry for the next drain when a refresh fails" (:268) that b1's snapshots weren't refreshed. -
Cosmetic: the slow note can show too early.
slow(_perception.tsx:61) is never cleared. If you go back to a filter set whose earlier read was slow,isSlow(:89) is true at once, so the note shows over the dimmed figures beforeSLOW_MS. Clear it when the read's effect ends, e.g.setSlow((s) => (s === `${key}#${attempt}` ? null : s))in the cleanup.
Checked on production, as authenticated (a member of the largest brand's organization). Each check ran in a rolled-back block against pg_temp copies of the new table and functions:
- Same output: the snapshot equals the current
mentions_perceptionoutput (jsonb equality) at 7, 30 and 90 days. It also matches, and still comes from the snapshot, when the request's start is 50 minutes before or 20 hours after now − N days (client clock skew). A start 2 hours early is a 31-day window and reads live, as it does today. - Live paths unchanged: a platform filter and an explicit end read live with identical output. A user from another organization gets null.
- Timings: the current read took 3.49 s (30 days, first read), 2.58 s (90 days) and 0.25 s (7 days). The snapshot read takes about 1 ms. Building the three windows took 0.25 s, 1.30 s and 1.78 s warm, and 15.8 s for all three cold. All 229 brands took 68 s in total, cold. Each payload is about 2.3 KB.
- Deploy order: fill the snapshots only after the server with
refreshPerceptionSnapshotsis live. A snapshot filled before that is served until the end of the next day, and no run refreshes it in the meantime.
gkhngyk
left a comment
There was a problem hiding this comment.
Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).
…ps (#1392) Follow-ups from #1385's review: - 00246: refresh_mention_terms_daily sets the brand's snapshots aside at its end, in the rebuild's transaction, so the default view reads the new rollups live until the snapshots are refreshed, even when that refresh fails or the rollups were rebuilt in SQL. - The deleted-prompt drain refreshes the snapshots only after the terms refresh has run. - The slow note is cleared when its read's effect ends, so going back to filters whose last read was slow doesn't show it before SLOW_MS. Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Gökhan Geyik <[email protected]>
Summary
The Perception tab's default view (7, 30 or 90 days, every platform and region, no topic or search) is now precomputed per brand and read in one lookup. A slow or failed read retries once and says it is still reading.
On prod today (2026-10-23, Ansvisor brand, read-only):
00237's rollup read is fast warm (0.95 s / 1.36 s as you timed it), but a cold read of the rollups' pages isn't. The target is 2–3 s on a cold load. My questions about prod (00237 live, rollups filled, EXPLAIN, nightly overlap, this morning's probe timings) are on #1325.
Migration 00241
mentions_perception_snapshots(brand_id, days ∈ {7,30,90}, day_to, payload, computed_at), primary key(brand_id, days). RLS is on, service role only.mentions_perception_liveis 00237's read word for word, minus the access check. It is service role only.mentions_perceptionkeeps the access check. It returns the brand's snapshot when the request is the default view:p_date_to, and 7, 30 or 90 whole days;day_tois today or yesterday (UTC).Anything else reads live, as before. A snapshot from yesterday shows its own window, ending yesterday; the day's runs refresh it.
refresh_mentions_perception_snapshot(brand, days)upserts one window per call, so the largest brand's 90 days stay well inside the service role's timeout. A slower refresh that started earlier never writes over a newer row (where computed_at < excluded.computed_at).Triggers on
competitors(delete, update of name / brand_id) andbrands(update of name) set the brand's snapshots aside:day_to1970, stampedclock_timestamp().health_probe(00239's body) gainsmentions_perception_live: an explicit end skips the snapshot, so it times what a filtered view still costs.Server
refreshPerceptionSnapshots(brandId)(mentions-rollups.js) makes 3 RPC calls. It runs once a brand's Mentions rollups have been refreshed, whatever the outcome, so it follows them as they now are:refreshForCompletedRun), not awaited, so the run's stamp doesn't wait;PGRST202(before 00241 is applied) is quiet.mentions_perception: what the tab opens;mentions_perception_live: the filtered read.Web (
mentions/_perception.tsx)role="status"paragraph is always in the page, so screen readers announce the words.Verified on the dev DB (demo brand, rows 2026-09-09..10-16):
windowkey aside, since it moves with now()).authenticatedhas no access to the table.Note on the health e-mail: the new
mentions_perception_liveprobes time the filtered path cold. They will likely read yellow or red on the Ansvisor brand (it's the 8 s read). That is the honest cost of a filtered view. A covering index on the rollups, like 00239's, would be the next step for filtered views if it matters.Known trade-off: between 00:00 UTC and a brand's first run or the morning sweep, the default view shows the snapshot computed yesterday, whose window ends yesterday. The web doesn't print the window's dates; MCP returns them in
window.Deploy order
select refresh_mentions_perception_snapshot('<brand>', d) from unnest(array[7,30,90]) d;Related issue
Page speed queue item 5 (Perception regression, 2026-10-23). Follows #1325 (00231/00237) and #1360 (00239 probes). No issue.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
select count(*) from mentions_perception_snapshots where brand_id = …is 0 until the next refresh.Tests:
mentions-rollups.test.js,insights-rollups.test.js,health/report.test.js;_perception.test.tsx(the retry, the slow note, Try again after two failures).Checked in the browser at 1024 px, in light and dark themes.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) (0 errors; the 6 warnings are in untouched files, as on main)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
yarn test(2,815 tests) andyarn build;npm run format,npm run lintandnpm test(2,050 tests);bash supabase/build-schema.sh.🤖 Generated with Claude Code