Skip to content

fix(server): count run results from the DB when stamping tracking_runs - #583

Merged
gkhngyk merged 2 commits into
mainfrom
fix/tracking-run-stamp-webhook-mode
Aug 3, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/tracking-run-stamp-webhook-mode

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Urgent follow-up to #578. This morning's cron ran on the new code but tracking_runs ended the day empty, so the insights 24h anchor never advanced past the backfill (the dashboard kept showing yesterday's run) and the Daily Pulse email disagreed with the product again.

Root cause: the zero-result guard read the worker's own insertedCount — but in webhook mode the scraper results are inserted by the /cloro/callback handler, not by the worker. The counter only sees the API-model phase, so on scraper-only brands every successful run ended with insertedCount === 0 and the guard deleted the just-completed run's ledger row.

Fix:

  • Stamp from a prompt_results count scoped to the run's started_at (insert-path-agnostic) — result_count now reflects the real run size.
  • The delete only fires for genuinely zero-result runs; a count error leaves the row uncompleted (dashboard keeps the previous completed run; the next run clears the dangling row).
  • Added a tracking run stamped info log so the next diagnosis is one grep away.

Today's missing run rows were repaired directly in the ledger (synthetic completed rows for the 20 brands that ran), so dashboards are already showing today's data — this PR makes tomorrow's 05:00 TR run stamp itself correctly.

Related issue

Follow-up to #578 (no tracked issue).

Type of change

  • fix — Bug fix

How to test

  1. Deploy the server, wait for (or trigger) a full run on a scraper-only brand.
  2. tracking_runs gains a completed row whose result_count matches the run's prompt_results rows, and the server logs tracking run stamped.
  3. The insights 24h view advances to the new run when it completes.

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 3, 2026 09:21
The zero-result guard read the worker's own insertedCount, but in
webhook mode scraper results are inserted by the /cloro/callback
handler — the counter only sees the API-model phase. On scraper-only
brands every successful run ended with insertedCount 0, so the guard
deleted the just-finished run's ledger row and tracking_runs stayed
empty: the insights 24h anchor never advanced past the backfill and the
Daily Pulse/dashboard mismatch resurfaced.

Stamp from a prompt_results count scoped to the run's started_at
instead, keep the delete only for genuinely zero-result runs, leave the
row uncompleted on a count error, and log the stamp so the next
diagnosis takes one grep.
The daily pulse compared wall-clock [now-24h] windows, which
double-counts whenever two runs land within 24h of each other — the
morning after the cron moved from 06:00 to 02:00 UTC, the email summed
both runs' mentions while the dashboard showed one run. Resolve the
current/previous KPI windows from tracking_runs exactly like the
Insights 24h view does, so the email and the dashboard read the same
window by construction. Weekly pulses, the trend detectors and brands
without a completed run keep the clock-based windows.
@gkhngyk

gkhngyk commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Added a second commit: the Daily Pulse's daily KPI windows now resolve from the tracking_runs ledger too — the same windows the Insights 24h view uses — so the email and the dashboard read identical ranges by construction. Wall-clock windows double-counted on the cron-time transition day (522 vs 278). Weekly pulses, detectors, and brands with no completed run keep clock-based windows.

@gkhngyk
gkhngyk merged commit e76c9bd into main Aug 3, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/tracking-run-stamp-webhook-mode branch August 3, 2026 06:35
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