Skip to content

Commit

Permalink
fetch logic tweaks, fix download label (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek authored Nov 12, 2024
1 parent 2b17c73 commit 385fc36
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/Library/MetaData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function generateData({ github, score, npm, npmPkg }: LibraryType, isDark: boole
icon: <Download fill={iconColor} width={16} height={18} />,
content: (
<A href={`https://www.npmjs.com/package/${npmPkg}`} style={styles.link}>
{`${npm.downloads.toLocaleString()}`} {npm.period}ly downloads
{`${npm.downloads.toLocaleString()}`} monthly downloads
</A>
),
}
Expand Down
13 changes: 6 additions & 7 deletions scripts/build-and-score-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ const buildAndScoreData = async () => {

if (SCRAPE_GH_IMAGES) {
console.log('\n📝 Scraping images from README');
await sleep(1000);
data = await Promise.all(data.map(project => fetchReadmeImages(project)));
}

console.log('\n🔖 Determining npm package names');
await sleep(1000);
data = data.map(fillNpmName);

console.log('\n⬇️ Fetching download stats from npm');
await sleep(1000);

// https://github.com/npm/registry/blob/master/docs/download-counts.md#bulk-queries
// https://github.com/npm/registry/blob/main/docs/download-counts.md#bulk-queries
let bulkList = [];

// Fetch scoped packages data
Expand All @@ -100,16 +97,18 @@ const buildAndScoreData = async () => {
})
);

// https://github.com/npm/registry/blob/main/docs/download-counts.md#limits
const CHUNK_SIZE = 128;

// Assemble and fetch regular packages data in bulk queries
const CHUNK_SIZE = 32;
bulkList = [...Array(Math.ceil(bulkList.length / CHUNK_SIZE))].map(_ =>
bulkList.splice(0, CHUNK_SIZE)
);

const downloadsList = (
await Promise.all(
bulkList.map(async (chunk, index) => {
await sleep(250 * index);
await sleep(500 * index);
return await fetchNpmDataBulk(chunk);
})
)
Expand All @@ -118,7 +117,7 @@ const buildAndScoreData = async () => {
const downloadsListWeek = (
await Promise.all(
bulkList.map(async (chunk, index) => {
await sleep(250 * index);
await sleep(500 * index);
return await fetchNpmDataBulk(chunk, 'week');
})
)
Expand Down
7 changes: 4 additions & 3 deletions scripts/fetch-github-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { config } from 'dotenv';
import { processTopics, sleep } from './helpers.js';
import GitHubLicensesQuery from './queries/GitHubLicensesQuery.js';
import GitHubRepositoryQuery from './queries/GitHubRepositoryQuery.js';
import { REQUEST_SLEEP } from '../util/Constants.js';

config();

Expand Down Expand Up @@ -116,15 +117,15 @@ export const fetchGithubData = async (data, retries = 2) => {
}

console.log(`[GH] Retrying fetch for ${data.githubUrl} due to error result`);
await sleep(2500);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}

if (!result?.data?.repository) {
console.log(
`[GH] Retrying fetch for ${data.githubUrl} due to ${result?.message?.toLowerCase() ?? 'missing data'} (status: ${result?.status ?? 'Unknown'})`
);
await sleep(2500);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}

Expand All @@ -135,7 +136,7 @@ export const fetchGithubData = async (data, retries = 2) => {
};
} catch (error) {
console.log(`[GH] Retrying fetch for ${data.githubUrl} due to an error`, error);
await sleep(2500);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}
};
Expand Down
5 changes: 3 additions & 2 deletions scripts/fetch-npm-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'cross-fetch';

import { sleep } from './helpers.js';
import { REQUEST_SLEEP } from '../util/Constants.js';

const ATTEMPTS_LIMIT = 5;

Expand Down Expand Up @@ -45,7 +46,7 @@ export const fetchNpmDataBulk = async (namesArray, period = 'month', attemptsCou
console.error('[NPM] Looks like we have reach the NPM API rate limit!', error);
return namesArray.map(name => ({ name, npm: null }));
}
await sleep(2500 + 500 * attemptsCount, 5000 + 500 * attemptsCount);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
console.log(`[NPM] Retrying fetch for ${namesArray} (${attemptsCount + 1})`);
return await fetchNpmDataBulk(namesArray, period, attemptsCount + 1);
}
Expand Down Expand Up @@ -85,7 +86,7 @@ export const fetchNpmData = async (pkgData, attemptsCount = 0) => {
console.error('[NPM] Looks like we have reach the NPM API rate limit!', error);
return { ...pkgData, npm: null };
}
await sleep(2500 + 500 * attemptsCount, 5000 + 500 * attemptsCount);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
console.log(`[NPM] Retrying fetch for ${npmPkg} (${attemptsCount + 1})`);
return await fetchNpmData(pkgData, attemptsCount + 1);
}
Expand Down
1 change: 1 addition & 0 deletions util/Constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const NUM_PER_PAGE = 30;
export const REQUEST_SLEEP = 5000;

0 comments on commit 385fc36

Please sign in to comment.