Skip to content

fix(insights): anchor the 24h view to the last completed tracking run - #578

Merged
gkhngyk merged 2 commits into
mainfrom
fix/insights-24h-stable-window
Aug 2, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/insights-24h-stable-window

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Every morning while the daily run streamed in, the Insights 24h view briefly showed "no results for the last 24 hrs" and then a shrinking tracked-prompt count: the rolling [now − 24h, now] window was dropping yesterday's results second by second while today's arrived over ~an hour.

This PR anchors the 24h view to the last completed tracking run:

  • 00044 migration — tracking_runs ledger (brand_id, source, started_at, completed_at, result_count) with member-select RLS, plus a synthetic backfilled run per brand so the fix applies immediately. Already applied to the hosted DB. Reads prompt_results only — no existing data is modified and nothing is re-scraped.
  • Worker stamping — full runs (cron or manual Run All) insert a ledger row up front and stamp completed_at after the drain loop finishes; by then the run's results are all inserted, so no webhook-side bookkeeping is needed. Zero-result runs delete their row (the anchor never moves onto an empty window); single/subset-prompt runs never stamp.
  • Insights 24h window — resolved from the ledger as (previous completed run, latest completed run]: always full, frozen between runs, and swapped atomically when the next run completes. Brands with no completed run yet (fresh onboarding) keep the live rolling window, where regression is impossible because nothing is older than 24h. Other presets are untouched.
  • ServerRunBanner — cron/teammate-started runs now show an in-progress banner (the old banner only knew about jobs started from that browser via localStorage, which is why users saw nothing during the morning run). A 60s ledger poll reloads the page silently the moment the active run completes.
  • Trend chart / leaderboard consistency — the chart's top-4 competitor cap now ranks by the last point's value (equal to the leaderboard's window score by construction), so both surfaces always show the same brands; on short ranges the old period-average pick could drop a competitor the leaderboard ranked 2nd.

Related issue

Fixes the customer-reported morning flash/regression on the Insights 24h view (no tracked issue).

Type of change

  • fix — Bug fix

How to test

  1. On a brand with tracking history, open Insights with the 24h preset — the KPI cards, trend chart, leaderboard, and SOV all reflect the latest completed run, at any time of day.
  2. Trigger a full run (or wait for the daily cron): a "tracking in progress" banner appears in every browser, the numbers stay frozen on the previous run, and the page refreshes itself to the new run's numbers when the drain finishes — no empty state, no shrinking counts.
  3. A brand created fresh (no completed run) still streams results live during its first run.
  4. The trend chart's plotted competitors match the leaderboard's top rows exactly.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

gkhngyk added 2 commits August 2, 2026 15:29
Every morning the rolling [now-24h, now] window emptied and refilled
while the daily run streamed in: users saw a 'no results for the last
24 hrs' flash, then the tracked-prompt count shrinking as yesterday's
results aged out one by one.

- 00044 migration: tracking_runs ledger (brand_id, source, started_at,
  completed_at, result_count) + RLS member select + a synthetic
  backfilled run per brand so the fix applies immediately
- worker: FULL runs (cron or manual Run All) insert a ledger row up
  front and stamp completed_at after the drain loop finishes — by then
  this run's results are all inserted, so no webhook-side bookkeeping.
  Zero-result runs delete their row instead of moving the anchor onto
  an empty window; single/subset-prompt runs never stamp
- insights 24h preset resolves its window from the ledger:
  (previous completed run, latest completed run] — always full, frozen
  between runs, swapped atomically when the next run completes. Brands
  with no completed run yet (fresh onboarding) keep the live rolling
  window, where regression is impossible
- new ServerRunBanner: cron/teammate-started runs now show an
  in-progress banner (the old one only knew jobs this browser started
  via localStorage); a 60s ledger poll also reloads the page silently
  the moment the active run completes
…oard shows

The chart capped competitors by period-average of the daily points while
the leaderboard sorts by the window score, so on short ranges (24h) a
competitor could rank 2nd on the leaderboard yet be missing from the
chart. Rank by the last point's value instead — the rolling window makes
that equal the leaderboard's window score, so both surfaces always show
the same brands (average kept only as tie-break).
@gkhngyk
gkhngyk merged commit af42eee into main Aug 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant