Skip to content

Commit

Permalink
[-] Linter - Rename opts to options in stat getters (ForestAdmin#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
larcin authored Jun 27, 2019
1 parent 57e8755 commit 7bf8c8c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]
### Changed
- Naming - Rename opts to options in stat getters.
- Technical - Apply ESLint rules to old files.

## RELEASE 3.2.3 - 2019-06-21
Expand Down
71 changes: 37 additions & 34 deletions src/services/line-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import BaseStatGetter from './base-stat-getter';
import { isMySQL, isMSSQL, isSQLite } from '../utils/database';

// jshint sub: true
function LineStatGetter(model, params, opts) {
BaseStatGetter.call(this, model, params, opts);
function LineStatGetter(model, params, options) {
BaseStatGetter.call(this, model, params, options);

const schema = Schemas.schemas[model.name];
const timeRange = params.time_range.toLowerCase();
Expand All @@ -31,25 +31,25 @@ function LineStatGetter(model, params, opts) {
const groupByDateFieldFormated = `\`${groupByDateField.replace('.', '`.`')}\``;
switch (currentTimeRange) {
case 'day':
return opts.sequelize.fn(
return options.sequelize.fn(
'DATE_FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'%Y-%m-%d 00:00:00',
);
case 'week':
return opts.sequelize
return options.sequelize
.literal(`DATE_FORMAT(DATE_SUB(${groupByDateFieldFormated}, \
INTERVAL ((7 + WEEKDAY(${groupByDateFieldFormated})) % 7) DAY), '%Y-%m-%d 00:00:00')`);
case 'month':
return opts.sequelize.fn(
return options.sequelize.fn(
'DATE_FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'%Y-%m-01 00:00:00',
);
case 'year':
return opts.sequelize.fn(
return options.sequelize.fn(
'DATE_FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'%Y-01-01 00:00:00',
);
default:
Expand All @@ -61,25 +61,25 @@ INTERVAL ((7 + WEEKDAY(${groupByDateFieldFormated})) % 7) DAY), '%Y-%m-%d 00:00:
const groupByDateFieldFormated = `[${groupByDateField.replace('.', '].[')}]`;
switch (currentTimeRange) {
case 'day':
return opts.sequelize.fn(
return options.sequelize.fn(
'FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'yyyy-MM-dd 00:00:00',
);
case 'week':
return opts.sequelize
return options.sequelize
.literal(`FORMAT(DATEADD(DAY, -DATEPART(dw,${groupByDateFieldFormated}),\
${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
case 'month':
return opts.sequelize.fn(
return options.sequelize.fn(
'FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'yyyy-MM-01 00:00:00',
);
case 'year':
return opts.sequelize.fn(
return options.sequelize.fn(
'FORMAT',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
'yyyy-01-01 00:00:00',
);
default:
Expand All @@ -90,31 +90,31 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
function getGroupByDateFieldFormatedForSQLite(currentTimeRange) {
switch (currentTimeRange) {
case 'day': {
return opts.sequelize.fn(
return options.sequelize.fn(
'STRFTIME',
'%Y-%m-%d',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
);
}
case 'week': {
return opts.sequelize.fn(
return options.sequelize.fn(
'STRFTIME',
'%Y-%W',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
);
}
case 'month': {
return opts.sequelize.fn(
return options.sequelize.fn(
'STRFTIME',
'%Y-%m-01',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
);
}
case 'year': {
return opts.sequelize.fn(
return options.sequelize.fn(
'STRFTIME',
'%Y-01-01',
opts.sequelize.col(groupByDateField),
options.sequelize.col(groupByDateField),
);
}
default:
Expand All @@ -123,20 +123,20 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
}

function getGroupByDateInterval() {
if (isMySQL(opts)) {
if (isMySQL(options)) {
return [getGroupByDateFieldFormatedForMySQL(timeRange), 'date'];
} else if (isMSSQL(opts)) {
} else if (isMSSQL(options)) {
return [getGroupByDateFieldFormatedForMSSQL(timeRange), 'date'];
} else if (isSQLite(opts)) {
} else if (isSQLite(options)) {
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
}
return [
opts.sequelize.fn(
options.sequelize.fn(
'to_char',
opts.sequelize.fn(
options.sequelize.fn(
'date_trunc',
params.time_range,
opts.sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`),
options.sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`),
),
'YYYY-MM-DD 00:00:00',
),
Expand All @@ -157,7 +157,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
function fillEmptyDateInterval(records) {
if (records.length) {
let sqlFormat = 'YYYY-MM-DD 00:00:00';
if (isSQLite(opts) && timeRange === 'week') {
if (isSQLite(options) && timeRange === 'week') {
sqlFormat = 'YYYY-WW';
}

Expand All @@ -182,7 +182,10 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);

function getAggregate() {
return [
opts.sequelize.fn(params.aggregate.toLowerCase(), opts.sequelize.col(getAggregateField())),
options.sequelize.fn(
params.aggregate.toLowerCase(),
options.sequelize.col(getAggregateField()),
),
'value',
];
}
Expand All @@ -203,11 +206,11 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
}

function getGroupBy() {
return isMSSQL(opts) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [opts.sequelize.literal('1')];
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.sequelize.literal('1')];
}

function getOrder() {
return isMSSQL(opts) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [opts.sequelize.literal('1')];
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.sequelize.literal('1')];
}

this.perform = () => model
Expand Down
16 changes: 8 additions & 8 deletions src/services/pie-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { isMSSQL } from '../utils/database';
const ALIAS_GROUP_BY = 'forest_alias_groupby';
const ALIAS_AGGREGATE = 'forest_alias_aggregate';

function PieStatGetter(model, params, opts) {
BaseStatGetter.call(this, model, params, opts);
function PieStatGetter(model, params, options) {
BaseStatGetter.call(this, model, params, options);

const needsDateOnlyFormating = isVersionLessThan4(opts.sequelize);
const needsDateOnlyFormating = isVersionLessThan4(options.sequelize);

const schema = Schemas.schemas[model.name];
let associationSplit;
Expand Down Expand Up @@ -72,7 +72,7 @@ function PieStatGetter(model, params, opts) {
}

function getGroupBy() {
return isMSSQL(opts) ? [opts.sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
return isMSSQL(options) ? [options.sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
}

function formatResults(records) {
Expand Down Expand Up @@ -102,21 +102,21 @@ function PieStatGetter(model, params, opts) {
.findAll({
attributes: [
[
opts.sequelize.col(groupByField),
options.sequelize.col(groupByField),
ALIAS_GROUP_BY,
],
[
opts.sequelize.fn(
options.sequelize.fn(
getAggregate(),
opts.sequelize.col(getAggregateField()),
options.sequelize.col(getAggregateField()),
),
ALIAS_AGGREGATE,
],
],
include: getIncludes(),
where: this.getFilters(),
group: getGroupBy(),
order: [[opts.sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
order: [[options.sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
raw: true,
})
.then(formatResults)
Expand Down
22 changes: 11 additions & 11 deletions src/services/resources-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import SearchBuilder from './search-builder';
import LiveQueryChecker from './live-query-checker';
import { ErrorHTTP422 } from './errors';

function ResourcesGetter(model, opts, params) {
function ResourcesGetter(model, options, params) {
const schema = Schemas.schemas[model.name];
const queryBuilder = new QueryBuilder(model, opts, params);
const queryBuilder = new QueryBuilder(model, options, params);
let segmentScope;
let segmentWhere;
const OPERATORS = new Operators(opts);
const OPERATORS = new Operators(options);
const primaryKey = _.keys(model.primaryKeys)[0];

function getFieldNamesRequested() {
Expand Down Expand Up @@ -45,7 +45,7 @@ function ResourcesGetter(model, opts, params) {

const searchBuilder = new SearchBuilder(
model,
opts,
options,
params,
fieldNamesRequested,
);
Expand All @@ -61,7 +61,7 @@ function ResourcesGetter(model, opts, params) {
}
values.split(',').forEach((value) => {
const condition = {};
condition[key] = new OperatorValueParser(opts)
condition[key] = new OperatorValueParser(options)
.perform(model, key, value, params.timezone);
conditions.push(condition);
});
Expand Down Expand Up @@ -97,9 +97,9 @@ function ResourcesGetter(model, opts, params) {

// WARNING: Choosing the first connection might generate issues if the model does not
// belongs to this database.
return opts.connections[0]
return options.connections[0]
.query(queryToFilterRecords, {
type: opts.sequelize.QueryTypes.SELECT,
type: options.sequelize.QueryTypes.SELECT,
})
.then((results) => {
const recordIds = results.map(result => result[primaryKey] || result.id);
Expand Down Expand Up @@ -169,21 +169,21 @@ function ResourcesGetter(model, opts, params) {

return getWhere()
.then((where) => {
const options = {
const countOptions = {
include,
where,
};

if (!primaryKey) {
// NOTICE: If no primary key is found, use * as a fallback for Sequelize.
options.col = '*';
countOptions.col = '*';
}

if (params.search) {
_.each(schema.fields, (field) => {
if (field.search) {
try {
field.search(options, params.search);
field.search(countOptions, params.search);
hasSmartFieldSearch = true;
} catch (error) {
logger.error(
Expand All @@ -205,7 +205,7 @@ function ResourcesGetter(model, opts, params) {
}
}

return scope.count(options);
return scope.count(countOptions);
});
}

Expand Down

0 comments on commit 7bf8c8c

Please sign in to comment.