Skip to content

Traffic: KPIs, platform breakdown and trend silently cap at 1000 rows #450

Closed

Description

@gkhngyk

Problem

The AI Traffic page's summary and trend queries fetch raw rows without pagination and count them in memory. PostgREST silently caps un-paginated selects at 1000 rows, so for a brand with more than 1000 AI-referred visits in the selected window:

  • the AI-Referred Visits KPI silently reads 1000 (and the previous-period comparison is equally wrong),
  • the platform breakdown (chart + table) and Top Landing Pages aggregate only the first 1000 rows,
  • the referral trend chart drops the tail of the window.

Same defect class as the citations scans fixed in #430 — the numbers look plausible, so the truncation is invisible to the user.

Where (verified in code)

web/src/lib/actions/traffic.ts:

  • getTrafficSummary — two un-paginated selects over ai_traffic_logs (current + previous period), then totalVisits: rows.length and in-memory maps for platform/page breakdowns.
  • getTrafficTrend — one un-paginated select over the window, bucketed by day in memory.

getTrafficLogs is fine (it uses .range() + count).

Fix

  • For the two totals, use a count query (select('*', { count: 'exact', head: true })) instead of fetching rows — exact and O(1) transfer.
  • For the breakdowns and the trend, page through the window with .range() (deterministic created_at, id ordering, sane hard ceiling) and aggregate per batch — the same pattern as scanFilteredResults in web/src/lib/actions/citations.ts. Only source_platform, url, created_at are needed, so pages stay cheap.

Acceptance criteria

  • A brand with >1000 visits in the window shows the true visit count, and platform/page/trend numbers sum consistently with it.
  • Previous-period comparison uses the same exact counting.
  • Bounded work: a pathological brand cannot pin the server action (hard row ceiling on the paged scans, like the citations scans).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions