Skip to content

Commit

Permalink
perf: use new stats api to fetch all download counts in one request
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Feb 11, 2024
1 parent ece5e72 commit 905253b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions scripts/algolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ const updateAlgoliaIndex = async (force?: boolean) => {
// as Algolia does not support random sorting natively
const randomIndexArr = shuffleArray(list.length);

const statsResp = await fetch('https://api.fontsource.org/v1/stats');
if (!statsResp.ok) {
throw new Error('Failed to fetch stats');
}
const stats = (await statsResp.json()) as Record<
string,
{ total: { npmDownloadMonthly: number } }
>;

let index = 0;
for (const id of list) {
const metadata = metadataImport[id];
if (!metadata)
console.warn(`No metadata found for ${id} when updating Algolia index`);

const stats = (await (
await fetch(`https://api.fontsource.org/v1/stats/${id}`)
).json()) as any;
const downloadCountMonthly = stats?.total?.npmDownloadMonthly;

const obj = {
objectID: id,
family: metadata.family,
Expand All @@ -64,7 +68,7 @@ const updateAlgoliaIndex = async (force?: boolean) => {
lastModified: Math.floor(
new Date(metadata.lastModified).getTime() / 1000
),
downloadMonth: downloadCountMonthly ?? 0,
downloadMonth: stats[id]?.total.npmDownloadMonthly ?? 0,
randomIndex: randomIndexArr[index],
};

Expand Down

0 comments on commit 905253b

Please sign in to comment.