Skip to content
This repository has been archived by the owner on Jun 29, 2019. It is now read-only.

Show total download counts in /hpm install #178

Merged
merged 2 commits into from
Sep 2, 2016
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
25 changes: 23 additions & 2 deletions app/main/plugins/hain-package-manager/downloads-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
const lo_assign = require('lodash.assign');
const got = require('got');

function _getDownloadsCount(packages, period) {
const RANGE_ALL = '1000-01-01:3000-01-01';
const PERIOD_LAST_MONTH = 'last-month';

function _getDownloadCountForPoint(packages, period) {
const query = packages.join(',');
const query_enc = encodeURIComponent(query);
const baseUrl = `http://api.npmjs.org/downloads/point/${period}/${query_enc},`
return got(baseUrl, { json: true }).then(res => res.body);
}

function _getDownloadCountForRange(packages, range) {
const query = packages.join(',');
const query_enc = encodeURIComponent(query);
const baseUrl = `http://api.npmjs.org/downloads/range/${range}/${query_enc},`
return got(baseUrl, { json: true }).then(res => {
const { body } = res;
Object.keys(body).forEach(pkg => {
body[pkg].downloads = body[pkg].downloads.reduce((sum, dl) => sum + dl.downloads, 0);
});
return body;
});
}

function splitIntoChunks(arr, chunkSize) {
const chunks = [];
for (let i = 0; i < arr.length; i += chunkSize) {
Expand All @@ -24,7 +40,12 @@ function getDownloadsCount(packages, period) {
const packageChunks = splitIntoChunks(packages, 20);
const promises = [];
for (const packageChunk of packageChunks) {
const promise = _getDownloadsCount(packageChunk, period || 'last-month');
let promise;
if (! period) {
promise = _getDownloadCountForRange(packageChunk, RANGE_ALL);
} else {
promise = _getDownloadCountForPoint(packageChunk, period || PERIOD_LAST_MONTH);
}
promises.push(promise);
}
return Promise.all(promises).then(results => {
Expand Down
2 changes: 1 addition & 1 deletion app/main/plugins/hain-package-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = (context) => {
const newestPkgNames = newestPackages.map(x => x.name);
const popularPackages = lo_reject(packages, x => newestPkgNames.indexOf(x.name) >= 0);

const popularResults = _toSearchResults('install', popularPackages, 'Popular (Monthly)');
const popularResults = _toSearchResults('install', popularPackages, 'Popular');
const newestResults = _toSearchResults('install', newestPackages, 'Newest');
return newestResults.concat(popularResults);
}
Expand Down