diff --git a/apps/cursor/src/data/queries.ts b/apps/cursor/src/data/queries.ts index 611c6234..6e3d29da 100644 --- a/apps/cursor/src/data/queries.ts +++ b/apps/cursor/src/data/queries.ts @@ -440,10 +440,19 @@ export async function getRuleRedirectTarget(ruleSlug: string): Promise<{ return { data: newest?.slug ?? null, error: null }; } +type InstallVelocityRow = { plugin_id: string; installs_window: number }; + // Returns a Map, derived // from `plugin_install_snapshots` via the `plugin_install_velocity` SQL -// function. Plugins with no snapshot history yet (or no fresh installs) -// will simply be absent from the map; callers should default to 0. +// function. Plugins with no installs in the window are absent from the map; +// callers should default to 0. +// +// The function returns a row for every active plugin, and the production +// Data API hands back 100 rows per request (see `getPluginListing`). Read in +// one request, all but the first 100 plugins looked like they had no recent +// installs, and Trending fell back to its estimate from lifetime installs +// for them. So this asks only for the plugins that have some, in a fixed +// order, a page at a time. export async function getPluginInstallVelocity(windowDays = 30): Promise<{ data: Map | null; error: unknown; @@ -453,17 +462,23 @@ export async function getPluginInstallVelocity(windowDays = 30): Promise<{ cacheTag("plugins"); const supabase = await createClient(); - const { data, error } = await supabase.rpc("plugin_install_velocity", { - window_days: windowDays, - }); + + const { data, error } = await fetchAllPages( + async (from, to) => { + const { data, error } = await supabase + .rpc("plugin_install_velocity", { window_days: windowDays }) + .gt("installs_window", 0) + .order("plugin_id") + .range(from, to); + return { data: data as InstallVelocityRow[] | null, error }; + }, + 100, + ); if (error) return { data: null, error }; const map = new Map(); - for (const row of (data ?? []) as { - plugin_id: string; - installs_window: number; - }[]) { + for (const row of data ?? []) { map.set(row.plugin_id, row.installs_window); } return { data: map, error: null };