Skip to content

perf(home): batch Site card analytics with and without MVs - #1211

Merged
goldflag merged 2 commits into
masterfrom
perf-homepage-site-cards
Sep 20, 2026
Merged

goldflag merged 2 commits into
masterfrom
perf-homepage-site-cards

Conversation

@goldflag

@goldflag goldflag commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

A homepage with 20 Site cards can start 60 analytics requests, repeatedly reading the same data and competing for ClickHouse resources. Both standard and lite deployments now send one organization-scoped request per page. The backend runs two queries concurrently: current/comparison sessions and users, plus session-only sparklines without the unused event JOIN and other chart metrics.

Standard deployments query events directly, with one scan per query. User identity is resolved per Site, session, and period using the existing overview definition, so identification in the current period cannot rewrite previous-period users; overlapping comparison windows also work. Lite deployments use their existing materialized views. No migration or new configuration is required.

The batch accepts at most 20 Sites and checks organization ownership and caller access before querying. Cards share the response, show totals independently of viewport visibility, and fetch settings only when opened. Cache keys include the endpoint, organization, Site IDs, both periods, timezone, and bucket. Lite mode retains the existing fallback for exact datetime ranges and unbounded hourly charts. Standard mode batches these too; its all-time charts also place zero-session activity buckets at their actual time instead of the old FULL JOIN's incorrect 1970 timestamp.

Validation:

  • Server build and 2,074 tests passed. Four existing PGlite setup hooks hit their 10-second timeout while server/client checks ran together; all 32 affected tests passed on an isolated rerun with two workers.
  • All 519 client tests and TypeScript checks passed. The regression reproduced 60 requests in standard mode before the change and now asserts one request for 20 cards in both modes, including cache isolation, pagination, comparison settings, errors, and fallback behavior.
  • Standard production build passed with NEXT_PUBLIC_LITE_DASHBOARD=false npm run build -- --webpack.
  • Twelve raw-event ClickHouse integration cases passed using read-only fixture CTEs, covering session identity changes, overlapping/identical periods, disabled comparisons, DST transitions, half-hour timezones, empty Sites, daily/minute buckets, and all-time charts. These can be rerun with CLICKHOUSE_TEST_URL without creating tables. Earlier lite checks covered another 73 fixture SELECTs.
  • Bounded checks of the compiled raw and lite SQL matched the existing current/comparison counts on three production Sites and every sparkline bucket on one Site.

For 20 Sites over the last 24 hours plus the previous 24-hour comparison, the raw totals query took 1.35–1.41 seconds and sparklines took 0.27–0.28 seconds in two runs. Earlier lite repeat runs took 1.28–2.13 seconds for totals and 0.19 seconds for sparklines, with an initial totals outlier of 18.17 seconds. These are query timings without a query-result cache, not an end-to-end browser benchmark; a consistent 10× homepage improvement still needs measurement after deployment.

Existing repository check limitations: the default Turbopack build cannot resolve the existing @rybbit/shared import in useOrgApiKeys.ts in this local checkout. npm run lint has an empty ESLint configuration, scans generated .next files, and does not lint the TypeScript source. Neither configuration is changed here.

Summary by CodeRabbit

  • New Features
    • Added organization-level analytics endpoints for site cards, including totals, comparisons, and session trends.
    • Added batched site-card loading for faster dashboard rendering across multiple sites.
    • Added support for standard and lightweight analytics modes, with appropriate handling for detailed time ranges.
    • Site cards now display loading, error, retry, comparison, and chart states more consistently.
  • Bug Fixes
    • Prevented stale metrics from appearing when switching organizations, periods, or pages.
    • Improved handling of missing time buckets and exact date-range queries.
    • Added access validation for organization site-card analytics.

@vercel

vercel Bot commented Sep 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
rybbit Ready Ready Preview Sep 20, 2026 10:20pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 4c0e827e-eb21-4a4a-b89e-5ec0aac25444

📥 Commits

Reviewing files that changed from the base of the PR and between 7035fcb and c2b7db8.

📒 Files selected for processing (11)
  • client/src/api/analytics/hooks/useGetSiteCards.ts
  • client/src/app/(home)/SiteCards.test.tsx
  • client/src/app/(home)/SiteCards.tsx
  • server/src/api/analytics/getSiteCards.test.ts
  • server/src/api/analytics/getSiteCards.ts
  • server/src/api/analytics/index.ts
  • server/src/api/analytics/lite/siteCardsQuery.ts
  • server/src/api/analytics/siteCardsQuery.clickhouse.test.ts
  • server/src/api/analytics/siteCardsQuery.test.ts
  • server/src/api/analytics/siteCardsQuery.ts
  • server/src/index.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change adds organization-scoped analytics requests, batched site-card endpoints, shared query builders, and homepage rendering. It validates access and time windows, supports standard and lite modes, and adds client, server, and ClickHouse coverage.

Changes

Site Card Analytics

Layer / File(s) Summary
Organization-scoped analytics client
client/src/api/analytics/...
Analytics requests and query keys now support organization scopes. A batched site-card hook and response types were added.
Site-card query engine
server/src/api/analytics/siteCardsQuery.ts, server/src/api/analytics/lite/siteCardsQuery.ts, server/src/api/analytics/utils/timeWindow.ts, server/src/api/analytics/*Query.test.ts
Standard and lite query builders now produce per-site totals and session series for current and comparison periods. Time resolution uses one supplied timestamp.
Batched site-card API
server/src/api/analytics/getSiteCards.ts, server/src/api/analytics/getSiteCards.test.ts, server/src/index.ts
The server validates requests, checks organization access, executes standard or lite queries, assembles metrics, and registers both organization routes.
Homepage batched rendering
client/src/app/(home)/SiteCards.tsx, client/src/app/(home)/SiteCard.tsx, client/src/app/(home)/page.tsx, client/src/app/(home)/SiteCards.test.tsx, client/src/components/SiteSessionChart.tsx
The homepage selects batched or per-site rendering based on dashboard mode and time range. Shared card rendering accepts prefetched metrics and conditionally renders charts.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant Homepage
  participant useGetSiteCards
  participant OrganizationAPI
  participant ClickHouse
  Homepage->>useGetSiteCards: request site metrics
  useGetSiteCards->>OrganizationAPI: POST organization site-cards endpoint
  OrganizationAPI->>ClickHouse: execute totals and series queries
  ClickHouse-->>OrganizationAPI: return aggregate rows
  OrganizationAPI-->>useGetSiteCards: return SiteCardsResponse
  useGetSiteCards-->>Homepage: render batched site cards
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.70% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 21 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: batching Site card analytics for homepage performance, with and without materialized views.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@goldflag goldflag changed the title perf(home): batch lite Site card analytics perf(home): batch Site card analytics with and without MVs Sep 20, 2026
@goldflag
goldflag merged commit eb2aaf5 into master Sep 20, 2026
11 checks passed

This branch was successfully deployed

1 active deployment
Preview c2b7db8d Deployed Sep 20, 2026 by vercel[bot]
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