Skip to content

Commit

Permalink
fix: don't update __all__ downloads every times (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 4, 2018
1 parent e2fc613 commit 8a2f744
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 46 deletions.
16 changes: 14 additions & 2 deletions controllers/registry/package/download.js
Expand Up @@ -76,19 +76,30 @@ module.exports = function* download(next) {
this.body = yield downloadAsReadStream(dist.key);
};

var saving = false;
defer.setInterval(function* () {
if (saving) {
return;
}

// save download count
var totals = [];
var allCount = 0;
for (var name in _downloads) {
var count = _downloads[name];
totals.push([name, count]);
if (name !== '__all__') {
totals.push([name, count]);
}
allCount += count;
}
_downloads = {};

if (totals.length === 0) {
if (allCount === 0) {
return;
}

saving = true;
totals.push([ '__all__', allCount ]);
debug('save download total: %j', totals);

var date = utility.YYYYMMDD();
Expand All @@ -107,4 +118,5 @@ defer.setInterval(function* () {
_downloads[name] = (_downloads[name] || 0) + count;
}
}
saving = false;
}, 5000 + Math.ceil(Math.random() * 1000));
48 changes: 18 additions & 30 deletions services/download_total.js
@@ -1,6 +1,8 @@
'use strict';

var utility = require('utility');
var config = require('../config');
var models = require('../models');
var DownloadTotal = require('../models').DownloadTotal;

exports.getModuleTotal = function* (name, start, end) {
Expand Down Expand Up @@ -41,30 +43,7 @@ exports.getTotalByName = function* (name) {

exports.plusModuleTotal = function* (data) {
var yearMonth = parseYearMonth(data.date);
// all module download total
var row = yield DownloadTotal.find({
where: {
name: '__all__',
date: yearMonth
}
});
if (!row) {
row = DownloadTotal.build({
name: '__all__',
date: yearMonth,
});
}
var field = 'd' + data.date.substring(8, 10);
if (typeof row[field] === 'string') {
// pg bigint is string...
row[field] = utility.toSafeNumber(row[field]);
}
row[field] += data.count;
if (row.changed()) {
yield row.save();
}

row = yield DownloadTotal.find({
where: {
name: data.name,
date: yearMonth,
Expand All @@ -75,16 +54,25 @@ exports.plusModuleTotal = function* (data) {
name: data.name,
date: yearMonth,
});
yield row.save();
}
var field = 'd' + data.date.substring(8, 10);
if (typeof row[field] === 'string') {
// pg bigint is string...
row[field] = utility.toSafeNumber(row[field]);
}
row[field] += data.count;
if (row.changed()) {
return yield row.save();
if (config.database.dialect === 'mysql') {
// mysql update set field = field + count
yield models.query(`UPDATE downloads SET ${field} = ${field} + ${data.count}, gmt_modified=? WHERE id = ?`,
[ new Date(), row.id ]);
} else {
// pg
if (typeof row[field] === 'string') {
// pg bigint is string...
row[field] = utility.toSafeNumber(row[field]);
}
row[field] += data.count;
if (row.changed()) {
return yield row.save();
}
}

return row;
};

Expand Down
52 changes: 38 additions & 14 deletions test/services/download_total.test.js
@@ -1,19 +1,5 @@
/**!
* cnpmjs.org - test/services/download_total.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var DownloadTotal = require('../../services/download_total');

describe('test/services/download_total.test.js', function () {
Expand All @@ -25,13 +11,25 @@ describe('test/services/download_total.test.js', function () {
count: 1000
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-10-21',
name: '__all__',
count: 1000
};
yield DownloadTotal.plusModuleTotal(data);

data = {
date: '2014-10-22',
name: 'plusModuleTotal-module',
count: 2
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-10-22',
name: '__all__',
count: 2
};
yield DownloadTotal.plusModuleTotal(data);

var rows = yield DownloadTotal.getModuleTotal(
'plusModuleTotal-module', '2014-10-21', '2014-10-21');
Expand All @@ -54,6 +52,12 @@ describe('test/services/download_total.test.js', function () {
count: 3
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-10-21',
name: '__all__',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);
rows = yield DownloadTotal.getModuleTotal(
'plusModuleTotal-module', '2014-10-21', '2014-10-21');
rows.should.length(1);
Expand All @@ -66,18 +70,38 @@ describe('test/services/download_total.test.js', function () {
count: 3
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-10-22',
name: '__all__',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);

data = {
date: '2014-12-22',
name: 'plusModuleTotal-module2',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-12-22',
name: '__all__',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);

data = {
date: '2014-12-21',
name: 'plusModuleTotal-module2',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);
data = {
date: '2014-12-21',
name: '__all__',
count: 3
};
yield DownloadTotal.plusModuleTotal(data);

rows = yield DownloadTotal.getTotal('2014-10-21', '2014-12-21');
rows.should.length(3);
Expand Down

0 comments on commit 8a2f744

Please sign in to comment.