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

Commit

Permalink
fix(services/download_total): fix download_total slow sql on `date >=…
Browse files Browse the repository at this point in the history
… $start and date <= $end`

Use `date in ($ranges)` instead.

Closes #540
  • Loading branch information
fengmk2 committed Dec 21, 2014
1 parent 01a7221 commit 3653daf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
26 changes: 22 additions & 4 deletions services/download_total.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
* Module dependencies.
*/

var moment = require('moment');
var models = require('../models');
var DownloadTotal = models.DownloadTotal;

exports.getModuleTotal = function* (name, start, end) {
var dates = getDateRanges(start, end);
return yield DownloadTotal.findAll({
where: {
date: {
gte: start,
lte: end
in: dates
},
name: name
}
Expand Down Expand Up @@ -52,6 +53,23 @@ exports.plusModuleTotal = function* (data) {

exports.getTotal = function* (start, end) {
var sql = 'SELECT date, sum(count) AS count FROM download_total \
WHERE date>=? AND date<=? GROUP BY date;';
return yield models.query(sql, [start, end]);
WHERE date in (?) GROUP BY date';
return yield models.query(sql, [getDateRanges(start, end)]);
};

function getDateRanges(start, end) {
var startDate = moment(start, 'YYYY-MM-DD');
var ranges = [start];
if (start < end) {
var next;
while (true) {
next = startDate.add(1, 'days').format('YYYY-MM-DD');
if (next >= end) {
break;
}
ranges.push(next);
}
ranges.push(end);
}
return ranges;
}
27 changes: 27 additions & 0 deletions test/services/download_total.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,24 @@ describe('services/download_total.test.js', function () {
};
yield* DownloadTotal.plusModuleTotal(data);

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

var rows = yield* DownloadTotal.getModuleTotal(
'plusModuleTotal-module', '2014-10-21', '2014-10-21');
rows.should.length(1);
rows[0].count.should.equal(1000);

rows = yield* DownloadTotal.getModuleTotal(
'plusModuleTotal-module', '2014-10-21', '2014-10-22');
rows.should.length(2);
rows[0].count.should.equal(1000);
rows[1].count.should.equal(2);

// save again
data = {
date: '2014-10-21',
Expand All @@ -42,6 +55,20 @@ describe('services/download_total.test.js', function () {
'plusModuleTotal-module', '2014-10-21', '2014-10-21');
rows.should.length(1);
rows[0].count.should.equal(1003);

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

rows = yield DownloadTotal.getTotal('2014-10-21', '2014-12-21');
rows.length.should.above(1);
rows[0].date.should.equal('2014-10-21');
rows[0].count.should.equal(1003);
rows[1].date.should.equal('2014-10-22');
rows[1].count.should.equal(5);
});
});
});

0 comments on commit 3653daf

Please sign in to comment.