diff --git a/src/libs/app.js b/src/libs/app.js index b0390720f2cb4badd2219dd4c9ba67887f3da5ed..3760a0b86812a6ee43df8823a2575f585492fee3 100644 --- a/src/libs/app.js +++ b/src/libs/app.js @@ -4,6 +4,7 @@ const bodyParser = require('body-parser'); const methodOverride = require('method-override'); const cors = require('cors'); const compression = require('compression'); +const squel = require('squel'); const libs = `${process.cwd()}/libs`; @@ -29,6 +30,11 @@ app.use(cors()); app.use(methodOverride()); app.use(cache('1 day')); app.use(compression(9)); +// Middleware tha adds the squel object to req +app.use((req, res, next) => { + req.sql = squel.select(); + next(); +}); app.use('/api/v1', api); // catch 404 and forward to error handler diff --git a/src/libs/middlewares/dimensions.js b/src/libs/middlewares/dimensions.js deleted file mode 100644 index 618a510e028742dbbf4a8e9ca04d19d936d9e39e..0000000000000000000000000000000000000000 --- a/src/libs/middlewares/dimensions.js +++ /dev/null @@ -1,58 +0,0 @@ -/** -* Dimensions middleware -* -* EXAMPLE: -* Use it with no parameters to get all the dimensions specified -* app.get('/', dimensions(), function(req, res, next){}) -* -* Use it with an array of accepted values -* app.get('/', dimensions(['year', 'location']), function(req, res, next){}) -* -* Use it globally -* app.use(dimensions()) -*/ - -/** - * This function returns the intersection of two arrays - * @param {array} a [description] - * @param {array} b [description] - * @return {array} [description] - */ -function intersect(a, b) { - let t; - if (b.length > a.length) { - t = b; b = a; a = t; - } - return a.filter((e) => b.indexOf(e) !== -1); -} - -function dimensions(dims) { - return (req, res, next) => { - req.dims = {}; - if (req.query.dims) { - const params = req.query.dims.split(','); - const dimObj = {}; - for (const param of params) { - const kv = param.split(':'); - dimObj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; - } - // for(let i=0; i<params.length; ++i) { - // let kv = params[i].split(':'); - // dimObj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; - // } - - // If the dims array exists and is not empty - if (typeof dims !== 'undefined' && dims.length > 0) { - const intersection = intersect(dims, Object.keys(dimObj)); - for (let i = 0; i < intersection.length; ++i) { - req.dims[intersection[i]] = dimObj[intersection[i]]; - } - } else { - req.dims = dimObj; - } - } - next(); - }; -} - -module.exports = dimensions; diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js new file mode 100644 index 0000000000000000000000000000000000000000..c74b6b491bc445b22cd480c85d9f232494a139de --- /dev/null +++ b/src/libs/middlewares/parseParams.js @@ -0,0 +1,66 @@ +/** +* ParseParams middleware +* +* EXAMPLE: +* Use it with no parameters to get all the params specified +* app.get('/', parseParams('dims'), function(req, res, next){}) +* +* Use it with an array of accepted values +* app.get('/', parseParams('filter', ['year', 'location']), function(req, res, next){}) +* +* Use it globally +* app.use(parseParams('dims')) +*/ + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + + // This function returns the intersection of two arrays +function intersect(a, b) { + let t; + if (b.length > a.length) { + t = b; b = a; a = t; + } + return a.filter((e) => b.indexOf(e) !== -1); +} + +function parseParams(queryParam, arr) { + return (req, res, next) => { + req[queryParam] = {}; + if (req.query[queryParam]) { + const params = req.query[queryParam].split(','); + // Temporary object to hold the params and it's values + const obj = {}; + for (const param of params) { + // Get the key and the value - state:41 is key 'state' whith value 41 + const kv = param.split(':'); + // Check if there is a value. If there isn't, assign null + obj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; + } + + // If the array exists and is not empty we intersect + if (typeof arr !== 'undefined' && arr.length > 0) { + // Intersect the keys of the obj with the array arr. + // The intersection array is assigned with the keys + const intersection = intersect(arr, Object.keys(obj)); + // This is a bit tricky... + // For each key in the intersection array we get it's value in the obj + // and assign it to the custom attribute in the req obj. + // For example: instersection => ["state"] so + // obj[intersection[i]] (with i=0) is obj["state"], that is 41 + // and req[queryParam]["state"] = 41 + for (let i = 0; i < intersection.length; ++i) { + req[queryParam][intersection[i]] = obj[intersection[i]]; + } + req[queryParam].size = intersection.length; + } else { + req[queryParam] = obj; + req[queryParam].size = Object.keys(obj).length; + } + } + next(); + }; +} + +module.exports = parseParams; diff --git a/src/libs/middlewares/query.js b/src/libs/middlewares/query.js index 74a0c505cf02285dde3a35c0d0fc6365b8b4ce86..4020631fce90720e709e72beab844738b559bac9 100644 --- a/src/libs/middlewares/query.js +++ b/src/libs/middlewares/query.js @@ -9,8 +9,9 @@ const execQuery = require(`${libs}/db/query_exec`); * @param {Function} next [description] */ function query(req, res, next) { - log.debug(req.sql); - execQuery(req.sql.text, req.sql.values).then((result) => { + let sql = req.sql.toParam(); + log.debug(sql); + execQuery(sql.text, sql.values).then((result) => { log.debug(result); req.result = result; next(); diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index c3a870c691d39212cbefa5135bf83eea468b3385..3faaa926c7055bc9849d4457e183e8ea6fd48cf8 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -12,7 +12,7 @@ const city = require('./city'); const school = require('./school'); -api.get('/api/v1', (req, res) => { +api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); diff --git a/src/libs/routes/city.js b/src/libs/routes/city.js index bf60f0770c6cd672e7587d2dabaab6d3db21bb98..e7f362ff57483de9bf973afba1d9ce769b6e6dfb 100644 --- a/src/libs/routes/city.js +++ b/src/libs/routes/city.js @@ -11,25 +11,25 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); cityApp.get('/', (req, res, next) => { - req.sql = squel.select().from('municipios').toParam(); + req.sql.from('municipios'); next(); }, query, response('city')); cityApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('pk_municipio_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('municipios') + .where('pk_municipio_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); cityApp.get('/ibge/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('codigo_ibge = ?', - req.params.id).toParam(); + req.sql.from('municipios') + .where('codigo_ibge = ?', req.params.id); next(); }, query, response('city')); cityApp.get('/state/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('fk_estado_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('municipios') + .where('fk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index 557695b8f4b84a93597e414cb0163f08d1e6d426..1d50297eb2ff7de19f47af6c0ca38ea31db8e6c4 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -12,28 +12,10 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); -// **Temporary** solution to add where clauses that are common to all requests -function filter(req, q) { - if (typeof req.min_year !== 'undefined') { - q.where('ano_censo>=?', req.min_year); - } +const parseParams = require(`${libs}/middlewares/parseParams`); - if (typeof req.max_year !== 'undefined') { - q.where('ano_censo<=?', req.max_year); - } - - if (typeof req.adm_dependency_id !== 'undefined') { - q.where('fk_dependencia_adm_id=?', req.adm_dependency_id); - } - - if (typeof req.location_id !== 'undefined') { - q.where('id_localizacao=?', req.location_id); - } +// **Temporary** solution to add where clauses that are common to all requests - if (typeof req.education_level_id !== 'undefined') { - q.where('fk_etapa_ensino_id=?', req.education_level_id); - } -} /** * Complete range of the enrollments dataset @@ -45,7 +27,7 @@ enrollmentApp.get('/year_range', (req, res, next) => { .from('turmas') .field('MIN(turmas.ano_censo)', 'start_year') .field('MAX(turmas.ano_censo)', 'end_year') - .toParam(); + ; next(); }, query, response('range')); @@ -59,7 +41,7 @@ enrollmentApp.get('/education_level', (req, res, next) => { .from('etapa_ensino') .field('pk_etapa_ensino_id', 'id') .field('desc_etapa', 'name') - .toParam(); + ; next(); }, query, response('education_level')); @@ -73,170 +55,176 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => { .from('dependencia_adms') .field('pk_dependencia_adm_id', 'id') .field('nome', 'name') - .toParam(); + ; next(); }, query, response('adm_dependency')); enrollmentApp.get('/data', (req, res, next) => { - req.sql = squel.select().from('turmas').toParam(); + req.sql = squel.select().from('turmas'); next(); }, query, response('data')); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - req.paramCnt = 0; +// Parse the filters and dimensions parameter in the query +enrollmentApp.use('/', parseParams('filter', [ + 'min_year', + 'max_year', + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), parseParams('dims', [ + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), (req, res, next) => { + log.debug(req.filter); + log.debug(req.dims); - if (typeof params.id !== 'undefined') { - req.id = parseInt(params.id, 10); - req.paramCnt += 1; + // Do the joins + if(typeof req.filter.adm_dependency_id !== 'undefined' + || typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); } - if (typeof params.location_id !== 'undefined') { - req.location_id = parseInt(params.location_id, 10); - req.paramCnt += 1; + if(typeof req.filter.education_level_id !== 'undefined' + || typeof req.dims.education_level_id !== 'undefined') { + req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id'); } - if (typeof params.adm_dependency_id !== 'undefined') { - req.adm_dependency_id = parseInt(params.adm_dependency_id, 10); - req.paramCnt += 1; + if(typeof req.filter.region !== 'undefined' + || typeof req.dims.region !== 'undefined') { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id') + .join('regioes', null, 'estados.fk_regiao_id=regioes.pk_regiao_id'); } - if (typeof params.min_year !== 'undefined') { - req.min_year = parseInt(params.min_year, 10); - req.paramCnt += 1; + if((typeof req.filter.state !== 'undefined' + || typeof req.dims.state !== 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id'); } - if (typeof params.max_year !== 'undefined') { - req.max_year = parseInt(params.max_year, 10); - req.paramCnt += 1; + if((typeof req.filter.city !== 'undefined' + || typeof req.dims.city !== 'undefined') + && (typeof req.filter.state === 'undefined' + && typeof req.dims.state === 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id'); } - if (typeof params.education_level_id !== 'undefined') { - req.education_level_id = parseInt(params.education_level_id, 10); - req.paramCnt += 1; + if(typeof req.dims.school !== 'undefined') { + req.sql.join('escolas', null, 'fk_escola_id=escolas.pk_escola_id'); } - next(); -}); + // Dimensions (add fields) -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'region') { - log.debug('Using enrollments query for regions'); - const q = squel.select().from('mat_regioes') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.education_level_id !== 'undefined') { + req.sql.field('desc_etapa', 'education_level') + .group('desc_etapa') + .order('desc_etapa'); + } - filter(req, q); + if(typeof req.dims.region !== 'undefined') { + req.sql.field('regioes.nome', 'region_name') + .group('regioes.nome') + .order('regioes.nome'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_regiao_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.state !== 'undefined') { + req.sql.field('estados.nome', 'state_name') + .group('estados.nome') + .order('estados.nome'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'state') { - log.debug('Using enrollments query for states'); - const q = squel.select().from('mat_estados') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.city !== 'undefined') { + req.sql.field('municipios.nome', 'city_name') + .group('municipios.nome') + .order('municipios.nome'); + } - filter(req, q); + if(typeof req.dims.school !== 'undefined') { + req.sql.field('escolas.nome_entidade', 'school_name') + .group('escolas.nome_entidade') + .order('escolas.nome_entidade'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_estado_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.field('dependencia_adms.nome', 'adm_dependency_name') + .group('dependencia_adms.nome') + .order('dependencia_adms.nome'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'city') { - log.debug('Using enrollments query for cities'); - const q = squel.select().from('mat_municipios') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.location_id !== 'undefined') { + req.sql.field('turmas.id_localizacao', 'location') + .group('turmas.id_localizacao') + .order('turmas.id_localizacao'); + } - filter(req, q); + if(typeof req.dims.region === 'undefined' + && typeof req.dims.state === 'undefined' + && typeof req.dims.city === 'undefined') { + req.sql.field("'Brasil'", 'name'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_municipio_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + // Filter (add where) + + if (typeof req.filter.min_year !== 'undefined') { + req.sql.where('turmas.ano_censo>=?', parseInt(req.filter.min_year, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'school') { - log.debug('Using enrollments query for schools'); - const q = squel.select().from('mat_escolas') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.max_year !== 'undefined') { + req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); + } - filter(req, q); + if (typeof req.filter.adm_dependency_id !== 'undefined') { + req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency_id, 10)); + } - if (typeof req.id !== 'undefined') { - q.where('pk_escola_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.location_id !== 'undefined') { + req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location_id, 10)); + } + + if (typeof req.filter.education_level_id !== 'undefined') { + req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level_id, 10)); + } + + if (typeof req.filter.region !== 'undefined') { + req.sql.where('pk_regiao_id=?', parseInt(req.filter.region, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate === 'undefined') { - log.debug('Using enrollments query for the whole country'); - const q = squel.select().from('turmas').field("'Brasil'", 'name') - .field('COALESCE(SUM(num_matriculas),0)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.state !== 'undefined') { + req.sql.where('pk_estado_id=?', parseInt(req.filter.state, 10)); + } - filter(req, q); + if (typeof req.filter.city !== 'undefined') { + req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filter.city, 10)); + } - req.sql = q.group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.school !== 'undefined') { + req.sql.where('turmas.fk_escola_id=?', parseInt(req.filter.school, 10)); } + log.debug(req.sql.toParam()); next(); }); enrollmentApp.get('/', (req, res, next) => { - log.debug(`Request parameters: ${req}`); + req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total') + .field('turmas.ano_censo', 'year') + .from('turmas') + .group('turmas.ano_censo') + .order('turmas.ano_censo'); next(); -}, query, response('enrollments')); - -// enrollmentApp.get('/', (req, res, next) => { -// log.debug(`Request parameters: ${req}`); -// if (typeof req.sqlQuery === 'undefined') { -// // Should only happen if there is a bug in the chaining of the -// // '/enrollments' route, since when no +aggregate+ parameter is given, -// // it defaults to use the query for the whole country. -// log.error('BUG -- No SQL query was found to be executed!'); -// next('Internal error, request could not be satisfied at this moment. Please, ' -// + 'try again later'); -// } else { -// log.debug('SQL query: ${ req.sqlQuery }?'); -// log.debug(req.sqlQuery); -// dbQuery(req.sqlQuery).then((result) => { -// req.result = result; -// return response(req, res); -// }, (error) => { -// log.error(`[${req.originalUrl}] SQL query error: ${error}`); -// next('Internal error, request could not be satisfied at this moment. Please, ' -// + 'try again later'); -// }); -// } -// }); +}, query, response('test')); module.exports = enrollmentApp; diff --git a/src/libs/routes/region.js b/src/libs/routes/region.js index c0e58ca4663ae8c0c03ec22b7df049a8928ade28..5ce732e77b2e75486fc40805a6b7befa4a8984cc 100644 --- a/src/libs/routes/region.js +++ b/src/libs/routes/region.js @@ -11,13 +11,13 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); regionApp.get('/', (req, res, next) => { - req.sql = squel.select().from('regioes').toParam(); + req.sql.from('regioes'); next(); }, query, response('region')); regionApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('regioes').where('pk_regiao_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('regioes') + .where('pk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('region')); diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index dc6e8ed5ad51dcffc3156900848f4016937377d1..4e9bf8ad4e46899e8da18e99350c2d6e60ee373e 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -22,40 +22,35 @@ const response = require(`${libs}/middlewares/response`); // .field('nome_entidade', 'name') // .field('ano_censo', 'year') // .field('fk_cod_estado') -// .field('fk_cod_municipio') -// .toParam(); +// .field('fk_cod_municipio'); // next(); // }, query, response('school')); schoolApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('escolas').where('pk_escola_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('escolas') + .where('pk_escola_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); schoolApp.get('/state/:id', (req, res, next) => { - req.sql = squel.select().from('escolas') + req.sql.from('escolas') .field('pk_escola_id') .field('nome_entidade', 'name') .field('ano_censo', 'year') .field('fk_cod_estado') .field('fk_cod_municipio') - .where('fk_cod_estado = ?', - parseInt(req.params.id, 10)) - .toParam(); + .where('fk_cod_estado = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); schoolApp.get('/city/:id', (req, res, next) => { - req.sql = squel.select().from('escolas') + req.sql.from('escolas') .field('pk_escola_id') .field('nome_entidade', 'name') .field('ano_censo', 'year') .field('fk_cod_estado') .field('fk_cod_municipio') - .where('fk_cod_municipio = ?', - parseInt(req.params.id, 10)) - .toParam(); + .where('fk_cod_municipio = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); diff --git a/src/libs/routes/state.js b/src/libs/routes/state.js index 3569957688a7d3156730b6d61bb58b7d81a6f6ca..9cb5224cdea16be24b3ae0c7bfac0af05305c768 100644 --- a/src/libs/routes/state.js +++ b/src/libs/routes/state.js @@ -11,19 +11,19 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); stateApp.get('/', (req, res, next) => { - req.sql = squel.select().from('estados').toParam(); + req.sql.from('estados'); next(); }, query, response('state')); stateApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('estados').where('pk_estado_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('estados') + .where('pk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state')); stateApp.get('/region/:id', (req, res, next) => { - req.sql = squel.select().from('estados').where('fk_regiao_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('estados') + .where('fk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state'));