Skip to content

Commit

Permalink
feat(data): add jsDelivr hits (#263)
Browse files Browse the repository at this point in the history
* Add jsDelivr hits

* Removed interval for re-fetching data

* Removed catch when loading jsDelivr hits

* Record schema in README.md updated
  • Loading branch information
drgy authored and Haroenv committed Sep 6, 2018
1 parent 12d4ffa commit adff89d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
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
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
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
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
@@ -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
Expand Up @@ -14,11 +14,9 @@ export function info() {
}));
}

const logWarning = ({ error, type, packages }) => {
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
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

0 comments on commit adff89d

Please sign in to comment.