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.

fix(competitors): show the last 30 days instead of all time (#951)

Gökhan Geyike36c8a7553b9
diff --git a/server/src/lib/health/report.js b/server/src/lib/health/report.js
index 3a3932381..abdfd683f 100644
--- a/server/src/lib/health/report.js
+++ b/server/src/lib/health/report.js
@@ -28,7 +28,7 @@ export const PROBES = [
{ probe: 'citations_domains', days: 30 },
{ probe: 'citations_urls', days: 30 },
{ probe: 'insights_aggregates_daily', days: 30 },
- { probe: 'competitor_aggregates_daily', days: null },
+ { probe: 'competitor_aggregates_daily', days: 30 },
{ probe: 'topics_overview_aggregates', days: null },
];
diff --git a/supabase/migrations/00110_competitor_aggregates_work_mem.sql b/supabase/migrations/00110_competitor_aggregates_work_mem.sql
new file mode 100644
index 000000000..25ceca648
--- /dev/null
+++ b/supabase/migrations/00110_competitor_aggregates_work_mem.sql
@@ -0,0 +1,13 @@
+-- Competitors page: let competitor_aggregates_daily sort in memory.
+--
+-- The function counts distinct prompts per competitor and per engine over
+-- the competitor-prompt rollup — ~240k rows on the largest brand's 30-day
+-- window — and with the default 5 MB work_mem each of those sorts spilled
+-- to disk (~20 MB). 64 MB keeps them in memory: 5.35 s -> 4.4 s on that
+-- window, as the page runs it. The citation reads already set 96 MB.
+--
+-- If this function is ever recreated (CREATE OR REPLACE resets its
+-- settings), keep `set work_mem = '64MB'` on the new definition.
+
+alter function public.competitor_aggregates_daily(uuid, text, text[], text, date, date)
+ set work_mem = '64MB';
diff --git a/supabase/schema.sql b/supabase/schema.sql
index f1ef28d87..7627a6aaf 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -12554,3 +12554,20 @@ grant execute on function public.citations_urls(
uuid, timestamptz, timestamptz, text[], text[], uuid[], uuid[], integer, text[], text[]
) to authenticated, service_role;
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00110_competitor_aggregates_work_mem.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- Competitors page: let competitor_aggregates_daily sort in memory.
+--
+-- The function counts distinct prompts per competitor and per engine over
+-- the competitor-prompt rollup — ~240k rows on the largest brand's 30-day
+-- window — and with the default 5 MB work_mem each of those sorts spilled
+-- to disk (~20 MB). 64 MB keeps them in memory: 5.35 s -> 4.4 s on that
+-- window, as the page runs it. The citation reads already set 96 MB.
+--
+-- If this function is ever recreated (CREATE OR REPLACE resets its
+-- settings), keep `set work_mem = '64MB'` on the new definition.
+
+alter function public.competitor_aggregates_daily(uuid, text, text[], text, date, date)
+ set work_mem = '64MB';
+
diff --git a/web/messages/en.json b/web/messages/en.json
index 5b361c860..9850fc03f 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -1847,6 +1847,7 @@
"citations": "Citations",
"trackedCompetitors": "Tracked Competitors",
"selectToCompare": "Select a competitor to see head-to-head comparison.",
+ "windowLast30Days": "Scores cover the last 30 days.",
"selected": "Selected",
"noData": "No data",
"emptyTitle": "No competitors yet",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/competitors/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/competitors/page.tsx
index 14747211d..ea23f330d 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/competitors/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/competitors/page.tsx
@@ -1145,6 +1145,15 @@ function HeadToHeadSection({
);
}
+/** The trailing 30 calendar days ending today (UTC), as Insights' 30d preset. */
+function last30Days() {
+ const dayTo = new Date().toISOString().slice(0, 10);
+ const from = new Date(`${dayTo}T00:00:00Z`);
+ from.setUTCDate(from.getUTCDate() - 29);
+ const dayFrom = from.toISOString().slice(0, 10);
+ return { dateFrom: `${dayFrom}T00:00:00.000Z`, days: { dayFrom, dayTo } };
+}
+
// ─── Main Page ────────────────────────────────────────────────────────────────
export default function CompetitorsPage() {
@@ -1170,10 +1179,10 @@ export default function CompetitorsPage() {
try {
const [comps, comparison] = await Promise.all([
getCompetitors(brand.id),
- // Empty day window = all-time served from the daily rollups; without
- // it the raw all-time scan exceeds the 8s statement timeout on
- // brands with a large result history.
- getCompetitorComparison(brand.id, { days: {} }),
+ // The last 30 days, served from the daily rollups. All-time read
+ // every rollup row the brand ever had and was nearing the 8s
+ // statement timeout on the largest brand.
+ getCompetitorComparison(brand.id, last30Days()),
]);
setCompetitors(comps);
setComparisonData(comparison.brands.length > 1 ? comparison : null);
@@ -1338,7 +1347,9 @@ export default function CompetitorsPage() {
{competitors.length}
</Badge>
</CardTitle>
- <p className="text-xs text-muted-foreground">{t('selectToCompare')}</p>
+ <p className="text-xs text-muted-foreground">
+ {t('selectToCompare')} {t('windowLast30Days')}
+ </p>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-3">