Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/components/npm-stats/NPMSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as React from 'react'
import { Suspense } from 'react'
import { useSuspenseQuery } from '@tanstack/react-query'
import { queryOptions } from '@tanstack/react-query'
import { BlankErrorBoundary } from '~/components/BlankErrorBoundary'
import { ossStatsQuery } from '~/queries/stats'
import { ossStatsQuery, recentDownloadsQuery } from '~/queries/stats'
import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter'
import { fetchRecentDownloadStats } from '~/utils/stats-queries.functions'
import type { Library } from '~/libraries'

/**
Expand All @@ -24,26 +22,6 @@ function formatNumber(num: number): string {
return num.toLocaleString()
}

/**
* Query options for recent download stats
*/
function recentDownloadsQuery(library: Library) {
return queryOptions({
queryKey: ['npm-recent-downloads', library.id],
queryFn: () =>
fetchRecentDownloadStats({
data: {
library: {
id: library.id,
repo: library.repo,
frameworks: library.frameworks,
},
},
}),
staleTime: 5 * 60 * 1000, // 5 minutes
})
}

/**
* Animated counter component for all-time downloads
*/
Expand Down Expand Up @@ -110,7 +88,9 @@ function NPMSummaryContent({ library }: { library: Library }) {
const { data: ossStats } = useSuspenseQuery(ossStatsQuery({ library }))

// Fetch recent download stats (daily, weekly, monthly)
const { data: recentStats } = useSuspenseQuery(recentDownloadsQuery(library))
const { data: recentStats } = useSuspenseQuery(
recentDownloadsQuery({ library }),
)

return (
<div className="my-6">
Expand Down
20 changes: 20 additions & 0 deletions src/utils/stats-db.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') {
{ npm: NpmStats; packageCount: number; updatedAt?: Date }
>()

// When a library declares explicit npmPackageNames, only those packages
// count toward its aggregate. This avoids counting internal sub-packages
// (e.g. start-server-core, start-plugin-core) that are co-installed as
// dependencies of a single user-facing install and would otherwise inflate
// the totals several times over.
const explicitPackagesByLibrary = new Map<string, Set<string>>()
for (const library of libraries) {
if (library.npmPackageNames?.length) {
explicitPackagesByLibrary.set(
library.id,
new Set(library.npmPackageNames),
)
}
}

for (const pkg of packages) {
if (pkg.downloads === null) {
continue
Expand All @@ -266,6 +281,11 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') {
continue
}

const explicitPackages = explicitPackagesByLibrary.get(pkg.libraryId)
if (explicitPackages && !explicitPackages.has(pkg.packageName)) {
continue
}

const existing = libraryNpmStatsMap.get(pkg.libraryId) ?? {
npm: { totalDownloads: 0 },
packageCount: 0,
Expand Down
Loading