Skip to content

Commit 21c6b45

Browse files
tombeckenhamclaude
andcommitted
fix(npm-stats): stop summary cards overcounting co-installed packages
The NPM Stats summary cards (all-time, monthly, weekly, daily) summed every @tanstack/*start* package mapped to the library. ~9 of those are transitive deps of a single `@tanstack/react-start` install, so each install was counted ~9x (weekly showed 148.1M instead of ~16.5M). Make the cards honor the library's declared `npmPackageNames`, matching the chart below them: - NPMSummary uses the shared `recentDownloadsQuery` (which passes npmPackageNames) instead of a local copy that dropped it, fixing the daily/weekly/monthly cards. - `rebuildOssStatsCache` now restricts each library's aggregate to its declared `npmPackageNames` when present, fixing the all-time card. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 599fad7 commit 21c6b45

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/components/npm-stats/NPMSummary.tsx

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import * as React from 'react'
22
import { Suspense } from 'react'
33
import { useSuspenseQuery } from '@tanstack/react-query'
4-
import { queryOptions } from '@tanstack/react-query'
54
import { BlankErrorBoundary } from '~/components/BlankErrorBoundary'
6-
import { ossStatsQuery } from '~/queries/stats'
5+
import { ossStatsQuery, recentDownloadsQuery } from '~/queries/stats'
76
import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter'
8-
import { fetchRecentDownloadStats } from '~/utils/stats-queries.functions'
97
import type { Library } from '~/libraries'
108

119
/**
@@ -24,26 +22,6 @@ function formatNumber(num: number): string {
2422
return num.toLocaleString()
2523
}
2624

27-
/**
28-
* Query options for recent download stats
29-
*/
30-
function recentDownloadsQuery(library: Library) {
31-
return queryOptions({
32-
queryKey: ['npm-recent-downloads', library.id],
33-
queryFn: () =>
34-
fetchRecentDownloadStats({
35-
data: {
36-
library: {
37-
id: library.id,
38-
repo: library.repo,
39-
frameworks: library.frameworks,
40-
},
41-
},
42-
}),
43-
staleTime: 5 * 60 * 1000, // 5 minutes
44-
})
45-
}
46-
4725
/**
4826
* Animated counter component for all-time downloads
4927
*/
@@ -110,7 +88,9 @@ function NPMSummaryContent({ library }: { library: Library }) {
11088
const { data: ossStats } = useSuspenseQuery(ossStatsQuery({ library }))
11189

11290
// Fetch recent download stats (daily, weekly, monthly)
113-
const { data: recentStats } = useSuspenseQuery(recentDownloadsQuery(library))
91+
const { data: recentStats } = useSuspenseQuery(
92+
recentDownloadsQuery({ library }),
93+
)
11494

11595
return (
11696
<div className="my-6">

src/utils/stats-db.server.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') {
248248
{ npm: NpmStats; packageCount: number; updatedAt?: Date }
249249
>()
250250

251+
// When a library declares explicit npmPackageNames, only those packages
252+
// count toward its aggregate. This avoids counting internal sub-packages
253+
// (e.g. start-server-core, start-plugin-core) that are co-installed as
254+
// dependencies of a single user-facing install and would otherwise inflate
255+
// the totals several times over.
256+
const explicitPackagesByLibrary = new Map<string, Set<string>>()
257+
for (const library of libraries) {
258+
if (library.npmPackageNames?.length) {
259+
explicitPackagesByLibrary.set(
260+
library.id,
261+
new Set(library.npmPackageNames),
262+
)
263+
}
264+
}
265+
251266
for (const pkg of packages) {
252267
if (pkg.downloads === null) {
253268
continue
@@ -266,6 +281,11 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') {
266281
continue
267282
}
268283

284+
const explicitPackages = explicitPackagesByLibrary.get(pkg.libraryId)
285+
if (explicitPackages && !explicitPackages.has(pkg.packageName)) {
286+
continue
287+
}
288+
269289
const existing = libraryNpmStatsMap.get(pkg.libraryId) ?? {
270290
npm: { totalDownloads: 0 },
271291
packageCount: 0,

0 commit comments

Comments
 (0)