Skip to content

fix(traffic): read Search Console pages a week at a time, and resume their backfill - #1253

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/gsc-pages-weekly-resume
Oct 20, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/gsc-pages-weekly-resume

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from #1177's review (non-blocking notes):

  • "each 28-day [page, date] pull stops at 25,000 rows ordered by clicks, so on a large site the zero-click page-days are what's dropped … Pulling a day or a week at a time spreads the cap evenly."
  • "Restart rather than resume: the backfill restarts from the newest window. A window that always fails would re-read all seven every day."

(The third note, an index for the prune's date < …, is already there: gsc_page_stats_date_idx in 00181.)

A week at a time (server/src/lib/gsc-sync.js)

  • Page pulls use PAGE_WINDOW_DAYS = 7.
  • The last 28 days are five pulls; the backfill goes back to 190 days a week at a time.
  • The row cap applies per week, so a large site loses its zero-click page-days evenly instead of on the older days of each 28-day pull.
  • The query sync is unchanged.

Resume the backfill (migration 00201, brands.gsc_pages_backfill_from date)

  • gsc_pages_backfill_from is the oldest day the property's backfill has read so far, kept after each window. It is null once the backfill is through.
  • A sync cut short resumes after its last window the next day, so a window that keeps failing is the only one tried again.
  • gsc_pages_backfilled_property now names the property the page rows belong to. It is set once the property's last 28 days are in, so a first sync that stops before that starts over without leaving a gap.
  • Compatible: a brand backfilled before this PR (property set, the new column null) reads as done. A new property still drops the old one's page rows first.
  • I walked through the days: the recent windows cover every day back to 28 days ago each day, and the backfill resumes the day after the stored date, so no day is skipped.

API calls: a daily sync makes about five page calls instead of one. A first backfill makes about 24 small ones instead of seven. Each is one page of up to 5,000 rows unless a week is larger.

Deploy: apply 00201 before the server, since the brand query selects the new column. As with 00181, migrations are applied at merge.

Numbering: main has 00196, 00198 and 00199. Open PRs: 00197 #1243, 00200 #1247, plus 00190/00191/00194/00195. schema.sql here is main at 8ee1044 plus 00201.

Related issue

Follow-up to #1177.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Unit tests

  • gsc-sync.test.js:
    • pageWindows covers the range a week at a time with no gap;
    • pagesSyncPlan covers a new property, done, resuming from a stored day, reaching 190 days, and a changed property.
  • gsc-sync-pages.test.js (new) runs runGscSync over mocked Composio and Supabase:
    • it reads five recent weeks, then the backfill, and marks it through; the next day it reads the five recent weeks only;
    • when the fourth backfill window fails, the next day starts at that window and reads nothing read before;
    • a first sync that stops in the recent days marks nothing.
  • All three fail on the old code.

Dev DB: 00201 applied; brands.gsc_pages_backfill_from exists. There is no Search Console connection locally, so a real-account run would be welcome.

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

Also: server npm run lint, npm test (1902); web yarn test (1796) and yarn build (only types/supabase.ts changed there); bash supabase/build-schema.sh.

🤖 Generated with Claude Code

…their backfill

Each 28-day [page, date] pull stopped at the 25,000-row cap ordered by
clicks, so on a large site the dropped rows were zero-click page-days,
and recent days, re-read daily, kept more of them than older days read
once. The page sync now pulls a week at a time (PAGE_WINDOW_DAYS), so the
cap falls evenly.

The 190-day backfill restarted from the newest window on every run until
one got through, so a window that kept failing made every daily run
read the whole history again. 00201 adds brands.gsc_pages_backfill_from,
the oldest day read so far, kept after each window: a sync cut short
resumes after it the next day. gsc_pages_backfilled_property now names
the property the page rows belong to, set once its last 28 days are in;
a brand backfilled before reads as done, and a new property still drops
the old one's pages first.

Follow-up to ansvisor#1177's review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk the remaining #1177 notes: page pulls go a week at a time, and the 190-day backfill keeps how far back it has read (brands.gsc_pages_backfill_from, migration 00201), so a sync cut short resumes after its last window. The prune index was already in 00181. Brands backfilled before read as done. There's no Search Console connection locally, so the end-to-end check is a mocked runGscSync test; a run on a real property would be welcome. Deploy 00201 before the server.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 20, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I walked the resume through by hand. A window stores its oldest day; the next day that day is one day older, and pageWindows(readTo + 1, …) starts on the day before it, so no day is skipped and none is read twice. A brand backfilled before this PR reads as done, and a new property still drops the old one's rows.

Tested with #1254 and #1255 on main: server 1,914 tests pass, and tsc, lint and Prettier are clean. 00201 is applied to production, ahead of the server deploy, since the brand query selects the new column.

Worth knowing: a daily page sync is now about five Composio calls per brand instead of one, and a first backfill about 24.

@gkhngyk
gkhngyk merged commit dd830fe into ansvisor:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants