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 overview from daily rollups (#992)
Gökhan Geyik93e42a1ff850
diff --git a/server/src/lib/citations-rollups.js b/server/src/lib/citations-rollups.jsnew file mode 100644index 000000000..82a3c3a29--- /dev/null+++ b/server/src/lib/citations-rollups.js@@ -0,0 +1,127 @@+/**+ * Citations daily-rollup maintenance (00118).+ *+ * The Citations page reads per-day domain and URL counts instead of+ * aggregating a window of citation rows per load; this module is the only+ * 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.+ *+ * Best-effort like the Insights rollups: the rows are derived state, always+ * recomputable from prompt_result_citations, so a failure costs freshness+ * until the next sweep and never throws into a tracking run.+ */++import supabaseAdmin from '../config/supabase.js';+import { logger } from './logger.js';++/** Days per refresh call when recomputing a brand's whole history. */+export const HISTORY_CHUNK_DAYS = 14;++function utcDay(iso = undefined) {+ return (iso ? new Date(iso) : new Date()).toISOString().slice(0, 10);+}++/** `day` plus `n` calendar days, as 'YYYY-MM-DD'. */+export function addDays(day, n) {+ const d = new Date(`${day}T00:00:00Z`);+ d.setUTCDate(d.getUTCDate() + n);+ return utcDay(d.toISOString());+}++/** Inclusive [from, to] ranges of at most `size` days covering firstDay..lastDay. */+export function dayChunks(firstDay, lastDay, size = HISTORY_CHUNK_DAYS) {+ const chunks = [];+ for (let from = firstDay; from <= lastDay; from = addDays(from, size)) {+ const end = addDays(from, size - 1);+ chunks.push([from, end < lastDay ? end : lastDay]);+ }+ return chunks;+}++/**+ * Recompute one brand's citation rollups for an inclusive UTC day range.+ * Resolves to true on success, false otherwise; never throws.+ */+export async function refreshCitationsDaily(brandId, dayFrom, dayTo) {+ const { error } = await supabaseAdmin.rpc('refresh_citations_daily', {+ p_brand_id: brandId,+ p_day_from: dayFrom,+ p_day_to: dayTo,+ });+ if (error) {+ logger.error({ err: error, brandId, dayFrom, dayTo }, '[citations-rollups] refresh failed');+ return false;+ }+ return true;+}++/** UTC day of a brand's first or last result, or null when it has none. */+async function resultDayBound(brandId, ascending) {+ const { data, error } = await supabaseAdmin+ .from('prompt_results')+ .select('created_at')+ .eq('brand_id', brandId)+ .order('created_at', { ascending })+ .limit(1);+ if (error) throw new Error(error.message);+ return data?.[0]?.created_at ? utcDay(data[0].created_at) : null;+}++/**+ * Recompute a brand's whole citation history, a chunk at a time so no call+ * nears the service role's statement timeout. Resolves to the number of+ * chunks written; throws when a chunk fails, so the caller can retry later.+ */+export async function refreshCitationHistory(brandId, { pauseMs = 0 } = {}) {+ const firstDay = await resultDayBound(brandId, true);+ if (!firstDay) return 0;+ const lastDay = await resultDayBound(brandId, false);++ const chunks = dayChunks(firstDay, lastDay);+ for (const [from, to] of chunks) {+ const ok = await refreshCitationsDaily(brandId, from, to);+ if (!ok) throw new Error(`citation rollup refresh failed for ${brandId} ${from}..${to}`);+ if (pauseMs > 0) await new Promise((resolve) => setTimeout(resolve, pauseMs));+ }+ return chunks.length;+}++/**+ * 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.+ */+export async function rebuildQueuedCitationRollups() {+ const { data: queued, error } = await supabaseAdmin+ .from('citation_rollup_rebuilds')+ .select('brand_id, requested_at');+ if (error) {+ logger.error({ err: error }, '[citations-rollups] could not read the rebuild queue');+ return { rebuilt: 0, failed: 0 };+ }++ let rebuilt = 0;+ let failed = 0;+ for (const { brand_id: brandId, requested_at: requestedAt } of queued ?? []) {+ try {+ await refreshCitationHistory(brandId);+ await supabaseAdmin+ .from('citation_rollup_rebuilds')+ .delete()+ .eq('brand_id', brandId)+ .lte('requested_at', requestedAt);+ rebuilt++;+ } catch (err) {+ failed++;+ logger.error({ err, brandId }, '[citations-rollups] topic rebuild failed');+ }+ }++ if (rebuilt + failed > 0) {+ logger.info({ rebuilt, failed }, '[citations-rollups] topic rebuilds complete');+ }+ return { rebuilt, failed };+}diff --git a/server/src/lib/citations-rollups.test.js b/server/src/lib/citations-rollups.test.jsnew file mode 100644index 000000000..d9a7634a2--- /dev/null+++ b/server/src/lib/citations-rollups.test.js@@ -0,0 +1,170 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++// The module reaches the supabase admin client at import time, whose config+// hard-exits when SUPABASE_* env is absent (as in CI).+const rpc = vi.fn();+const from = vi.fn();+vi.mock('../config/supabase.js', () => ({+ default: { rpc: (...a) => rpc(...a), from: (...a) => from(...a) },+}));++import {+ addDays,+ dayChunks,+ rebuildQueuedCitationRollups,+ refreshCitationHistory,+ refreshCitationsDaily,+} from './citations-rollups.js';++/** A prompt_results bound query: first call ascending (first day), then descending. */+function resultBounds(first, last) {+ const days = [first, last];+ from.mockImplementation((table) => {+ if (table !== 'prompt_results') throw new Error(`unexpected table ${table}`);+ const day = days.shift();+ const q = {+ select: () => q,+ eq: () => q,+ order: () => q,+ limit: () =>+ Promise.resolve({ data: day ? [{ created_at: `${day}T04:00:00Z` }] : [], error: null }),+ };+ return q;+ });+}++beforeEach(() => {+ rpc.mockReset();+ from.mockReset();+});++describe('dayChunks', () => {+ it('covers the range in inclusive chunks, the last one short', () => {+ expect(dayChunks('2026-08-01', '2026-08-30', 14)).toEqual([+ ['2026-08-01', '2026-08-14'],+ ['2026-08-15', '2026-08-28'],+ ['2026-08-29', '2026-08-30'],+ ]);+ });++ it('returns one chunk for a single day', () => {+ expect(dayChunks('2026-08-01', '2026-08-01')).toEqual([['2026-08-01', '2026-08-01']]);+ });++ it('crosses month ends', () => {+ expect(addDays('2026-08-31', 1)).toBe('2026-09-01');+ });+});++describe('refreshCitationsDaily', () => {+ it('calls the refresh function with the brand and inclusive day range', async () => {+ rpc.mockResolvedValue({ error: null });+ await expect(refreshCitationsDaily('brand-1', '2026-08-19', '2026-08-21')).resolves.toBe(true);+ expect(rpc).toHaveBeenCalledWith('refresh_citations_daily', {+ p_brand_id: 'brand-1',+ p_day_from: '2026-08-19',+ p_day_to: '2026-08-21',+ });+ });++ it('reports failure without throwing', async () => {+ rpc.mockResolvedValue({ error: { message: 'boom' } });+ await expect(refreshCitationsDaily('brand-1', '2026-08-21', '2026-08-21')).resolves.toBe(false);+ });+});++describe('refreshCitationHistory', () => {+ it('refreshes every chunk from the first to the last result day', async () => {+ resultBounds('2026-08-01', '2026-08-20');+ rpc.mockResolvedValue({ error: null });++ await expect(refreshCitationHistory('brand-1')).resolves.toBe(2);+ expect(rpc.mock.calls.map((c) => [c[1].p_day_from, c[1].p_day_to])).toEqual([+ ['2026-08-01', '2026-08-14'],+ ['2026-08-15', '2026-08-20'],+ ]);+ });++ it('does nothing for a brand without results', async () => {+ resultBounds(null, null);+ await expect(refreshCitationHistory('brand-1')).resolves.toBe(0);+ expect(rpc).not.toHaveBeenCalled();+ });++ it('throws when a chunk fails', async () => {+ resultBounds('2026-08-01', '2026-08-02');+ rpc.mockResolvedValue({ error: { message: 'timeout' } });+ await expect(refreshCitationHistory('brand-1')).rejects.toThrow(/refresh failed/);+ });+});++describe('rebuildQueuedCitationRollups', () => {+ it('rebuilds each queued brand and clears its entry only up to the time it read', async () => {+ const deleted = [];+ const boundDays = ['2026-08-01', '2026-08-02'];+ from.mockImplementation((table) => {+ if (table === 'citation_rollup_rebuilds') {+ return {+ select: () =>+ Promise.resolve({+ data: [{ brand_id: 'brand-1', requested_at: '2026-08-21T01:00:00Z' }],+ error: null,+ }),+ delete: () => ({+ eq: (_c, brandId) => ({+ lte: (_c2, at) => {+ deleted.push([brandId, at]);+ return Promise.resolve({ error: null });+ },+ }),+ }),+ };+ }+ const day = boundDays.shift();+ const q = {+ select: () => q,+ eq: () => q,+ order: () => q,+ limit: () => Promise.resolve({ data: [{ created_at: `${day}T04:00:00Z` }], error: null }),+ };+ return q;+ });+ rpc.mockResolvedValue({ error: null });++ await expect(rebuildQueuedCitationRollups()).resolves.toEqual({ rebuilt: 1, failed: 0 });+ expect(rpc).toHaveBeenCalledWith('refresh_citations_daily', {+ p_brand_id: 'brand-1',+ p_day_from: '2026-08-01',+ p_day_to: '2026-08-02',+ });+ expect(deleted).toEqual([['brand-1', '2026-08-21T01:00:00Z']]);+ });++ it('keeps the queue entry when the rebuild fails', async () => {+ let deletes = 0;+ from.mockImplementation((table) => {+ if (table === 'citation_rollup_rebuilds') {+ return {+ select: () =>+ Promise.resolve({ data: [{ brand_id: 'b', requested_at: 'x' }], error: null }),+ delete: () => {+ deletes++;+ return { eq: () => ({ lte: () => Promise.resolve({ error: null }) }) };+ },+ };+ }+ const q = {+ select: () => q,+ eq: () => q,+ order: () => q,+ limit: () =>+ Promise.resolve({ data: [{ created_at: '2026-08-01T04:00:00Z' }], error: null }),+ };+ return q;+ });+ rpc.mockResolvedValue({ error: { message: 'boom' } });++ await expect(rebuildQueuedCitationRollups()).resolves.toEqual({ rebuilt: 0, failed: 1 });+ expect(deletes).toBe(0);+ });+});diff --git a/server/src/lib/health/report.js b/server/src/lib/health/report.jsindex abdfd683f..6da19dd94 100644--- a/server/src/lib/health/report.js+++ b/server/src/lib/health/report.js@@ -20,16 +20,19 @@ import { sendEmail } from '../email.js';export const READ_BUDGET_S = 8;export const WARN_AT_S = 4;-/** Reads timed against the probe brand: the page default, then a long window. */+/**+ * Reads timed against the probe brand: the page default, then a long window.+ * Topics goes first: it reads raw results, and timed after the long Citations+ * windows it measured their cache eviction rather than its own cost.+ */export const PROBES = [- { probe: 'citations_window_stats', days: 1 },- { probe: 'citations_domains', days: 1 },- { probe: 'citations_urls', days: 1 },- { probe: 'citations_domains', days: 30 },- { probe: 'citations_urls', days: 30 },+ { probe: 'topics_overview_aggregates', days: null },+ { probe: 'citations_domains_daily', days: 1 },+ { probe: 'citations_urls_daily', days: 1 },+ { probe: 'citations_domains_daily', days: 30 },+ { probe: 'citations_urls_daily', days: 30 },{ probe: 'insights_aggregates_daily', days: 30 },{ probe: 'competitor_aggregates_daily', days: 30 },- { probe: 'topics_overview_aggregates', days: null },];const DAY_MS = 24 * 60 * 60 * 1000;diff --git a/server/src/lib/health/report.test.js b/server/src/lib/health/report.test.jsindex e76ee1296..ac3b3bd1b 100644--- a/server/src/lib/health/report.test.js+++ b/server/src/lib/health/report.test.js@@ -62,7 +62,7 @@ describe('collectHealthReport', () => {expect(db.rpc).toHaveBeenCalledWith('health_probe', {p_user_id: 'user-probe',p_brand_id: 'brand-probe',- p_probe: 'citations_domains',+ p_probe: 'citations_domains_daily',p_days: 1,});expect(report.probes.results.every((r) => r.status === 'green')).toBe(true);diff --git a/server/src/lib/insights-rollups.js b/server/src/lib/insights-rollups.jsindex 5ff2a8065..9633e1c73 100644--- a/server/src/lib/insights-rollups.js+++ b/server/src/lib/insights-rollups.js@@ -19,6 +19,7 @@import supabaseAdmin from '../config/supabase.js';import { logger } from './logger.js';+import { rebuildQueuedCitationRollups, refreshCitationsDaily } from './citations-rollups.js';/** How many trailing days the daily sweep recomputes, today included. */export const SWEEP_DAYS = 3;@@ -49,13 +50,19 @@ export async function refreshInsightsDaily(brandId, dayFrom, dayTo) {/*** Refresh the days a completed tracking run touched: from the run's start* day through today (a run crossing UTC midnight lands rows on two days).+ * The Citations rollups (00118) are refreshed for the same days.*/export async function refreshForCompletedRun(brandId, runStartedAtIso) {- return refreshInsightsDaily(brandId, utcDay(runStartedAtIso), utcDay());+ const dayFrom = utcDay(runStartedAtIso);+ const dayTo = utcDay();+ const ok = await refreshInsightsDaily(brandId, dayFrom, dayTo);+ await refreshCitationsDaily(brandId, dayFrom, dayTo);+ return ok;}/**- * Daily backstop: refresh the trailing SWEEP_DAYS for every brand.+ * Daily backstop: refresh the trailing SWEEP_DAYS for every brand, Insights+ * and Citations rollups alike, then rebuild the brands queued by topic moves.** Deliberately every brand rather than "brands with recent results" — a* refresh over days with no rows deletes nothing and inserts nothing, so the@@ -80,10 +87,14 @@ export async function sweepInsightsRollups({ days = SWEEP_DAYS } = {}) {let failed = 0;for (const brand of brands ?? []) {const ok = await refreshInsightsDaily(brand.id, dayFrom, dayTo);- if (ok) refreshed++;+ const citationsOk = await refreshCitationsDaily(brand.id, dayFrom, dayTo);+ if (ok && citationsOk) refreshed++;else failed++;}logger.info({ refreshed, failed, dayFrom, dayTo }, '[insights-rollups] sweep complete');++ // Brands whose prompts changed topic get their citation history rebuilt.+ await rebuildQueuedCitationRollups();return { refreshed, failed };}diff --git a/server/src/lib/insights-rollups.test.js b/server/src/lib/insights-rollups.test.jsindex 1d8b981df..fa5ed8be7 100644--- a/server/src/lib/insights-rollups.test.js+++ b/server/src/lib/insights-rollups.test.js@@ -8,6 +8,15 @@ vi.mock('../config/supabase.js', () => ({default: { rpc: (...a) => rpc(...a), from: (...a) => from(...a) },}));+// The Citations rollups ride on the same callers; their own behavior is+// covered in citations-rollups.test.js.+const refreshCitationsDaily = vi.fn();+const rebuildQueuedCitationRollups = vi.fn();+vi.mock('./citations-rollups.js', () => ({+ refreshCitationsDaily: (...a) => refreshCitationsDaily(...a),+ rebuildQueuedCitationRollups: (...a) => rebuildQueuedCitationRollups(...a),+}));+import {refreshInsightsDaily,refreshForCompletedRun,@@ -19,6 +28,8 @@ import {beforeEach(() => {rpc.mockReset();from.mockReset();+ refreshCitationsDaily.mockReset().mockResolvedValue(true);+ rebuildQueuedCitationRollups.mockReset().mockResolvedValue({ rebuilt: 0, failed: 0 });vi.useFakeTimers();vi.setSystemTime(new Date('2026-08-21T05:30:00Z'));});@@ -87,6 +98,12 @@ describe('refreshForCompletedRun', () => {p_day_to: '2026-08-21',});});++ it('refreshes the Citations rollups for the same days', async () => {+ rpc.mockResolvedValue({ error: null });+ await refreshForCompletedRun('brand-1', '2026-08-20T23:45:00Z');+ expect(refreshCitationsDaily).toHaveBeenCalledWith('brand-1', '2026-08-20', '2026-08-21');+ });});describe('sweepInsightsRollups', () => {@@ -109,6 +126,18 @@ describe('sweepInsightsRollups', () => {p_day_to: '2026-08-21',});expect(SWEEP_DAYS).toBe(3);+ expect(refreshCitationsDaily).toHaveBeenCalledWith('b', '2026-08-19', '2026-08-21');+ expect(rebuildQueuedCitationRollups).toHaveBeenCalledTimes(1);+ });++ it('counts a brand whose Citations refresh failed', async () => {+ brandsList([{ id: 'a' }, { id: 'b' }]);+ rpc.mockResolvedValue({ error: null });+ refreshCitationsDaily.mockResolvedValueOnce(false);++ const res = await sweepInsightsRollups();++ expect(res).toEqual({ refreshed: 1, failed: 1 });});// One brand's failure must not stop the sweep — the whole point of thediff --git a/server/src/scripts/backfill-citations-daily.js b/server/src/scripts/backfill-citations-daily.jsnew file mode 100644index 000000000..e0307a13e--- /dev/null+++ b/server/src/scripts/backfill-citations-daily.js@@ -0,0 +1,56 @@+/**+ * Backfill the Citations daily rollups (00118) from historical citations.+ *+ * Run: `node src/scripts/backfill-citations-daily.js`+ *+ * Options via env:+ * - CITATIONS_BACKFILL_PAUSE milliseconds between refresh calls+ * (default 250)+ * - CITATIONS_BACKFILL_BRAND limit the run to one brand id+ *+ * Covers every brand with results, cancelled orgs included, for the same+ * reason as the Insights backfill: the read path switches for everyone at+ * once, and a reactivated account must not open an empty page.+ *+ * Idempotent and restartable. `refresh_citations_daily` is delete + insert+ * per day range, so an interrupted run can be started again. Chunked by+ * HISTORY_CHUNK_DAYS (the largest brand writes ~2 weeks in ~12s), with a+ * pause between calls because this shares the instance with live dashboards.+ */++import 'dotenv/config';+import supabaseAdmin from '../config/supabase.js';+import { refreshCitationHistory } from '../lib/citations-rollups.js';++const PAUSE_MS = Number.parseInt(process.env.CITATIONS_BACKFILL_PAUSE ?? '250', 10);+const ONLY_BRAND = process.env.CITATIONS_BACKFILL_BRAND ?? null;++async function main() {+ const query = supabaseAdmin.from('brands').select('id');+ const { data: brands, error } = ONLY_BRAND ? await query.eq('id', ONLY_BRAND) : await query;+ if (error) throw new Error(error.message);++ let done = 0;+ let failed = 0;+ for (const brand of brands ?? []) {+ try {+ const chunks = await refreshCitationHistory(brand.id, { pauseMs: PAUSE_MS });+ done++;+ console.log(+ chunks === 0+ ? `[${done}/${brands.length}] ${brand.id} — no results, skipped`+ : `[${done}/${brands.length}] ${brand.id} — ${chunks} chunk(s)`,+ );+ } catch (err) {+ failed++;+ console.error(`FAILED ${brand.id}: ${err.message}`);+ }+ }+ console.log(`Backfill complete: ${done} brand(s), ${failed} failure(s).`);+ if (failed > 0) process.exitCode = 1;+}++main().catch((err) => {+ console.error(err);+ process.exit(1);+});diff --git a/supabase/migrations/00118_citations_daily_rollups.sql b/supabase/migrations/00118_citations_daily_rollups.sqlnew file mode 100644index 000000000..0a1418ab9--- /dev/null+++ b/supabase/migrations/00118_citations_daily_rollups.sql@@ -0,0 +1,425 @@+-- Citations stops recomputing the window on every page load.+--+-- The Citations overview aggregated prompt_result_citations per request. On+-- the largest brand a 30-day window is 935k citation rows over 128k URLs and+-- 15.8k domains: citations_domains took 17s against the authenticated role's+-- 8s statement timeout, and every brand's window grows with its history.+--+-- Same cure as Insights (00066): each brand-day is aggregated once, when the+-- brand's tracking run completes, with the daily sweep as backstop, and the+-- page reads the daily rows. Measured on the largest brand's 30 days: the+-- domain table in 0.06s and the top 2,000 URLs in 0.25s.+--+-- Grain: (day, model_used, platform, region, topic_id) — the page's filter+-- dimensions. A result belongs to exactly one bucket, so results_citing sums+-- across days, engines, regions and topics without double counting. A prompt+-- grain was measured too and compresses nothing (886k rows for 935k+-- citations), so a single-prompt filter stays on the raw functions, which a+-- single prompt keeps small.+--+-- Topic is stored rather than joined at read time. Moving a prompt to another+-- topic (or deleting it) therefore queues its brand in+-- citation_rollup_rebuilds, and the daily sweep recomputes that brand's+-- history. Until then only topic-filtered views show the old attribution;+-- unfiltered totals are unaffected.+--+-- The raw functions stay: the per-prompt filter, Competitor Gaps, the URL+-- detail page, MCP and reports still call them.++-- ─── Tables ─────────────────────────────────────────────────────────────────++-- Answers per bucket: the denominator of every usage %, and the regions list.+create table if not exists public.citation_result_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,+ answers integer not null+);++create index if not exists idx_citation_result_daily_brand_day+ on public.citation_result_daily (brand_id, day);++create table if not exists public.citation_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,+ citations integer not null,+ -- Answers citing at least one URL of the domain.+ results integer not null+);++create index if not exists idx_citation_domain_daily_brand_day+ on public.citation_domain_daily (brand_id, day);++create table if not exists public.citation_url_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,+ url_id bigint not null,+ citations integer not null,+ -- Answers citing the URL.+ results integer not null+);++create index if not exists idx_citation_url_daily_brand_day+ on public.citation_url_daily (brand_id, day);++-- Brands whose prompts changed topic since their rollups were written. No+-- foreign key on purpose: deleting a brand cascades through its prompts, and+-- the trigger below must never make that delete fail. A brand gone by the+-- time the sweep reads its entry has no results, so the rebuild is a no-op+-- and the entry is cleared.+create table if not exists public.citation_rollup_rebuilds (+ brand_id uuid primary key,+ requested_at timestamptz not null default now()+);++-- ─── RLS — same shape as the Insights rollups ───────────────────────────────++alter table public.citation_result_daily enable row level security;+alter table public.citation_domain_daily enable row level security;+alter table public.citation_url_daily enable row level security;+-- Service role only: no policies.+alter table public.citation_rollup_rebuilds enable row level security;++drop policy if exists "Users can read own org citation result rollups" on public.citation_result_daily;+create policy "Users can read own org citation result rollups"+ on public.citation_result_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 domain rollups" on public.citation_domain_daily;+create policy "Users can read own org citation domain rollups"+ on public.citation_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 url rollups" on public.citation_url_daily;+create policy "Users can read own org citation url rollups"+ on public.citation_url_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 ────────────────────────────────────────────────────────────────++-- Recompute one brand's citation rollups for an inclusive UTC day range:+-- delete + insert, so it is idempotent and safe to rerun. Called by the+-- server under the service role (run completion, the daily sweep, the topic+-- rebuild and the backfill script).+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';+begin+ 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;++ insert into public.citation_result_daily (+ brand_id, day, model_used, platform, region, topic_id, answers)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id,+ count(*)+ 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+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id;++ -- A citation row carries its answer's created_at, so the citation index+ -- bounds the scan; the answer supplies the bucket.+ 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;++-- ─── Topic moves queue a rebuild ────────────────────────────────────────────++create or replace function public.queue_citation_rollup_rebuild()+returns trigger+language plpgsql+security definer+set search_path to 'public'+as $$+declare+ v_set uuid := case when tg_op = 'DELETE' then old.prompt_set_id else new.prompt_set_id end;+begin+ if tg_op = 'UPDATE' and new.topic_id is not distinct from old.topic_id then+ return new;+ end if;+ -- Only for a brand that still exists: during a brand delete the cascade+ -- removes the brand first, so its prompts queue nothing.+ insert into public.citation_rollup_rebuilds (brand_id)+ select ps.brand_id from public.prompt_sets ps+ join public.brands b on b.id = ps.brand_id+ where ps.id = v_set+ on conflict (brand_id) do update set requested_at = now();+ return null;+end;+$$;++revoke execute on function public.queue_citation_rollup_rebuild()+ from public, anon, authenticated;++drop trigger if exists prompts_queue_citation_rollup_rebuild on public.prompts;+create trigger prompts_queue_citation_rollup_rebuild+ after update of topic_id or delete on public.prompts+ for each row execute function public.queue_citation_rollup_rebuild();++-- ─── Reads ──────────────────────────────────────────────────────────────────+-- Security invoker: the RLS policies above scope every read to the caller's+-- organization. plpgsql with force_custom_plan so each call is planned with+-- its own brand and window (00113).++create or replace function public.citations_domains_daily(+ p_brand_id uuid,+ 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, total_citations bigint, results_citing bigint, models text[])+language plpgsql+stable+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ select d.domain,+ sum(d.citations)::bigint,+ sum(d.results)::bigint,+ array_agg(distinct coalesce(d.model_used, d.platform))+ from public.citation_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+ order by 2 desc;+end;+$$;++create or replace function public.citations_urls_daily(+ p_brand_id uuid,+ 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,+ p_limit integer default 2000,+ p_domains text[] default null,+ p_exclude_domains text[] default null+) returns table(url text, domain text, title text, total_citations bigint,+ results_citing bigint, models text[], total_urls bigint)+language plpgsql+stable+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ with agg as (+ select u.url_id as uid,+ sum(u.citations)::bigint as tc,+ sum(u.results)::bigint as rc,+ array_agg(distinct coalesce(u.model_used, u.platform)) as ms+ from public.citation_url_daily u+ where u.brand_id = p_brand_id+ and (p_day_from is null or u.day >= p_day_from)+ and (p_day_to is null or u.day <= p_day_to)+ and (p_models is null or u.model_used = any(p_models))+ and (p_regions is null or u.region = any(p_regions))+ and (p_topic_ids is null or u.topic_id = any(p_topic_ids))+ group by u.url_id+ ),+ include_ids as (+ select cu.id from public.citation_urls cu+ where p_domains is not null and cu.domain = any(p_domains)+ ),+ exclude_ids as (+ select cu.id from public.citation_urls cu+ where p_exclude_domains is not null and cu.domain = any(p_exclude_domains)+ ),+ scoped as materialized (+ select a.*+ from agg a+ where (p_domains is null+ or exists (select 1 from include_ids i where i.id = a.uid))+ and (p_exclude_domains is null+ or not exists (select 1 from exclude_ids e where e.id = a.uid))+ )+ select cu.url, cu.domain, cu.title, a.tc, a.rc, a.ms,+ (select count(*)::bigint from scoped)+ from (select * from scoped order by tc desc, uid limit p_limit) a+ join public.citation_urls cu on cu.id = a.uid+ order by a.tc desc;+end;+$$;++create or replace function public.citations_window_stats_daily(+ p_brand_id uuid,+ 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(results bigint, regions text[])+language plpgsql+stable+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ select coalesce(sum(r.answers), 0)::bigint,+ coalesce(array_agg(distinct r.region) filter (where r.region is not null), '{}')+ 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));+end;+$$;++-- ─── Health report probes ───────────────────────────────────────────────────+-- The daily health report times the page's reads (00108). The Citations page+-- now reads the daily functions, so the report can time those: same body as+-- 00108 plus two probes. Their window counts today, as the page's does.++create or replace function public.health_probe(+ p_user_id uuid,+ p_brand_id uuid,+ p_probe text,+ p_days integer default null+) returns numeric+language plpgsql+security invoker+set search_path to 'public'+as $$+declare+ t0 timestamptz;+ ts_from timestamptz := case when p_days is null then null else now() - make_interval(days => p_days) end;+ day_from date := (now() at time zone 'utc')::date - coalesce(p_days, 0);+ page_day_from date := case when p_days is null then null+ else (now() at time zone 'utc')::date - (p_days - 1) end;+ n bigint;+begin+ perform set_config('role', 'authenticated', true);+ perform set_config(+ 'request.jwt.claims',+ json_build_object('sub', p_user_id, 'role', 'authenticated')::text,+ true+ );+ t0 := clock_timestamp();++ case p_probe+ when 'citations_window_stats' then+ select count(*) into n from public.citations_window_stats(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null);+ when 'citations_domains' then+ select count(*) into n from public.citations_domains(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null);+ when 'citations_urls' then+ select count(*) into n from public.citations_urls(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null,+ p_limit => 100, p_domains => null, p_exclude_domains => null);+ when 'citations_domains_daily' then+ select count(*) into n from public.citations_domains_daily(+ p_brand_id => p_brand_id, p_day_from => page_day_from, p_day_to => null);+ when 'citations_urls_daily' then+ select count(*) into n from public.citations_urls_daily(+ p_brand_id => p_brand_id, p_day_from => page_day_from, p_day_to => null,+ p_limit => 2000);+ when 'insights_aggregates_daily' then+ select count(*) into n from public.insights_aggregates_daily(+ p_brand_id => p_brand_id, p_platform => null, p_models => null, p_region => null,+ p_day_from => case when p_days is null then null else day_from end, p_day_to => null);+ when 'competitor_aggregates_daily' then+ select count(*) into n from public.competitor_aggregates_daily(+ p_brand_id => p_brand_id, p_platform => null, p_models => null, p_region => null,+ p_day_from => case when p_days is null then null else day_from end, p_day_to => null);+ when 'topics_overview_aggregates' then+ select count(*) into n from public.topics_overview_aggregates(p_brand_id => p_brand_id);+ else+ raise exception 'unknown health probe: %', p_probe;+ end case;++ return round(extract(epoch from clock_timestamp() - t0)::numeric, 2);+end;+$$;++revoke all on function public.health_probe(uuid, uuid, text, integer) from public, anon, authenticated;+grant execute on function public.health_probe(uuid, uuid, text, integer) to service_role;diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 5f6303824..141f4129b 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -13715,3 +13715,432 @@ $$;revoke all on function public.delete_brand_results_batch(uuid, integer) from public, anon, authenticated;grant execute on function public.delete_brand_results_batch(uuid, integer) to service_role;+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00118_citations_daily_rollups.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Citations stops recomputing the window on every page load.+--+-- The Citations overview aggregated prompt_result_citations per request. On+-- the largest brand a 30-day window is 935k citation rows over 128k URLs and+-- 15.8k domains: citations_domains took 17s against the authenticated role's+-- 8s statement timeout, and every brand's window grows with its history.+--+-- Same cure as Insights (00066): each brand-day is aggregated once, when the+-- brand's tracking run completes, with the daily sweep as backstop, and the+-- page reads the daily rows. Measured on the largest brand's 30 days: the+-- domain table in 0.06s and the top 2,000 URLs in 0.25s.+--+-- Grain: (day, model_used, platform, region, topic_id) — the page's filter+-- dimensions. A result belongs to exactly one bucket, so results_citing sums+-- across days, engines, regions and topics without double counting. A prompt+-- grain was measured too and compresses nothing (886k rows for 935k+-- citations), so a single-prompt filter stays on the raw functions, which a+-- single prompt keeps small.+--+-- Topic is stored rather than joined at read time. Moving a prompt to another+-- topic (or deleting it) therefore queues its brand in+-- citation_rollup_rebuilds, and the daily sweep recomputes that brand's+-- history. Until then only topic-filtered views show the old attribution;+-- unfiltered totals are unaffected.+--+-- The raw functions stay: the per-prompt filter, Competitor Gaps, the URL+-- detail page, MCP and reports still call them.++-- ─── Tables ─────────────────────────────────────────────────────────────────++-- Answers per bucket: the denominator of every usage %, and the regions list.+create table if not exists public.citation_result_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,+ answers integer not null+);++create index if not exists idx_citation_result_daily_brand_day+ on public.citation_result_daily (brand_id, day);++create table if not exists public.citation_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,+ citations integer not null,+ -- Answers citing at least one URL of the domain.+ results integer not null+);++create index if not exists idx_citation_domain_daily_brand_day+ on public.citation_domain_daily (brand_id, day);++create table if not exists public.citation_url_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,+ url_id bigint not null,+ citations integer not null,+ -- Answers citing the URL.+ results integer not null+);++create index if not exists idx_citation_url_daily_brand_day+ on public.citation_url_daily (brand_id, day);++-- Brands whose prompts changed topic since their rollups were written. No+-- foreign key on purpose: deleting a brand cascades through its prompts, and+-- the trigger below must never make that delete fail. A brand gone by the+-- time the sweep reads its entry has no results, so the rebuild is a no-op+-- and the entry is cleared.+create table if not exists public.citation_rollup_rebuilds (+ brand_id uuid primary key,+ requested_at timestamptz not null default now()+);++-- ─── RLS — same shape as the Insights rollups ───────────────────────────────++alter table public.citation_result_daily enable row level security;+alter table public.citation_domain_daily enable row level security;+alter table public.citation_url_daily enable row level security;+-- Service role only: no policies.+alter table public.citation_rollup_rebuilds enable row level security;++drop policy if exists "Users can read own org citation result rollups" on public.citation_result_daily;+create policy "Users can read own org citation result rollups"+ on public.citation_result_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 domain rollups" on public.citation_domain_daily;+create policy "Users can read own org citation domain rollups"+ on public.citation_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 url rollups" on public.citation_url_daily;+create policy "Users can read own org citation url rollups"+ on public.citation_url_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 ────────────────────────────────────────────────────────────────++-- Recompute one brand's citation rollups for an inclusive UTC day range:+-- delete + insert, so it is idempotent and safe to rerun. Called by the+-- server under the service role (run completion, the daily sweep, the topic+-- rebuild and the backfill script).+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';+begin+ 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;++ insert into public.citation_result_daily (+ brand_id, day, model_used, platform, region, topic_id, answers)+ select p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region, p.topic_id,+ count(*)+ 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+ group by 2, pr.model_used, pr.platform, pr.region, p.topic_id;++ -- A citation row carries its answer's created_at, so the citation index+ -- bounds the scan; the answer supplies the bucket.+ 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;++-- ─── Topic moves queue a rebuild ────────────────────────────────────────────++create or replace function public.queue_citation_rollup_rebuild()+returns trigger+language plpgsql+security definer+set search_path to 'public'+as $$+declare+ v_set uuid := case when tg_op = 'DELETE' then old.prompt_set_id else new.prompt_set_id end;+begin+ if tg_op = 'UPDATE' and new.topic_id is not distinct from old.topic_id then+ return new;+ end if;+ -- Only for a brand that still exists: during a brand delete the cascade+ -- removes the brand first, so its prompts queue nothing.+ insert into public.citation_rollup_rebuilds (brand_id)+ select ps.brand_id from public.prompt_sets ps+ join public.brands b on b.id = ps.brand_id+ where ps.id = v_set+ on conflict (brand_id) do update set requested_at = now();+ return null;+end;+$$;++revoke execute on function public.queue_citation_rollup_rebuild()+ from public, anon, authenticated;++drop trigger if exists prompts_queue_citation_rollup_rebuild on public.prompts;+create trigger prompts_queue_citation_rollup_rebuild+ after update of topic_id or delete on public.prompts+ for each row execute function public.queue_citation_rollup_rebuild();++-- ─── Reads ──────────────────────────────────────────────────────────────────+-- Security invoker: the RLS policies above scope every read to the caller's+-- organization. plpgsql with force_custom_plan so each call is planned with+-- its own brand and window (00113).++create or replace function public.citations_domains_daily(+ p_brand_id uuid,+ 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, total_citations bigint, results_citing bigint, models text[])+language plpgsql+stable+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ select d.domain,+ sum(d.citations)::bigint,+ sum(d.results)::bigint,+ array_agg(distinct coalesce(d.model_used, d.platform))+ from public.citation_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+ order by 2 desc;+end;+$$;++create or replace function public.citations_urls_daily(+ p_brand_id uuid,+ 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,+ p_limit integer default 2000,+ p_domains text[] default null,+ p_exclude_domains text[] default null+) returns table(url text, domain text, title text, total_citations bigint,+ results_citing bigint, models text[], total_urls bigint)+language plpgsql+stable+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ with agg as (+ select u.url_id as uid,+ sum(u.citations)::bigint as tc,+ sum(u.results)::bigint as rc,+ array_agg(distinct coalesce(u.model_used, u.platform)) as ms+ from public.citation_url_daily u+ where u.brand_id = p_brand_id+ and (p_day_from is null or u.day >= p_day_from)+ and (p_day_to is null or u.day <= p_day_to)+ and (p_models is null or u.model_used = any(p_models))+ and (p_regions is null or u.region = any(p_regions))+ and (p_topic_ids is null or u.topic_id = any(p_topic_ids))+ group by u.url_id+ ),+ include_ids as (+ select cu.id from public.citation_urls cu+ where p_domains is not null and cu.domain = any(p_domains)+ ),+ exclude_ids as (+ select cu.id from public.citation_urls cu+ where p_exclude_domains is not null and cu.domain = any(p_exclude_domains)+ ),+ scoped as materialized (+ select a.*+ from agg a+ where (p_domains is null+ or exists (select 1 from include_ids i where i.id = a.uid))+ and (p_exclude_domains is null+ or not exists (select 1 from exclude_ids e where e.id = a.uid))+ )+ select cu.url, cu.domain, cu.title, a.tc, a.rc, a.ms,+ (select count(*)::bigint from scoped)+ from (select * from scoped order by tc desc, uid limit p_limit) a+ join public.citation_urls cu on cu.id = a.uid+ order by a.tc desc;+end;+$$;++create or replace function public.citations_window_stats_daily(+ p_brand_id uuid,+ 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(results bigint, regions text[])+language plpgsql+stable+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+#variable_conflict use_column+begin+ return query+ select coalesce(sum(r.answers), 0)::bigint,+ coalesce(array_agg(distinct r.region) filter (where r.region is not null), '{}')+ 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));+end;+$$;++-- ─── Health report probes ───────────────────────────────────────────────────+-- The daily health report times the page's reads (00108). The Citations page+-- now reads the daily functions, so the report can time those: same body as+-- 00108 plus two probes. Their window counts today, as the page's does.++create or replace function public.health_probe(+ p_user_id uuid,+ p_brand_id uuid,+ p_probe text,+ p_days integer default null+) returns numeric+language plpgsql+security invoker+set search_path to 'public'+as $$+declare+ t0 timestamptz;+ ts_from timestamptz := case when p_days is null then null else now() - make_interval(days => p_days) end;+ day_from date := (now() at time zone 'utc')::date - coalesce(p_days, 0);+ page_day_from date := case when p_days is null then null+ else (now() at time zone 'utc')::date - (p_days - 1) end;+ n bigint;+begin+ perform set_config('role', 'authenticated', true);+ perform set_config(+ 'request.jwt.claims',+ json_build_object('sub', p_user_id, 'role', 'authenticated')::text,+ true+ );+ t0 := clock_timestamp();++ case p_probe+ when 'citations_window_stats' then+ select count(*) into n from public.citations_window_stats(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null);+ when 'citations_domains' then+ select count(*) into n from public.citations_domains(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null);+ when 'citations_urls' then+ select count(*) into n from public.citations_urls(+ p_brand_id => p_brand_id, p_date_from => ts_from, p_date_to => null,+ p_models => null, p_regions => null, p_prompt_ids => null, p_topic_ids => null,+ p_limit => 100, p_domains => null, p_exclude_domains => null);+ when 'citations_domains_daily' then+ select count(*) into n from public.citations_domains_daily(+ p_brand_id => p_brand_id, p_day_from => page_day_from, p_day_to => null);+ when 'citations_urls_daily' then+ select count(*) into n from public.citations_urls_daily(+ p_brand_id => p_brand_id, p_day_from => page_day_from, p_day_to => null,+ p_limit => 2000);+ when 'insights_aggregates_daily' then+ select count(*) into n from public.insights_aggregates_daily(+ p_brand_id => p_brand_id, p_platform => null, p_models => null, p_region => null,+ p_day_from => case when p_days is null then null else day_from end, p_day_to => null);+ when 'competitor_aggregates_daily' then+ select count(*) into n from public.competitor_aggregates_daily(+ p_brand_id => p_brand_id, p_platform => null, p_models => null, p_region => null,+ p_day_from => case when p_days is null then null else day_from end, p_day_to => null);+ when 'topics_overview_aggregates' then+ select count(*) into n from public.topics_overview_aggregates(p_brand_id => p_brand_id);+ else+ raise exception 'unknown health probe: %', p_probe;+ end case;++ return round(extract(epoch from clock_timestamp() - t0)::numeric, 2);+end;+$$;++revoke all on function public.health_probe(uuid, uuid, text, integer) from public, anon, authenticated;+grant execute on function public.health_probe(uuid, uuid, text, integer) to service_role;+diff --git a/web/src/lib/actions/citations.ts b/web/src/lib/actions/citations.tsindex 6fa23971e..1f3711518 100644--- a/web/src/lib/actions/citations.ts+++ b/web/src/lib/actions/citations.ts@@ -12,6 +12,7 @@ import {import { classifyArticleType } from '@/lib/citations/article-type';import { scopeDomainArgs, type CitationsSourceScope } from '@/lib/citations/scope';import { citationUrlMatchKey, normalizeCitationUrl } from '@/lib/citations/normalize';+import { rollupDayWindow, runDayWindow, type DayWindow } from '@/lib/citations/rollup-window';// ─── Types ────────────────────────────────────────────────────────────────────@@ -121,6 +122,44 @@ function resolveDateRange(filters: CitationsFilters): { from?: string; to?: strireturn { from: from.toISOString(), to: to.toISOString() };}+// ─── Daily rollups (00118) ────────────────────────────────────────────────────++/**+ * The UTC day window the daily rollups answer these filters with, or null when+ * the raw functions must (see rollupDayWindow). The 24h view is the latest+ * completed run's day(s); before a brand's first run it stays on the raw path.+ */+async function rollupWindow(+ supabase: Awaited<ReturnType<typeof createClient>>,+ brandId: string,+ filters: CitationsFilters,+): Promise<DayWindow | null> {+ const window = rollupDayWindow(filters);+ if (window !== 'latest-run') return window;++ const { data } = await supabase+ .from('tracking_runs')+ .select('started_at, completed_at')+ .eq('brand_id', brandId)+ .not('completed_at', 'is', null)+ .order('completed_at', { ascending: false })+ .limit(1);+ const run = data?.[0] as { started_at: string; completed_at: string } | undefined;+ return run ? runDayWindow(run) : null;+}++/** Arguments shared by the three daily functions. */+function dailyArgs(brandId: string, filters: CitationsFilters, window: DayWindow) {+ return {+ p_brand_id: brandId,+ p_day_from: window.dayFrom,+ p_day_to: window.dayTo,+ p_models: modelFilterList(filters.platforms) ?? undefined,+ p_regions: filters.regions && filters.regions.length > 0 ? filters.regions : undefined,+ p_topic_ids: filters.topicIds && filters.topicIds.length > 0 ? filters.topicIds : undefined,+ };+}+// ─── Main action ──────────────────────────────────────────────────────────────/** URL rows the overview asks for. The table paginates a hundred at a time. */@@ -208,10 +247,10 @@ export async function getCitationsOverview(): Promise<CitationsOverview> {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('domain').eq('brand_id', brandId),- resolveTopicPromptIds(supabase, filters),+ rollupWindow(supabase, brandId, filters),]);const brandDomains = (brandDomainRows ?? [])@@ -222,7 +261,24 @@ export async function getCitationsOverview(.filter(Boolean);const classifyCtx = { brandDomains, competitorDomains };- const args = overviewArgs(brandId, filters, topicPromptIds);+ // Daily rollups unless the filters need the raw rows (see rollupWindow).+ // The raw path resolves a topic to its prompts; the rollups carry topic.+ const rawArgs = window+ ? null+ : overviewArgs(brandId, filters, await resolveTopicPromptIds(supabase, filters));+ const daily = window ? dailyArgs(brandId, filters, window) : null;+ const readDomains = () =>+ daily+ ? supabase.rpc('citations_domains_daily', daily)+ : supabase.rpc('citations_domains', rawArgs!);+ const readStats = () =>+ daily+ ? supabase.rpc('citations_window_stats_daily', daily)+ : supabase.rpc('citations_window_stats', rawArgs!);+ const readUrls = (extra: { p_domains?: string[]; p_exclude_domains?: string[] } = {}) =>+ daily+ ? supabase.rpc('citations_urls_daily', { ...daily, p_limit: CITATIONS_URL_LIMIT, ...extra })+ : supabase.rpc('citations_urls', { ...rawArgs!, p_limit: CITATIONS_URL_LIMIT, ...extra });const scoped = Boolean(filters.sourceScope && filters.sourceScope !== 'all');// A scope is resolved from the classified domain list, so a scoped URL query@@ -230,9 +286,9 @@ export async function getCitationsOverview(// page open starts on — keeps all three in flight, because serializing it// would double the default load for a filter nobody selected.const [domainRes, statsRes, parallelUrlRes] = await Promise.all([- supabase.rpc('citations_domains', args),- supabase.rpc('citations_window_stats', args),- scoped ? null : supabase.rpc('citations_urls', { ...args, p_limit: CITATIONS_URL_LIMIT }),+ readDomains(),+ readStats(),+ scoped ? null : readUrls(),]);if (domainRes.error) throw new Error(domainRes.error.message);@@ -273,13 +329,7 @@ export async function getCitationsOverview(};});- const urlRes =- parallelUrlRes ??- (await supabase.rpc('citations_urls', {- ...args,- p_limit: CITATIONS_URL_LIMIT,- ...scopeDomainArgs(filters.sourceScope, rows),- }));+ const urlRes = parallelUrlRes ?? (await readUrls(scopeDomainArgs(filters.sourceScope, rows)));if (urlRes.error) throw new Error(urlRes.error.message);const urlRowsRaw = (urlRes.data as UrlAggRow[] | null) ?? [];diff --git a/web/src/lib/citations/rollup-window.test.ts b/web/src/lib/citations/rollup-window.test.tsnew file mode 100644index 000000000..c4e1e9aaa--- /dev/null+++ b/web/src/lib/citations/rollup-window.test.ts@@ -0,0 +1,52 @@+import { describe, expect, it } from 'vitest';+import { rollupDayWindow, runDayWindow } from './rollup-window';++const NOW = new Date('2026-10-08T21:00:00Z');++describe('rollupDayWindow', () => {+ it('counts today in the 7, 30 and 90 day windows', () => {+ expect(rollupDayWindow({ datePreset: '7d' }, NOW)).toEqual({+ dayFrom: '2026-10-02',+ dayTo: '2026-10-08',+ });+ expect(rollupDayWindow({ datePreset: '30d' }, NOW)).toEqual({+ dayFrom: '2026-09-09',+ dayTo: '2026-10-08',+ });+ expect(rollupDayWindow({ datePreset: '90d' }, NOW)).toEqual({+ dayFrom: '2026-07-11',+ dayTo: '2026-10-08',+ });+ });++ it('leaves all time unbounded', () => {+ expect(rollupDayWindow({ datePreset: 'all' }, NOW)).toEqual({});+ });++ it('takes the days of a custom range', () => {+ expect(+ rollupDayWindow({ datePreset: 'custom', dateFrom: '2026-09-01', dateTo: '2026-09-15' }, NOW),+ ).toEqual({ dayFrom: '2026-09-01', dayTo: '2026-09-15' });+ expect(rollupDayWindow({ datePreset: 'custom', dateFrom: '2026-09-01' }, NOW)).toEqual({+ dayFrom: '2026-09-01',+ dayTo: undefined,+ });+ });++ it('asks for the latest run on the 24h view', () => {+ expect(rollupDayWindow({ datePreset: '24h' }, NOW)).toBe('latest-run');+ });++ it('sends a prompt filter to the raw functions', () => {+ expect(rollupDayWindow({ datePreset: '30d', promptIds: ['p1'] }, NOW)).toBeNull();+ expect(rollupDayWindow({ datePreset: '30d', promptIds: [] }, NOW)).not.toBeNull();+ });+});++describe('runDayWindow', () => {+ it('spans a run that crossed UTC midnight', () => {+ expect(+ runDayWindow({ started_at: '2026-10-07T23:40:00Z', completed_at: '2026-10-08T02:10:00Z' }),+ ).toEqual({ dayFrom: '2026-10-07', dayTo: '2026-10-08' });+ });+});diff --git a/web/src/lib/citations/rollup-window.ts b/web/src/lib/citations/rollup-window.tsnew file mode 100644index 000000000..bdf2b57c5--- /dev/null+++ b/web/src/lib/citations/rollup-window.ts@@ -0,0 +1,54 @@+/**+ * Which window of the Citations daily rollups (00118) answers a filter set.+ *+ * Windows are whole UTC days, as on Insights: 7/30/90 days count today, and+ * 24h is the latest completed tracking run's day(s), which the caller looks+ * up (`'latest-run'`). Null means the raw functions must answer: a+ * single-prompt filter, which the rollups don't carry and which keeps the raw+ * read small.+ */++export type RollupDatePreset = '24h' | '7d' | '30d' | '90d' | 'all' | 'custom';++export interface RollupWindowFilters {+ datePreset: RollupDatePreset;+ dateFrom?: string;+ dateTo?: string;+ promptIds?: string[];+}++export interface DayWindow {+ dayFrom?: string;+ dayTo?: string;+}++const PRESET_DAYS: Partial<Record<RollupDatePreset, number>> = { '7d': 7, '30d': 30, '90d': 90 };++/** UTC calendar day of a date, as 'YYYY-MM-DD'. */+export function utcDay(date: Date): string {+ return date.toISOString().slice(0, 10);+}++export function rollupDayWindow(+ filters: RollupWindowFilters,+ now: Date = new Date(),+): DayWindow | 'latest-run' | null {+ if (filters.promptIds && filters.promptIds.length > 0) return null;+ if (filters.datePreset === '24h') return 'latest-run';+ if (filters.datePreset === 'all') return {};+ if (filters.datePreset === 'custom') {+ return {+ dayFrom: filters.dateFrom ? filters.dateFrom.slice(0, 10) : undefined,+ dayTo: filters.dateTo ? filters.dateTo.slice(0, 10) : undefined,+ };+ }+ const days = PRESET_DAYS[filters.datePreset] ?? 30;+ const from = new Date(now);+ from.setUTCDate(from.getUTCDate() - (days - 1));+ return { dayFrom: utcDay(from), dayTo: utcDay(now) };+}++/** The days a completed run touched: from its start day through its completion day. */+export function runDayWindow(run: { started_at: string; completed_at: string }): DayWindow {+ return { dayFrom: utcDay(new Date(run.started_at)), dayTo: utcDay(new Date(run.completed_at)) };+}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 6f13516c3..243585f44 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -2825,6 +2825,22 @@ export type Database = {total_citations: number}[]}+ citations_domains_daily: {+ Args: {+ p_brand_id: string+ p_day_from?: string+ p_day_to?: string+ p_models?: string[]+ p_regions?: string[]+ p_topic_ids?: string[]+ }+ Returns: {+ domain: string+ models: string[]+ results_citing: number+ total_citations: number+ }[]+ }citations_urls: {Args: {p_brand_id: string@@ -2848,6 +2864,28 @@ export type Database = {url: string}[]}+ citations_urls_daily: {+ Args: {+ p_brand_id: string+ p_day_from?: string+ p_day_to?: string+ p_domains?: string[]+ p_exclude_domains?: string[]+ p_limit?: number+ p_models?: string[]+ p_regions?: string[]+ p_topic_ids?: string[]+ }+ Returns: {+ domain: string+ models: string[]+ results_citing: number+ title: string+ total_citations: number+ total_urls: number+ url: string+ }[]+ }citations_window_stats: {Args: {p_brand_id: string@@ -2863,6 +2901,20 @@ export type Database = {results: number}[]}+ citations_window_stats_daily: {+ Args: {+ p_brand_id: string+ p_day_from?: string+ p_day_to?: string+ p_models?: string[]+ p_regions?: string[]+ p_topic_ids?: string[]+ }+ Returns: {+ regions: string[]+ results: number+ }[]+ }competitor_aggregates: {Args: {p_brand_id: string