Skip to content

Commit

Permalink
Support SQLite for line stat SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
SeyZ committed Sep 19, 2017
1 parent bab466c commit a0b0992
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## [Unreleased]
### Added
- Line Charts - Support SQLite dialect for groupBy fields.

## RELEASE 1.3.6 - 2017-09-10
### Changed
Expand Down
36 changes: 32 additions & 4 deletions services/line-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,34 @@ function LineStatGetter(model, params, opts) {
}
}

function getGroupByDateFieldFormatedForSQLite(timeRange) {
switch (timeRange) {
case 'day': {
return opts.sequelize.fn('STRFTIME', '%Y-%m-%d',
opts.sequelize.col(groupByDateField));
}
case 'week': {
return opts.sequelize.fn('STRFTIME', '%Y-%W',
opts.sequelize.col(groupByDateField));
}
case 'month': {
return opts.sequelize.fn('STRFTIME', '%Y-%m-01',
opts.sequelize.col(groupByDateField));
}
case 'year': {
return opts.sequelize.fn('STRFTIME', '%Y-01-01',
opts.sequelize.col(groupByDateField));
}
}
}

function getGroupByDateInterval() {
if (Database.isMySQL(opts)) {
return [getGroupByDateFieldFormatedForMySQL(timeRange), 'date'];
} else if (Database.isMSSQL(opts)) {
return [getGroupByDateFieldFormatedForMSSQL(timeRange), 'date'];
} else if (Database.isSQLite(opts)) {
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
} else {
var timezone = (-parseInt(params.timezone, 10)).toString();
return [
Expand All @@ -93,13 +116,18 @@ function LineStatGetter(model, params, opts) {

function fillEmptyDateInterval(records) {
if (records.length) {
var firstDate = moment(records[0].label);
var lastDate = moment(records[records.length - 1].label);
var sqlFormat = 'YYYY-MM-DD 00:00:00';
if (Database.isSQLite(opts) && timeRange === 'week') {
sqlFormat = 'YYYY-WW';
}

var firstDate = moment(records[0].label, sqlFormat);
var lastDate = moment(records[records.length - 1].label, sqlFormat);

for (var i = firstDate ; i.toDate() <= lastDate.toDate() ;
i = i.add(1, timeRange)) {

var label = i.format('YYYY-MM-DD 00:00:00');
var label = i.format(sqlFormat);
if (!_.find(records, { label: label })) {
records.push({ label: label, values: { value: 0 }});
}
Expand All @@ -108,7 +136,7 @@ function LineStatGetter(model, params, opts) {
records = _.sortBy(records, 'label');
return _.map(records, function (record) {
return {
label: moment(record.label).format(getFormat()),
label: moment(record.label, sqlFormat).format(getFormat()),
values: record.values
};
});
Expand Down
5 changes: 5 additions & 0 deletions utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ exports.isMSSQL = function(options) {
if (optionsInvalid(options)) { return false; }
return options.connections[0].options.dialect === 'mssql';
};

exports.isSQLite = function(options) {
if (optionsInvalid(options)) { return false; }
return options.connections[0].options.dialect === 'sqlite';
};

0 comments on commit a0b0992

Please sign in to comment.