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
perf(citations): serve the Gaps tab from daily rollups (#1257)
Gökhan Geyik34f7efa9e7fb
diff --git a/server/src/lib/citations-rollups.js b/server/src/lib/citations-rollups.jsindex 78f2a9657..f499ffba1 100644--- a/server/src/lib/citations-rollups.js+++ b/server/src/lib/citations-rollups.js@@ -6,7 +6,8 @@* thing that writes them, through the `refresh_citations_daily` SQL function.* It rides on the Insights rollup callers (insights-rollups.js): a run's days* are refreshed when the run stamps, and the daily sweep re-refreshes the- * trailing days and rebuilds brands whose prompts changed topic.+ * trailing days and rebuilds brands whose prompts changed topic or whose+ * domains changed.** Best-effort like the Insights rollups: the rows are derived state, always* recomputable from prompt_result_citations, so a failure costs freshness@@ -96,9 +97,12 @@ let rebuildInFlight = null;/*** Rebuild the brands queued by a topic move (the trigger on prompts in- * 00118). Topic is stored in the rollup rows, so a prompt moved to another- * topic leaves its history under the old one until this runs. A queue entry- * is cleared only if nothing re-queued the brand while it was rebuilt.+ * 00118) or a brand domain change (the trigger on brand_domains in 00203).+ * Topic is stored in the rollup rows, so a prompt moved to another topic+ * leaves its history under the old one until this runs; and Competitor Gaps'+ * rows record whether the brand was present, which its own domains decide.+ * A queue entry is cleared only if nothing re-queued the brand while it was+ * rebuilt.** Called on its own schedule and at the end of the nightly sweep. Runs never* overlap: a call made while one is in progress returns at once with@@ -136,12 +140,12 @@ async function drainRebuildQueue() {rebuilt++;} catch (err) {failed++;- logger.error({ err, brandId }, '[citations-rollups] topic rebuild failed');+ logger.error({ err, brandId }, '[citations-rollups] queued rebuild failed');}}if (rebuilt + failed > 0) {- logger.info({ rebuilt, failed }, '[citations-rollups] topic rebuilds complete');+ logger.info({ rebuilt, failed }, '[citations-rollups] queued rebuilds complete');}return { rebuilt, failed };}diff --git a/server/src/scripts/backfill-citations-daily.js b/server/src/scripts/backfill-citations-daily.jsindex e0307a13e..fa5f7bf9d 100644--- a/server/src/scripts/backfill-citations-daily.js+++ b/server/src/scripts/backfill-citations-daily.js@@ -1,5 +1,6 @@/**- * Backfill the Citations daily rollups (00118) from historical citations.+ * Backfill the Citations daily rollups (00118, and Competitor Gaps' rows from+ * 00203) from historical citations.** Run: `node src/scripts/backfill-citations-daily.js`*diff --git a/supabase/migrations/00203_citation_gap_rollups.sql b/supabase/migrations/00203_citation_gap_rollups.sqlnew file mode 100644index 000000000..bc4b8e99d--- /dev/null+++ b/supabase/migrations/00203_citation_gap_rollups.sql@@ -0,0 +1,520 @@+-- ============================================================================+-- 00203: Competitor Gaps from the daily rollups (#1002)+-- ============================================================================+-- The Citations page's Domains and URLs tabs read daily rollups since 00118,+-- but the Gaps tab still ran citation_gap_domains and+-- citation_competitor_sources (00111) on every load. Both scan every answer+-- in the window and every citation of those answers, and unpack+-- competitor_mentions. On the largest brand a 30-day window took 69 s, far+-- past the authenticated role's 8 s statement timeout, so the tab failed.+--+-- The existing rollups can't answer these reads, because the measures are+-- per answer:+-- * present: the brand is mentioned, or one of its domains is cited in the+-- same answer;+-- * gap: a competitor is mentioned and the brand isn't present;+-- * weight: each answer splits 1 between the distinct domains it cites.+--+-- refresh_citations_daily now writes them too, at the same grain as+-- citation_domain_daily (day, model_used, platform, region, topic_id):+-- * citation_result_daily.present_answers, next to answers, for+-- our_answer_count;+-- * citation_gap_domain_daily, per domain: gap answers, their weight, and+-- present answers (appears_in_ours);+-- * citation_gap_competitor_daily, per (domain, competitor): the answers+-- mentioning the competitor that cite the domain and their weight (for+-- citation_competitor_sources), and how many of them are gap answers+-- (for competitor_names).+-- All three are written from one pass over the day range's answers and+-- citations.+--+-- Kept at read time, as the raw functions do:+-- * tagging a domain as the brand's or a competitor's, against the lists+-- the page sends, so a change to either is a filter;+-- * competitor names, from the live competitors table, falling back to the+-- name in the answer when the competitor is gone.+--+-- Whether the brand is present depends on its own domains when the rows are+-- written. A change to brand_domains therefore queues the brand in+-- citation_rollup_rebuilds, as a topic move does (00118), and the server's+-- rebuild drain (every five minutes) recomputes its history.+--+-- A single-prompt filter stays on the raw functions, as on the other tabs.+--+-- Rollout: apply, run `node src/scripts/backfill-citations-daily.js` so the+-- history gets the new columns and rows, then deploy the web.++-- ─── Tables ─────────────────────────────────────────────────────────────────++alter table public.citation_result_daily+ add column if not exists present_answers integer not null default 0;++comment on column public.citation_result_daily.present_answers is+ 'Answers where the brand is present: mentioned, or one of its brand_domains cited (#1002).';++create table if not exists public.citation_gap_domain_daily (+ brand_id uuid not null references public.brands(id) on delete cascade,+ day date not null,+ model_used text,+ platform text,+ region text,+ topic_id uuid,+ domain text not null,+ -- Answers citing the domain where a competitor is mentioned and the brand+ -- isn't present.+ gap_answers integer not null,+ -- The same answers' weight: each answer's 1 split between its domains.+ gap_strength double precision not null,+ -- Answers citing the domain where the brand is present.+ present_answers integer not null+);++create index if not exists idx_citation_gap_domain_daily_brand_day+ on public.citation_gap_domain_daily (brand_id, day);++create unique index if not exists citation_gap_domain_daily_key+ on public.citation_gap_domain_daily (brand_id, day, model_used, platform, region, topic_id, domain)+ nulls not distinct;++create table if not exists public.citation_gap_competitor_daily (+ brand_id uuid not null references public.brands(id) on delete cascade,+ day date not null,+ model_used text,+ platform text,+ region text,+ topic_id uuid,+ domain text not null,+ -- competitor_mentions[].competitor_id; text, as the answers carry it.+ competitor_id text,+ -- The name the answer gave, for a competitor no longer in competitors.+ competitor_name text not null,+ -- Answers mentioning the competitor that cite the domain, and their weight.+ answers integer not null,+ strength double precision not null,+ -- Of those, the answers where the brand isn't present.+ gap_answers integer not null+);++create index if not exists idx_citation_gap_competitor_daily_brand_day+ on public.citation_gap_competitor_daily (brand_id, day);++create unique index if not exists citation_gap_competitor_daily_key+ on public.citation_gap_competitor_daily+ (brand_id, day, model_used, platform, region, topic_id, domain, competitor_id)+ nulls not distinct;++-- ─── RLS, as the other citation rollups ─────────────────────────────────────++alter table public.citation_gap_domain_daily enable row level security;+alter table public.citation_gap_competitor_daily enable row level security;++drop policy if exists "Users can read own org citation gap domain rollups"+ on public.citation_gap_domain_daily;+create policy "Users can read own org citation gap domain rollups"+ on public.citation_gap_domain_daily for select+ using (brand_id in (+ select b.id from public.brands b+ join public.profiles p on p.organization_id = b.organization_id+ where p.id = auth.uid()));++drop policy if exists "Users can read own org citation gap competitor rollups"+ on public.citation_gap_competitor_daily;+create policy "Users can read own org citation gap competitor rollups"+ on public.citation_gap_competitor_daily for select+ using (brand_id in (+ select b.id from public.brands b+ join public.profiles p on p.organization_id = b.organization_id+ where p.id = auth.uid()));++-- ─── Refresh ────────────────────────────────────────────────────────────────+-- 00199's body, plus the gap rows. citation_result_daily's insert moves into+-- the gap pass, which already has each answer's bucket and presence.++create or replace function public.refresh_citations_daily(+ p_brand_id uuid,+ p_day_from date,+ p_day_to date+) returns void+language plpgsql+set search_path to 'public'+set work_mem to '256MB'+as $$+declare+ ts_from timestamptz := p_day_from::timestamp at time zone 'utc';+ ts_to timestamptz := (p_day_to + 1)::timestamp at time zone 'utc';+ v_brand_domains text[];+begin+ -- One refresh per brand at a time (00199); the lock releases at commit.+ perform pg_advisory_xact_lock(+ hashtextextended('refresh_citations_daily:' || p_brand_id::text, 0));++ delete from public.citation_result_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_domain_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_url_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_gap_domain_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_gap_competitor_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;++ -- The brand's own domains as the page sends them (normalizeDomain in+ -- web/src/lib/citations/classify.ts): lower case, no scheme, no www.+ select coalesce(array_agg(distinct x.d) filter (where x.d is not null and x.d <> ''), '{}')+ into v_brand_domains+ from (+ select substring(lower(trim(bd.domain)) from '^(?:[a-z][a-z0-9+.-]*://)?(?:www\.)?([^/?#\s:]+)') as d+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ) x;++ with ans as materialized (+ select pr.id as rid,+ (pr.created_at at time zone 'utc')::date as day,+ pr.model_used, pr.platform, pr.region, p.topic_id,+ coalesce(pr.mention_count, 0) > 0 as we_mention,+ coalesce(+ jsonb_path_exists(pr.competitor_mentions, '$[*] ? (@.mention_count > 0)'),+ false+ ) as comp_present,+ pr.competitor_mentions+ from public.prompt_results pr+ left join public.prompts p on p.id = pr.prompt_id+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping' -- #155 — isolate from analytics+ and pr.created_at >= ts_from and pr.created_at < ts_to+ ),+ -- The distinct domains each answer cites. A citation row carries its+ -- answer's created_at, so the citation index bounds the scan.+ adom as materialized (+ select distinct c.prompt_result_id as rid, cu.domain as d+ from public.prompt_result_citations c+ join public.citation_urls cu on cu.id = c.url_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ ),+ dtags as materialized (+ select s.d,+ exists (select 1 from unnest(v_brand_domains) e+ where s.d = e or right(s.d, length(e) + 1) = '.' || e) as is_you+ from (select distinct d from adom) s+ ),+ per_answer as (+ select ad.rid, count(*) as n, bool_or(t.is_you) as you_cited+ from adom ad join dtags t on t.d = ad.d+ group by ad.rid+ ),+ flags as materialized (+ select a.rid, a.day, a.model_used, a.platform, a.region, a.topic_id,+ a.comp_present, a.competitor_mentions,+ a.we_mention or coalesce(pa.you_cited, false) as present,+ a.comp_present and not (a.we_mention or coalesce(pa.you_cited, false)) as gap,+ coalesce(1.0 / pa.n, 0)::float8 as w+ from ans a+ left join per_answer pa on pa.rid = a.rid+ ),+ ins_result as (+ insert into public.citation_result_daily (+ brand_id, day, model_used, platform, region, topic_id, answers, present_answers)+ select p_brand_id, f.day, f.model_used, f.platform, f.region, f.topic_id,+ count(*), count(*) filter (where f.present)+ from flags f+ group by f.day, f.model_used, f.platform, f.region, f.topic_id+ ),+ ins_gap_domain as (+ insert into public.citation_gap_domain_daily (+ brand_id, day, model_used, platform, region, topic_id, domain,+ gap_answers, gap_strength, present_answers)+ select p_brand_id, f.day, f.model_used, f.platform, f.region, f.topic_id, ad.d,+ count(*) filter (where f.gap),+ coalesce(sum(f.w) filter (where f.gap), 0),+ count(*) filter (where f.present)+ from adom ad+ join flags f on f.rid = ad.rid+ group by f.day, f.model_used, f.platform, f.region, f.topic_id, ad.d+ having count(*) filter (where f.gap or f.present) > 0+ ),+ mentions as (+ select f.rid, f.day, f.model_used, f.platform, f.region, f.topic_id, f.gap, f.w,+ x.e ->> 'competitor_id' as competitor_id,+ coalesce(nullif(trim(x.e ->> 'name'), ''), 'Competitor') as competitor_name+ from flags f+ cross join lateral jsonb_array_elements(f.competitor_mentions) x(e)+ where f.comp_present+ and coalesce((x.e ->> 'mention_count')::numeric, 0) > 0+ )+ insert into public.citation_gap_competitor_daily (+ brand_id, day, model_used, platform, region, topic_id, domain,+ competitor_id, competitor_name, answers, strength, gap_answers)+ select p_brand_id, m.day, m.model_used, m.platform, m.region, m.topic_id, ad.d,+ m.competitor_id, max(m.competitor_name),+ count(distinct m.rid), sum(m.w),+ count(distinct m.rid) filter (where m.gap)+ from mentions m+ join adom ad on ad.rid = m.rid+ group by m.day, m.model_used, m.platform, m.region, m.topic_id, ad.d, m.competitor_id;++ insert into public.citation_domain_daily (+ brand_id, day, model_used, platform, region, topic_id, domain, citations, results)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id, cu.domain,+ count(*),+ count(distinct c.prompt_result_id)+ from public.prompt_result_citations c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ left join public.prompts p on p.id = pr.prompt_id+ join public.citation_urls cu on cu.id = c.url_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ and pr.platform <> 'chatgpt-shopping'+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id, cu.domain;++ insert into public.citation_url_daily (+ brand_id, day, model_used, platform, region, topic_id, url_id, citations, results)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id, c.url_id,+ count(*),+ count(distinct c.prompt_result_id)+ from public.prompt_result_citations c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ left join public.prompts p on p.id = pr.prompt_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ and pr.platform <> 'chatgpt-shopping'+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id, c.url_id;+end;+$$;++revoke execute on function public.refresh_citations_daily(uuid, date, date)+ from public, anon, authenticated;++-- ─── A brand domain change queues a rebuild ─────────────────────────────────++create or replace function public.queue_citation_rollup_rebuild_on_domains()+returns trigger+language plpgsql+security definer+set search_path to 'public'+as $$+declare+ v_brands uuid[];+begin+ if tg_op = 'INSERT' then+ v_brands := array[new.brand_id];+ elsif tg_op = 'DELETE' then+ v_brands := array[old.brand_id];+ elsif new.domain is not distinct from old.domain and new.brand_id = old.brand_id then+ return null;+ else+ v_brands := array[old.brand_id, new.brand_id];+ end if;+ -- Only for a brand that still exists: a brand delete removes the brand+ -- before its domains, so the cascade queues nothing.+ insert into public.citation_rollup_rebuilds (brand_id)+ select b.id from public.brands b where b.id = any(v_brands)+ on conflict (brand_id) do update set requested_at = now();+ return null;+end;+$$;++revoke execute on function public.queue_citation_rollup_rebuild_on_domains()+ from public, anon, authenticated;++drop trigger if exists brand_domains_queue_citation_rollup_rebuild on public.brand_domains;+create trigger brand_domains_queue_citation_rollup_rebuild+ after insert or update or delete on public.brand_domains+ for each row execute function public.queue_citation_rollup_rebuild_on_domains();++-- ─── Reads ──────────────────────────────────────────────────────────────────+-- Same rows as the raw functions for whole-day windows. Security definer with+-- the organization check made explicit (00119), and plpgsql with+-- force_custom_plan so each call is planned for its own brand and window.++create or replace function public.citation_gap_domains_daily(+ p_brand_id uuid,+ p_brand_domains text[],+ p_competitor_domains text[],+ p_day_from date default null,+ p_day_to date default null,+ p_models text[] default null,+ p_regions text[] default null,+ p_topic_ids uuid[] default null+)+returns table (+ domain text,+ competitor_answers bigint,+ appears_in_ours boolean,+ strength double precision,+ competitor_names text[],+ our_answer_count bigint,+ total_answers bigint+)+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return;+ end if;++ return query+ with g as (+ select d.domain as dm,+ sum(d.gap_answers)::bigint as ga,+ sum(d.gap_strength) as gs,+ sum(d.present_answers)::bigint as pa+ from public.citation_gap_domain_daily d+ where d.brand_id = p_brand_id+ and (p_day_from is null or d.day >= p_day_from)+ and (p_day_to is null or d.day <= p_day_to)+ and (p_models is null or d.model_used = any(p_models))+ and (p_regions is null or d.region = any(p_regions))+ and (p_topic_ids is null or d.topic_id = any(p_topic_ids))+ group by d.domain+ ),+ -- Third-party domains only: neither the brand's nor a competitor's.+ third_party as (+ select g.* from g+ where not exists (select 1 from unnest(p_brand_domains) e+ where g.dm = e or right(g.dm, length(e) + 1) = '.' || e)+ and not exists (select 1 from unnest(p_competitor_domains) e+ where g.dm = e or right(g.dm, length(e) + 1) = '.' || e)+ ),+ -- The competitors mentioned in the gap answers citing each domain.+ names as (+ select n.dm, array_agg(distinct n.cname order by n.cname) as names+ from (+ select c.domain as dm,+ case when co.id is not null+ then coalesce(nullif(trim(co.name), ''), 'Competitor')+ else c.competitor_name+ end as cname+ from public.citation_gap_competitor_daily c+ left join public.competitors co+ on co.brand_id = p_brand_id and co.id::text = c.competitor_id+ where c.brand_id = p_brand_id+ and c.gap_answers > 0+ and (p_day_from is null or c.day >= p_day_from)+ and (p_day_to is null or c.day <= p_day_to)+ and (p_models is null or c.model_used = any(p_models))+ and (p_regions is null or c.region = any(p_regions))+ and (p_topic_ids is null or c.topic_id = any(p_topic_ids))+ ) n+ group by n.dm+ ),+ totals as (+ select coalesce(sum(r.answers), 0)::bigint as total_answers,+ coalesce(sum(r.present_answers), 0)::bigint as our_answer_count+ from public.citation_result_daily r+ where r.brand_id = p_brand_id+ and (p_day_from is null or r.day >= p_day_from)+ and (p_day_to is null or r.day <= p_day_to)+ and (p_models is null or r.model_used = any(p_models))+ and (p_regions is null or r.region = any(p_regions))+ and (p_topic_ids is null or r.topic_id = any(p_topic_ids))+ )+ select t.dm, t.ga, t.pa > 0, coalesce(t.gs, 0)::float8,+ coalesce(n.names, '{}'::text[]), tt.our_answer_count, tt.total_answers+ from third_party t+ left join names n on n.dm = t.dm+ cross join totals tt+ where t.ga > 0 or t.pa > 0+ union all+ select null, 0::bigint, false, 0::float8, '{}'::text[], tt.our_answer_count, tt.total_answers+ from totals tt;+end;+$$;++create or replace function public.citation_competitor_sources_daily(+ p_brand_id uuid,+ p_brand_domains text[],+ p_competitor_domains text[],+ p_day_from date default null,+ p_day_to date default null,+ p_models text[] default null,+ p_regions text[] default null,+ p_topic_ids uuid[] default null+)+returns table (+ competitor_id text,+ domain text,+ answers_feeding bigint,+ strength double precision+)+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return;+ end if;++ return query+ with agg as materialized (+ select c.competitor_id as cid, c.domain as dm,+ sum(c.answers)::bigint as af,+ sum(c.strength)::float8 as st+ from public.citation_gap_competitor_daily c+ where c.brand_id = p_brand_id+ and (p_day_from is null or c.day >= p_day_from)+ and (p_day_to is null or c.day <= p_day_to)+ and (p_models is null or c.model_used = any(p_models))+ and (p_regions is null or c.region = any(p_regions))+ and (p_topic_ids is null or c.topic_id = any(p_topic_ids))+ group by c.competitor_id, c.domain+ ),+ -- Third-party domains only, tagged once per domain.+ third_party as (+ select s.dm+ from (select distinct dm from agg) s+ where not exists (select 1 from unnest(p_brand_domains) e+ where s.dm = e or right(s.dm, length(e) + 1) = '.' || e)+ and not exists (select 1 from unnest(p_competitor_domains) e+ where s.dm = e or right(s.dm, length(e) + 1) = '.' || e)+ )+ select a.cid, a.dm, a.af, a.st+ from agg a+ join third_party t on t.dm = a.dm;+end;+$$;++revoke all on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) from public, anon;+grant execute on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) to authenticated, service_role;++revoke all on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) from public, anon;+grant execute on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) to authenticated, service_role;++comment on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) is+ 'Competitor Gaps (#1002): citation_gap_domains over whole UTC days, from the daily rollups. Third-party domains with their gap answers, strength, whether they appear in an answer where the brand is present, and the competitors in those gap answers; plus a null-domain row with the window''s totals. Security definer with an organization check.';++comment on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) is+ 'Competitor Gaps (#1002): citation_competitor_sources over whole UTC days, from the daily rollups. Per competitor, the third-party domains cited in answers that mention it, with the answer count and weight. Security definer with an organization check.';++notify pgrst, 'reload schema';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 36ace93f5..66296caad 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -28030,3 +28030,527 @@ create policy "getting_started_steps: member update"));+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00203_citation_gap_rollups.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00203: Competitor Gaps from the daily rollups (#1002)+-- ============================================================================+-- The Citations page's Domains and URLs tabs read daily rollups since 00118,+-- but the Gaps tab still ran citation_gap_domains and+-- citation_competitor_sources (00111) on every load. Both scan every answer+-- in the window and every citation of those answers, and unpack+-- competitor_mentions. On the largest brand a 30-day window took 69 s, far+-- past the authenticated role's 8 s statement timeout, so the tab failed.+--+-- The existing rollups can't answer these reads, because the measures are+-- per answer:+-- * present: the brand is mentioned, or one of its domains is cited in the+-- same answer;+-- * gap: a competitor is mentioned and the brand isn't present;+-- * weight: each answer splits 1 between the distinct domains it cites.+--+-- refresh_citations_daily now writes them too, at the same grain as+-- citation_domain_daily (day, model_used, platform, region, topic_id):+-- * citation_result_daily.present_answers, next to answers, for+-- our_answer_count;+-- * citation_gap_domain_daily, per domain: gap answers, their weight, and+-- present answers (appears_in_ours);+-- * citation_gap_competitor_daily, per (domain, competitor): the answers+-- mentioning the competitor that cite the domain and their weight (for+-- citation_competitor_sources), and how many of them are gap answers+-- (for competitor_names).+-- All three are written from one pass over the day range's answers and+-- citations.+--+-- Kept at read time, as the raw functions do:+-- * tagging a domain as the brand's or a competitor's, against the lists+-- the page sends, so a change to either is a filter;+-- * competitor names, from the live competitors table, falling back to the+-- name in the answer when the competitor is gone.+--+-- Whether the brand is present depends on its own domains when the rows are+-- written. A change to brand_domains therefore queues the brand in+-- citation_rollup_rebuilds, as a topic move does (00118), and the server's+-- rebuild drain (every five minutes) recomputes its history.+--+-- A single-prompt filter stays on the raw functions, as on the other tabs.+--+-- Rollout: apply, run `node src/scripts/backfill-citations-daily.js` so the+-- history gets the new columns and rows, then deploy the web.++-- ─── Tables ─────────────────────────────────────────────────────────────────++alter table public.citation_result_daily+ add column if not exists present_answers integer not null default 0;++comment on column public.citation_result_daily.present_answers is+ 'Answers where the brand is present: mentioned, or one of its brand_domains cited (#1002).';++create table if not exists public.citation_gap_domain_daily (+ brand_id uuid not null references public.brands(id) on delete cascade,+ day date not null,+ model_used text,+ platform text,+ region text,+ topic_id uuid,+ domain text not null,+ -- Answers citing the domain where a competitor is mentioned and the brand+ -- isn't present.+ gap_answers integer not null,+ -- The same answers' weight: each answer's 1 split between its domains.+ gap_strength double precision not null,+ -- Answers citing the domain where the brand is present.+ present_answers integer not null+);++create index if not exists idx_citation_gap_domain_daily_brand_day+ on public.citation_gap_domain_daily (brand_id, day);++create unique index if not exists citation_gap_domain_daily_key+ on public.citation_gap_domain_daily (brand_id, day, model_used, platform, region, topic_id, domain)+ nulls not distinct;++create table if not exists public.citation_gap_competitor_daily (+ brand_id uuid not null references public.brands(id) on delete cascade,+ day date not null,+ model_used text,+ platform text,+ region text,+ topic_id uuid,+ domain text not null,+ -- competitor_mentions[].competitor_id; text, as the answers carry it.+ competitor_id text,+ -- The name the answer gave, for a competitor no longer in competitors.+ competitor_name text not null,+ -- Answers mentioning the competitor that cite the domain, and their weight.+ answers integer not null,+ strength double precision not null,+ -- Of those, the answers where the brand isn't present.+ gap_answers integer not null+);++create index if not exists idx_citation_gap_competitor_daily_brand_day+ on public.citation_gap_competitor_daily (brand_id, day);++create unique index if not exists citation_gap_competitor_daily_key+ on public.citation_gap_competitor_daily+ (brand_id, day, model_used, platform, region, topic_id, domain, competitor_id)+ nulls not distinct;++-- ─── RLS, as the other citation rollups ─────────────────────────────────────++alter table public.citation_gap_domain_daily enable row level security;+alter table public.citation_gap_competitor_daily enable row level security;++drop policy if exists "Users can read own org citation gap domain rollups"+ on public.citation_gap_domain_daily;+create policy "Users can read own org citation gap domain rollups"+ on public.citation_gap_domain_daily for select+ using (brand_id in (+ select b.id from public.brands b+ join public.profiles p on p.organization_id = b.organization_id+ where p.id = auth.uid()));++drop policy if exists "Users can read own org citation gap competitor rollups"+ on public.citation_gap_competitor_daily;+create policy "Users can read own org citation gap competitor rollups"+ on public.citation_gap_competitor_daily for select+ using (brand_id in (+ select b.id from public.brands b+ join public.profiles p on p.organization_id = b.organization_id+ where p.id = auth.uid()));++-- ─── Refresh ────────────────────────────────────────────────────────────────+-- 00199's body, plus the gap rows. citation_result_daily's insert moves into+-- the gap pass, which already has each answer's bucket and presence.++create or replace function public.refresh_citations_daily(+ p_brand_id uuid,+ p_day_from date,+ p_day_to date+) returns void+language plpgsql+set search_path to 'public'+set work_mem to '256MB'+as $$+declare+ ts_from timestamptz := p_day_from::timestamp at time zone 'utc';+ ts_to timestamptz := (p_day_to + 1)::timestamp at time zone 'utc';+ v_brand_domains text[];+begin+ -- One refresh per brand at a time (00199); the lock releases at commit.+ perform pg_advisory_xact_lock(+ hashtextextended('refresh_citations_daily:' || p_brand_id::text, 0));++ delete from public.citation_result_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_domain_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_url_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_gap_domain_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.citation_gap_competitor_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;++ -- The brand's own domains as the page sends them (normalizeDomain in+ -- web/src/lib/citations/classify.ts): lower case, no scheme, no www.+ select coalesce(array_agg(distinct x.d) filter (where x.d is not null and x.d <> ''), '{}')+ into v_brand_domains+ from (+ select substring(lower(trim(bd.domain)) from '^(?:[a-z][a-z0-9+.-]*://)?(?:www\.)?([^/?#\s:]+)') as d+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ) x;++ with ans as materialized (+ select pr.id as rid,+ (pr.created_at at time zone 'utc')::date as day,+ pr.model_used, pr.platform, pr.region, p.topic_id,+ coalesce(pr.mention_count, 0) > 0 as we_mention,+ coalesce(+ jsonb_path_exists(pr.competitor_mentions, '$[*] ? (@.mention_count > 0)'),+ false+ ) as comp_present,+ pr.competitor_mentions+ from public.prompt_results pr+ left join public.prompts p on p.id = pr.prompt_id+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping' -- #155 — isolate from analytics+ and pr.created_at >= ts_from and pr.created_at < ts_to+ ),+ -- The distinct domains each answer cites. A citation row carries its+ -- answer's created_at, so the citation index bounds the scan.+ adom as materialized (+ select distinct c.prompt_result_id as rid, cu.domain as d+ from public.prompt_result_citations c+ join public.citation_urls cu on cu.id = c.url_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ ),+ dtags as materialized (+ select s.d,+ exists (select 1 from unnest(v_brand_domains) e+ where s.d = e or right(s.d, length(e) + 1) = '.' || e) as is_you+ from (select distinct d from adom) s+ ),+ per_answer as (+ select ad.rid, count(*) as n, bool_or(t.is_you) as you_cited+ from adom ad join dtags t on t.d = ad.d+ group by ad.rid+ ),+ flags as materialized (+ select a.rid, a.day, a.model_used, a.platform, a.region, a.topic_id,+ a.comp_present, a.competitor_mentions,+ a.we_mention or coalesce(pa.you_cited, false) as present,+ a.comp_present and not (a.we_mention or coalesce(pa.you_cited, false)) as gap,+ coalesce(1.0 / pa.n, 0)::float8 as w+ from ans a+ left join per_answer pa on pa.rid = a.rid+ ),+ ins_result as (+ insert into public.citation_result_daily (+ brand_id, day, model_used, platform, region, topic_id, answers, present_answers)+ select p_brand_id, f.day, f.model_used, f.platform, f.region, f.topic_id,+ count(*), count(*) filter (where f.present)+ from flags f+ group by f.day, f.model_used, f.platform, f.region, f.topic_id+ ),+ ins_gap_domain as (+ insert into public.citation_gap_domain_daily (+ brand_id, day, model_used, platform, region, topic_id, domain,+ gap_answers, gap_strength, present_answers)+ select p_brand_id, f.day, f.model_used, f.platform, f.region, f.topic_id, ad.d,+ count(*) filter (where f.gap),+ coalesce(sum(f.w) filter (where f.gap), 0),+ count(*) filter (where f.present)+ from adom ad+ join flags f on f.rid = ad.rid+ group by f.day, f.model_used, f.platform, f.region, f.topic_id, ad.d+ having count(*) filter (where f.gap or f.present) > 0+ ),+ mentions as (+ select f.rid, f.day, f.model_used, f.platform, f.region, f.topic_id, f.gap, f.w,+ x.e ->> 'competitor_id' as competitor_id,+ coalesce(nullif(trim(x.e ->> 'name'), ''), 'Competitor') as competitor_name+ from flags f+ cross join lateral jsonb_array_elements(f.competitor_mentions) x(e)+ where f.comp_present+ and coalesce((x.e ->> 'mention_count')::numeric, 0) > 0+ )+ insert into public.citation_gap_competitor_daily (+ brand_id, day, model_used, platform, region, topic_id, domain,+ competitor_id, competitor_name, answers, strength, gap_answers)+ select p_brand_id, m.day, m.model_used, m.platform, m.region, m.topic_id, ad.d,+ m.competitor_id, max(m.competitor_name),+ count(distinct m.rid), sum(m.w),+ count(distinct m.rid) filter (where m.gap)+ from mentions m+ join adom ad on ad.rid = m.rid+ group by m.day, m.model_used, m.platform, m.region, m.topic_id, ad.d, m.competitor_id;++ insert into public.citation_domain_daily (+ brand_id, day, model_used, platform, region, topic_id, domain, citations, results)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id, cu.domain,+ count(*),+ count(distinct c.prompt_result_id)+ from public.prompt_result_citations c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ left join public.prompts p on p.id = pr.prompt_id+ join public.citation_urls cu on cu.id = c.url_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ and pr.platform <> 'chatgpt-shopping'+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id, cu.domain;++ insert into public.citation_url_daily (+ brand_id, day, model_used, platform, region, topic_id, url_id, citations, results)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id, c.url_id,+ count(*),+ count(distinct c.prompt_result_id)+ from public.prompt_result_citations c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ left join public.prompts p on p.id = pr.prompt_id+ where c.brand_id = p_brand_id+ and c.created_at >= ts_from and c.created_at < ts_to+ and pr.platform <> 'chatgpt-shopping'+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id, c.url_id;+end;+$$;++revoke execute on function public.refresh_citations_daily(uuid, date, date)+ from public, anon, authenticated;++-- ─── A brand domain change queues a rebuild ─────────────────────────────────++create or replace function public.queue_citation_rollup_rebuild_on_domains()+returns trigger+language plpgsql+security definer+set search_path to 'public'+as $$+declare+ v_brands uuid[];+begin+ if tg_op = 'INSERT' then+ v_brands := array[new.brand_id];+ elsif tg_op = 'DELETE' then+ v_brands := array[old.brand_id];+ elsif new.domain is not distinct from old.domain and new.brand_id = old.brand_id then+ return null;+ else+ v_brands := array[old.brand_id, new.brand_id];+ end if;+ -- Only for a brand that still exists: a brand delete removes the brand+ -- before its domains, so the cascade queues nothing.+ insert into public.citation_rollup_rebuilds (brand_id)+ select b.id from public.brands b where b.id = any(v_brands)+ on conflict (brand_id) do update set requested_at = now();+ return null;+end;+$$;++revoke execute on function public.queue_citation_rollup_rebuild_on_domains()+ from public, anon, authenticated;++drop trigger if exists brand_domains_queue_citation_rollup_rebuild on public.brand_domains;+create trigger brand_domains_queue_citation_rollup_rebuild+ after insert or update or delete on public.brand_domains+ for each row execute function public.queue_citation_rollup_rebuild_on_domains();++-- ─── Reads ──────────────────────────────────────────────────────────────────+-- Same rows as the raw functions for whole-day windows. Security definer with+-- the organization check made explicit (00119), and plpgsql with+-- force_custom_plan so each call is planned for its own brand and window.++create or replace function public.citation_gap_domains_daily(+ p_brand_id uuid,+ p_brand_domains text[],+ p_competitor_domains text[],+ p_day_from date default null,+ p_day_to date default null,+ p_models text[] default null,+ p_regions text[] default null,+ p_topic_ids uuid[] default null+)+returns table (+ domain text,+ competitor_answers bigint,+ appears_in_ours boolean,+ strength double precision,+ competitor_names text[],+ our_answer_count bigint,+ total_answers bigint+)+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return;+ end if;++ return query+ with g as (+ select d.domain as dm,+ sum(d.gap_answers)::bigint as ga,+ sum(d.gap_strength) as gs,+ sum(d.present_answers)::bigint as pa+ from public.citation_gap_domain_daily d+ where d.brand_id = p_brand_id+ and (p_day_from is null or d.day >= p_day_from)+ and (p_day_to is null or d.day <= p_day_to)+ and (p_models is null or d.model_used = any(p_models))+ and (p_regions is null or d.region = any(p_regions))+ and (p_topic_ids is null or d.topic_id = any(p_topic_ids))+ group by d.domain+ ),+ -- Third-party domains only: neither the brand's nor a competitor's.+ third_party as (+ select g.* from g+ where not exists (select 1 from unnest(p_brand_domains) e+ where g.dm = e or right(g.dm, length(e) + 1) = '.' || e)+ and not exists (select 1 from unnest(p_competitor_domains) e+ where g.dm = e or right(g.dm, length(e) + 1) = '.' || e)+ ),+ -- The competitors mentioned in the gap answers citing each domain.+ names as (+ select n.dm, array_agg(distinct n.cname order by n.cname) as names+ from (+ select c.domain as dm,+ case when co.id is not null+ then coalesce(nullif(trim(co.name), ''), 'Competitor')+ else c.competitor_name+ end as cname+ from public.citation_gap_competitor_daily c+ left join public.competitors co+ on co.brand_id = p_brand_id and co.id::text = c.competitor_id+ where c.brand_id = p_brand_id+ and c.gap_answers > 0+ and (p_day_from is null or c.day >= p_day_from)+ and (p_day_to is null or c.day <= p_day_to)+ and (p_models is null or c.model_used = any(p_models))+ and (p_regions is null or c.region = any(p_regions))+ and (p_topic_ids is null or c.topic_id = any(p_topic_ids))+ ) n+ group by n.dm+ ),+ totals as (+ select coalesce(sum(r.answers), 0)::bigint as total_answers,+ coalesce(sum(r.present_answers), 0)::bigint as our_answer_count+ from public.citation_result_daily r+ where r.brand_id = p_brand_id+ and (p_day_from is null or r.day >= p_day_from)+ and (p_day_to is null or r.day <= p_day_to)+ and (p_models is null or r.model_used = any(p_models))+ and (p_regions is null or r.region = any(p_regions))+ and (p_topic_ids is null or r.topic_id = any(p_topic_ids))+ )+ select t.dm, t.ga, t.pa > 0, coalesce(t.gs, 0)::float8,+ coalesce(n.names, '{}'::text[]), tt.our_answer_count, tt.total_answers+ from third_party t+ left join names n on n.dm = t.dm+ cross join totals tt+ where t.ga > 0 or t.pa > 0+ union all+ select null, 0::bigint, false, 0::float8, '{}'::text[], tt.our_answer_count, tt.total_answers+ from totals tt;+end;+$$;++create or replace function public.citation_competitor_sources_daily(+ p_brand_id uuid,+ p_brand_domains text[],+ p_competitor_domains text[],+ p_day_from date default null,+ p_day_to date default null,+ p_models text[] default null,+ p_regions text[] default null,+ p_topic_ids uuid[] default null+)+returns table (+ competitor_id text,+ domain text,+ answers_feeding bigint,+ strength double precision+)+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return;+ end if;++ return query+ with agg as materialized (+ select c.competitor_id as cid, c.domain as dm,+ sum(c.answers)::bigint as af,+ sum(c.strength)::float8 as st+ from public.citation_gap_competitor_daily c+ where c.brand_id = p_brand_id+ and (p_day_from is null or c.day >= p_day_from)+ and (p_day_to is null or c.day <= p_day_to)+ and (p_models is null or c.model_used = any(p_models))+ and (p_regions is null or c.region = any(p_regions))+ and (p_topic_ids is null or c.topic_id = any(p_topic_ids))+ group by c.competitor_id, c.domain+ ),+ -- Third-party domains only, tagged once per domain.+ third_party as (+ select s.dm+ from (select distinct dm from agg) s+ where not exists (select 1 from unnest(p_brand_domains) e+ where s.dm = e or right(s.dm, length(e) + 1) = '.' || e)+ and not exists (select 1 from unnest(p_competitor_domains) e+ where s.dm = e or right(s.dm, length(e) + 1) = '.' || e)+ )+ select a.cid, a.dm, a.af, a.st+ from agg a+ join third_party t on t.dm = a.dm;+end;+$$;++revoke all on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) from public, anon;+grant execute on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) to authenticated, service_role;++revoke all on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) from public, anon;+grant execute on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) to authenticated, service_role;++comment on function public.citation_gap_domains_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) is+ 'Competitor Gaps (#1002): citation_gap_domains over whole UTC days, from the daily rollups. Third-party domains with their gap answers, strength, whether they appear in an answer where the brand is present, and the competitors in those gap answers; plus a null-domain row with the window''s totals. Security definer with an organization check.';++comment on function public.citation_competitor_sources_daily(+ uuid, text[], text[], date, date, text[], text[], uuid[]) is+ 'Competitor Gaps (#1002): citation_competitor_sources over whole UTC days, from the daily rollups. Per competitor, the third-party domains cited in answers that mention it, with the answer count and weight. Security definer with an organization check.';++notify pgrst, 'reload schema';+diff --git a/web/src/lib/actions/citations-gaps.test.ts b/web/src/lib/actions/citations-gaps.test.tsnew file mode 100644index 000000000..5c215ed10--- /dev/null+++ b/web/src/lib/actions/citations-gaps.test.ts@@ -0,0 +1,161 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * #1002 — Competitor Gaps reads the daily rollups (00203) like the other+ * Citations tabs, and the raw functions only for a single-prompt filter, or+ * a 24h view before the brand's first completed run.+ */++let rpcCalls: Array<{ fn: string; args: Record<string, unknown> }> = [];+let tablesRead: string[] = [];+let latestRun: { started_at: string; completed_at: string } | null = null;++const TABLE_ROWS: Record<string, unknown[]> = {+ brand_domains: [{ domain: 'www.Acme.example' }],+ competitors: [{ id: 'c1', name: 'Rival', domain: 'https://rival.example/' }],+ prompts: [{ id: 'p-topic' }],+};++function fakeQueryBuilder(table: string) {+ tablesRead.push(table);+ const builder = {+ select: () => builder,+ eq: () => builder,+ in: () => builder,+ not: () => builder,+ order: () => builder,+ limit: () => builder,+ then: (onFulfilled: (v: unknown) => unknown, onRejected?: (e: unknown) => unknown) => {+ const data = table === 'tracking_runs' ? (latestRun ? [latestRun] : []) : TABLE_ROWS[table];+ return Promise.resolve({ data: data ?? [], error: null }).then(onFulfilled, onRejected);+ },+ };+ return builder;+}++function rpcResult(fn: string) {+ if (fn.startsWith('citation_gap_domains')) {+ return [+ {+ domain: null,+ competitor_answers: 0,+ appears_in_ours: false,+ strength: 0,+ competitor_names: [],+ our_answer_count: 4,+ total_answers: 20,+ },+ {+ domain: 'reviews.example',+ competitor_answers: 3,+ appears_in_ours: false,+ strength: 0.75,+ competitor_names: ['Rival'],+ our_answer_count: 4,+ total_answers: 20,+ },+ {+ domain: 'news.example',+ competitor_answers: 5,+ appears_in_ours: true,+ strength: 1.2,+ competitor_names: ['Rival'],+ our_answer_count: 4,+ total_answers: 20,+ },+ ];+ }+ return [+ { competitor_id: 'c1', domain: 'reviews.example', answers_feeding: 3, strength: 0.75 },+ { competitor_id: 'c1', domain: 'news.example', answers_feeding: 6, strength: 1.5 },+ ];+}++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: rpcResult(fn), error: null };+ },+ }),+}));++import { getCitationGaps } from './citations';++beforeEach(() => {+ rpcCalls = [];+ tablesRead = [];+ latestRun = null;+ vi.useFakeTimers();+ vi.setSystemTime(new Date('2026-10-20T12:00:00Z'));+});++afterEach(() => {+ vi.useRealTimers();+});++describe('getCitationGaps', () => {+ it('reads the daily rollups for a preset, over whole UTC days', async () => {+ const gaps = await getCitationGaps('b1', { datePreset: '30d' });++ expect(rpcCalls.map((c) => c.fn).sort()).toEqual([+ 'citation_competitor_sources_daily',+ 'citation_gap_domains_daily',+ ]);+ for (const { args } of rpcCalls) {+ expect(args).toMatchObject({+ p_brand_id: 'b1',+ p_day_from: '2026-09-21',+ p_day_to: '2026-10-20',+ p_brand_domains: ['acme.example'],+ p_competitor_domains: ['rival.example'],+ });+ expect(args).not.toHaveProperty('p_date_from');+ }++ // The rows map as before: a domain seen where we're present is no gap,+ // but still marks the competitor's source as citing us too.+ expect(gaps.totalAnswers).toBe(20);+ expect(gaps.ourAnswerCount).toBe(4);+ expect(gaps.gapDomains.map((g) => g.domain)).toEqual(['reviews.example']);+ expect(gaps.byCompetitor.c1.map((s) => [s.domain, s.alsoCitesUs])).toEqual([+ ['news.example', true],+ ['reviews.example', false],+ ]);+ expect(gaps.competitors).toEqual([{ id: 'c1', name: 'Rival' }]);+ });++ it('passes a topic filter to the rollups without resolving its prompts', async () => {+ await getCitationGaps('b1', { datePreset: '7d', topicIds: ['t1'] });++ expect(tablesRead).not.toContain('prompts');+ expect(rpcCalls.every((c) => c.fn.endsWith('_daily'))).toBe(true);+ expect(rpcCalls[0].args).toMatchObject({ p_topic_ids: ['t1'], p_day_from: '2026-10-14' });+ });++ it('keeps a single-prompt filter on the raw functions', async () => {+ await getCitationGaps('b1', { datePreset: '30d', promptIds: ['p1'] });++ expect(rpcCalls.map((c) => c.fn).sort()).toEqual([+ 'citation_competitor_sources',+ 'citation_gap_domains',+ ]);+ expect(rpcCalls[0].args).toMatchObject({ p_prompt_ids: ['p1'] });+ expect(rpcCalls[0].args).not.toHaveProperty('p_day_from');+ });++ it("reads 24h as the latest completed run's days", async () => {+ latestRun = { started_at: '2026-10-19T23:30:00Z', completed_at: '2026-10-20T01:10:00Z' };+ await getCitationGaps('b1', { datePreset: '24h' });++ expect(rpcCalls.every((c) => c.fn.endsWith('_daily'))).toBe(true);+ expect(rpcCalls[0].args).toMatchObject({ p_day_from: '2026-10-19', p_day_to: '2026-10-20' });+ });++ it('stays on the raw functions for 24h before the first completed run', async () => {+ await getCitationGaps('b1', { datePreset: '24h' });++ expect(rpcCalls.some((c) => c.fn.endsWith('_daily'))).toBe(false);+ });+});diff --git a/web/src/lib/actions/citations.ts b/web/src/lib/actions/citations.tsindex 7e9dcb1ec..34d24bb69 100644--- a/web/src/lib/actions/citations.ts+++ b/web/src/lib/actions/citations.ts@@ -457,6 +457,9 @@ const GAP_MAX_COMPETITOR_CHIPS = 6;* competitor in an answer where we're absent and never appears in an answer* where we're present. Each co-occurrence is weighted by `1 / distinct sources* in the answer`, so a focused 2-source answer counts more than a 20-source one.+ *+ * Read from the daily rollups (00203) over whole UTC days, as the other tabs+ * are, unless a single prompt is selected (#1002).*/export async function getCitationGaps(brandId: string,@@ -464,10 +467,10 @@ export async function getCitationGaps(): Promise<CitationGaps> {const supabase = await createClient();- const [{ data: brandDomainRows }, { data: competitorRows }, topicPromptIds] = await Promise.all([+ const [{ data: brandDomainRows }, { data: competitorRows }, window] = await Promise.all([supabase.from('brand_domains').select('domain').eq('brand_id', brandId),supabase.from('competitors').select('id, name, domain').eq('brand_id', brandId),- resolveTopicPromptIds(supabase, filters),+ rollupWindow(supabase, brandId, filters),]);const brandDomains = (brandDomainRows ?? []).map((r) => normalizeDomain((r as { domain: string }).domain))@@ -506,14 +509,24 @@ export async function getCitationGaps(strength: number;}- const rpcArgs = {- ...overviewArgs(brandId, filters, topicPromptIds),- p_brand_domains: brandDomains,- p_competitor_domains: competitorDomains,- };+ // Daily rollups (00203) unless the filters need the raw rows, as on the+ // other tabs (see rollupWindow). The raw path resolves a topic to its+ // prompts; the rollups carry topic.+ const domainArgs = { p_brand_domains: brandDomains, p_competitor_domains: competitorDomains };+ const daily = window ? { ...dailyArgs(brandId, filters, window), ...domainArgs } : null;+ const rawArgs = window+ ? null+ : {+ ...overviewArgs(brandId, filters, await resolveTopicPromptIds(supabase, filters)),+ ...domainArgs,+ };const [gapRes, compRes] = await Promise.all([- supabase.rpc('citation_gap_domains', rpcArgs),- supabase.rpc('citation_competitor_sources', rpcArgs),+ daily+ ? supabase.rpc('citation_gap_domains_daily', daily)+ : supabase.rpc('citation_gap_domains', rawArgs!),+ daily+ ? supabase.rpc('citation_competitor_sources_daily', daily)+ : supabase.rpc('citation_competitor_sources', rawArgs!),]);if (gapRes.error) throw new Error(gapRes.error.message);if (compRes.error) throw new Error(compRes.error.message);diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 377f1d408..1daf9ab52 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3064,6 +3064,24 @@ export type Database = {strength: number}[]}+ citation_competitor_sources_daily: {+ Args: {+ p_brand_domains: string[]+ p_brand_id: string+ p_competitor_domains: string[]+ p_day_from?: string+ p_day_to?: string+ p_models?: string[]+ p_regions?: string[]+ p_topic_ids?: string[]+ }+ Returns: {+ answers_feeding: number+ competitor_id: string+ domain: string+ strength: number+ }[]+ }citation_gap_domains: {Args: {p_brand_domains: string[]@@ -3086,6 +3104,27 @@ export type Database = {total_answers: number}[]}+ citation_gap_domains_daily: {+ Args: {+ p_brand_domains: string[]+ p_brand_id: string+ p_competitor_domains: string[]+ p_day_from?: string+ p_day_to?: string+ p_models?: string[]+ p_regions?: string[]+ p_topic_ids?: string[]+ }+ Returns: {+ appears_in_ours: boolean+ competitor_answers: number+ competitor_names: string[]+ domain: string+ our_answer_count: number+ strength: number+ total_answers: number+ }[]+ }citation_url_candidates: {Args: { p_brand_id: string; p_domain: string }Returns: {