Skip to content

feat(ops): daily health report email - #949

Merged
gkhngyk merged 2 commits into
mainfrom
feature/daily-health-report
Oct 6, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/daily-health-report

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Summary

A daily health email so slow pages and broken nightly runs are caught by us, not by users. In one day, two such problems were found only after someone hit them: nightly runs left waiting, and a Citations read past the 8 s timeout.

Every day at 06:00 UTC (Vercel cron → POST /api/internal/health-report, CRON_SECRET), the server checks four things and emails a report whose subject starts with [OK] or [ISSUES]:

  • Page reads. Eight of the dashboard's heaviest reads are timed on a reference brand: Citations, Insights, Competitors and Topics, at the page default and a long window. Under 4 s is green, 4–8 s yellow, 8 s or an error red. The 8 s budget is the page's statement timeout.
  • Nightly tracking (last 24 h). Failed runs with their reason, runs stuck waiting/active for more than 2 h, brands that ran nightly in the past week but have no completed run, and runs failed by a server restart.
  • Cloro. Tasks pending for over an hour.
  • Server. Uptime.

The report only reports; it never repairs anything. A check that fails shows up as "could not check" instead of suppressing the email, so a silent day always means the job itself did not run.

health_probe (00108) runs one whitelisted read exactly as the page does: as the authenticated role with a given member's claims, for its own transaction only, and returns the seconds it took. Only service_role may execute it. The role matters: with row security bypassed, the Competitors read planned differently and ran past 120 s, against 6.9 s as the page runs it. Already applied to the hosted database.

Resend sending moves to lib/email.js and is shared with the Daily Pulse, which keeps its List-Unsubscribe header.

Config (server env, not in the repo):

HEALTH_REPORT_RECIPIENTS=<comma-separated addresses>
HEALTH_PROBE_BRAND_ID=<reference brand>
HEALTH_PROBE_USER_ID=<a member of its organization>

Without recipients no email is sent. Without the probe brand or user, the timing section reports itself skipped.

First measurements on the largest brand, run one at a time:

Read Window Seconds
citations_window_stats / domains / urls 24 h 1.00 / 0.20 / 1.29
citations_domains 30 d ~4.5 (after #948)
citations_urls 30 d 9.61
insights_aggregates_daily 30 d 0.01
competitor_aggregates_daily all time 6.90
topics_overview_aggregates — 4.23

So the first report will already be [ISSUES]: citations_urls (30 d) is past the budget, and Competitors and Topics are close to it.

Not covered yet:

  • Real-user statement timeouts from the Supabase logs, which would need a management token on the server.
  • Front-end errors, which need an error tracker.

Related issue

None.

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

  1. Set the three env vars on the server and deploy server and web.
  2. Trigger once: curl -X POST <server>/api/internal/health-report -H "Authorization: Bearer $CRON_SECRET". It returns 202, and the email arrives within a minute or two.
  3. npx vitest run src/lib/health from server/ (8 tests). The full server suite passes (998).

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

🤖 Generated with Claude Code

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 6, 2026
Every morning at 06:00 UTC the server times the dashboard's heaviest reads
on a reference brand against their 8 s budget (health_probe runs each as
an organization member), checks the night's tracking runs (failed, stuck,
brands with no completed run, runs killed by a restart) and long-pending
Cloro tasks, and emails a short report flagged [OK] or [ISSUES].

Recipients, reference brand and member come from env. Resend sending is
pulled into lib/email.js and shared with the Daily Pulse.
… run

Run with row security bypassed, the Competitors read planned differently
and ran past 120 s against 6.9 s as the page runs it. health_probe is now
security invoker and switches to the authenticated role for its
transaction before setting the member's claims.
@gkhngyk
gkhngyk force-pushed the feature/daily-health-report branch from 69addd7 to 435f63d Compare October 6, 2026 15:19
@gkhngyk
gkhngyk merged commit bb6041c into main Oct 6, 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.

1 participant