Skip to content

Commit d77b904

Browse files
ci: apply automated fixes
1 parent 11b6577 commit d77b904

3 files changed

Lines changed: 71 additions & 29 deletions

File tree

src/components/NpmStatsSummaryBar.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ function isValidMetric(value: number | undefined | null): boolean {
2626

2727
function NpmStatsSummaryContent({ library }: { library: LibrarySlim }) {
2828
const { data: stats } = useSuspenseQuery(ossStatsQuery({ library }))
29-
const { data: recentStats } = useSuspenseQuery(recentDownloadStatsQuery({ library }))
29+
const { data: recentStats } = useSuspenseQuery(
30+
recentDownloadStatsQuery({ library }),
31+
)
3032

3133
const npmDownloads = stats.npm?.totalDownloads ?? 0
3234
const hasNpmDownloads = isValidMetric(npmDownloads)
@@ -46,11 +48,7 @@ function NpmStatsSummaryContent({ library }: { library: LibrarySlim }) {
4648
{/* All Time Downloads (Animated with ref callback) */}
4749
<div className="text-left">
4850
<div className="text-3xl font-bold text-gray-900 dark:text-gray-100 relative">
49-
{hasNpmDownloads ? (
50-
<span ref={counterRef}>0</span>
51-
) : (
52-
<span>0</span>
53-
)}
51+
{hasNpmDownloads ? <span ref={counterRef}>0</span> : <span>0</span>}
5452
</div>
5553
<div className="text-sm text-gray-500 dark:text-gray-400 font-medium mt-1">
5654
All Time Downloads
@@ -91,7 +89,11 @@ function NpmStatsSummaryContent({ library }: { library: LibrarySlim }) {
9189
)
9290
}
9391

94-
export default function NpmStatsSummaryBar({ library }: { library: LibrarySlim }) {
92+
export default function NpmStatsSummaryBar({
93+
library,
94+
}: {
95+
library: LibrarySlim
96+
}) {
9597
return (
9698
<Suspense
9799
fallback={

src/queries/stats.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@ export function ossStatsQuery({ library }: { library?: LibrarySlim } = {}) {
3030
export const recentDownloadStatsQueryOptions = (library: LibrarySlim) =>
3131
queryOptions({
3232
queryKey: ['stats', 'recent-downloads', library.id],
33-
queryFn: () => fetchRecentDownloadStats({
34-
data: {
35-
library: {
36-
id: library.id,
37-
repo: library.repo,
38-
frameworks: library.frameworks,
33+
queryFn: () =>
34+
fetchRecentDownloadStats({
35+
data: {
36+
library: {
37+
id: library.id,
38+
repo: library.repo,
39+
frameworks: library.frameworks,
40+
},
3941
},
40-
},
41-
}),
42+
}),
4243
staleTime: 1000 * 60 * 10, // Cache for 10 minutes (fresher than all-time stats)
4344
})
4445

45-
export function recentDownloadStatsQuery({ library }: { library: LibrarySlim }) {
46+
export function recentDownloadStatsQuery({
47+
library,
48+
}: {
49+
library: LibrarySlim
50+
}) {
4651
return recentDownloadStatsQueryOptions(library)
4752
}

src/utils/stats.server.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -805,16 +805,18 @@ export const fetchRecentDownloadStats = createServerFn({ method: 'POST' })
805805
// Add HTTP caching headers - shorter cache for recent data
806806
setResponseHeaders(
807807
new Headers({
808-
'Cache-Control':
809-
'public, max-age=300, stale-while-revalidate=600',
808+
'Cache-Control': 'public, max-age=300, stale-while-revalidate=600',
810809
'Netlify-CDN-Cache-Control':
811810
'public, max-age=300, durable, stale-while-revalidate=600',
812811
}),
813812
)
814813

815814
// Import db functions dynamically
816-
const { getRegisteredPackages, getBatchNpmDownloadChunks, setCachedNpmDownloadChunk } =
817-
await import('./stats-db.server')
815+
const {
816+
getRegisteredPackages,
817+
getBatchNpmDownloadChunks,
818+
setCachedNpmDownloadChunk,
819+
} = await import('./stats-db.server')
818820

819821
// Get all registered packages for this library (includes framework adapters)
820822
let packageNames = await getRegisteredPackages(data.library.id)
@@ -828,17 +830,41 @@ export const fetchRecentDownloadStats = createServerFn({ method: 'POST' })
828830
const todayStr = today.toISOString().substring(0, 10)
829831

830832
// Calculate date ranges
831-
const dailyStart = new Date(today.getTime() - 24 * 60 * 60 * 1000).toISOString().substring(0, 10)
832-
const weeklyStart = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000).toISOString().substring(0, 10)
833-
const monthlyStart = new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000).toISOString().substring(0, 10)
833+
const dailyStart = new Date(today.getTime() - 24 * 60 * 60 * 1000)
834+
.toISOString()
835+
.substring(0, 10)
836+
const weeklyStart = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
837+
.toISOString()
838+
.substring(0, 10)
839+
const monthlyStart = new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000)
840+
.toISOString()
841+
.substring(0, 10)
834842

835843
// Create chunk requests for all packages and time periods
836844
const chunkRequests = []
837845
for (const packageName of packageNames) {
838846
chunkRequests.push(
839-
{ packageName, dateFrom: dailyStart, dateTo: todayStr, binSize: 'daily' as const, period: 'daily' },
840-
{ packageName, dateFrom: weeklyStart, dateTo: todayStr, binSize: 'daily' as const, period: 'weekly' },
841-
{ packageName, dateFrom: monthlyStart, dateTo: todayStr, binSize: 'daily' as const, period: 'monthly' },
847+
{
848+
packageName,
849+
dateFrom: dailyStart,
850+
dateTo: todayStr,
851+
binSize: 'daily' as const,
852+
period: 'daily',
853+
},
854+
{
855+
packageName,
856+
dateFrom: weeklyStart,
857+
dateTo: todayStr,
858+
binSize: 'daily' as const,
859+
period: 'weekly',
860+
},
861+
{
862+
packageName,
863+
dateFrom: monthlyStart,
864+
dateTo: todayStr,
865+
binSize: 'daily' as const,
866+
period: 'monthly',
867+
},
842868
)
843869
}
844870

@@ -908,23 +934,32 @@ export const fetchRecentDownloadStats = createServerFn({ method: 'POST' })
908934
dateFrom: req.dateFrom,
909935
dateTo: req.dateTo,
910936
binSize: req.binSize,
911-
totalDownloads: downloads.reduce((sum: number, d: any) => sum + d.downloads, 0),
937+
totalDownloads: downloads.reduce(
938+
(sum: number, d: any) => sum + d.downloads,
939+
0,
940+
),
912941
dailyData: downloads,
913942
isImmutable: false, // Recent data is mutable
914943
updatedAt: Date.now(),
915944
}
916945

917946
// Cache this chunk asynchronously
918947
setCachedNpmDownloadChunk(chunkData).catch((err) =>
919-
console.warn(`Failed to cache recent downloads for ${req.packageName}:`, err)
948+
console.warn(
949+
`Failed to cache recent downloads for ${req.packageName}:`,
950+
err,
951+
),
920952
)
921953

922954
return {
923955
key: `${req.packageName}|${req.dateFrom}|${req.dateTo}|${req.binSize}`,
924956
data: chunkData,
925957
}
926958
} catch (error) {
927-
console.error(`Failed to fetch recent downloads for ${req.packageName}:`, error)
959+
console.error(
960+
`Failed to fetch recent downloads for ${req.packageName}:`,
961+
error,
962+
)
928963
// Return zero data on error
929964
return {
930965
key: `${req.packageName}|${req.dateFrom}|${req.dateTo}|${req.binSize}`,

0 commit comments

Comments
 (0)