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 all commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ For every single NPM package, we create a record in the Algolia index. The resul
"downloadsLast30Days": 10978749,
"downloadsRatio": 0.08310651682685861,
"humanDownloadsLast30Days": "11m",
"jsDelivrHits": 11684192,
"popular": true,
"version": "6.26.0",
"versions": {
Expand Down
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
23 changes: 23 additions & 0 deletions src/jsDelivr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import got from 'got';
import c from './config.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 });
const hitsJSON = (await hitsJSONpromise).body;
hits.clear();
hitsJSON.forEach(formatHits);
}

export function getHits(pkgs) {
return pkgs.map(({ name }) => ({ jsDelivrHits: hits.get(name) || 0 }));
}
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