Skip to content
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

Merged
merged 6 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Object {
"type": "synonym",
},
],
"jsDelivrHitsEndpoint": "https://data.jsdelivr.com/v1/stats/packages/month/all",
"maxObjSize": 450000,
"npmDownloadsEndpoint": "https://api.npmjs.org/downloads",
"npmRegistryEndpoint": "https://replicate.npmjs.com/registry",
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ms from 'ms';
const defaultConfig = {
npmRegistryEndpoint: 'https://replicate.npmjs.com/registry',
npmDownloadsEndpoint: 'https://api.npmjs.org/downloads',
jsDelivrHitsEndpoint: 'https://data.jsdelivr.com/v1/stats/packages/month/all',
maxObjSize: 450000,
popularDownloadsRatio: 0.005,
appId: 'OFCNCOG2CU',
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import log from './log.js';
import ms from 'ms';
import cargo from 'async/cargo';
import queue from 'async/queue';
import { loadHits } from './jsDelivr';

log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰');

Expand Down Expand Up @@ -96,6 +97,7 @@ async function bootstrap(state) {

if (state.bootstrapLastId) {
log.info('⛷ Bootstrap: starting at doc %s', state.bootstrapLastId);
await loadHits();
return loop(state.bootstrapLastId);
} else {
const { taskID } = await client.deleteIndex(c.bootstrapIndexName);
Expand All @@ -105,6 +107,7 @@ async function bootstrap(state) {
// first time this launches, we need to remember the last seq our bootstrap can trust
await stateManager.save({ seq });
await setSettings(bootstrapIndex);
await loadHits();
return loop(state.bootstrapLastId);
}

Expand Down
35 changes: 35 additions & 0 deletions src/jsDelivr.js
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();
Copy link
Collaborator

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:

  1. export a function which starts this caching
  2. call that at init phase in index.js rather than a side-effect of this module
  3. persist and read it from disk (decide whether this it possible / worth it, we're using Heroku)

Copy link
Collaborator

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.

Copy link
Collaborator

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


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 => {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ms('1 day') is preferable here

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

10 changes: 4 additions & 6 deletions src/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ export function info() {
}));
}

const logWarning = ({ error, type, packages }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 packages to be an array but it was actually a string everywhere so it would fail on packages.join()

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}`
);
};

Expand Down Expand Up @@ -58,7 +56,7 @@ export async function getDownloads(pkgs) {
logWarning({
error,
type: 'downloads',
packages: pkgsNames,
packagesStr: pkgsNames,
});
return { body: {} };
})
Expand All @@ -72,7 +70,7 @@ export async function getDownloads(pkgs) {
logWarning({
error,
type: 'scoped downloads',
packages: [pkg],
packagesStr: pkg,
});
return { body: {} };
})
Expand Down
6 changes: 5 additions & 1 deletion src/saveDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import formatPkg from './formatPkg.js';
import log from './log.js';
import { getDownloads, getDependents } from './npm.js';
import { getChangelogs } from './changelog.js';
import { getHits } from './jsDelivr';

export default function saveDocs({ docs, index }) {
const rawPkgs = docs
Expand All @@ -24,17 +25,20 @@ function addMetaData(pkgs) {
getDownloads(pkgs),
getDependents(pkgs),
getChangelogs(pkgs),
]).then(([downloads, dependents, changelogs]) =>
getHits(pkgs),
]).then(([downloads, dependents, changelogs, hits]) =>
pkgs.map((pkg, index) => ({
...pkg,
...downloads[index],
...dependents[index],
...changelogs[index],
...hits[index],
_searchInternal: {
...pkg._searchInternal,
...downloads[index]._searchInternal,
...dependents[index]._searchInternal,
...changelogs[index]._searchInternal,
...hits[index]._searchInternal,
},
}))
);
Expand Down