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.

perf(insights): serve the 24h view from daily rollups using the latest run's days (#1194)

Sanjeev Kumar915d20020d6d
diff --git a/web/src/lib/actions/tracking.test.ts b/web/src/lib/actions/tracking.test.ts
index 2a24738f9..a3a70b772 100644
--- a/web/src/lib/actions/tracking.test.ts
+++ b/web/src/lib/actions/tracking.test.ts
@@ -47,6 +47,9 @@ function fakeQueryBuilder(table: string) {
select: record('select'),
eq: record('eq'),
neq: record('neq'),
+ not: record('not'),
+ is: record('is'),
+ gte: record('gte'),
maybeSingle: async () => promptResultRead,
in: record('in'),
order: record('order'),
@@ -154,6 +157,137 @@ describe('getPromptResultById', () => {
});
});
+describe('getTrackingWindow (#999)', () => {
+ beforeEach(() => {
+ listQueries = [];
+ listRows = {
+ tracking_runs: [
+ {
+ started_at: '2026-10-18T23:30:00.000Z',
+ completed_at: '2026-10-19T01:15:00.000Z',
+ source: 'cron',
+ },
+ {
+ started_at: '2026-10-16T22:00:00.000Z',
+ completed_at: '2026-10-16T23:00:00.000Z',
+ source: 'cron',
+ },
+ ],
+ };
+ });
+
+ afterEach(() => {
+ listRows = {};
+ listQueries = [];
+ });
+
+ it('returns UTC day windows for the latest and previous completed runs', async () => {
+ const { getTrackingWindow } = await import('./tracking');
+ const window = await getTrackingWindow('brand-id');
+
+ expect(window.runDays).toEqual({ dayFrom: '2026-10-18', dayTo: '2026-10-19' });
+ expect(window.previousRunDays).toEqual({ dayFrom: '2026-10-16', dayTo: '2026-10-16' });
+ expect(window.lastRunAt).toBe('2026-10-19T01:15:00.000Z');
+ expect(listQueries.find((q) => q.table === 'tracking_runs')?.calls).toContainEqual([
+ 'select',
+ 'started_at, completed_at',
+ ]);
+ });
+});
+
+describe('Insights 24h rollup routing (#999)', () => {
+ beforeEach(() => {
+ rpcCalls = [];
+ listQueries = [];
+ rpcByName = true;
+ listRows = {
+ tracking_runs: [
+ {
+ started_at: '2026-10-18T23:30:00.000Z',
+ completed_at: '2026-10-19T01:15:00.000Z',
+ source: 'cron',
+ },
+ {
+ started_at: '2026-10-16T22:00:00.000Z',
+ completed_at: '2026-10-16T23:00:00.000Z',
+ source: 'cron',
+ },
+ ],
+ };
+ });
+
+ afterEach(() => {
+ rpcByName = false;
+ listRows = {};
+ listQueries = [];
+ rpcCalls = [];
+ });
+
+ it('uses daily rollups for the latest run and the previous run for deltas', async () => {
+ const { getInsightsData } = await import('./tracking');
+ const data = await getInsightsData('brand-id', { anchor24h: true });
+
+ expect(data.trackingWindow?.runDays).toEqual({
+ dayFrom: '2026-10-18',
+ dayTo: '2026-10-19',
+ });
+ expect(
+ rpcCalls.some(
+ (call) =>
+ call.name === 'insights_aggregates_daily' &&
+ call.args.p_day_from === '2026-10-18' &&
+ call.args.p_day_to === '2026-10-19',
+ ),
+ ).toBe(true);
+ expect(
+ rpcCalls.some(
+ (call) =>
+ call.name === 'insights_aggregates_daily' &&
+ call.args.p_day_from === '2026-10-16' &&
+ call.args.p_day_to === '2026-10-16',
+ ),
+ ).toBe(true);
+ expect(rpcCalls.some((call) => call.name === 'insights_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'competitor_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'share_of_voice_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'ai_visibility_aggregates')).toBe(false);
+ });
+
+ it('uses topic-scoped daily rollups for a topic-filtered 24h view (#867)', async () => {
+ const { getInsightsData } = await import('./tracking');
+ await getInsightsData('brand-id', { anchor24h: true, topicId: 'topic-1' });
+
+ expect(
+ rpcCalls.some(
+ (call) =>
+ call.name === 'insights_aggregates_daily' &&
+ call.args.p_day_from === '2026-10-18' &&
+ call.args.p_day_to === '2026-10-19' &&
+ call.args.p_topic_id === 'topic-1',
+ ),
+ ).toBe(true);
+ const dailyCalls = rpcCalls.filter((call) => call.name.endsWith('_daily'));
+ expect(dailyCalls.length).toBeGreaterThan(0);
+ for (const call of dailyCalls) {
+ expect(call.args.p_topic_id).toBe('topic-1');
+ }
+ expect(rpcCalls.some((call) => call.name === 'insights_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'competitor_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'share_of_voice_aggregates')).toBe(false);
+ expect(rpcCalls.some((call) => call.name === 'ai_visibility_aggregates')).toBe(false);
+ });
+
+ it('retains the raw fallback when no completed run exists', async () => {
+ listRows = {};
+ rpcByName = false;
+ const { getInsightsData } = await import('./tracking');
+ await getInsightsData('brand-id', { anchor24h: true });
+
+ expect(rpcCalls.some((call) => call.name === 'insights_aggregates')).toBe(true);
+ expect(rpcCalls.some((call) => call.name === 'insights_aggregates_daily')).toBe(false);
+ });
+});
+
describe('getInsightsSummary', () => {
it('divides positive sentiment by brand-mentioning results, not all results (#508)', async () => {
rpcMock = vi.fn(async () => ({ data: insightsAggregatesRow, error: null }));
diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.ts
index a33ce52fc..82336d11b 100644
--- a/web/src/lib/actions/tracking.ts
+++ b/web/src/lib/actions/tracking.ts
@@ -3,6 +3,7 @@
import { unstable_cache } from 'next/cache';
import { dbClient, withDbClient } from '@/lib/supabase/scoped';
import { expandDateToEndOfDay } from '@/lib/dates';
+import { runDayWindow } from '@/lib/citations/rollup-window';
import {
comparisonWindows,
dailyWindow,
@@ -782,6 +783,10 @@ export interface TrackingWindow {
* because a new brand has nothing older than 24h to age out.
*/
anchored: { dateFrom: string; dateTo: string } | null;
+ /** UTC day(s) touched by the latest completed run, for daily rollups. */
+ runDays?: DayWindow | null;
+ /** UTC day(s) touched by the prior completed run, for run-over-run deltas. */
+ previousRunDays?: DayWindow | null;
/** A full run currently in flight (cron or manual), for the progress banner. */
activeRun: { startedAt: string; source: string } | null;
/** The brand's last completed full run, scheduled or Run All (VIS-012); null before its first. */
@@ -809,7 +814,7 @@ export async function getTrackingWindow(brandId: string): Promise<TrackingWindow
const [completedResp, activeResp, scheduledResp] = await Promise.all([
supabase
.from('tracking_runs')
- .select('completed_at')
+ .select('started_at, completed_at')
.eq('brand_id', brandId)
.not('completed_at', 'is', null)
.order('completed_at', { ascending: false })
@@ -834,13 +839,32 @@ export async function getTrackingWindow(brandId: string): Promise<TrackingWindow
.limit(2),
]);
- const completed = completedResp.data ?? [];
- const latest = completed[0]?.completed_at as string | undefined;
+ const completed = (completedResp.data ?? []) as {
+ started_at?: string | null;
+ completed_at?: string | null;
+ }[];
+ const latestRun = completed[0];
+ const previousRun = completed[1];
+ const latest = latestRun?.completed_at ?? undefined;
+ const runDays =
+ latestRun?.started_at && latestRun.completed_at
+ ? runDayWindow({
+ started_at: latestRun.started_at,
+ completed_at: latestRun.completed_at,
+ })
+ : null;
+ const previousRunDays =
+ previousRun?.started_at && previousRun.completed_at
+ ? runDayWindow({
+ started_at: previousRun.started_at,
+ completed_at: previousRun.completed_at,
+ })
+ : null;
let anchored: TrackingWindow['anchored'] = null;
if (latest) {
const latestMs = new Date(latest).getTime();
- const prev = completed[1]?.completed_at as string | undefined;
+ const prev = previousRun?.completed_at ?? undefined;
const lowerMs = Math.max(latestMs - 24 * 3600_000, prev ? new Date(prev).getTime() + 1 : 0);
anchored = { dateFrom: new Date(lowerMs).toISOString(), dateTo: latest };
}
@@ -848,6 +872,8 @@ export async function getTrackingWindow(brandId: string): Promise<TrackingWindow
const active = activeResp.data?.[0];
return {
anchored,
+ runDays,
+ previousRunDays,
activeRun: active
? { startedAt: active.started_at as string, source: (active.source as string) ?? 'manual' }
: null,
@@ -892,6 +918,8 @@ type InsightsFilterArgs = {
dateFrom?: string;
dateTo?: string;
days?: DayWindow;
+ /** Previous completed run's UTC day(s), for run-over-run comparisons. */
+ previousDays?: DayWindow;
/** "Compare: Same prompts" (VIS-002); on unless explicitly false. */
samePrompts?: boolean;
};
@@ -1159,7 +1187,7 @@ async function getChangeDrivers(
let res;
if (windows.cur.kind === 'days') {
- const { cur, prev } = deltaDayWindows(windows.cur.days);
+ const { cur, prev } = deltaDayWindows(windows.cur.days, opts?.previousDays);
res = await supabase.rpc('insights_change_drivers_daily', {
...base,
p_cur_from: cur.dayFrom,
@@ -1264,7 +1292,7 @@ async function getSamePromptComparison(
let res;
if (windows.cur.kind === 'days') {
// Explicit bounds for both windows: the same days the card compares.
- const { cur, prev } = deltaDayWindows(windows.cur.days);
+ const { cur, prev } = deltaDayWindows(windows.cur.days, opts?.previousDays);
res = await supabase.rpc('insights_same_prompt_compare_daily', {
...base,
p_cur_from: cur.dayFrom,
@@ -1291,10 +1319,9 @@ async function getSamePromptComparison(
}
/**
- * The anchored 24h view only changes when a tracking run completes, yet each
- * page open recomputed it from raw results: ~10 reads over the same answers,
- * 6-8 s on the largest brand once they contend. Cache it per brand, filters
- * and anchored window; the next completed run moves the window and so the key.
+ * Raw fallback for anchored 24h reads that cannot use daily rollups (currently
+ * topic-filtered reads). The result is cached per brand, filters and anchored
+ * window; the next completed run moves the window and so the key.
*
* Computed with the service-role client because the cache cannot read the
* caller's cookies; every loader scopes its reads by brand, and the caller's
@@ -1350,34 +1377,40 @@ export async function getInsightsData(
/** When the current view is filtered, check unfiltered data existence too. */
checkUnfiltered?: boolean;
/**
- * The 24h preset: anchor dateFrom/dateTo to the last completed tracking
- * run (see getTrackingWindow). Resolved here rather than by a separate
- * client call, because each server action is its own queued request.
+ * The 24h preset: resolve the latest completed run here so the view can use
+ * its UTC day window (including topic-filtered daily rollups), without a
+ * separate client action request.
*/
anchor24h?: boolean;
},
): Promise<InsightsData> {
const trackingWindow = opts.anchor24h ? await getTrackingWindow(brandId).catch(() => null) : null;
const anchored = trackingWindow?.anchored ?? null;
+ const rollup24h = Boolean(opts.anchor24h && trackingWindow?.runDays);
const filterOpts: InsightsFilterArgs = {
model: opts.model,
region: opts.region,
topicId: opts.topicId,
- dateFrom: anchored?.dateFrom ?? opts.dateFrom,
- dateTo: anchored?.dateTo ?? opts.dateTo,
- days: opts.days,
+ dateFrom: rollup24h ? undefined : (anchored?.dateFrom ?? opts.dateFrom),
+ dateTo: rollup24h ? undefined : (anchored?.dateTo ?? opts.dateTo),
+ days: rollup24h ? (trackingWindow?.runDays ?? undefined) : opts.days,
+ previousDays: rollup24h ? (trackingWindow?.previousRunDays ?? undefined) : undefined,
samePrompts: opts.samePrompts,
};
- // An anchored window is served from the cache; a failure there (including a
- // section that would have degraded) falls back to the live, lenient read.
- const windowedRead = anchored
- ? cachedAnchoredInsights(brandId, filterOpts, anchored).catch((err) => {
- console.error('[insights] cached 24h read failed, reading live', err);
- return loadWindowedInsights(brandId, filterOpts, { strict: false });
- })
- : loadWindowedInsights(brandId, filterOpts, { strict: false });
+ // All 24h reads use daily rollups for the latest run's UTC day(s), with
+ // p_topic_id passed through when a topic is selected. Without a completed
+ // run-day window, retain the existing raw, anchored fallback. Keep the cached
+ // path until the rollup path's latency has been measured.
+ const windowedRead = rollup24h
+ ? loadWindowedInsights(brandId, filterOpts, { strict: false })
+ : anchored
+ ? cachedAnchoredInsights(brandId, filterOpts, anchored).catch((err) => {
+ console.error('[insights] cached 24h read failed, reading live', err);
+ return loadWindowedInsights(brandId, filterOpts, { strict: false });
+ })
+ : loadWindowedInsights(brandId, filterOpts, { strict: false });
// Quota (tracked prompts), opportunities and the unfiltered check depend
// on the caller's org and session, so they are read live on every open.
@@ -1800,6 +1833,7 @@ export async function getInsightsSummary(
dateTo?: string;
topicId?: string;
days?: DayWindow;
+ previousDays?: DayWindow;
},
): Promise<InsightsSummary> {
const supabase = await dbClient();
@@ -1919,7 +1953,7 @@ export async function getInsightsSummary(
const duration = currentTo.getTime() - currentFrom.getTime();
const prevFrom = new Date(currentFrom.getTime() - duration);
- const deltaDays = daily ? deltaDayWindows(daily) : null;
+ const deltaDays = daily ? deltaDayWindows(daily, opts?.previousDays) : null;
// Current-window aggregate: when no dateFrom was passed, the top-level
// call above is unbounded — for the delta math we need the same
@@ -2580,6 +2614,7 @@ export async function getCompetitorComparison(
dateTo?: string;
topicId?: string;
days?: DayWindow;
+ previousDays?: DayWindow;
},
): Promise<CompetitorComparisonData> {
const supabase = await dbClient();
@@ -2655,7 +2690,7 @@ export async function getCompetitorComparison(
const duration = currentTo.getTime() - currentFrom.getTime();
const prevFrom = new Date(currentFrom.getTime() - duration);
- const deltaDays = daily ? deltaDayWindows(daily) : null;
+ const deltaDays = daily ? deltaDayWindows(daily, opts?.previousDays) : null;
const [curRes, prevRes, visPrevRes] = await Promise.all([
deltaDays
@@ -2872,6 +2907,7 @@ export async function getShareOfVoiceData(
dateTo?: string;
topicId?: string;
days?: DayWindow;
+ previousDays?: DayWindow;
},
): Promise<ShareOfVoiceData> {
const supabase = await dbClient();
@@ -2919,7 +2955,7 @@ export async function getShareOfVoiceData(
}
const duration = currentTo.getTime() - currentFrom.getTime();
const prevFrom = new Date(currentFrom.getTime() - duration);
- const deltaDays = daily ? deltaDayWindows(daily) : null;
+ const deltaDays = daily ? deltaDayWindows(daily, opts?.previousDays) : null;
const [{ data: curData, error: curErr }, { data: prevData, error: prevErr }] = await Promise.all([
daily
@@ -3100,6 +3136,7 @@ export async function getVisibilityRateTrend(
dateTo?: string;
topicId?: string;
days?: DayWindow;
+ previousDays?: DayWindow;
},
): Promise<VisibilityRateTrendData> {
const supabase = await dbClient();
@@ -3146,7 +3183,7 @@ export async function getVisibilityRateTrend(
});
// The chart's warm-up range on the rollup path starts at the previous
// window's first day.
- const deltaDays = daily?.dayFrom ? deltaDayWindows(daily) : null;
+ const deltaDays = daily?.dayFrom ? deltaDayWindows(daily, opts?.previousDays) : null;
const [
{ data: brand },
diff --git a/web/src/lib/insights-windows.test.ts b/web/src/lib/insights-windows.test.ts
index b2e7a9c99..a8bd05485 100644
--- a/web/src/lib/insights-windows.test.ts
+++ b/web/src/lib/insights-windows.test.ts
@@ -67,6 +67,16 @@ describe('comparisonWindows', () => {
expect(w.prev).toEqual({ kind: 'days', days: deltaDayWindows(days).prev });
});
+
+ it("uses the previous completed run's days for a run-anchored comparison (#999)", () => {
+ const days = { dayFrom: '2026-10-18', dayTo: '2026-10-19' };
+ const previousDays = { dayFrom: '2026-10-16', dayTo: '2026-10-16' };
+ const w = comparisonWindows({ days, previousDays });
+
+ expect(w.cur).toEqual({ kind: 'days', days });
+ expect(w.prev).toEqual({ kind: 'days', days: previousDays });
+ expect(deltaDayWindows(days, previousDays).prev).toEqual(previousDays);
+ });
});
describe('visibilityChange', () => {
diff --git a/web/src/lib/insights-windows.ts b/web/src/lib/insights-windows.ts
index 255c8a0e8..b07ce3e65 100644
--- a/web/src/lib/insights-windows.ts
+++ b/web/src/lib/insights-windows.ts
@@ -4,9 +4,9 @@ import { expandDateToEndOfDay } from '@/lib/dates';
//
// The aggregate RPCs used to rescan a brand's whole history per page load;
// past ~55k results that exceeds the authenticated role's 8s statement
-// timeout and the wide presets die. Every preset except 24h now describes its
-// window as whole UTC days, and calls that carry one are answered from the
-// pre-aggregated daily tables instead — cost scales with days in the window,
+// timeout and the wide presets die. Presets describe their rollup windows as
+// whole UTC days (24h uses the latest completed run's day(s)), and calls that
+// carry one are answered from the daily tables — cost scales with days in the window,
// never with accumulated results.
//
// Topic-filtered calls are served from the rollups too (00164): with
@@ -28,6 +28,8 @@ export interface DayWindow {
export interface WindowedOpts {
topicId?: string;
days?: DayWindow;
+ /** Previous completed run's day window, when a view is anchored to a run. */
+ previousDays?: DayWindow;
}
/** The day window to serve from rollups, or null when the raw path must answer. */
@@ -48,11 +50,16 @@ export function addDays(day: string, n: number): string {
/**
* Current + previous equal-length day windows for the ↑/↓ delta math. An
* unbounded window anchors to the trailing 7 days, mirroring what the raw
- * path's `now() - 7d` default has always compared against. The previous
- * window ends the day BEFORE the current one starts — the raw path's
+ * path's `now() - 7d` default has always compared against. When a previous
+ * completed run's window is supplied, use it instead of deriving an adjacent
+ * equal-length period. The previous window ends the day BEFORE the current
+ * one starts by default — the raw path's
* timestamp arithmetic let the two windows share their boundary instant.
*/
-export function deltaDayWindows(days: DayWindow): {
+export function deltaDayWindows(
+ days: DayWindow,
+ previousDays?: DayWindow,
+): {
cur: { dayFrom: string; dayTo: string };
prev: { dayFrom: string; dayTo: string };
} {
@@ -60,9 +67,14 @@ export function deltaDayWindows(days: DayWindow): {
const curFrom = days.dayFrom ?? addDays(curTo, -6);
const len = Math.round((Date.parse(curTo) - Date.parse(curFrom)) / DAY_MS) + 1;
const prevTo = addDays(curFrom, -1);
+ const defaultPrev = { dayFrom: addDays(prevTo, -(len - 1)), dayTo: prevTo };
+ const prev =
+ previousDays?.dayFrom && previousDays?.dayTo
+ ? { dayFrom: previousDays.dayFrom, dayTo: previousDays.dayTo }
+ : defaultPrev;
return {
cur: { dayFrom: curFrom, dayTo: curTo },
- prev: { dayFrom: addDays(prevTo, -(len - 1)), dayTo: prevTo },
+ prev,
};
}
@@ -80,7 +92,7 @@ export type MetricWindow =
export interface ComparisonWindows {
/** The window the number on screen covers. */
cur: MetricWindow;
- /** The equal-length window right before it; null for All time, which has none. */
+ /** The previous comparison window; null for All time, which has none. */
prev: MetricWindow | null;
/** Length of `cur` in days (24h is 1); null for All time. */
windowDays: number | null;
@@ -92,7 +104,7 @@ export interface ComparisonWindows {
* the same filters always compare the same answers (VIS-001).
*
* - Whole-day windows compare against the equal run of whole days ending
- * the day before (deltaDayWindows).
+ * the day before by default (deltaDayWindows), or a supplied previous run.
* - Timestamp windows compare against the equal span ending where the
* current one starts.
* - All time has no previous window: there is no earlier period of the same
@@ -105,7 +117,7 @@ export function comparisonWindows(
const daily = dailyWindow(opts);
if (daily) {
if (!daily.dayFrom) return { cur: { kind: 'days', days: daily }, prev: null, windowDays: null };
- const { cur, prev } = deltaDayWindows(daily);
+ const { cur, prev } = deltaDayWindows(daily, opts?.previousDays);
return {
cur: { kind: 'days', days: daily },
prev: { kind: 'days', days: prev },