Skip to content

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.

Merge commit '47d1f37' into feature/insights-trend

Cihan5de84eb63bd1
diff --cc supabase/schema.sql
index c9bf78ca5,c833d0f19..7a004d561
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@@ -16375,360 -16375,689 +16375,1047 @@@ as $
$$;
-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00135_insights_change_drivers.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- Insights "Why it changed" (VIS-009).
+--
+-- The v2 mockup explains the Visibility change beside the By AI platform
+-- table: the platforms, topics and prompts that moved it most, each with its
+-- own score before → after and its effect on the overall score, plus a line
+-- for the prompts added this period ("509 new prompts · Sep 29 · own
+-- baseline, not counted").
+--
+-- These return the Visibility components per prompt and per (model,
+-- platform) in both comparison windows, over the prompts answered in both
+-- when p_same_prompts (00130), with each prompt's text and topic; the app
+-- folds engines into platforms and prompts into topics, and scores every
+-- slice with the card's formula. They also count the prompts answered this
+-- period but not the one before, and when the ones created this period were
+-- added. The rollups serve whole days, so the box costs what the cards cost;
+-- prompt_results serves the 24h and topic views, like
+-- insights_platform_compare (00132).
+-- `complete` is false while the prompt-grain rollups behind the window are
+-- not backfilled (00130).
+
+create or replace function public.insights_change_drivers_daily(
+ p_brand_id uuid,
+ p_cur_from date,
+ p_cur_to date,
+ p_prev_from date,
+ p_prev_to date,
+ p_models text[] default null,
+ p_region text default null,
+ p_same_prompts boolean default true
+)
+returns jsonb
+language sql
+stable
+security invoker
+set search_path to 'public'
+as $$
+ with prompt_rows as (
+ select d.*,
+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win
+ from public.insights_prompt_daily d
+ where d.brand_id = p_brand_id
+ and (p_models is null or d.model_used = any (p_models))
+ and (p_region is null or d.region = p_region)
+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(win = 'cur') as in_cur,
+ bool_or(win = 'prev') as in_prev
+ from prompt_rows
+ group by prompt_id
+ ),
+ scoped as (
+ select r.* from prompt_rows r join presence p using (prompt_id)
+ where not p_same_prompts or (p.in_cur and p.in_prev)
+ ),
+ by_prompt as (
+ select prompt_id, win,
+ sum(answer_count) as answers,
+ sum(mention_answers) as mention_answers,
+ sum(citation_answers) as citation_answers,
+ sum(sum_inv_position) as sum_inv_position,
+ sum(position_count) as position_count
+ from scoped
+ group by prompt_id, win
+ ),
+ by_engine as (
+ select model_used, platform, win,
+ sum(answer_count) as answers,
+ sum(mention_answers) as mention_answers,
+ sum(citation_answers) as citation_answers,
+ sum(sum_inv_position) as sum_inv_position,
+ sum(position_count) as position_count
+ from scoped
+ group by model_used, platform, win
+ ),
+ added as (
+ select p.created_at
+ from presence pr
+ join public.prompts p on p.id = pr.prompt_id
+ where pr.in_cur and not pr.in_prev
+ )
+ select jsonb_build_object(
+ 'complete', not exists (select 1 from prompt_rows where mention_answers is null),
+ 'prompts', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'prompt_id', b.prompt_id,
+ 'win', b.win,
+ 'answers', b.answers,
+ 'mention_answers', b.mention_answers,
+ 'citation_answers', b.citation_answers,
+ 'sum_inv_position', b.sum_inv_position,
+ 'position_count', b.position_count))
+ from by_prompt b),
+ '[]'::jsonb),
+ 'engines', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'model_used', e.model_used,
+ 'platform', e.platform,
+ 'win', e.win,
+ 'answers', e.answers,
+ 'mention_answers', e.mention_answers,
+ 'citation_answers', e.citation_answers,
+ 'sum_inv_position', e.sum_inv_position,
+ 'position_count', e.position_count))
+ from by_engine e),
+ '[]'::jsonb),
+ 'prompt_info', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'id', p.id,
+ 'text', p.text,
+ 'topic_id', p.topic_id,
+ 'topic_name', t.name))
+ from public.prompts p
+ left join public.topics t on t.id = p.topic_id
+ where p.id in (select prompt_id from by_prompt)),
+ '[]'::jsonb),
+ -- Dates only for prompts created this period: one answered again after
+ -- a pause is new to the comparison, not newly added.
+ 'new_prompts', (select jsonb_build_object(
+ 'count', count(*),
+ 'first_added', min(created_at) filter (
+ where created_at >= (p_cur_from::timestamp at time zone 'utc')),
+ 'last_added', max(created_at) filter (
+ where created_at >= (p_cur_from::timestamp at time zone 'utc')))
+ from added)
+ );
+$$;
+
+create or replace function public.insights_change_drivers(
+ p_brand_id uuid,
+ p_cur_from timestamptz,
+ p_cur_to timestamptz,
+ p_prev_from timestamptz,
+ p_prev_to timestamptz,
+ p_models text[] default null,
+ p_region text default null,
+ p_topic_id uuid default null,
+ p_same_prompts boolean default true
+)
+returns jsonb
+language sql
+stable
+security invoker
+set search_path to 'public'
+as $$
+ with filtered as (
+ select pr.prompt_id, pr.model_used, pr.platform, pr.created_at,
+ pr.mention_count, pr.citation_count, pr.mention_position
+ from public.prompt_results pr
+ where pr.brand_id = p_brand_id
+ and pr.platform <> 'chatgpt-shopping'
+ and (p_models is null or pr.model_used = any (p_models))
+ and (p_region is null or pr.region = p_region)
+ and pr.created_at >= p_prev_from
+ and pr.created_at <= p_cur_to
+ and (p_topic_id is null or exists (
+ select 1 from public.prompts p
+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))
+ ),
+ -- Each window selected on its own, inclusive at both ends like the card.
+ windowed as (
+ select 'cur' as win, f.* from filtered f
+ where f.created_at >= p_cur_from and f.created_at <= p_cur_to
+ union all
+ select 'prev' as win, f.* from filtered f
+ where f.created_at >= p_prev_from and f.created_at <= p_prev_to
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(win = 'cur') as in_cur,
+ bool_or(win = 'prev') as in_prev
+ from windowed
+ group by prompt_id
+ ),
+ scoped as (
+ select w.* from windowed w join presence p using (prompt_id)
+ where not p_same_prompts or (p.in_cur and p.in_prev)
+ ),
+ by_prompt as (
+ select prompt_id, win,
+ count(*) as answers,
+ count(*) filter (where mention_count > 0) as mention_answers,
+ count(*) filter (where citation_count > 0) as citation_answers,
+ sum(1.0 / mention_position) filter (where mention_position is not null)
+ as sum_inv_position,
+ count(*) filter (where mention_position is not null) as position_count
+ from scoped
+ group by prompt_id, win
+ ),
+ by_engine as (
+ select model_used, platform, win,
+ count(*) as answers,
+ count(*) filter (where mention_count > 0) as mention_answers,
+ count(*) filter (where citation_count > 0) as citation_answers,
+ sum(1.0 / mention_position) filter (where mention_position is not null)
+ as sum_inv_position,
+ count(*) filter (where mention_position is not null) as position_count
+ from scoped
+ group by model_used, platform, win
+ ),
+ added as (
+ select p.created_at
+ from presence pr
+ join public.prompts p on p.id = pr.prompt_id
+ where pr.in_cur and not pr.in_prev
+ )
+ select jsonb_build_object(
+ 'complete', true,
+ 'prompts', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'prompt_id', b.prompt_id,
+ 'win', b.win,
+ 'answers', b.answers,
+ 'mention_answers', b.mention_answers,
+ 'citation_answers', b.citation_answers,
+ 'sum_inv_position', b.sum_inv_position,
+ 'position_count', b.position_count))
+ from by_prompt b),
+ '[]'::jsonb),
+ 'engines', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'model_used', e.model_used,
+ 'platform', e.platform,
+ 'win', e.win,
+ 'answers', e.answers,
+ 'mention_answers', e.mention_answers,
+ 'citation_answers', e.citation_answers,
+ 'sum_inv_position', e.sum_inv_position,
+ 'position_count', e.position_count))
+ from by_engine e),
+ '[]'::jsonb),
+ 'prompt_info', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'id', p.id,
+ 'text', p.text,
+ 'topic_id', p.topic_id,
+ 'topic_name', t.name))
+ from public.prompts p
+ left join public.topics t on t.id = p.topic_id
+ where p.id in (select prompt_id from by_prompt)),
+ '[]'::jsonb),
+ -- Dates only for prompts created this period: one answered again after
+ -- a pause is new to the comparison, not newly added.
+ 'new_prompts', (select jsonb_build_object(
+ 'count', count(*),
+ 'first_added', min(created_at) filter (where created_at >= p_cur_from),
+ 'last_added', max(created_at) filter (where created_at >= p_cur_from))
+ from added)
+ );
+$$;
+
+revoke all on function public.insights_change_drivers_daily(
+ uuid, date, date, date, date, text[], text, boolean
+) from public;
+grant execute on function public.insights_change_drivers_daily(
+ uuid, date, date, date, date, text[], text, boolean
+) to authenticated, service_role;
+
+revoke all on function public.insights_change_drivers(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean
+) from public;
+grant execute on function public.insights_change_drivers(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean
+) to authenticated, service_role;
+
+comment on function public.insights_change_drivers_daily(
+ uuid, date, date, date, date, text[], text, boolean
+) is
+ 'Insights "Why it changed" (VIS-009, 00135): Visibility components per prompt and per (model, platform) in both comparison windows, over the prompts answered in both when p_same_prompts, with prompt text and topic, and the prompts new this period, from the prompt-grain rollups. complete=false until they are backfilled (00130).';
+
+comment on function public.insights_change_drivers(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean
+) is
+ 'Insights "Why it changed" (VIS-009, 00135) for timestamp windows (24h, topic views), from prompt_results. Same output as insights_change_drivers_daily.';
+
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00136_insights_trend.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- Insights visibility trend (VIS-007).
+--
+-- The v2 mockup's trend keeps the competitors you pin (up to five) on the
+-- chart whatever the range, and marks the days that explain a step in the
+-- line: prompts added ("+509 prompts added · Sep 29") and Action Center
+-- actions sent ("AC-61 sent · Oct 4").
+--
+-- Pins are saved on the brand, so the whole team sees the same lines; like
+-- every brand setting they're written by admins and managers (the brands
+-- update policy). Ids of competitors deleted since are ignored on read.
+
+alter table public.brands
+ add column if not exists insights_pinned_competitors uuid[] not null default '{}'
+ constraint brands_insights_pinned_competitors_max
+ check (cardinality(insights_pinned_competitors) <= 5);
+
+comment on column public.brands.insights_pinned_competitors is
+ 'Competitors pinned on the Insights visibility trend (VIS-007, 00136), in pin order; at most five.';
+
+-- Per UTC day (the trend's days), the prompts created for the brand (in the
+-- topic, when the trend is filtered to one) and the actions a person sent
+-- from it. Actions the nightly generator created have no actor on their
+-- "created" event and aren't markers.
+create or replace function public.insights_trend_markers(
+ p_brand_id uuid,
+ p_from timestamptz default null,
+ p_to timestamptz default null,
+ p_topic_id uuid default null
+)
+returns jsonb
+language sql
+stable
+security invoker
+set search_path to 'public'
+as $$
+ select jsonb_build_object(
+ 'prompts_added', coalesce(
+ (select jsonb_agg(jsonb_build_object('day', d.day, 'count', d.n) order by d.day)
+ from (
+ select (p.created_at at time zone 'utc')::date as day, count(*) as n
+ from public.prompts p
+ join public.prompt_sets ps on ps.id = p.prompt_set_id
+ where ps.brand_id = p_brand_id
+ and (p_topic_id is null or p.topic_id = p_topic_id)
+ and (p_from is null or p.created_at >= p_from)
+ and (p_to is null or p.created_at <= p_to)
+ group by 1
+ ) d),
+ '[]'::jsonb),
+ 'actions_sent', coalesce(
+ (select jsonb_agg(jsonb_build_object('day', d.day, 'action_nos', d.nos) order by d.day)
+ from (
+ select (a.created_at at time zone 'utc')::date as day,
+ array_agg(a.action_no order by a.action_no) as nos
+ from public.actions a
+ where a.brand_id = p_brand_id
+ and (p_from is null or a.created_at >= p_from)
+ and (p_to is null or a.created_at <= p_to)
+ and exists (
+ select 1 from public.action_events e
+ where e.action_id = a.id
+ and e.event = 'created'
+ and e.actor_id is not null)
+ group by 1
+ ) d),
+ '[]'::jsonb)
+ );
+$$;
+
+revoke all on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid) from public;
+grant execute on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid)
+ to authenticated, service_role;
+
+comment on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid) is
+ 'Insights trend markers (VIS-007, 00136): per UTC day, the prompts created for the brand (optionally in one topic) and the Action Center actions a person sent, between two optional bounds. Security invoker.';
+
++-- ─────────────────────────────────────────────────────────────────────────
+ -- migrations/00147_insights_same_prompts_definer.sql
+ -- ─────────────────────────────────────────────────────────────────────────
+ -- "Compare: Same prompts" timed out on the largest brand (#1036 review).
+ --
+ -- insights_same_prompt_compare_daily (00130) took 4.5 s as the table owner
+ -- and over 30 s for a signed-in member on the largest brand (~219k answers,
+ -- 30 days against the 30 before). The authenticated role's statement_timeout
+ -- is 8 s, so every Insights open fell back to comparing all prompts. Two
+ -- causes:
+ --
+ -- * A SQL-language function is planned without its argument values
+ -- (PostgreSQL 17 and older), so the brand and both day ranges were
+ -- guesses: a range between unknown bounds is estimated at 0.5% of the
+ -- rows. The plan, and the RLS membership subquery planned into it, were
+ -- built for a few hundred rows and got ~200k. plan_cache_mode does not
+ -- reach SQL functions, which is why force_custom_plan did not help.
+ -- * The per-day rollup rows were joined to the shared prompts again for
+ -- the totals, after being grouped once for the presence check.
+ --
+ -- Both functions become plpgsql with force_custom_plan, so every call is
+ -- planned with its real arguments, and security definer with the
+ -- organization check made explicit, as citations_urls_daily does (00119), so
+ -- RLS no longer runs inside the scan. The service role still reads: the
+ -- cached 24h view (cachedAnchoredInsights) calls the raw variant with it,
+ -- after checking the caller's access to the brand. The rollup rows are now
+ -- folded to one row per prompt and window first, and the raw variant keeps
+ -- one copy of its rows instead of three. Output and counting rules are
+ -- 00130's.
+
+ -- ─── Over the rollups (whole-day windows) ──────────────────────────────────
+
+ create or replace function public.insights_same_prompt_compare_daily(
+ p_brand_id uuid,
+ p_cur_from date,
+ p_cur_to date,
+ p_prev_from date,
+ p_prev_to date,
+ p_models text[] default null,
+ p_region text default null
+ )
+ returns jsonb
+ language plpgsql
+ stable
+ security definer
+ set search_path to 'public'
+ set work_mem to '64MB'
+ set plan_cache_mode to 'force_custom_plan'
+ as $$
+ declare
+ v_result jsonb;
+ begin
+ if auth.role() is distinct from 'service_role' and 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 null;
+ end if;
+
+ with per_prompt as (
+ -- One row per prompt and window: nothing below reads the per-day rows.
+ select d.prompt_id,
+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ sum(d.answer_count) as answers,
+ sum(d.mention_answers) as mention_answers,
+ sum(d.citation_answers) as citation_answers,
+ sum(d.sum_inv_position) as sum_inv_position,
+ sum(d.position_count) as position_count,
+ sum(d.total_mentions) as total_mentions,
+ sum(d.total_citations) as total_citations,
+ sum(d.positive_count) as positive_count,
+ sum(d.mentioning_answers) as mentioning_answers,
+ bool_or(d.mention_answers is null) as stale
+ from public.insights_prompt_daily d
+ where d.brand_id = p_brand_id
+ and (p_models is null or d.model_used = any (p_models))
+ and (p_region is null or d.region = p_region)
+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)
+ group by 1, 2
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(win = 'cur' and answers > 0) as in_cur,
+ bool_or(win = 'prev' and answers > 0) as in_prev
+ from per_prompt
+ group by prompt_id
+ ),
+ same_prompts as (
+ select prompt_id from presence where in_cur and in_prev
+ ),
+ brand_agg as (
+ select p.win,
+ sum(p.answers) as answers,
+ sum(p.mention_answers) as mention_answers,
+ sum(p.citation_answers) as citation_answers,
+ sum(p.sum_inv_position) / nullif(sum(p.position_count), 0) as position_factor,
+ sum(p.total_mentions) as total_mentions,
+ sum(p.total_citations) as total_citations,
+ sum(p.positive_count) as positive_count,
+ sum(p.mentioning_answers) as mentioning_answers
+ from per_prompt p
+ join same_prompts s using (prompt_id)
+ group by p.win
+ ),
+ comp_sums as (
+ select case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ cd.competitor_id,
+ sum(cd.mention_answers) as mention_answers,
+ sum(cd.citation_answers) as citation_answers,
+ sum(cd.sum_inv_position) / nullif(sum(cd.position_count), 0) as position_factor,
+ bool_or(cd.mention_answers is null) as stale
+ from public.insights_competitor_prompt_daily cd
+ where cd.brand_id = p_brand_id
+ and cd.prompt_id in (select prompt_id from same_prompts)
+ and (p_models is null or cd.model_used = any (p_models))
+ and (p_region is null or cd.region = p_region)
+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)
+ group by 1, 2
+ ),
+ -- Competitors tracked in each window, from the dense day-grain table (see
+ -- 00130): one added during the period then has no previous value.
+ tracked as (
+ select distinct
+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ cd.competitor_id
+ from public.insights_competitor_daily cd
+ where cd.brand_id = p_brand_id
+ and (p_models is null or cd.model_used = any (p_models))
+ and (p_region is null or cd.region = p_region)
+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)
+ ),
+ comp_agg as (
+ select t.win,
+ t.competitor_id,
+ c.name,
+ coalesce(cs.mention_answers, 0) as mention_answers,
+ coalesce(cs.citation_answers, 0) as citation_answers,
+ cs.position_factor
+ from tracked t
+ join public.competitors c
+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id
+ left join comp_sums cs
+ on cs.win = t.win and cs.competitor_id = t.competitor_id
+ ),
+ windows(win) as (values ('cur'), ('prev'))
+ select jsonb_build_object(
+ 'complete', not exists (select 1 from per_prompt where stale)
+ and not exists (select 1 from comp_sums where stale),
+ 'same_prompts', (select count(*) from same_prompts),
+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),
+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)
+ ) || (
+ select jsonb_object_agg(w.win, jsonb_build_object(
+ 'answers', coalesce(b.answers, 0),
+ 'mention_answers', coalesce(b.mention_answers, 0),
+ 'citation_answers', coalesce(b.citation_answers, 0),
+ 'position_factor', b.position_factor,
+ 'total_mentions', coalesce(b.total_mentions, 0),
+ 'total_citations', coalesce(b.total_citations, 0),
+ 'positive_count', coalesce(b.positive_count, 0),
+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),
+ 'by_competitor', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'competitor_id', ca.competitor_id,
+ 'name', ca.name,
+ 'mention_answers', ca.mention_answers,
+ 'citation_answers', ca.citation_answers,
+ 'position_factor', ca.position_factor)
+ order by ca.mention_answers desc, ca.competitor_id)
+ from comp_agg ca where ca.win = w.win),
+ '[]'::jsonb)))
+ from windows w
+ left join brand_agg b on b.win = w.win
+ )
+ into v_result;
+
+ return v_result;
+ end;
+ $$;
+
+ -- ─── Over the raw rows (24h and topic views) ───────────────────────────────
+
+ create or replace function public.insights_same_prompt_compare(
+ p_brand_id uuid,
+ p_cur_from timestamptz,
+ p_cur_to timestamptz,
+ p_prev_from timestamptz,
+ p_prev_to timestamptz,
+ p_models text[] default null,
+ p_region text default null,
+ p_topic_id uuid default null
+ )
+ returns jsonb
+ language plpgsql
+ stable
+ security definer
+ set search_path to 'public'
+ set work_mem to '64MB'
+ set plan_cache_mode to 'force_custom_plan'
+ as $$
+ declare
+ v_result jsonb;
+ begin
+ if auth.role() is distinct from 'service_role' and 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 null;
+ end if;
+
+ with filtered as (
+ select pr.prompt_id, pr.mention_count, pr.citation_count,
+ pr.mention_position, pr.sentiment, pr.competitor_mentions,
+ pr.created_at >= p_cur_from and pr.created_at <= p_cur_to as in_cur,
+ pr.created_at >= p_prev_from and pr.created_at <= p_prev_to as in_prev
+ from public.prompt_results pr
+ where pr.brand_id = p_brand_id
+ and pr.platform <> 'chatgpt-shopping'
+ and (p_models is null or pr.model_used = any (p_models))
+ and (p_region is null or pr.region = p_region)
+ and pr.created_at >= p_prev_from
+ and pr.created_at <= p_cur_to
+ and (p_topic_id is null or exists (
+ select 1 from public.prompts p
+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(in_cur) as in_cur,
+ bool_or(in_prev) as in_prev
+ from filtered
+ group by prompt_id
+ ),
+ same_prompts as (
+ select prompt_id from presence where in_cur and in_prev
+ ),
+ -- Each window selected on its own, inclusive at both ends like
+ -- ai_visibility_aggregates: an answer exactly on the boundary counts in
+ -- both, as it does on the card.
+ same_rows as not materialized (
+ select w.win, f.*
+ from filtered f
+ join same_prompts s using (prompt_id)
+ cross join lateral (values ('cur', f.in_cur), ('prev', f.in_prev)) w(win, inside)
+ where w.inside
+ ),
+ brand_agg as (
+ select win,
+ count(*) as answers,
+ count(*) filter (where mention_count > 0) as mention_answers,
+ count(*) filter (where citation_count > 0) as citation_answers,
+ avg(1.0 / mention_position) filter (where mention_position is not null)
+ as position_factor,
+ coalesce(sum(mention_count), 0) as total_mentions,
+ coalesce(sum(citation_count), 0) as total_citations,
+ count(*) filter (where sentiment = 'positive') as positive_count,
+ count(*) filter (where mention_count > 0 or citation_count > 0)
+ as mentioning_answers
+ from same_rows
+ group by win
+ ),
+ comp_agg as (
+ select r.win,
+ cm.value->>'competitor_id' as competitor_id,
+ max(c.name) as name,
+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0)
+ as mention_answers,
+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0)
+ as citation_answers,
+ avg(1.0 / (cm.value->>'mention_position')::numeric)
+ filter (where (cm.value->>'mention_position') is not null)
+ as position_factor
+ from same_rows r
+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm
+ join public.competitors c
+ on c.id::text = cm.value->>'competitor_id' and c.brand_id = p_brand_id
+ where cm.value ? 'competitor_id'
+ group by r.win, cm.value->>'competitor_id'
+ ),
+ windows(win) as (values ('cur'), ('prev'))
+ select jsonb_build_object(
+ 'complete', true,
+ 'same_prompts', (select count(*) from same_prompts),
+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),
+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)
+ ) || (
+ select jsonb_object_agg(w.win, jsonb_build_object(
+ 'answers', coalesce(b.answers, 0),
+ 'mention_answers', coalesce(b.mention_answers, 0),
+ 'citation_answers', coalesce(b.citation_answers, 0),
+ 'position_factor', b.position_factor,
+ 'total_mentions', coalesce(b.total_mentions, 0),
+ 'total_citations', coalesce(b.total_citations, 0),
+ 'positive_count', coalesce(b.positive_count, 0),
+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),
+ 'by_competitor', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'competitor_id', ca.competitor_id,
+ 'name', ca.name,
+ 'mention_answers', ca.mention_answers,
+ 'citation_answers', ca.citation_answers,
+ 'position_factor', ca.position_factor)
+ order by ca.mention_answers desc, ca.competitor_id)
+ from comp_agg ca where ca.win = w.win),
+ '[]'::jsonb)))
+ from windows w
+ left join brand_agg b on b.win = w.win
+ )
+ into v_result;
+
+ return v_result;
+ end;
+ $$;
+
+ revoke execute on function public.insights_same_prompt_compare_daily(
+ uuid, date, date, date, date, text[], text
+ ) from public, anon;
+ grant execute on function public.insights_same_prompt_compare_daily(
+ uuid, date, date, date, date, text[], text
+ ) to authenticated, service_role;
+
+ revoke execute on function public.insights_same_prompt_compare(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid
+ ) from public, anon;
+ grant execute on function public.insights_same_prompt_compare(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid
+ ) to authenticated, service_role;
+
+ -- ─────────────────────────────────────────────────────────────────────────
+ -- migrations/00149_insights_kpi_same_prompts_definer.sql
+ -- ─────────────────────────────────────────────────────────────────────────
+ -- The KPI cards' "Same prompts" read, made fast again (#1036 review).
+ --
+ -- 00147 (#1036) rewrote insights_same_prompt_compare[_daily] as plpgsql,
+ -- security definer, with the organization check made explicit, because the
+ -- SQL-language version timed out on the largest brand: 4.5 s as the table
+ -- owner, over 30 s for a signed-in member against the authenticated role's
+ -- 8 s statement_timeout. It carries 00130's output, so on this branch it
+ -- would drop the components 00131 added (negative count, position sum and
+ -- count, visible prompts, competitor mentions). This is 00131's output with
+ -- 00147's shape: planned per call with its real arguments
+ -- (force_custom_plan), no RLS inside the scan, the rollup rows folded to one
+ -- row per prompt and window first, and the raw rows kept once. The service
+ -- role still reads, for the cached 24h view (cachedAnchoredInsights).
+
+ -- ─── Over the rollups (whole-day windows) ──────────────────────────────────
+
+ create or replace function public.insights_same_prompt_compare_daily(
+ p_brand_id uuid,
+ p_cur_from date,
+ p_cur_to date,
+ p_prev_from date,
+ p_prev_to date,
+ p_models text[] default null,
+ p_region text default null
+ )
+ returns jsonb
+ language plpgsql
+ stable
+ security definer
+ set search_path to 'public'
+ set work_mem to '64MB'
+ set plan_cache_mode to 'force_custom_plan'
+ as $$
+ declare
+ v_result jsonb;
+ begin
+ if auth.role() is distinct from 'service_role' and 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 null;
+ end if;
+
+ with per_prompt as (
+ -- One row per prompt and window: nothing below reads the per-day rows.
+ select d.prompt_id,
+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ sum(d.answer_count) as answers,
+ sum(d.mention_answers) as mention_answers,
+ sum(d.citation_answers) as citation_answers,
+ sum(d.sum_inv_position) as sum_inv_position,
+ sum(d.position_count) as position_count,
+ sum(d.total_mentions) as total_mentions,
+ sum(d.total_citations) as total_citations,
+ sum(d.positive_count) as positive_count,
+ sum(d.mentioning_answers) as mentioning_answers,
+ sum(d.negative_count) as negative_count,
+ sum(d.sum_position) as sum_position,
+ bool_or(d.has_mention or d.has_citation) as visible,
+ bool_or(d.mention_answers is null or d.negative_count is null) as stale
+ from public.insights_prompt_daily d
+ where d.brand_id = p_brand_id
+ and (p_models is null or d.model_used = any (p_models))
+ and (p_region is null or d.region = p_region)
+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)
+ group by 1, 2
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(win = 'cur' and answers > 0) as in_cur,
+ bool_or(win = 'prev' and answers > 0) as in_prev
+ from per_prompt
+ group by prompt_id
+ ),
+ same_prompts as (
+ select prompt_id from presence where in_cur and in_prev
+ ),
+ brand_agg as (
+ select p.win,
+ sum(p.answers) as answers,
+ sum(p.mention_answers) as mention_answers,
+ sum(p.citation_answers) as citation_answers,
+ sum(p.sum_inv_position) / nullif(sum(p.position_count), 0) as position_factor,
+ sum(p.total_mentions) as total_mentions,
+ sum(p.total_citations) as total_citations,
+ sum(p.positive_count) as positive_count,
+ sum(p.mentioning_answers) as mentioning_answers,
+ sum(p.negative_count) as negative_count,
+ sum(p.sum_position) as sum_position,
+ sum(p.position_count) as position_count,
+ count(*) filter (where p.visible) as visible_prompts
+ from per_prompt p
+ join same_prompts s using (prompt_id)
+ group by p.win
+ ),
+ comp_sums as (
+ select case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ cd.competitor_id,
+ sum(cd.mention_answers) as mention_answers,
+ sum(cd.citation_answers) as citation_answers,
+ sum(cd.sum_inv_position) / nullif(sum(cd.position_count), 0) as position_factor,
+ sum(cd.mention_count) as mentions,
+ bool_or(cd.mention_answers is null) as stale
+ from public.insights_competitor_prompt_daily cd
+ where cd.brand_id = p_brand_id
+ and cd.prompt_id in (select prompt_id from same_prompts)
+ and (p_models is null or cd.model_used = any (p_models))
+ and (p_region is null or cd.region = p_region)
+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)
+ group by 1, 2
+ ),
+ -- Competitors tracked in each window, from the dense day-grain table (see
+ -- 00130): one added during the period then has no previous value.
+ tracked as (
+ select distinct
+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,
+ cd.competitor_id
+ from public.insights_competitor_daily cd
+ where cd.brand_id = p_brand_id
+ and (p_models is null or cd.model_used = any (p_models))
+ and (p_region is null or cd.region = p_region)
+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)
+ ),
+ comp_agg as (
+ select t.win,
+ t.competitor_id,
+ c.name,
+ coalesce(cs.mention_answers, 0) as mention_answers,
+ coalesce(cs.citation_answers, 0) as citation_answers,
+ cs.position_factor
+ from tracked t
+ join public.competitors c
+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id
+ left join comp_sums cs
+ on cs.win = t.win and cs.competitor_id = t.competitor_id
+ ),
+ -- Every competitor's mentions on the shared prompts, for share of voice
+ -- (which counts all competitors, as share_of_voice_aggregates does).
+ comp_mentions as (
+ select win, sum(mentions) as mentions from comp_sums group by win
+ ),
+ windows(win) as (values ('cur'), ('prev'))
+ select jsonb_build_object(
+ 'complete', not exists (select 1 from per_prompt where stale)
+ and not exists (select 1 from comp_sums where stale),
+ 'same_prompts', (select count(*) from same_prompts),
+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),
+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)
+ ) || (
+ select jsonb_object_agg(w.win, jsonb_build_object(
+ 'answers', coalesce(b.answers, 0),
+ 'mention_answers', coalesce(b.mention_answers, 0),
+ 'citation_answers', coalesce(b.citation_answers, 0),
+ 'position_factor', b.position_factor,
+ 'total_mentions', coalesce(b.total_mentions, 0),
+ 'total_citations', coalesce(b.total_citations, 0),
+ 'positive_count', coalesce(b.positive_count, 0),
+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),
+ 'negative_count', coalesce(b.negative_count, 0),
+ 'sum_position', b.sum_position,
+ 'position_count', coalesce(b.position_count, 0),
+ 'visible_prompts', coalesce(b.visible_prompts, 0),
+ 'competitor_mentions', coalesce((select cm.mentions from comp_mentions cm where cm.win = w.win), 0),
+ 'by_competitor', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'competitor_id', ca.competitor_id,
+ 'name', ca.name,
+ 'mention_answers', ca.mention_answers,
+ 'citation_answers', ca.citation_answers,
+ 'position_factor', ca.position_factor)
+ order by ca.mention_answers desc, ca.competitor_id)
+ from comp_agg ca where ca.win = w.win),
+ '[]'::jsonb)))
+ from windows w
+ left join brand_agg b on b.win = w.win
+ )
+ into v_result;
+
+ return v_result;
+ end;
+ $$;
+
+ -- ─── Over the raw rows (24h and topic views) ───────────────────────────────
+
+ create or replace function public.insights_same_prompt_compare(
+ p_brand_id uuid,
+ p_cur_from timestamptz,
+ p_cur_to timestamptz,
+ p_prev_from timestamptz,
+ p_prev_to timestamptz,
+ p_models text[] default null,
+ p_region text default null,
+ p_topic_id uuid default null
+ )
+ returns jsonb
+ language plpgsql
+ stable
+ security definer
+ set search_path to 'public'
+ set work_mem to '64MB'
+ set plan_cache_mode to 'force_custom_plan'
+ as $$
+ declare
+ v_result jsonb;
+ begin
+ if auth.role() is distinct from 'service_role' and 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 null;
+ end if;
+
+ with filtered as (
+ select pr.prompt_id, pr.mention_count, pr.citation_count,
+ pr.mention_position, pr.sentiment, pr.competitor_mentions,
+ pr.created_at >= p_cur_from and pr.created_at <= p_cur_to as in_cur,
+ pr.created_at >= p_prev_from and pr.created_at <= p_prev_to as in_prev
+ from public.prompt_results pr
+ where pr.brand_id = p_brand_id
+ and pr.platform <> 'chatgpt-shopping'
+ and (p_models is null or pr.model_used = any (p_models))
+ and (p_region is null or pr.region = p_region)
+ and pr.created_at >= p_prev_from
+ and pr.created_at <= p_cur_to
+ and (p_topic_id is null or exists (
+ select 1 from public.prompts p
+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))
+ ),
+ presence as (
+ select prompt_id,
+ bool_or(in_cur) as in_cur,
+ bool_or(in_prev) as in_prev
+ from filtered
+ group by prompt_id
+ ),
+ same_prompts as (
+ select prompt_id from presence where in_cur and in_prev
+ ),
+ -- Each window selected on its own, inclusive at both ends like
+ -- ai_visibility_aggregates: an answer exactly on the boundary counts in
+ -- both, as it does on the card.
+ same_rows as not materialized (
+ select w.win, f.*
+ from filtered f
+ join same_prompts s using (prompt_id)
+ cross join lateral (values ('cur', f.in_cur), ('prev', f.in_prev)) w(win, inside)
+ where w.inside
+ ),
+ brand_agg as (
+ select win,
+ count(*) as answers,
+ count(*) filter (where mention_count > 0) as mention_answers,
+ count(*) filter (where citation_count > 0) as citation_answers,
+ avg(1.0 / mention_position) filter (where mention_position is not null)
+ as position_factor,
+ coalesce(sum(mention_count), 0) as total_mentions,
+ coalesce(sum(citation_count), 0) as total_citations,
+ count(*) filter (where sentiment = 'positive') as positive_count,
+ count(*) filter (where mention_count > 0 or citation_count > 0)
+ as mentioning_answers,
+ count(*) filter (where sentiment = 'negative') as negative_count,
+ sum(mention_position) filter (where mention_position is not null)
+ as sum_position,
+ count(*) filter (where mention_position is not null) as position_count,
+ count(distinct prompt_id) filter (where mention_count > 0 or citation_count > 0)
+ as visible_prompts
+ from same_rows
+ group by win
+ ),
+ -- The competitor entries of the shared prompts' answers, unpacked once.
+ comp_entries as materialized (
+ select r.win, cm.value as entry
+ from same_rows r
+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm
+ where cm.value ? 'competitor_id'
+ ),
+ comp_agg as (
+ select e.win,
+ e.entry->>'competitor_id' as competitor_id,
+ max(c.name) as name,
+ count(*) filter (where coalesce((e.entry->>'mention_count')::int, 0) > 0)
+ as mention_answers,
+ count(*) filter (where coalesce((e.entry->>'citation_count')::int, 0) > 0)
+ as citation_answers,
+ avg(1.0 / (e.entry->>'mention_position')::numeric)
+ filter (where (e.entry->>'mention_position') is not null)
+ as position_factor
+ from comp_entries e
+ join public.competitors c
+ on c.id::text = e.entry->>'competitor_id' and c.brand_id = p_brand_id
+ group by e.win, e.entry->>'competitor_id'
+ ),
+ -- Every competitor's mentions on the shared prompts, for share of voice.
+ comp_mentions as (
+ select win, sum(coalesce((entry->>'mention_count')::int, 0)) as mentions
+ from comp_entries
+ group by win
+ ),
+ windows(win) as (values ('cur'), ('prev'))
+ select jsonb_build_object(
+ 'complete', true,
+ 'same_prompts', (select count(*) from same_prompts),
+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),
+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)
+ ) || (
+ select jsonb_object_agg(w.win, jsonb_build_object(
+ 'answers', coalesce(b.answers, 0),
+ 'mention_answers', coalesce(b.mention_answers, 0),
+ 'citation_answers', coalesce(b.citation_answers, 0),
+ 'position_factor', b.position_factor,
+ 'total_mentions', coalesce(b.total_mentions, 0),
+ 'total_citations', coalesce(b.total_citations, 0),
+ 'positive_count', coalesce(b.positive_count, 0),
+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),
+ 'negative_count', coalesce(b.negative_count, 0),
+ 'sum_position', b.sum_position,
+ 'position_count', coalesce(b.position_count, 0),
+ 'visible_prompts', coalesce(b.visible_prompts, 0),
+ 'competitor_mentions', coalesce((select cm.mentions from comp_mentions cm where cm.win = w.win), 0),
+ 'by_competitor', coalesce(
+ (select jsonb_agg(jsonb_build_object(
+ 'competitor_id', ca.competitor_id,
+ 'name', ca.name,
+ 'mention_answers', ca.mention_answers,
+ 'citation_answers', ca.citation_answers,
+ 'position_factor', ca.position_factor)
+ order by ca.mention_answers desc, ca.competitor_id)
+ from comp_agg ca where ca.win = w.win),
+ '[]'::jsonb)))
+ from windows w
+ left join brand_agg b on b.win = w.win
+ )
+ into v_result;
+
+ return v_result;
+ end;
+ $$;
+
+ revoke execute on function public.insights_same_prompt_compare_daily(
+ uuid, date, date, date, date, text[], text
+ ) from public, anon;
+ grant execute on function public.insights_same_prompt_compare_daily(
+ uuid, date, date, date, date, text[], text
+ ) to authenticated, service_role;
+
+ revoke execute on function public.insights_same_prompt_compare(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid
+ ) from public, anon;
+ grant execute on function public.insights_same_prompt_compare(
+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid
+ ) to authenticated, service_role;
+
diff --cc web/src/lib/actions/tracking.ts
index 4829d54cf,8b67e1f0d..20afb8442
--- a/web/src/lib/actions/tracking.ts
+++ b/web/src/lib/actions/tracking.ts
@@@ -888,40 -873,27 +888,40 @@@ async function loadWindowedInsights
return fallback;
});
- const [summary, competitors, sov, filterOptions, trend, same, coverage, kpiSeries, platformData] =
- await Promise.all([
- getInsightsSummary(brandId, filterOpts),
- lenient(
- getCompetitorComparison(brandId, filterOpts),
- EMPTY_COMPETITORS,
- 'competitor comparison',
- ),
- lenient(getShareOfVoiceData(brandId, filterOpts), EMPTY_SOV, 'share of voice'),
- getInsightsFilterOptions(brandId),
- // The chart follows the same window as every other number on the page.
- lenient<VisibilityRateTrendData | null>(
- getVisibilityRateTrend(brandId, filterOpts),
- null,
- 'visibility trend',
- ),
- lenient(getSamePromptComparison(brandId, filterOpts), null, 'same-prompt comparison'),
- lenient(getCoverageKpi(brandId, filterOpts), null, 'coverage'),
- lenient(getKpiSeries(brandId, filterOpts), null, 'KPI sparklines'),
- lenient(getPlatformData(brandId, filterOpts), null, 'platforms'),
- ]);
+ const [
+ summary,
+ competitors,
+ sov,
+ filterOptions,
+ trend,
+ same,
+ coverage,
+ kpiSeries,
+ platformData,
+ drivers,
+ trendMarkers,
+ ] = await Promise.all([
+ getInsightsSummary(brandId, filterOpts),
+ lenient(
+ getCompetitorComparison(brandId, filterOpts),
+ EMPTY_COMPETITORS,
+ 'competitor comparison',
+ ),
+ lenient(getShareOfVoiceData(brandId, filterOpts), EMPTY_SOV, 'share of voice'),
+ getInsightsFilterOptions(brandId),
+ // The chart follows the same window as every other number on the page.
+ lenient<VisibilityRateTrendData | null>(
+ getVisibilityRateTrend(brandId, filterOpts),
+ null,
+ 'visibility trend',
+ ),
- getSamePromptComparison(brandId, filterOpts),
++ lenient(getSamePromptComparison(brandId, filterOpts), null, 'same-prompt comparison'),
+ lenient(getCoverageKpi(brandId, filterOpts), null, 'coverage'),
+ lenient(getKpiSeries(brandId, filterOpts), null, 'KPI sparklines'),
+ lenient(getPlatformData(brandId, filterOpts), null, 'platforms'),
+ lenient(getChangeDrivers(brandId, filterOpts), null, 'why it changed'),
+ lenient(getTrendMarkers(brandId, filterOpts), null, 'trend markers'),
+ ]);
const windowed = {
...(comparisonWindows(filterOpts).prev ? { summary, sov } : withoutChanges(summary, sov)),
competitors,