Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(shopping): count every card instead of the first 1,000 (#966)
Gökhan Geyik4075298e1d60
diff --git a/supabase/migrations/00115_shopping_overview.sql b/supabase/migrations/00115_shopping_overview.sqlnew file mode 100644index 000000000..b2aad11ee--- /dev/null+++ b/supabase/migrations/00115_shopping_overview.sql@@ -0,0 +1,129 @@+-- Shopping: compute the Overview tab in the database (#922).+--+-- The Overview KPIs, the per-platform card rate and the 30-day trend were+-- aggregated in JavaScript from rows fetched without paging, so PostgREST's+-- 1,000-row cap silently truncated them: the largest brand's 30-day window has+-- ~5.8k cards and ~117k results. The card-bearing count also filtered on the+-- wide `shopping_cards` jsonb column, an 11 s heap scan on that brand, past the+-- 8 s statement timeout. Here a result "has cards" when it has a row in+-- prompt_result_shopping_cards, read from that table's unique index, and the+-- results themselves come from the covering (brand_id, created_at) index:+-- ~0.5 s on the same window.+--+-- Security invoker: the caller's RLS on prompt_results and+-- prompt_result_shopping_cards still applies.++create or replace function public.shopping_overview(+ p_brand_id uuid,+ p_from timestamptz default null,+ p_to timestamptz default null,+ p_platforms text[] default null,+ p_regions text[] default null,+ p_trend_from timestamptz default null+) returns jsonb+language plpgsql+stable+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ return (+ with results as materialized (+ select r.id, r.platform+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and (p_from is null or r.created_at >= p_from)+ and (p_to is null or r.created_at <= p_to)+ and (p_platforms is null or r.platform = any (p_platforms))+ and (p_regions is null or r.region = any (p_regions))+ ),+ by_platform as (+ select res.platform,+ count(*) as total_results,+ count(*) filter (where exists (+ select 1 from public.prompt_result_shopping_cards c+ where c.prompt_result_id = res.id+ )) as results_with_cards+ from results res+ group by res.platform+ ),+ cards as materialized (+ select c.matched_brand_role, c.merchant_domain+ from public.prompt_result_shopping_cards c+ where c.brand_id = p_brand_id+ and (p_from is null or c.created_at >= p_from)+ and (p_to is null or c.created_at <= p_to)+ and (p_platforms is null or c.platform = any (p_platforms))+ and (p_regions is null or c.region = any (p_regions))+ ),+ top_merchant as (+ select merchant_domain, count(*) as card_count+ from cards+ where matched_brand_role = 'own' and merchant_domain is not null+ group by merchant_domain+ order by count(*) desc, merchant_domain+ limit 1+ ),+ trend as (+ select (c.created_at at time zone 'UTC')::date as day,+ count(*) filter (where c.matched_brand_role = 'own') as own_cards,+ count(*) as total_cards+ from public.prompt_result_shopping_cards c+ where c.brand_id = p_brand_id+ and p_trend_from is not null+ and c.created_at >= p_trend_from+ and (p_platforms is null or c.platform = any (p_platforms))+ and (p_regions is null or c.region = any (p_regions))+ group by 1+ )+ select jsonb_build_object(+ 'total_results', coalesce((select sum(total_results) from by_platform), 0),+ 'results_with_cards', coalesce((select sum(results_with_cards) from by_platform), 0),+ 'total_cards', (select count(*) from cards),+ 'own_cards', (select count(*) from cards where matched_brand_role = 'own'),+ 'top_merchant', (select jsonb_build_object('domain', merchant_domain,+ 'card_count', card_count)+ from top_merchant),+ 'by_platform', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'platform', platform,+ 'total_results', total_results,+ 'results_with_cards', results_with_cards)+ order by platform)+ from by_platform),+ '[]'::jsonb),+ 'trend', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'day', day,+ 'own_cards', own_cards,+ 'total_cards', total_cards)+ order by day)+ from trend),+ '[]'::jsonb)+ )+ );+end;+$$;++-- The Shopping filter bar's platform and region options, from every result the+-- brand has: the previous read took the first 1,000 results only.+create or replace function public.shopping_filter_options(p_brand_id uuid)+returns table(platforms text[], regions text[])+language sql+stable+set search_path to 'public'+as $$+ select+ coalesce((+ select array_agg(distinct r.platform order by r.platform)+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and r.platform is not null and r.platform <> ''+ ), '{}') as platforms,+ coalesce((+ select array_agg(distinct r.region order by r.region)+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and r.region is not null and r.region <> ''+ ), '{}') as regions+$$;diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 2cd6bd9d5..a20b3518e 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -13544,3 +13544,136 @@ $$;revoke all on function public.sync_opportunity_status() from public, anon, authenticated;+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00115_shopping_overview.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Shopping: compute the Overview tab in the database (#922).+--+-- The Overview KPIs, the per-platform card rate and the 30-day trend were+-- aggregated in JavaScript from rows fetched without paging, so PostgREST's+-- 1,000-row cap silently truncated them: the largest brand's 30-day window has+-- ~5.8k cards and ~117k results. The card-bearing count also filtered on the+-- wide `shopping_cards` jsonb column, an 11 s heap scan on that brand, past the+-- 8 s statement timeout. Here a result "has cards" when it has a row in+-- prompt_result_shopping_cards, read from that table's unique index, and the+-- results themselves come from the covering (brand_id, created_at) index:+-- ~0.5 s on the same window.+--+-- Security invoker: the caller's RLS on prompt_results and+-- prompt_result_shopping_cards still applies.++create or replace function public.shopping_overview(+ p_brand_id uuid,+ p_from timestamptz default null,+ p_to timestamptz default null,+ p_platforms text[] default null,+ p_regions text[] default null,+ p_trend_from timestamptz default null+) returns jsonb+language plpgsql+stable+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ return (+ with results as materialized (+ select r.id, r.platform+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and (p_from is null or r.created_at >= p_from)+ and (p_to is null or r.created_at <= p_to)+ and (p_platforms is null or r.platform = any (p_platforms))+ and (p_regions is null or r.region = any (p_regions))+ ),+ by_platform as (+ select res.platform,+ count(*) as total_results,+ count(*) filter (where exists (+ select 1 from public.prompt_result_shopping_cards c+ where c.prompt_result_id = res.id+ )) as results_with_cards+ from results res+ group by res.platform+ ),+ cards as materialized (+ select c.matched_brand_role, c.merchant_domain+ from public.prompt_result_shopping_cards c+ where c.brand_id = p_brand_id+ and (p_from is null or c.created_at >= p_from)+ and (p_to is null or c.created_at <= p_to)+ and (p_platforms is null or c.platform = any (p_platforms))+ and (p_regions is null or c.region = any (p_regions))+ ),+ top_merchant as (+ select merchant_domain, count(*) as card_count+ from cards+ where matched_brand_role = 'own' and merchant_domain is not null+ group by merchant_domain+ order by count(*) desc, merchant_domain+ limit 1+ ),+ trend as (+ select (c.created_at at time zone 'UTC')::date as day,+ count(*) filter (where c.matched_brand_role = 'own') as own_cards,+ count(*) as total_cards+ from public.prompt_result_shopping_cards c+ where c.brand_id = p_brand_id+ and p_trend_from is not null+ and c.created_at >= p_trend_from+ and (p_platforms is null or c.platform = any (p_platforms))+ and (p_regions is null or c.region = any (p_regions))+ group by 1+ )+ select jsonb_build_object(+ 'total_results', coalesce((select sum(total_results) from by_platform), 0),+ 'results_with_cards', coalesce((select sum(results_with_cards) from by_platform), 0),+ 'total_cards', (select count(*) from cards),+ 'own_cards', (select count(*) from cards where matched_brand_role = 'own'),+ 'top_merchant', (select jsonb_build_object('domain', merchant_domain,+ 'card_count', card_count)+ from top_merchant),+ 'by_platform', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'platform', platform,+ 'total_results', total_results,+ 'results_with_cards', results_with_cards)+ order by platform)+ from by_platform),+ '[]'::jsonb),+ 'trend', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'day', day,+ 'own_cards', own_cards,+ 'total_cards', total_cards)+ order by day)+ from trend),+ '[]'::jsonb)+ )+ );+end;+$$;++-- The Shopping filter bar's platform and region options, from every result the+-- brand has: the previous read took the first 1,000 results only.+create or replace function public.shopping_filter_options(p_brand_id uuid)+returns table(platforms text[], regions text[])+language sql+stable+set search_path to 'public'+as $$+ select+ coalesce((+ select array_agg(distinct r.platform order by r.platform)+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and r.platform is not null and r.platform <> ''+ ), '{}') as platforms,+ coalesce((+ select array_agg(distinct r.region order by r.region)+ from public.prompt_results r+ where r.brand_id = p_brand_id+ and r.region is not null and r.region <> ''+ ), '{}') as regions+$$;+diff --git a/web/src/app/[locale]/(dashboard)/dashboard/shopping/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/shopping/page.tsxindex 592eae120..44b882e78 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/shopping/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/shopping/page.tsx@@ -35,9 +35,8 @@ import {import { useBrandStore } from '@/stores/use-brand-store';import { useFeatureGate } from '@/hooks/use-feature-gate';import {- getShoppingChartData,getShoppingFilterOptions,- getShoppingKpis,+ getShoppingOverview,getOwnProducts,getCompetitorProducts,getCompetitorSummary,@@ -167,10 +166,10 @@ export default function ShoppingPage() {setTableLoading(true);}try {- const promises: [Promise<ShoppingKpis>, Promise<ShoppingChartData>, ...Promise<unknown>[]] = [- getShoppingKpis(activeBrandId, filters),- getShoppingChartData(activeBrandId, filters),- ];+ const promises: [+ Promise<{ kpis: ShoppingKpis; charts: ShoppingChartData }>,+ ...Promise<unknown>[],+ ] = [getShoppingOverview(activeBrandId, filters)];if (hasFullAccess) {promises.push(@@ -184,13 +183,14 @@ export default function ShoppingPage() {const results = await Promise.all(promises);if (reqId === reqIdRef.current) {- setKpis(results[0]);- setCharts(results[1]);- if (hasFullAccess && results.length >= 6) {- setOwnProducts(results[2] as ShoppingProduct[]);- setCompetitorProducts(results[3] as ShoppingProduct[]);- setCompetitorSummary(results[4] as CompetitorShoppingSummary[]);- setPromptsData(results[5] as CardEligiblePromptsResponse);+ const overview = results[0] as { kpis: ShoppingKpis; charts: ShoppingChartData };+ setKpis(overview.kpis);+ setCharts(overview.charts);+ if (hasFullAccess && results.length >= 5) {+ setOwnProducts(results[1] as ShoppingProduct[]);+ setCompetitorProducts(results[2] as ShoppingProduct[]);+ setCompetitorSummary(results[3] as CompetitorShoppingSummary[]);+ setPromptsData(results[4] as CardEligiblePromptsResponse);}}} finally {diff --git a/web/src/lib/actions/shopping.test.ts b/web/src/lib/actions/shopping.test.tsnew file mode 100644index 000000000..b2d494ed6--- /dev/null+++ b/web/src/lib/actions/shopping.test.ts@@ -0,0 +1,213 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * #922 — the Shopping page used to aggregate unpaged selects, which PostgREST+ * caps at 1,000 rows. These tests pin that the card reads page through every+ * row and that the Overview comes from the `shopping_overview` RPC.+ */++interface QueryState {+ table: string;+ eq: Record<string, unknown>;+ range?: [number, number];+ head?: boolean;+}++const PAGE_CAP = 1000;++let cards: Array<Record<string, unknown>> = [];+let overviewRow: Record<string, unknown> = {};+let rpcCalls: Array<{ fn: string; args: Record<string, unknown> }> = [];+let cardCalls: QueryState[] = [];++function resolveQuery(state: QueryState) {+ if (state.table === 'competitors') {+ return {+ data: [+ { id: 'c-1', name: 'Rival One', domain: 'rival-one.example' },+ { id: 'c-2', name: 'Rival Two', domain: 'rival-two.example' },+ ],+ error: null,+ };+ }+ if (state.table === 'prompt_result_shopping_cards') {+ const rows = cards.filter(+ (c) =>+ state.eq.matched_brand_role === undefined ||+ c.matched_brand_role === state.eq.matched_brand_role,+ );+ if (state.head) return { data: null, count: rows.length, error: null };+ // Like PostgREST: an unpaged select still stops at the cap.+ const [from, to] = state.range ?? [0, PAGE_CAP - 1];+ return { data: rows.slice(from, Math.min(to + 1, from + PAGE_CAP)), error: null };+ }+ return { data: [], error: null };+}++function fakeQueryBuilder(table: string) {+ const state: QueryState = { table, eq: {} };+ const builder = {+ select: (_columns: string, opts?: { head?: boolean }) => {+ state.head = opts?.head;+ return builder;+ },+ eq: (column: string, value: unknown) => {+ state.eq[column] = value;+ return builder;+ },+ gte: () => builder,+ lte: () => builder,+ in: () => builder,+ order: () => builder,+ range: (from: number, to: number) => {+ state.range = [from, to];+ return builder;+ },+ then: (onFulfilled: (v: unknown) => unknown, onRejected?: (e: unknown) => unknown) => {+ if (table === 'prompt_result_shopping_cards') cardCalls.push({ ...state });+ return Promise.resolve(resolveQuery(state)).then(onFulfilled, onRejected);+ },+ };+ return builder;+}++vi.mock('@/lib/supabase/server', () => ({+ createClient: async () => ({+ from: (table: string) => fakeQueryBuilder(table),+ rpc: async (fn: string, args: Record<string, unknown>) => {+ rpcCalls.push({ fn, args });+ return { data: overviewRow, error: null };+ },+ }),+}));++import {+ getCompetitorProducts,+ getCompetitorSummary,+ getOwnProducts,+ getShoppingKpis,+ getShoppingOverview,+} from './shopping';++function card(i: number, role: 'own' | 'competitor' | 'other', matched?: string) {+ return {+ id: `card-${i}`,+ created_at: '2026-10-01T12:00:00.000Z',+ platform: 'google-aimode',+ region: 'US',+ product_title: `Product ${i % 7}`,+ product_brand: 'Acme',+ price_amount: 10,+ price_currency: 'USD',+ image_url: null,+ merchant_url: null,+ merchant_domain: 'shop.example',+ raw: null,+ matched_brand_id: matched ?? null,+ matched_brand_role: role,+ prompt_results: { prompt: { id: 'p-1', text: 'Best running shoes?' } },+ };+}++beforeEach(() => {+ cards = [];+ rpcCalls = [];+ cardCalls = [];+ overviewRow = {+ total_results: 4000,+ results_with_cards: 1000,+ total_cards: 5845,+ own_cards: 1169,+ top_merchant: { domain: 'shop.example', card_count: 900 },+ by_platform: [+ { platform: 'chatgpt', total_results: 3000, results_with_cards: 600 },+ { platform: 'google-aimode', total_results: 1000, results_with_cards: 400 },+ { platform: 'perplexity', total_results: 0, results_with_cards: 0 },+ ],+ trend: [],+ };+});++describe('getOwnProducts', () => {+ it('counts every own card past the 1,000-row cap', async () => {+ cards = Array.from({ length: 2500 }, (_, i) => card(i, 'own'));+ const products = await getOwnProducts('brand-1', { datePreset: '30d' });++ expect(products.reduce((sum, p) => sum + p.impressions, 0)).toBe(2500);+ expect(cardCalls.map((c) => c.range)).toEqual([+ [0, 999],+ [1000, 1999],+ [2000, 2999],+ ]);+ });+});++describe('getCompetitorProducts', () => {+ it('pages through competitor cards', async () => {+ cards = Array.from({ length: 1200 }, (_, i) => card(i, 'competitor', 'c-1'));+ const products = await getCompetitorProducts('brand-1', { datePreset: '30d' });++ expect(products.reduce((sum, p) => sum + p.impressions, 0)).toBe(1200);+ expect(products[0].competitor_name).toBe('Rival One');+ });+});++describe('getCompetitorSummary', () => {+ it('counts each competitor across pages', async () => {+ cards = [+ ...Array.from({ length: 1500 }, (_, i) => card(i, 'competitor', 'c-1')),+ ...Array.from({ length: 300 }, (_, i) => card(i, 'competitor', 'c-2')),+ ...Array.from({ length: 200 }, (_, i) => card(i, 'other')),+ ];+ const summary = await getCompetitorSummary('brand-1', { datePreset: '30d' });++ expect(summary.map((s) => [s.competitor_id, s.card_count])).toEqual([+ ['c-1', 1500],+ ['c-2', 300],+ ]);+ expect(summary[0].sov).toBeCloseTo(1500 / 2000);+ });+});++describe('getShoppingOverview', () => {+ it('builds KPIs and charts from one RPC call', async () => {+ const { kpis, charts } = await getShoppingOverview('brand-1', {+ datePreset: 'all',+ platforms: ['chatgpt'],+ });++ expect(rpcCalls).toHaveLength(1);+ expect(rpcCalls[0].fn).toBe('shopping_overview');+ expect(rpcCalls[0].args).toMatchObject({+ p_brand_id: 'brand-1',+ p_platforms: ['chatgpt'],+ });+ expect(rpcCalls[0].args.p_trend_from).toEqual(expect.any(String));++ expect(kpis).toEqual({+ shoppingCardRate: 0.25,+ shoppingCardRateSampleSize: 4000,+ productsSurfaced: 1169,+ shoppingSov: 1169 / 5845,+ topMerchant: { domain: 'shop.example', cardCount: 900 },+ });+ expect(charts.platformCardRate.map((p) => p.platform)).toEqual(['google-aimode', 'chatgpt']);+ expect(charts.ownPresenceTrend).toHaveLength(30);+ });++ it('places trend days on their UTC date and fills empty days with zeros', async () => {+ const today = new Date().toISOString().slice(0, 10);+ overviewRow.trend = [{ day: today, own_cards: 3, total_cards: 12 }];+ const { charts } = await getShoppingOverview('brand-1', { datePreset: '30d' });++ expect(charts.ownPresenceTrend.at(-1)).toEqual({ date: today, ownCards: 3, totalCards: 12 });+ expect(charts.ownPresenceTrend[0]).toMatchObject({ ownCards: 0, totalCards: 0 });+ });+});++describe('getShoppingKpis', () => {+ it('skips the trend', async () => {+ await getShoppingKpis('brand-1', { datePreset: '30d' });+ expect(rpcCalls[0].args.p_trend_from).toBeUndefined();+ });+});diff --git a/web/src/lib/actions/shopping.ts b/web/src/lib/actions/shopping.tsindex 70fdeb4ec..93d469c4b 100644--- a/web/src/lib/actions/shopping.ts+++ b/web/src/lib/actions/shopping.ts@@ -71,107 +71,56 @@ function daysAgoIso(days: number): string {return new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();}-// ── KPI query ─────────────────────────────────────────────────────────────────+// ── Overview (KPIs + charts) ──────────────────────────────────────────────────++interface ShoppingOverviewRow {+ total_results: number;+ results_with_cards: number;+ total_cards: number;+ own_cards: number;+ top_merchant: { domain: string; card_count: number } | null;+ by_platform: Array<{ platform: string; total_results: number; results_with_cards: number }>;+ trend: Array<{ day: string; own_cards: number; total_cards: number }>;+}/**- * Build the four overview KPIs in a single round-trip per metric.- *- * `shoppingCardRate` is the only KPI that needs to know about prompts- * that returned *zero* cards, so it queries `prompt_results` directly to- * get the denominator. Everything else aggregates over the normalized- * `prompt_result_shopping_cards` table.+ * One `shopping_overview` call (#922). The KPIs and charts are aggregated in+ * Postgres: fetched as rows they were cut off at PostgREST's 1,000-row cap.+ * `trendFrom` is omitted when the caller doesn't need the trend.*/-export async function getShoppingKpis(+async function loadShoppingOverview(brandId: string,filters: ShoppingFilters,-): Promise<ShoppingKpis> {+ trendFrom?: string,+): Promise<ShoppingOverviewRow> {const supabase = await createClient();const { from, to } = resolveDateRange(filters);- const expandedTo = expandDateToEndOfDay(to);-- // ── 1. Shopping card rate ──- // numerator = prompt_results with at least one card- // denominator = prompt_results in the window- // Both queries hit only `prompt_results.id` so they stay cheap.- let totalQuery = supabase- .from('prompt_results')- .select('id', { count: 'exact', head: true })- .eq('brand_id', brandId);- if (from) totalQuery = totalQuery.gte('created_at', from);- if (expandedTo) totalQuery = totalQuery.lte('created_at', expandedTo);- if (filters.platforms?.length) totalQuery = totalQuery.in('platform', filters.platforms);- if (filters.regions?.length) totalQuery = totalQuery.in('region', filters.regions);-- let cardBearingQuery = supabase- .from('prompt_results')- .select('id', { count: 'exact', head: true })- .eq('brand_id', brandId)- .not('shopping_cards', 'is', null)- .filter('shopping_cards', 'neq', '[]');- if (from) cardBearingQuery = cardBearingQuery.gte('created_at', from);- if (expandedTo) cardBearingQuery = cardBearingQuery.lte('created_at', expandedTo);- if (filters.platforms?.length)- cardBearingQuery = cardBearingQuery.in('platform', filters.platforms);- if (filters.regions?.length) cardBearingQuery = cardBearingQuery.in('region', filters.regions);-- // ── 2-3. Cards by role for products surfaced + SoV ──- let cardsQuery = supabase- .from('prompt_result_shopping_cards')- .select('matched_brand_role, merchant_domain', { count: 'exact' })- .eq('brand_id', brandId);- if (from) cardsQuery = cardsQuery.gte('created_at', from);- if (expandedTo) cardsQuery = cardsQuery.lte('created_at', expandedTo);- if (filters.platforms?.length) cardsQuery = cardsQuery.in('platform', filters.platforms);- if (filters.regions?.length) cardsQuery = cardsQuery.in('region', filters.regions);-- const [- { count: totalResults },- { count: cardBearingResults },- { data: cards, error: cardsError },- ] = await Promise.all([totalQuery, cardBearingQuery, cardsQuery]);-- if (cardsError) throw new Error(cardsError.message);-- const shoppingCardRate =- totalResults && totalResults > 0 ? (cardBearingResults ?? 0) / totalResults : 0;-- const rows = (cards ?? []) as Array<{- matched_brand_role: 'own' | 'competitor' | 'other';- merchant_domain: string | null;- }>;-- let ownCount = 0;- const merchantTotals = new Map<string, number>();- for (const row of rows) {- if (row.matched_brand_role === 'own') {- ownCount += 1;- if (row.merchant_domain) {- merchantTotals.set(row.merchant_domain, (merchantTotals.get(row.merchant_domain) ?? 0) + 1);- }- }- }-- const totalCards = rows.length;- const shoppingSov = totalCards > 0 ? ownCount / totalCards : 0;-- let topMerchant: { domain: string; cardCount: number } | null = null;- for (const [domain, count] of merchantTotals.entries()) {- if (!topMerchant || count > topMerchant.cardCount) {- topMerchant = { domain, cardCount: count };- }- }+ const { data, error } = await supabase.rpc('shopping_overview', {+ p_brand_id: brandId,+ p_from: from,+ p_to: expandDateToEndOfDay(to),+ p_platforms: filters.platforms?.length ? filters.platforms : undefined,+ p_regions: filters.regions?.length ? filters.regions : undefined,+ p_trend_from: trendFrom,+ });+ if (error) throw new Error(error.message);+ return data as unknown as ShoppingOverviewRow;+}+function toKpis(row: ShoppingOverviewRow): ShoppingKpis {+ const totalResults = Number(row.total_results);+ const totalCards = Number(row.total_cards);return {- shoppingCardRate,- shoppingCardRateSampleSize: totalResults ?? 0,- productsSurfaced: ownCount,- shoppingSov,- topMerchant,+ shoppingCardRate: totalResults > 0 ? Number(row.results_with_cards) / totalResults : 0,+ shoppingCardRateSampleSize: totalResults,+ productsSurfaced: Number(row.own_cards),+ shoppingSov: totalCards > 0 ? Number(row.own_cards) / totalCards : 0,+ topMerchant: row.top_merchant+ ? { domain: row.top_merchant.domain, cardCount: Number(row.top_merchant.card_count) }+ : null,};}-// ── Chart query ───────────────────────────────────────────────────────────────-/*** Two compact chart payloads for the Overview tab:*@@ -180,118 +129,137 @@ export async function getShoppingKpis(* - `ownPresenceTrend` — line chart, one bucket per UTC day for the* last 30 days, with own + total card counts.*/-export async function getShoppingChartData(- brandId: string,- filters: ShoppingFilters,-): Promise<ShoppingChartData> {- const supabase = await createClient();- const { from, to } = resolveDateRange(filters);- const expandedTo = expandDateToEndOfDay(to);-- // ── Platform card rate ──- // Pulls platform + a boolean "has cards" off prompt_results. Aggregating- // in JS is cheap at this row count and avoids defining another RPC.- let pageQuery = supabase- .from('prompt_results')- .select('platform, shopping_cards')- .eq('brand_id', brandId);- if (from) pageQuery = pageQuery.gte('created_at', from);- if (expandedTo) pageQuery = pageQuery.lte('created_at', expandedTo);- if (filters.platforms?.length) pageQuery = pageQuery.in('platform', filters.platforms);- if (filters.regions?.length) pageQuery = pageQuery.in('region', filters.regions);-- const { data: platformRows, error: platformError } = await pageQuery;- if (platformError) throw new Error(platformError.message);-- const perPlatform = new Map<string, { total: number; withCards: number }>();- for (const row of (platformRows ?? []) as Array<{- platform: string;- shopping_cards: unknown;- }>) {- const slot = perPlatform.get(row.platform) ?? { total: 0, withCards: 0 };- slot.total += 1;- if (Array.isArray(row.shopping_cards) && row.shopping_cards.length > 0) {- slot.withCards += 1;- }- perPlatform.set(row.platform, slot);- }- const platformCardRate: PlatformCardRatePoint[] = [...perPlatform.entries()]- .filter(([, slot]) => slot.total > 0)- .map(([platform, slot]) => ({- platform,- cardRate: slot.withCards / slot.total,- totalResults: slot.total,+function toChartData(row: ShoppingOverviewRow): ShoppingChartData {+ const platformCardRate: PlatformCardRatePoint[] = row.by_platform+ .filter((p) => Number(p.total_results) > 0)+ .map((p) => ({+ platform: p.platform,+ cardRate: Number(p.results_with_cards) / Number(p.total_results),+ totalResults: Number(p.total_results),})).sort((a, b) => b.cardRate - a.cardRate);- // ── 30-day own-presence trend ──- // Always 30d regardless of filter so the trend chart shows a stable- // window. Same brand + platform + region filters apply.- const trendFrom = daysAgoIso(30);- let trendQuery = supabase- .from('prompt_result_shopping_cards')- .select('matched_brand_role, created_at')- .eq('brand_id', brandId)- .gte('created_at', trendFrom);- if (filters.platforms?.length) trendQuery = trendQuery.in('platform', filters.platforms);- if (filters.regions?.length) trendQuery = trendQuery.in('region', filters.regions);-- const { data: trendRows, error: trendError } = await trendQuery;- if (trendError) throw new Error(trendError.message);-- // Bucket by UTC day.- const buckets = new Map<string, { ownCards: number; totalCards: number }>();- for (const row of (trendRows ?? []) as Array<{- matched_brand_role: 'own' | 'competitor' | 'other';- created_at: string;- }>) {- const day = row.created_at.slice(0, 10);- const slot = buckets.get(day) ?? { ownCards: 0, totalCards: 0 };- slot.totalCards += 1;- if (row.matched_brand_role === 'own') slot.ownCards += 1;- buckets.set(day, slot);- }-+ const buckets = new Map(row.trend.map((d) => [d.day, d]));// Fill in zero buckets so the line chart doesn't skip empty days.const ownPresenceTrend: OwnPresenceTrendPoint[] = [];for (let i = 29; i >= 0; i--) {const date = new Date(Date.now() - i * 24 * 60 * 60 * 1000).toISOString().slice(0, 10);- const slot = buckets.get(date) ?? { ownCards: 0, totalCards: 0 };+ const slot = buckets.get(date);ownPresenceTrend.push({date,- ownCards: slot.ownCards,- totalCards: slot.totalCards,+ ownCards: Number(slot?.own_cards ?? 0),+ totalCards: Number(slot?.total_cards ?? 0),});}return { platformCardRate, ownPresenceTrend };}+/** The four overview KPIs for a window (also used by reports). */+export async function getShoppingKpis(+ brandId: string,+ filters: ShoppingFilters,+): Promise<ShoppingKpis> {+ return toKpis(await loadShoppingOverview(brandId, filters));+}++/**+ * The Overview tab in one round trip: KPIs plus charts. The trend is always+ * the last 30 days regardless of the date filter, so the chart shows a stable+ * window; the platform and region filters still apply.+ */+export async function getShoppingOverview(+ brandId: string,+ filters: ShoppingFilters,+): Promise<{ kpis: ShoppingKpis; charts: ShoppingChartData }> {+ const row = await loadShoppingOverview(brandId, filters, daysAgoIso(30));+ return { kpis: toKpis(row), charts: toChartData(row) };+}+/*** Collected once at page load so the filter bar shows only platform / region- * values that actually appear in this brand's data.+ * values that actually appear in this brand's data — all of it, not the first+ * 1,000 results.*/export async function getShoppingFilterOptions(brandId: string): Promise<{platforms: string[];regions: string[];}> {const supabase = await createClient();- const { data, error } = await supabase- .from('prompt_results')- .select('platform, region')- .eq('brand_id', brandId)- .limit(2000);-- if (error || !data) {- return { platforms: [], regions: [] };- }+ const { data, error } = await supabase.rpc('shopping_filter_options', { p_brand_id: brandId });+ if (error) return { platforms: [], regions: [] };+ const row = (data as { platforms: string[] | null; regions: string[] | null }[] | null)?.[0];+ return { platforms: row?.platforms ?? [], regions: row?.regions ?? [] };+}++// ── Paged card reads ──────────────────────────────────────────────────────────- const platforms = Array.from(new Set(data.map((d) => d.platform).filter(Boolean))) as string[];- const regions = Array.from(new Set(data.map((d) => d.region).filter(Boolean))) as string[];+const CARD_PAGE_SIZE = 1000;+const CARD_MAX_ROWS = 50_000;- return { platforms, regions };+/**+ * Every row a card query matches, read in `.range()` pages: an unpaged select+ * stops at PostgREST's 1,000-row cap (#922). `build` returns a fresh, filtered+ * query per page; the order makes the pages deterministic. Bounded by+ * CARD_MAX_ROWS so a pathological brand can't pin the server action.+ */+async function fetchAllCards<T>(+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- PostgREST builder generics+ build: () => any,+): Promise<T[]> {+ const rows: T[] = [];+ for (let start = 0; start < CARD_MAX_ROWS; start += CARD_PAGE_SIZE) {+ const { data, error } = await build()+ .order('created_at', { ascending: false })+ .order('id', { ascending: false })+ .range(start, start + CARD_PAGE_SIZE - 1);+ if (error) throw new Error(error.message);+ const batch = (data ?? []) as T[];+ rows.push(...batch);+ if (batch.length < CARD_PAGE_SIZE) break;+ }+ return rows;}+/** A card query over the filter window: brand, dates, platforms and regions. */+function cardsInWindow(+ supabase: Awaited<ReturnType<typeof createClient>>,+ brandId: string,+ filters: ShoppingFilters,+ columns: string,+) {+ const { from, to } = resolveDateRange(filters);+ const expandedTo = expandDateToEndOfDay(to);+ let query = supabase.from('prompt_result_shopping_cards').select(columns).eq('brand_id', brandId);+ if (from) query = query.gte('created_at', from);+ if (expandedTo) query = query.lte('created_at', expandedTo);+ if (filters.platforms?.length) query = query.in('platform', filters.platforms);+ if (filters.regions?.length) query = query.in('region', filters.regions);+ return query;+}++const PRODUCT_CARD_COLUMNS = `+ id,+ created_at,+ platform,+ region,+ product_title,+ product_brand,+ price_amount,+ price_currency,+ image_url,+ merchant_url,+ merchant_domain,+ raw,+ matched_brand_id,+ prompt_results:prompt_result_id (+ prompt:prompt_id (+ id,+ text+ )+ )+ `;+// ── My Products / Competitors tab actions ──────────────────────────────────────export interface ShoppingProductAppearance {@@ -472,46 +440,12 @@ export async function getOwnProducts(filters: ShoppingFilters,): Promise<ShoppingProduct[]> {const supabase = await createClient();- const { from, to } = resolveDateRange(filters);- const expandedTo = expandDateToEndOfDay(to);- let query = supabase- .from('prompt_result_shopping_cards')- .select(- `- id,- created_at,- platform,- region,- product_title,- product_brand,- price_amount,- price_currency,- image_url,- merchant_url,- merchant_domain,- raw,- matched_brand_id,- prompt_results:prompt_result_id (- prompt:prompt_id (- id,- text- )- )- `,- )- .eq('brand_id', brandId)- .eq('matched_brand_role', 'own');+ const data = await fetchAllCards<CardRow>(() =>+ cardsInWindow(supabase, brandId, filters, PRODUCT_CARD_COLUMNS).eq('matched_brand_role', 'own'),+ );- if (from) query = query.gte('created_at', from);- if (expandedTo) query = query.lte('created_at', expandedTo);- if (filters.platforms?.length) query = query.in('platform', filters.platforms);- if (filters.regions?.length) query = query.in('region', filters.regions);-- const { data, error } = await query;- if (error) throw new Error(error.message);-- return aggregateProducts(data || []);+ return aggregateProducts(data);}export async function getCompetitorProducts(@@ -519,8 +453,6 @@ export async function getCompetitorProducts(filters: ShoppingFilters,): Promise<ShoppingProduct[]> {const supabase = await createClient();- const { from, to } = resolveDateRange(filters);- const expandedTo = expandDateToEndOfDay(to);const { data: competitorsData, error: compError } = await supabase.from('competitors')@@ -530,43 +462,14 @@ export async function getCompetitorProducts(if (compError) throw new Error(compError.message);const competitorMap = new Map((competitorsData ?? []).map((c) => [c.id, c]));- let query = supabase- .from('prompt_result_shopping_cards')- .select(- `- id,- created_at,- platform,- region,- product_title,- product_brand,- price_amount,- price_currency,- image_url,- merchant_url,- merchant_domain,- raw,- matched_brand_id,- prompt_results:prompt_result_id (- prompt:prompt_id (- id,- text- )- )- `,- )- .eq('brand_id', brandId)- .eq('matched_brand_role', 'competitor');-- if (from) query = query.gte('created_at', from);- if (expandedTo) query = query.lte('created_at', expandedTo);- if (filters.platforms?.length) query = query.in('platform', filters.platforms);- if (filters.regions?.length) query = query.in('region', filters.regions);+ const data = await fetchAllCards<CardRow>(() =>+ cardsInWindow(supabase, brandId, filters, PRODUCT_CARD_COLUMNS).eq(+ 'matched_brand_role',+ 'competitor',+ ),+ );- const { data, error } = await query;- if (error) throw new Error(error.message);-- return aggregateProducts(data || [], competitorMap);+ return aggregateProducts(data, competitorMap);}export async function getCompetitorSummary(@@ -588,31 +491,25 @@ export async function getCompetitorSummary(totalCardsQuery = totalCardsQuery.in('platform', filters.platforms);if (filters.regions?.length) totalCardsQuery = totalCardsQuery.in('region', filters.regions);- let competitorCardsQuery = supabase- .from('prompt_result_shopping_cards')- .select('matched_brand_id, product_title, product_brand')- .eq('brand_id', brandId)- .eq('matched_brand_role', 'competitor');-- if (from) competitorCardsQuery = competitorCardsQuery.gte('created_at', from);- if (expandedTo) competitorCardsQuery = competitorCardsQuery.lte('created_at', expandedTo);- if (filters.platforms?.length)- competitorCardsQuery = competitorCardsQuery.in('platform', filters.platforms);- if (filters.regions?.length)- competitorCardsQuery = competitorCardsQuery.in('region', filters.regions);+ const competitorCardsQuery = fetchAllCards<{+ matched_brand_id: string | null;+ product_title: string | null;+ product_brand: string | null;+ }>(() =>+ cardsInWindow(supabase, brandId, filters, 'matched_brand_id, product_title, product_brand').eq(+ 'matched_brand_role',+ 'competitor',+ ),+ );const competitorsQuery = supabase.from('competitors').select('id, name, domain').eq('brand_id', brandId);- const [- { count: totalCount },- { data: competitorCards, error: cardsError },- { data: competitors, error: compError },- ] = await Promise.all([totalCardsQuery, competitorCardsQuery, competitorsQuery]);+ const [{ count: totalCount }, competitorCards, { data: competitors, error: compError }] =+ await Promise.all([totalCardsQuery, competitorCardsQuery, competitorsQuery]);- if (cardsError) throw new Error(cardsError.message);if (compError) throw new Error(compError.message);const total = totalCount || 0;@@ -705,27 +602,7 @@ function formatPrice(amount: number | null, currency: string | null): string {return isSymbolWord ? `${formattedAmount} ${symbol}` : `${symbol}${formattedAmount}`;}-export async function getCardEligiblePrompts(- brandId: string,- filters: ShoppingFilters,-): Promise<CardEligiblePromptsResponse> {- const supabase = await createClient();- const { from, to } = resolveDateRange(filters);- const expandedTo = expandDateToEndOfDay(to);-- // 1. Get total number of tracked prompts for the brand- const { count: totalTrackedPrompts, error: countError } = await supabase- .from('prompts')- .select('id, prompt_sets!inner(brand_id)', { count: 'exact', head: true })- .eq('prompt_sets.brand_id', brandId);-- if (countError) throw new Error(countError.message);-- // 2. Fetch all normalized shopping cards matching the brand and filters- let query = supabase- .from('prompt_result_shopping_cards')- .select(- `+const ELIGIBLE_CARD_COLUMNS = `id,matched_brand_role,platform,@@ -750,17 +627,26 @@ export async function getCardEligiblePrompts()))- `,- )- .eq('brand_id', brandId);+ `;- if (from) query = query.gte('created_at', from);- if (expandedTo) query = query.lte('created_at', expandedTo);- if (filters.platforms?.length) query = query.in('platform', filters.platforms);- if (filters.regions?.length) query = query.in('region', filters.regions);+export async function getCardEligiblePrompts(+ brandId: string,+ filters: ShoppingFilters,+): Promise<CardEligiblePromptsResponse> {+ const supabase = await createClient();++ // 1. Get total number of tracked prompts for the brand+ const { count: totalTrackedPrompts, error: countError } = await supabase+ .from('prompts')+ .select('id, prompt_sets!inner(brand_id)', { count: 'exact', head: true })+ .eq('prompt_sets.brand_id', brandId);- const { data: cards, error: cardsError } = await query;- if (cardsError) throw new Error(cardsError.message);+ if (countError) throw new Error(countError.message);++ // 2. Fetch all normalized shopping cards matching the brand and filters+ const cards = await fetchAllCards<unknown>(() =>+ cardsInWindow(supabase, brandId, filters, ELIGIBLE_CARD_COLUMNS),+ );const promptMap = new Map<string,diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 8a21ec040..6f13516c3 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3171,6 +3171,24 @@ export type Database = {}Returns: Json}+ shopping_filter_options: {+ Args: { p_brand_id: string }+ Returns: {+ platforms: string[]+ regions: string[]+ }[]+ }+ shopping_overview: {+ Args: {+ p_brand_id: string+ p_from?: string+ p_platforms?: string[]+ p_regions?: string[]+ p_to?: string+ p_trend_from?: string+ }+ Returns: Json+ }signals_summary: {Args: { p_brand_id: string; p_from: string; p_to: string }Returns: Json