-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(data): add jsDelivr hits #263
Changes from 1 commit
c3dd8f0
ee38a8e
5eab2d9
86e69ea
450a070
357ed36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import got from 'got'; | ||
import c from './config.js'; | ||
import log from './log.js'; | ||
|
||
const hits = new Map(); | ||
|
||
function formatHits(pkg) { | ||
if (pkg.type !== 'npm') { | ||
return; | ||
} | ||
|
||
hits.set(pkg.name, pkg.hits); | ||
} | ||
|
||
export async function loadHits() { | ||
const hitsJSONpromise = got(c.jsDelivrHitsEndpoint, { json: true }).catch( | ||
error => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if it would be better to reject with the error here because if this fails at the start there won't be hits for any packages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we should probably reject here, since otherwise every package will log if this can't be loaded |
||
log.warn( | ||
`Can't download hits data from ${ | ||
c.jsDelivrHitsEndpoint | ||
}, error: ${error}` | ||
); | ||
} | ||
); | ||
|
||
const hitsJSON = (await hitsJSONpromise).body; | ||
hits.clear(); | ||
hitsJSON.forEach(formatHits); | ||
} | ||
|
||
export function getHits(pkgs) { | ||
return pkgs.map(({ name }) => ({ jsDelivrHits: hits.get(name) || 0 })); | ||
} | ||
|
||
setInterval(loadHits, 24 * 60 * 60 * 1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, we do a weekly "bootstrap", where we throw away all data and start from scratch. It's probably enough to have data up to date weekly, rather than adding a timer IMO |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,9 @@ export function info() { | |
})); | ||
} | ||
|
||
const logWarning = ({ error, type, packages }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Haroenv just a note that changes in this file are a bugfix not related to jsDelivr. This function expected |
||
const logWarning = ({ error, type, packagesStr }) => { | ||
log.warn( | ||
`Something went wrong asking the ${type} for \n${packages.join( | ||
',' | ||
)} \n${error}` | ||
`Something went wrong asking the ${type} for \n${packagesStr} \n${error}` | ||
); | ||
}; | ||
|
||
|
@@ -58,7 +56,7 @@ export async function getDownloads(pkgs) { | |
logWarning({ | ||
error, | ||
type: 'downloads', | ||
packages: pkgsNames, | ||
packagesStr: pkgsNames, | ||
}); | ||
return { body: {} }; | ||
}) | ||
|
@@ -72,7 +70,7 @@ export async function getDownloads(pkgs) { | |
logWarning({ | ||
error, | ||
type: 'scoped downloads', | ||
packages: [pkg], | ||
packagesStr: pkg, | ||
}); | ||
return { body: {} }; | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work in memory? I'd expect this to blow up the memory usage (which is already at / over the limit of our runtime now).
I like the idea of fetching it on beforehand, but I wonder if it shouldn't be something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is called in init phase, the side-effect here is only the
setInterval
which runs later to refresh the data. I agree that it isn't so nice but wasn't sure if there's a better place to put it.It's less than 1 MB in JSON, I'd expect not much more in memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were talking about a significantly bigger amount of data, 1MB shouldn't be a problem