From 7bf8c8c4c1e7662550082b4c5b64d6b2e27fe96b Mon Sep 17 00:00:00 2001 From: Valentin Lamatte Date: Thu, 27 Jun 2019 10:41:10 +0200 Subject: [PATCH] [-] Linter - Rename opts to options in stat getters (#278) --- CHANGELOG.md | 1 + src/services/line-stat-getter.js | 71 +++++++++++++++++--------------- src/services/pie-stat-getter.js | 16 +++---- src/services/resources-getter.js | 22 +++++----- 4 files changed, 57 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 507e5978..e8ac12ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/services/line-stat-getter.js b/src/services/line-stat-getter.js index 64f21fa0..23153455 100644 --- a/src/services/line-stat-getter.js +++ b/src/services/line-stat-getter.js @@ -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(); @@ -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: @@ -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: @@ -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: @@ -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', ), @@ -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'; } @@ -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', ]; } @@ -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 diff --git a/src/services/pie-stat-getter.js b/src/services/pie-stat-getter.js index ffb17155..2175951d 100644 --- a/src/services/pie-stat-getter.js +++ b/src/services/pie-stat-getter.js @@ -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; @@ -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) { @@ -102,13 +102,13 @@ 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, ], @@ -116,7 +116,7 @@ function PieStatGetter(model, params, opts) { 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) diff --git a/src/services/resources-getter.js b/src/services/resources-getter.js index 5b15990b..ca0e9611 100644 --- a/src/services/resources-getter.js +++ b/src/services/resources-getter.js @@ -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() { @@ -45,7 +45,7 @@ function ResourcesGetter(model, opts, params) { const searchBuilder = new SearchBuilder( model, - opts, + options, params, fieldNamesRequested, ); @@ -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); }); @@ -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); @@ -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( @@ -205,7 +205,7 @@ function ResourcesGetter(model, opts, params) { } } - return scope.count(options); + return scope.count(countOptions); }); }