Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(citations): answer "has any citations" without counting every row (#707)
Gökhan Geyik1e9ed9b0ce70
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsxindex ad3a97154..033d4ae25 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx@@ -39,7 +39,7 @@ import {import {getCitationsOverview,getCitationGaps,- getCitationsTotal,+ brandHasCitations,type CitationsFilters,type CitationsOverview,type CitationDomainRow,@@ -435,7 +435,7 @@ const UrlsTable = memo(function UrlsTable({/*** Period-aware empty state for the Domains / URLs tables (#485).*- * hasAnyCitations is fetched once in loadData (via getCitationsTotal) and+ * hasAnyCitations is fetched once in loadData (via brandHasCitations) and* passed down, so both keepMounted panels share a single server call instead* of each firing their own (#313 action queue note).*@@ -891,13 +891,13 @@ export default function CitationsPage() {setIsLoading(true);setLoadFailed(false);try {- const [overview, total] = await Promise.all([+ const [overview, hasAny] = await Promise.all([getCitationsOverview(activeBrandId, apiFilters),- getCitationsTotal(activeBrandId),+ brandHasCitations(activeBrandId),]);if (stale()) return;setData(overview);- setHasAnyCitations(total > 0);+ setHasAnyCitations(hasAny);// Surface filter options from the observed models/regions.const platformOptions = buildPlatformOptions(overview.rows);diff --git a/web/src/lib/actions/citations.ts b/web/src/lib/actions/citations.tsindex 5a9743204..fe4783e00 100644--- a/web/src/lib/actions/citations.ts+++ b/web/src/lib/actions/citations.ts@@ -671,23 +671,38 @@ export async function getCitationGaps(// ─── Existence check (#485) ───────────────────────────────────────────────────/**- * Cheap unfiltered existence check — counts prompt_results rows for a brand- * with no date/filter constraints, so the Citations page can distinguish- * "no data in this window" from "no data at all".+ * Unfiltered existence check — does this brand have any cited answer at all,+ * ignoring the page's date and filter selection? Lets the Citations page+ * distinguish "no data in this window" from "no data at all".*- * Mirrors getBrandResultsTotal in tracking.ts (used by the Insights page).- * Uses `head: true` so Supabase returns only the count, not any rows.+ * Deliberately not a count (#706). `citations <> '[]'` can't use an index, so+ * an exact count made Postgres read and detoast every citation payload the+ * brand has ever produced — ~1.8s and 780 MB of buffers on a large brand with+ * a warm cache, and past the 8s statement timeout once the nightly run had+ * churned the cache, which failed the whole page load. Only the yes/no answer+ * was ever used. Stopping at the first match answers it in ~0.1ms.+ *+ * The ordering is load-bearing: without it the planner picks a sequential scan+ * over the entire table, which is unbounded for a brand whose answers never+ * cite anything. Ordering by created_at keeps the scan on this brand's rows+ * via idx_prompt_results_brand_created.+ *+ * Note that prompt_results.citation_count is NOT a substitute for the+ * `citations <> '[]'` predicate — it counts something narrower, and disagrees+ * on the overwhelming majority of rows.*/-export async function getCitationsTotal(brandId: string): Promise<number> {+export async function brandHasCitations(brandId: string): Promise<boolean> {const supabase = await createClient();- const { count, error } = await supabase+ const { data, error } = await supabase.from('prompt_results')- .select('id', { count: 'exact', head: true })+ .select('id').eq('brand_id', brandId).neq('platform', 'chatgpt-shopping')- .neq('citations', '[]');+ .neq('citations', '[]')+ .order('created_at', { ascending: false })+ .limit(1);if (error) throw new Error(error.message);- return count ?? 0;+ return (data ?? []).length > 0;}// ─── Per-URL detail (#535) ────────────────────────────────────────────────────