1- import type { Config } from " @netlify/functions" ;
1+ import type { Config } from ' @netlify/functions'
22import {
33 refreshNpmOrgStats ,
44 fetchGitHubOwnerStats ,
55 fetchGitHubRepoStats ,
6- } from " ~/utils/stats.functions" ;
7- import { setCachedGitHubStats } from " ~/utils/stats-db.server" ;
6+ } from ' ~/utils/stats.functions'
7+ import { setCachedGitHubStats } from ' ~/utils/stats-db.server'
88
99/**
1010 * Netlify Scheduled Function - Refresh org-level stats cache
@@ -21,116 +21,116 @@ import { setCachedGitHubStats } from "~/utils/stats-db.server";
2121 * - After refresh: ~100-200ms per request (served from cache)
2222 */
2323const handler = async ( req : Request ) => {
24- const { next_run } = await req . json ( ) ;
24+ const { next_run } = await req . json ( )
2525
26- console . log ( " [refresh-stats-cache-background] Starting stats refresh..." ) ;
26+ console . log ( ' [refresh-stats-cache-background] Starting stats refresh...' )
2727
28- const startTime = Date . now ( ) ;
28+ const startTime = Date . now ( )
2929
3030 try {
31- const org = " tanstack" ;
31+ const org = ' tanstack'
3232
3333 // Refresh NPM org stats (fetches all packages, aggregates, and caches)
34- console . log ( " [refresh-stats-cache-background] Refreshing NPM org stats..." ) ;
35- const npmStats = await refreshNpmOrgStats ( org ) ;
34+ console . log ( ' [refresh-stats-cache-background] Refreshing NPM org stats...' )
35+ const npmStats = await refreshNpmOrgStats ( org )
3636
3737 // Refresh GitHub org stats
3838 console . log (
39- " [refresh-stats-cache-background] Refreshing GitHub org stats..."
40- ) ;
41- const githubCacheKey = `org:${ org } ` ;
42- const githubStats = await fetchGitHubOwnerStats ( org ) ;
43- await setCachedGitHubStats ( githubCacheKey , githubStats , 1 ) ;
39+ ' [refresh-stats-cache-background] Refreshing GitHub org stats...' ,
40+ )
41+ const githubCacheKey = `org:${ org } `
42+ const githubStats = await fetchGitHubOwnerStats ( org )
43+ await setCachedGitHubStats ( githubCacheKey , githubStats , 1 )
4444
4545 // Refresh GitHub stats for each library repo
4646 console . log (
47- " [refresh-stats-cache-background] Refreshing GitHub stats for individual libraries..."
48- ) ;
49- const { libraries } = await import ( " ~/libraries" ) ;
47+ ' [refresh-stats-cache-background] Refreshing GitHub stats for individual libraries...' ,
48+ )
49+ const { libraries } = await import ( ' ~/libraries' )
5050 console . log (
5151 `[refresh-stats-cache-background] Found ${ libraries . length } libraries to process:` ,
52- libraries . map ( ( lib ) => ( { id : lib . id , repo : lib . repo } ) )
53- ) ;
54- const libraryResults = [ ] ;
55- const libraryErrors = [ ] ;
52+ libraries . map ( ( lib ) => ( { id : lib . id , repo : lib . repo } ) ) ,
53+ )
54+ const libraryResults = [ ]
55+ const libraryErrors = [ ]
5656
5757 for ( let i = 0 ; i < libraries . length ; i ++ ) {
58- const library = libraries [ i ] ;
58+ const library = libraries [ i ]
5959 if ( ! library . repo ) {
6060 console . log (
61- `[refresh-stats-cache-background] Skipping library ${ library . id } - no repo`
62- ) ;
63- continue ;
61+ `[refresh-stats-cache-background] Skipping library ${ library . id } - no repo` ,
62+ )
63+ continue
6464 }
6565
6666 console . log (
67- `[refresh-stats-cache-background] Processing library ${ library . id } (${ library . repo } )...`
68- ) ;
67+ `[refresh-stats-cache-background] Processing library ${ library . id } (${ library . repo } )...` ,
68+ )
6969 try {
70- const repoStats = await fetchGitHubRepoStats ( library . repo ) ;
70+ const repoStats = await fetchGitHubRepoStats ( library . repo )
7171 console . log (
7272 `[refresh-stats-cache-background] Fetched stats for ${
7373 library . repo
7474 } : ${ repoStats . starCount } stars, ${
7575 repoStats . contributorCount
76- } contributors, ${ repoStats . dependentCount ?? " N/A" } dependents`
77- ) ;
78- await setCachedGitHubStats ( library . repo , repoStats , 1 ) ;
76+ } contributors, ${ repoStats . dependentCount ?? ' N/A' } dependents`,
77+ )
78+ await setCachedGitHubStats ( library . repo , repoStats , 1 )
7979 console . log (
80- `[refresh-stats-cache-background] ✓ Successfully cached stats for ${ library . repo } `
81- ) ;
80+ `[refresh-stats-cache-background] ✓ Successfully cached stats for ${ library . repo } ` ,
81+ )
8282 libraryResults . push ( {
8383 repo : library . repo ,
8484 stars : repoStats . starCount ,
85- } ) ;
85+ } )
8686
8787 // Add delay between requests to avoid rate limiting (except for last item)
8888 if ( i < libraries . length - 1 ) {
89- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
89+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) )
9090 }
9191 } catch ( error ) {
9292 const errorMessage =
93- error instanceof Error ? error . message : String ( error ) ;
93+ error instanceof Error ? error . message : String ( error )
9494 console . error (
9595 `[refresh-stats-cache-background] Failed to refresh ${ library . repo } :` ,
96- errorMessage
97- ) ;
96+ errorMessage ,
97+ )
9898 libraryErrors . push ( {
9999 repo : library . repo ,
100100 error : errorMessage ,
101- } ) ;
101+ } )
102102 }
103103 }
104104
105- const duration = Date . now ( ) - startTime ;
105+ const duration = Date . now ( ) - startTime
106106 console . log (
107107 `[refresh-stats-cache-background] ✓ Completed in ${ duration } ms - NPM: ${ npmStats . totalDownloads . toLocaleString ( ) } downloads (${
108108 Object . keys ( npmStats . packageStats || { } ) . length
109109 } packages), GitHub Org: ${ githubStats . starCount . toLocaleString ( ) } stars, Libraries: ${
110110 libraryResults . length
111- } refreshed, ${ libraryErrors . length } failed`
112- ) ;
111+ } refreshed, ${ libraryErrors . length } failed`,
112+ )
113113 console . log (
114- " [refresh-stats-cache-background] Next invocation at:" ,
115- next_run
116- ) ;
114+ ' [refresh-stats-cache-background] Next invocation at:' ,
115+ next_run ,
116+ )
117117 } catch ( error ) {
118- const duration = Date . now ( ) - startTime ;
119- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
120- const errorStack = error instanceof Error ? error . stack : undefined ;
118+ const duration = Date . now ( ) - startTime
119+ const errorMessage = error instanceof Error ? error . message : String ( error )
120+ const errorStack = error instanceof Error ? error . stack : undefined
121121
122122 console . error (
123123 `[refresh-stats-cache-background] ✗ Failed after ${ duration } ms:` ,
124- errorMessage
125- ) ;
124+ errorMessage ,
125+ )
126126 if ( errorStack ) {
127- console . error ( " [refresh-stats-cache-background] Stack:" , errorStack ) ;
127+ console . error ( ' [refresh-stats-cache-background] Stack:' , errorStack )
128128 }
129129 }
130- } ;
130+ }
131131
132- export default handler ;
132+ export default handler
133133
134134export const config : Config = {
135- schedule : " 0 */6 * * *" , // Every 6 hours
136- } ;
135+ schedule : ' 0 */6 * * *' , // Every 6 hours
136+ }
0 commit comments