Skip to content

Commit

Permalink
fix: cache total downloads (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bodin committed Jul 11, 2021
1 parent a3c41f3 commit 99be307
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Expand Up @@ -171,6 +171,7 @@ export const config = {
indexRules,
expiresAt: ms('30 days'),
popularExpiresAt: ms('7 days'),
cacheTotalDownloads: ms('1 minute'),
};

export type Config = typeof config;
Expand Down
22 changes: 20 additions & 2 deletions src/npm/index.ts
Expand Up @@ -30,6 +30,7 @@ type GetDownload = {
downloadsMagnitude: number;
};
};
let cacheTotalDownloads: { total: number; date: number } | undefined;

const registry = nano({
url: config.npmRegistryEndpoint,
Expand Down Expand Up @@ -187,6 +188,15 @@ function getDependent(_pkg: Pick<RawPkg, 'name'>): GetDependent {
* Get total npm downloads.
*/
async function getTotalDownloads(): Promise<number> {
const start = Date.now();

if (
cacheTotalDownloads &&
Date.now() - cacheTotalDownloads.date < config.cacheTotalDownloads
) {
return cacheTotalDownloads.total;
}

const {
body: { downloads: totalNpmDownloadsPerDay },
} = await request<{ downloads: Array<{ downloads: number }> }>(
Expand All @@ -196,10 +206,18 @@ async function getTotalDownloads(): Promise<number> {
}
);

return totalNpmDownloadsPerDay.reduce(
(total, { downloads: dayDownloads }) => total + dayDownloads,
const total = totalNpmDownloadsPerDay.reduce(
(agg, { downloads: dayDownloads }) => agg + dayDownloads,
0
);
cacheTotalDownloads = {
date: start,
total,
};

datadog.timing('npm.getTotalDownloads', Date.now() - start);

return total;
}

/**
Expand Down

0 comments on commit 99be307

Please sign in to comment.