Skip to content
Open
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
70 changes: 70 additions & 0 deletions config/elasticsearch/npms.json5
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,76 @@
}
},
},
"dependents": {
"type": "text",
"properties": {
"name": {
"type": "text",
"fields": {
"standard": {
"type": "text",
"analyzer": "standard",
},
"english": {
"type": "text",
"analyzer": "package_english",
},
"english_aggressive": {
"type": "text",
"analyzer": "package_english_aggressive",
},
"edge_ngram": {
"type": "text",
"analyzer": "package_edge_ngram",
"search_analyzer": "package",
},
"raw": {
"type": "text",
"analyzer": "raw",
}
},
},
"version": {
"type": "text",
"index": false
},
},
},
"dependencies": {
"type": "text",
"properties": {
"name": {
"type": "text",
"fields": {
"standard": {
"type": "text",
"analyzer": "standard",
},
"english": {
"type": "text",
"analyzer": "package_english",
},
"english_aggressive": {
"type": "text",
"analyzer": "package_english_aggressive",
},
"edge_ngram": {
"type": "text",
"analyzer": "package_edge_ngram",
"search_analyzer": "package",
},
"raw": {
"type": "text",
"analyzer": "raw",
}
},
},
"version": {
"type": "text",
"index": false
},
},
},
"version": {
"type": "text",
"index": false,
Expand Down
36 changes: 28 additions & 8 deletions lib/analyze/collect/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ function fetchDownloads(name) {
}

/**
* Fetches the dependents count.
* Fetches the dependents and dependents count.
*
* @param {String} name - The package name.
* @param {Nano} npmNano - The client nano instance for npm.
*
* @returns {Promise} The promise for the dependents count.
* @returns {Promise} The promise for the dependents and dependents count.
*/
function fetchDependentsCount(name, npmNano) {
return npmNano.viewAsync('app', 'dependedUpon', {
function fetchDependents(name, npmNano) {
return npmNano.viewAsync('app', 'dependentVersions', {
startkey: [name],
endkey: [name, '\ufff0'],
limit: 1,
reduce: true,
reduce: false,
stale: 'update_after',
})
.then((response) => !response.rows.length ? 0 : response.rows[0].value)
.then((response) => response.rows.map(({ key }) => ({ name: key[2], version: key[1] })))
.catch((err) => {
/* istanbul ignore next */
log.error({ err }, `Failed to fetch ${name} dependents count from CouchDB`);
Expand All @@ -89,6 +88,26 @@ function fetchDependentsCount(name, npmNano) {
});
}

/**
* Fetches the dependencies.
*
* @param {Object} packageJson - The latest package.json data (normalized).
*
* @returns {Promise} The promise for the dependencies.
*/
function fetchDependencies(packageJson) {
const depProps = [
'dependencies',
'devDependencies',
'peerDependencies',
];
const deps = depProps.map((prop) =>
Object.entries(packageJson[prop] || {}).map(([name, version]) => ({ name, version }))
);

return Promise.resolve([].concat(...deps));
}

/**
* Extract the stars count.
*
Expand All @@ -115,7 +134,8 @@ function extractStarsCount(data) {
function npm(data, packageJson, npmNano) {
return promisePropsSettled({
downloads: fetchDownloads(packageJson.name, { timeout: 30000 }),
dependentsCount: fetchDependentsCount(packageJson.name, npmNano),
dependents: fetchDependents(packageJson.name, npmNano),
dependencies: fetchDependencies(packageJson),
starsCount: extractStarsCount(data),
})
.tap(() => log.debug(`The npm collector for ${packageJson.name} completed successfully`));
Expand Down
2 changes: 1 addition & 1 deletion lib/analyze/evaluate/popularity.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function popularity(collected) {
communityInterest: evaluateCommunityInterest(collected),
downloadsCount: evaluateDownloadsCount(collected),
downloadsAcceleration: evaluateDownloadsAcceleration(collected),
dependentsCount: collected.npm ? collected.npm.dependentsCount : 0,
dependentsCount: collected.npm ? collected.npm.dependents.length : 0,
};
}

Expand Down