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

Commit

Permalink
fix(download_total): change column date to DateTime
Browse files Browse the repository at this point in the history
Fixes #540
  • Loading branch information
fengmk2 committed Dec 21, 2014
1 parent d651c2e commit 9ed3b3a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
5 changes: 3 additions & 2 deletions docs/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,14 @@ CREATE TABLE IF NOT EXISTS `download_total` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`date` varchar(10) NOT NULL COMMENT 'YYYY-MM-DD format',
`date` datetime NOT NULL COMMENT 'YYYY-MM-DD format',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'download count',
PRIMARY KEY (`id`),
UNIQUE KEY `date_name` (`date`, `name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
-- ALTER TABLE `download_total` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
-- ALTER TABLE `download_total` CHANGE `name` `name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
-- ALTER TABLE `download_total` CHANGE `date` `date` datetime NOT NULL COMMENT 'YYYY-MM-DD format';

CREATE TABLE IF NOT EXISTS `module_deps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
Expand Down
4 changes: 2 additions & 2 deletions models/download_total.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
// `gmt_create` datetime NOT NULL COMMENT 'create time',
// `gmt_modified` datetime NOT NULL COMMENT 'modified time',
// `date` varchar(10) NOT NULL COMMENT 'YYYY-MM-DD format',
// `date` datetime NOT NULL COMMENT 'YYYY-MM-DD format',
// `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
// `count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'download count',
// PRIMARY KEY (`id`),
Expand All @@ -28,7 +28,7 @@
module.exports = function (sequelize, DataTypes) {
return sequelize.define('DownloadTotal', {
date: {
type: DataTypes.STRING(10),
type: DataTypes.DATE,
allowNull: false,
comment: 'YYYY-MM-DD format',
},
Expand Down
40 changes: 19 additions & 21 deletions services/download_total.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ var models = require('../models');
var DownloadTotal = models.DownloadTotal;

exports.getModuleTotal = function* (name, start, end) {
var dates = getDateRanges(start, end);
return yield DownloadTotal.findAll({
start += ' 00:00:00';
end += ' 23:59:59';
var rows = yield DownloadTotal.findAll({
where: {
date: {
in: dates
gte: start,
lte: end
},
name: name
}
});
return formatRows(rows);
};

exports.plusModuleTotal = function* (data) {
Expand All @@ -50,26 +53,21 @@ exports.plusModuleTotal = function* (data) {
return row;
};


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

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;
function formatRows(rows) {
return rows.map(function (row) {
return {
name: row.name,
count: row.count,
date: moment(row.date).format('YYYY-MM-DD'),
};
});
}
4 changes: 4 additions & 0 deletions test/services/download_total.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ describe('services/download_total.test.js', function () {
'plusModuleTotal-module', '2014-10-21', '2014-10-21');
rows.should.length(1);
rows[0].count.should.equal(1000);
rows[0].date.should.equal('2014-10-21');

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

// save again
data = {
Expand All @@ -55,6 +58,7 @@ 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);
rows[0].date.should.equal('2014-10-21');

data = {
date: '2014-10-22',
Expand Down

0 comments on commit 9ed3b3a

Please sign in to comment.