diff --git a/.gitignore b/.gitignore index 2fa21185e28b55b01382a70bdf4fbd61f652e8f0..e71efa9fd7b138a798949ce410d9601b04b5cfb8 100644 --- a/.gitignore +++ b/.gitignore @@ -30,5 +30,7 @@ DockerfileAntigo src/libs/db/postgres.js docker-compose.yml entrypoint.sh +gulpfile* gulpfile.template.js +gulpfile.babel.js diff --git a/gulpfile.babel.js b/gulpfile.babel.js deleted file mode 100644 index 7c8b741852d94433abf9034871069a8d0594e315..0000000000000000000000000000000000000000 --- a/gulpfile.babel.js +++ /dev/null @@ -1,117 +0,0 @@ -require('babel-core/register'); - -const gulp = require('gulp'); - -const babel = require('gulp-babel'); - -const eslint = require('gulp-eslint'); - -const mocha = require('gulp-mocha'); - -const istanbul = require('gulp-istanbul'); - -const nodemon = require('gulp-nodemon'); - -const Cache = require('gulp-file-cache'); - -const docco = require('gulp-docco'); - -const mkdirp = require('mkdirp'); - -const cache = new Cache(); - -function createLogDir() { - const logDirPath = 'build/logs'; - mkdirp(logDirPath, (err) => { - if(err) console.error(err); - }); -} - -gulp.task('lint', () => { - // run ESLint - gulp.src('src/**/*.js') - .pipe(eslint()) - .pipe(eslint.format()); -}) - -/** -* Compile source files -*/ -gulp.task('compile', ['lint'], () => { - - // compile source to ES5 - gulp.src('src/**/*.js') - .pipe(cache.filter()) // cache source files - .pipe(babel()) // compile only modified files - // .pipe(cache.cache()) // cache compiled files - .pipe(gulp.dest('build')); // move compiled files to build directory -}); - -gulp.task('build', ['compile'], () => { - var filesToCopy = [ 'config.json', 'package.json' ]; - // copy configuration file to build directory - gulp.src(filesToCopy) - .pipe(gulp.dest('build')); - - createLogDir(); -}); - -gulp.task('docco', () => { - gulp.src('./src/**/*.js') - .pipe(docco()) - .pipe(gulp.dest('./docs')); -}); - -gulp.task('doc', ['docco']); - -gulp.task('pre-test', () => { - return gulp.src(['build/**/*.js', '!build/{test,test/**}']) - .pipe(istanbul()) - .pipe(istanbul.hookRequire()); -}); - -gulp.task('test', ['pre-test'], () => { - process.chdir('build'); - gulp.src(['test/**/*.js'], {read: false}) - .pipe(mocha({timeout: 60000})) - .pipe(istanbul.writeReports()) - .pipe(istanbul.enforceThresholds({ - thresholds: { - global: { - statements: 80, - branches: 70, - lines: 80, - functions: 80 - } - } - })) - .on('error', () => { - process.exit(1); - }) - .on('end', () => { - process.exit(); - }); -}); - -gulp.task('watch', ['compile'], () => { - console.log('Watching source directory for changes'); - gulp.watch('src/**/*.js').on('change', () => { - console.log('Recompiling source'); - gulp.start('compile'); - console.log('Source recompilation done'); - }); -}); - -gulp.task('run', () => { - process.chdir('build'); - nodemon({ - script: 'server.js', - tasks: ['watch'], - ignore: ["test/test.js", "gulpfile.babel.js"], - ext: 'js html json', - env: { 'NODE_ENV': process.env.NODE_ENV } - }); -}); - -gulp.task('default', ['run']); - diff --git a/script_req.sh b/script_req.sh deleted file mode 100755 index 125204b86fb256e0608e25f161dccddb4cbc1ddc..0000000000000000000000000000000000000000 --- a/script_req.sh +++ /dev/null @@ -1,10 +0,0 @@ -curl -X 'POST' \ - 'http://10.254.221.20:3000/api/v1//auth/token' \ - -H 'accept: application/json' \ - -H 'Content-Type: application/json' \ - -d '{ - "email": "eduardomsouza@ufpr.br", - "password": "teste123", - "client_secret": "LDE", - "grant_type": "password" -}' \ No newline at end of file diff --git a/src/libs/convert/ageRange19to64.js b/src/libs/convert/ageRange19to64.js new file mode 100644 index 0000000000000000000000000000000000000000..470208675967b09f129effcdfe114f7386a02219 --- /dev/null +++ b/src/libs/convert/ageRange19to64.js @@ -0,0 +1,38 @@ +/* +Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +module.exports = function ageRange19to64(id) { + switch (id) { + case 6: + return '19 a 24 anos'; + case 7: + return '25 a 29 anos'; + case 8: + return '30 a 40 anos'; + case 9: + return '41 a 50 anos'; + case 10: + return '51 a 64 anos'; + case 11: + return 'Mais que 64 anos'; + default: + return 'Não declarada'; + } +}; diff --git a/src/libs/convert/ageRangeAll.js b/src/libs/convert/ageRangeAll.js index 1ad1c54ee82d26a9d93d9608b38e9cc624a82428..ebc2336ac35478cf950669b96ff2b3fcf35a5d7c 100644 --- a/src/libs/convert/ageRangeAll.js +++ b/src/libs/convert/ageRangeAll.js @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ -module.exports = function ageRange(id) { +module.exports = function ageRangeAll(id) { switch (id) { case 1: return '0 a 3 anos'; diff --git a/src/libs/convert/ageRangePopSchool.js b/src/libs/convert/ageRangePopSchool.js new file mode 100644 index 0000000000000000000000000000000000000000..dc5ef13e222a7efc006e397099fe435103f8cbb8 --- /dev/null +++ b/src/libs/convert/ageRangePopSchool.js @@ -0,0 +1,40 @@ +/* +Copyright (C) 2025 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +module.exports = function ageRangePopSchool(id) { + switch (id) { + case 1: + return '0 a 3 anos'; + case 2: + return '4 a 5 anos'; + case 3: + return '6 a 10 anos'; + case 4: + return '11 a 14 anos'; + case 5: + return '15 a 17 anos'; + case 6: + return '18 a 24 anos'; + case 7: + return 'Acima de 24 anos'; + default: + return 'Não declarada'; + } +}; diff --git a/src/libs/convert/educationLevelModAgg.js b/src/libs/convert/educationLevelModAgg.js index 6ebfa6274210a5f8956aebfe937aabae6faf07ea..c55a7e8fc3e2f9e2549fff244f61fc57a63ba143 100644 --- a/src/libs/convert/educationLevelModAgg.js +++ b/src/libs/convert/educationLevelModAgg.js @@ -40,8 +40,6 @@ module.exports = function educationLevelMod(id) { return 'EJA - EF e EM Integrado - tecnico'; case 10: return 'Educacao Profissional - concomitante e subsequente'; - case 11: - return 'suporte_total' default: return 'Não classificada'; } diff --git a/src/libs/convert/expenses.js b/src/libs/convert/expenses.js new file mode 100644 index 0000000000000000000000000000000000000000..874cd0f47eee6100283866c1fc559c9ad9cb9a6a --- /dev/null +++ b/src/libs/convert/expenses.js @@ -0,0 +1,42 @@ +/* +Copyright (C) 2024 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +module.exports = function regionCode(id) { + switch (id) { + case 1: + return "Ano"; + case 2: + return "Entidade"; + case 3: + return 'Despesas'; + case 4: + return 'Matrículas Públicas'; + case 5: + return 'Matrículas Públicas Mais Conveniada'; + case 6: + return 'Despesas Aluno Ano Pública' + case 7: + return 'Despesas Aluno Mês Pública'; + case 8: + return 'Despesas Aluno Ano Pública Mais Conveniada'; + case 9: + return 'Despesas Aluno Mês Pública Mais Conveniada'; + } +}; diff --git a/src/libs/convert/potentialRevenue.js b/src/libs/convert/potentialRevenue.js new file mode 100644 index 0000000000000000000000000000000000000000..ae7855bdadd3c63b6996bbfa96d50eac34357152 --- /dev/null +++ b/src/libs/convert/potentialRevenue.js @@ -0,0 +1,34 @@ +/* +Copyright (C) 2024 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +module.exports = function regionCode(id) { + switch (id) { + case 1: + return "Ano"; + case 2: + return "Nome Ente"; + case 3: + return 'Receitas'; + case 4: + return 'Matrículas Públicas'; + case 5: + return 'Matrículas Públicas Mais Conveniada'; + } +}; diff --git a/src/libs/convert/regionCode.js b/src/libs/convert/regionCode.js index fd21550366426a1a3ef8d1def4c6520a870a9ec6..e2f2e729e95fdf16e17e661f20cdab7de96513ae 100644 --- a/src/libs/convert/regionCode.js +++ b/src/libs/convert/regionCode.js @@ -30,5 +30,7 @@ module.exports = function regionCode(id) { return 'Sul' case 5: return 'Centro-Oeste'; + case 6: + return 'Exterior/Curso à distância'; } }; diff --git a/src/libs/convert/revenue.js b/src/libs/convert/revenue.js index 18cfeb8c0e2cf05ba10a15e4e4801f0327562ddc..fb3c819ac639bf49ec12abcd4c0af2049e705e21 100644 --- a/src/libs/convert/revenue.js +++ b/src/libs/convert/revenue.js @@ -21,22 +21,12 @@ along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. module.exports = function regionCode(id) { switch (id) { case 1: - return "Ano"; + return 'Rede pública (anual)' case 2: - return "Nome Ente" + return 'Rede pública (mensal)'; case 3: - return 'Receitas'; + return 'Rede pública e conveniada (anual)'; case 4: - return 'Matrículas Públicas'; - case 5: - return 'Matrículas Públicas Mais Conveniada'; - case 6: - return 'Receita Aluno Ano Pública' - case 7: - return 'Receita Aluno Mês Pública'; - case 8: - return 'Receita Aluno Ano Pública Mais Conveniada'; - case 9: - return 'Receita Aluno Mês Pública Mais Conveniada'; + return 'Rede pública e conveniada (mensal)'; } }; diff --git a/src/libs/convert/stateName.js b/src/libs/convert/stateName.js index 9254af006be3f66badfd578c98fc17a2c120c927..0c37b0d4a2f94cdc8eae04c0d108da75e7e14f9d 100644 --- a/src/libs/convert/stateName.js +++ b/src/libs/convert/stateName.js @@ -75,7 +75,7 @@ module.exports = function stateName(id) { case 53: return 'Distrito Federal'; case 99: - return 'Exterior'; + return 'Exterior/Curso à distância'; default: return 'Não declarada'; } diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js index ab7dfdd957843df41e5fffabbad43b579d36fe51..8dbf01d7bbae35fbd18821f850c26ba17c93cbd7 100644 --- a/src/libs/middlewares/aggregateData.js +++ b/src/libs/middlewares/aggregateData.js @@ -36,8 +36,10 @@ const convert = { total_doc: "totalDoc", education_degree_entity: "educationDegreeEntity", government_agreement: "governmentAgreement", + despesas_alunos: "expenses", + receita_potencial: "potentialRevenue", receitas: "revenue", - despesas: "studentCost" + despesas: "revenue" } function aggregateData(req, res, next) { @@ -65,10 +67,12 @@ function aggregateData(req, res, next) { 'post_graduation_entity', 'contract_type_entity', 'education_degree_entity', + 'despesas_alunos', 'receitas', + 'receita_potencial', 'despesas' ]; - const baseFields = ["school"] + const baseFields = ["school", "city"] let id; const fields = req.query.dims.split(','); let currentAggregateField; diff --git a/src/libs/middlewares/expensesParser.js b/src/libs/middlewares/expensesParser.js new file mode 100644 index 0000000000000000000000000000000000000000..5ba0c8eb2e9e9bbe9966d0aabb1af7c3c187088c --- /dev/null +++ b/src/libs/middlewares/expensesParser.js @@ -0,0 +1,21 @@ +/* +Middleware que remove o campo "region" quando o campo "city" está ativo nos filtros. +Devido à limitações na tabela de dados, quando ambos campos estão ativos o filtro por +"região" funciona incorretamente. +*/ +module.exports = function expensesParser(req, res, next) { + if (req.query && req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + const obj = JSON.parse(jsonString); + + if ("city" in obj && "region" in obj) { + delete obj.region; + const customString = Object.entries(obj) + .map(([key, value]) => `${key}:"${value}"`) + .join(','); + req.query.filter = customString; + } + } + + next(); +}; diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js index 730d620e18c1bc1708af165eebd5de1b3a54c931..f7ccbcc66c9d6fded2cf82d72f172b4d2236946a 100644 --- a/src/libs/middlewares/id2str.js +++ b/src/libs/middlewares/id2str.js @@ -18,6 +18,8 @@ You should have received a copy of the GNU General Public License along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ +const ageRangePopSchool = require("../convert/ageRangePopSchool"); + const libs = `${process.cwd()}/libs`; const gender = require(`${libs}/convert/gender`); const period = require(`${libs}/convert/period`); @@ -126,8 +128,12 @@ const ageRangeEntity = require(`${libs}/convert/ageRangeEntity`); const postGraduationEntity = require(`${libs}/convert/postGraduationEntity`); const totalDoc = require(`${libs}/convert/totalDoc`); const educationDegreeEntity = require(`${libs}/convert/educationDegreeEntity`); +const expenses = require(`${libs}/convert/expenses.js`) +const potentialRevenue = require(`${libs}/convert/potentialRevenue.js`) const revenue = require(`${libs}/convert/revenue`); const studentCost = require(`${libs}/convert/studentCost`); +const ageRangeOutSchool = require(`${libs}/convert/ageRangePopSchool`); +const ageRange19to64 = require(`${libs}/convert/ageRange19to64`); const ids = { gender_id: gender, @@ -247,8 +253,12 @@ const ids = { special_education_entity: specialEducationEntity, total_doc: totalDoc, education_degree_entity: educationDegreeEntity, + despesas_id: expenses, + receita_potencial_id: potentialRevenue, receitas_id: revenue, - student_cost: studentCost + student_cost: studentCost, + age_range_pop_school_id: ageRangePopSchool, + age_range_19_to_64_id: ageRange19to64 }; function transform(removeId=false) { @@ -258,7 +268,7 @@ function transform(removeId=false) { } // Para cada objeto do resultado req.result.forEach((obj) => { - console.log(obj), + //console.log(obj), Object.keys(obj).forEach((key) => { // Se não há uma função especificada, retorna if(typeof ids[key] === 'undefined') return; @@ -391,6 +401,10 @@ module.exports = { specialEducationEntity, totalDoc, educationDegreeEntity, + expenses, + potentialRevenue, revenue, - studentCost + studentCost, + ageRangePopSchool, + ageRange19to64 }; diff --git a/src/libs/middlewares/potentialRevenueParser.js b/src/libs/middlewares/potentialRevenueParser.js new file mode 100644 index 0000000000000000000000000000000000000000..23ceb6529632b2f1abe21f4770d04716ec8f9d19 --- /dev/null +++ b/src/libs/middlewares/potentialRevenueParser.js @@ -0,0 +1,21 @@ +/* +Middleware que remove o campo "region" quando o campo "city" está ativo nos filtros. +Devido à limitações na tabela de dados, quando ambos campos estão ativos o filtro por +"região" funciona incorretamente. +*/ +module.exports = function potentialRevenueParser(req, res, next) { + if (req.query && req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + const obj = JSON.parse(jsonString); + + if ("city" in obj && "region" in obj) { + delete obj.region; + const customString = Object.entries(obj) + .map(([key, value]) => `${key}:"${value}"`) + .join(','); + req.query.filter = customString; + } + } + + next(); +}; diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js index d11f98f5c7f02a508e8bf51b40c9d4554019a905..876dfb753dd59c0977aafdb5d49b352746dfe34f 100644 --- a/src/libs/middlewares/studentRevenueParser.js +++ b/src/libs/middlewares/studentRevenueParser.js @@ -19,4 +19,4 @@ module.exports = function studentRevenueParser(req, res, next) { } next(); -}; +}; \ No newline at end of file diff --git a/src/libs/routes_v1/activity.js b/src/libs/routes_v1/activity.js index cb35c2de1e741d37ec860ecb142f8d499f1ab7c5..1cc63016c61d99caa6e262eefc1f9aa8423e365d 100644 --- a/src/libs/routes_v1/activity.js +++ b/src/libs/routes_v1/activity.js @@ -199,7 +199,6 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut } }); } - console.log("TEste"); act.type = req.body.tipo || act.type; act.title = req.body.titulo || act.title; act.subtitle = req.body.subtitulo || act.subtitle; diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js index 5b79740d1cee31efb2b14da8ca2977ca64e44dae..09ce8efb46d6503ef23f195b74eff8a48916525d 100644 --- a/src/libs/routes_v1/adjustedLiquidFrequency.js +++ b/src/libs/routes_v1/adjustedLiquidFrequency.js @@ -62,6 +62,34 @@ adjustedLiquidFrequency.get('/income_range', (req, res, next) => { next(); }, response('income_range')); +adjustedLiquidFrequency.get('/cap_code', (req, res, next) => { + req.result = [] + for (let i = 11; i < 54; i++) { + if (id2str.capitalCode(i) !== 'Não informado') { + req.result.push({ + id: i, name: id2str.capitalCode(i) + }); + } + } + req.result.push({id: 99, name: id2str.capitalCode(99)}); + + next(); +}, response('cap_code')); + +adjustedLiquidFrequency.get('/metro_code', (req, res, next) => { + req.result = [] + for (let i = 13; i < 53; i++) { + if (id2str.metroCode(i) !== 'Não informado') { + req.result.push({ + id: i, name: id2str.metroCode(i) + }); + } + } + req.result.push({id: 99, name: id2str.metroCode(99)}); + + next(); +}, response('metro_code')); + adjustedLiquidFrequency.get('/gender', (req, res, next) => { req.result = [] for (let i = 1; i < 3; i++) { @@ -152,6 +180,16 @@ rqf.addField({ foreign: 'cod_uf', foreignTable: 'pnad_novo' } +}).addValue({ + name: 'cap_code', + table: 'pnad_novo', + tableField: 'cod_cap', + resultField: 'cap_code_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_cap' + } }).addValue({ name: 'bolsa_familia', table: 'pnad_novo', @@ -175,12 +213,12 @@ rqf.addField({ }).addValue({ name: 'income_range', table: 'pnad_novo', - tableField: 'faixa_rendimento_aux_tx', + tableField: 'faixa_rendimento_aux', resultField: 'income_range_id', where: { relation: '=', type: 'integer', - field: 'faixa_rendimento_aux_tx' + field: 'faixa_rendimento_aux' } }).addValue({ name: 'age_range_all', @@ -202,6 +240,16 @@ rqf.addField({ type: 'integer', field: 'sexo' } +}).addValue({ + name: 'metro_code', + table: 'pnad_novo', + tableField: 'cod_rm_ride', + resultField: 'metro_code_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_rm_ride' + } }).addValue({ name: 'region', table: 'pnad_novo', @@ -293,7 +341,12 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { ${dimensions.gender ? " AND total_pop.gender_id = pnad_novo.sexo" : ""} ${dimensions.bolsa_familia ? "AND total_pop.bolsa_familia_id = pnad_novo.recebeu_rendimentos_de_programa_bolsa_familia" : ""} ${dimensions.new_pnad_ethnic_group ? "AND total_pop.new_pnad_ethnic_group_id = pnad_novo.cor_raca" : ""} - ${dimensions.income_range ? "AND total_pop.income_range_id = pnad_novo.faixa_rendimento_aux_tx" : ""} + ${dimensions.income_range ? "AND total_pop.income_range_id = pnad_novo.faixa_rendimento_aux" : ""} + ${dimensions.cap_code ? "AND total_pop.cap_code_id = pnad_novo.cod_cap" : ""} + ${dimensions.metro_code ? "AND pnad_novo.cod_rm_ride <> 99 AND total_pop.metro_code_id = pnad_novo.cod_rm_ride" : ""} + ${dimensions.region ? "AND total_pop.region_id = pnad_novo.cod_regiao" : ""} + ${dimensions.state ? "AND total_pop.state_id = pnad_novo.cod_uf" : ""} + ` // Subquery: total_apoio_freq @@ -337,6 +390,18 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { if (dimensions.income_range) mainQuery.field("total_apoio_freq.income_range_id") + if (dimensions.cap_code) + mainQuery.field("total_apoio_freq.cap_code_id") + + if (dimensions.metro_code) + mainQuery.field("total_apoio_freq.metro_code_id") + + if (dimensions.region) + mainQuery.field("total_apoio_freq.region_id") + + if (dimensions.state) + mainQuery.field("total_apoio_freq.state_id") + req.sql = mainQuery; next(); diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js index 5331b389e96c49e06b8ee0df8b8868be68f2422a..d49ecac0f15898ed35e7c4ab309d478840023489 100644 --- a/src/libs/routes_v1/api.js +++ b/src/libs/routes_v1/api.js @@ -155,7 +155,15 @@ const federativeEntity = require(`${libs}/routes_v1/federativeEntity`); const email = require(`${libs}/routes_v1/email`); -const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`) +const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`); + +const expenses = require(`${libs}/routes_v1/expenses`); + +const expensesParser = require(`${libs}/middlewares/expensesParser`); + +const potentialRevenue = require(`${libs}/routes_v1/potentialRevenue`); + +const potentialRevenueParser = require(`${libs}/middlewares/potentialRevenueParser`); const yearsOfStudy = require(`${libs}/routes_v1/yearsOfStudy`) @@ -174,10 +182,13 @@ const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequen const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`); const studentRevenue = require(`${libs}/routes_v1/studentRevenue`); + const studentRevenueParser = require(`${libs}/middlewares/studentRevenueParser`); const studentCost = require(`${libs}/routes_v1/studentCost`); +const populationOutOfSchool = require(`${libs}/routes_v1/populationOutOfSchool`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API v1 is running' }); }); @@ -246,6 +257,8 @@ api.use('/employeesAggregate', employeesAggregate); api.use('/course_aggregate', courseAggregate); api.use('/federativeEntity', federativeEntity); api.use('/uni_offer_aggregate', uniLocalOfferAggregate); +api.use('/expenses', expensesParser, expenses); +api.use('/potential_revenue', potentialRevenueParser, potentialRevenue); api.use('/years_of_study', yearsOfStudy) api.use('/students_receiving_aid_tax', studentsReceivingAidTax); api.use('/superior_education_conclusion_tax', superiorEducationConclusionTax) @@ -254,6 +267,7 @@ api.use('/adjusted_liquid_frequency', adjustedLiquidFrequency); api.use('/iliteracy_rate', iliteracyRate); api.use('/student_revenue', studentRevenueParser, studentRevenue); api.use('/student_cost', studentRevenueParser, studentCost); +api.use('/pop_out_school', populationOutOfSchool); //Publication api.use('/publication', publication); diff --git a/src/libs/routes_v1/basicEducationConclusion.js b/src/libs/routes_v1/basicEducationConclusion.js index fb7452990b4a1be888247802cea0e9e424af606c..3b22f4d3a96eabb0fcd2987a93be342e14854373 100644 --- a/src/libs/routes_v1/basicEducationConclusion.js +++ b/src/libs/routes_v1/basicEducationConclusion.js @@ -36,9 +36,9 @@ const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); const id2str = require(`${libs}/middlewares/id2str`); -const config = require(`${libs}/config`); +const config = require(`${libs}/config`); -const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; +const cache = require('apicache').options({ debug: config.debug, statusCodes: { include: [200] } }).middleware; let rqf = new ReqQueryFields(); @@ -46,8 +46,8 @@ basicEducationConclusion.use(cache('15 day')); basicEducationConclusion.get('/years', (req, res, next) => { req.sql.from('pnad_novo') - .field('DISTINCT pnad_novo.ano_ref', 'year') - .where('pnad_novo.ano_ref >= 2019') + .field('DISTINCT pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019') next(); }, query, response('years')); @@ -60,7 +60,7 @@ basicEducationConclusion.get('/illiteracy', (req, res, next) => { }); } - req.result.push({id: 9, name: id2str.illiteracy(9)}); + req.result.push({ id: 9, name: id2str.illiteracy(9) }); next(); }, response('illiteracy')); @@ -73,7 +73,7 @@ basicEducationConclusion.get('/years_of_study', (req, res, next) => { }); } - req.result.push({id: 99, name: id2str.yearsOfStudy(99)}); + req.result.push({ id: 99, name: id2str.yearsOfStudy(99) }); next(); }, response('years_of_study')); @@ -85,7 +85,7 @@ basicEducationConclusion.get('/instruction_level', (req, res, next) => { id: i, name: id2str.instructionLevel(i) }); } - req.result.push({id: 99, name: id2str.instructionLevel(99)}); + req.result.push({ id: 99, name: id2str.instructionLevel(99) }); next(); }, response('instruction_level')); @@ -96,7 +96,7 @@ basicEducationConclusion.get('/new_pnad_adm_dependency', (req, res, next) => { id: i, name: id2str.newPnadAdmDependency(i) }); } - req.result.push({id: 99, name: id2str.newPnadAdmDependency(99)}); + req.result.push({ id: 99, name: id2str.newPnadAdmDependency(99) }); next(); }, response('new_pnad_adm_dependency')); @@ -115,12 +115,11 @@ basicEducationConclusion.get('/cap_code', (req, res, next) => { req.result = [] for (let i = 11; i < 54; i++) { if (id2str.capitalCode(i) !== 'Não informado') { - req.result.push({ - id: i, name: id2str.capitalCode(i) - }); + req.result.push({ + id: i, name: id2str.capitalCode(i) + }); } } - req.result.push({id: 99, name: id2str.capitalCode(99)}); next(); }, response('cap_code')); @@ -129,12 +128,12 @@ basicEducationConclusion.get('/metro_code', (req, res, next) => { req.result = [] for (let i = 13; i < 53; i++) { if (id2str.metroCode(i) !== 'Não informado') { - req.result.push({ - id: i, name: id2str.metroCode(i) - }); + req.result.push({ + id: i, name: id2str.metroCode(i) + }); } } - req.result.push({id: 99, name: id2str.metroCode(99)}); + req.result.push({ id: 99, name: id2str.metroCode(99) }); next(); }, response('metro_code')); @@ -148,7 +147,7 @@ basicEducationConclusion.get('/attended_modality', (req, res, next) => { } // Remove the option with id equals 10 => This option exists in the database, a better solution to this would be remove the option from the database req.result.splice(req.result.findIndex((item) => item.id === 10), 1); - req.result.push({id: 99, name: id2str.attendedModality(99)}); + req.result.push({ id: 99, name: id2str.attendedModality(99) }); next(); }, response('attended_modality')); @@ -159,7 +158,7 @@ basicEducationConclusion.get('/income_range', (req, res, next) => { id: i, name: id2str.incomeRange(i) }); } - req.result.push({id: 10, name: id2str.incomeRange(10)}); + req.result.push({ id: 9, name: id2str.incomeRange(9) }); next(); }, response('income_range')); @@ -190,7 +189,7 @@ basicEducationConclusion.get('/new_pnad_ethnic_group', (req, res, next) => { id: i, name: id2str.ethnicGroupNewPnad(i) }); } - req.result.push({id: 9, name: id2str.ethnicGroupNewPnad(9)}); + req.result.push({ id: 9, name: id2str.ethnicGroupNewPnad(9) }); next(); }, response('new_pnad_ethnic_group')); @@ -201,7 +200,7 @@ basicEducationConclusion.get('/bolsa_familia', (req, res, next) => { id: i, name: id2str.attendsSchool(i) }); } - req.result.push({id: 9, name: id2str.attendsSchool(9)}); + req.result.push({ id: 9, name: id2str.attendsSchool(9) }); next(); }, response('bolsa_familia')); @@ -212,7 +211,7 @@ basicEducationConclusion.get('/modality', (req, res, next) => { id: i, name: id2str.modality(i) }); } - req.result.push({id: 99, name: id2str.modality(99)}); + req.result.push({ id: 99, name: id2str.modality(99) }); next(); }, response('modality')); @@ -223,8 +222,8 @@ basicEducationConclusion.get('/modality_shift', (req, res, next) => { id: i, name: id2str.modalityShift(i) }); } - req.result.push({id: 9, name: id2str.modalityShift(9)}); - req.result.push({id: 99, name: id2str.modalityShift(99)}); + req.result.push({ id: 9, name: id2str.modalityShift(9) }); + req.result.push({ id: 99, name: id2str.modalityShift(99) }); next(); }, response('modality_shift')); @@ -237,21 +236,21 @@ basicEducationConclusion.get('/state', (req, res, next) => { }); } } - req.result.push({id: 99, name: id2str.stateName(99)}); + req.result.push({ id: 99, name: id2str.stateName(99) }); next(); }, response('state')); -basicEducationConclusion.get('/age_range_all', (req, res, next) => { +basicEducationConclusion.get('/age_range_19_to_64', (req, res, next) => { req.result = [] - for (let i = 1; i < 12; i++) { + for (let i = 6; i <= 11; i++) { req.result.push({ - id: i, name: id2str.ageRangeAll(i) + id: i, name: id2str.ageRange19to64(i) }); } next(); -}, response('age_range_all')); +}, response('age_range_19_to_64')); basicEducationConclusion.get('/location', (req, res, next) => { req.result = [] @@ -414,10 +413,10 @@ rqf.addField({ field: 'cor_raca' } }).addValue({ - name: 'age_range_all', + name: 'age_range_19_to_64', table: 'pnad_novo', tableField: 'faixa_etaria', - resultField: 'age_range_all_id', + resultField: 'age_range_19_to_64_id', where: { relation: '=', type: 'integer', @@ -426,12 +425,12 @@ rqf.addField({ }).addValue({ name: 'income_range', table: 'pnad_novo', - tableField: 'faixa_rendimento_aux_tx', + tableField: 'faixa_rendimento_aux', resultField: 'income_range_id', where: { relation: '=', type: 'integer', - field: 'faixa_rendimento_aux_tx' + field: 'faixa_rendimento_aux' } }).addValue({ name: 'gender', @@ -545,26 +544,26 @@ function matchQueries(queryPartial, queryTotal) { newObj[key] = result[key]; }); let index = keys.indexOf('total'); - if(index > -1) keys.splice(index, 1); + if (index > -1) keys.splice(index, 1); let objMatch = null; - for(let i = 0; i < queryPartial.length; ++i) { + for (let i = 0; i < queryPartial.length; ++i) { let partial = queryPartial[i]; let foundMatch = true; - for(let j = 0; j < keys.length; ++j) { + for (let j = 0; j < keys.length; ++j) { let key = keys[j]; - if(partial[key] !== result[key]) { + if (partial[key] !== result[key]) { foundMatch = false; break; } } - if(foundMatch) { + if (foundMatch) { objMatch = partial; break; } } - if(objMatch) { + if (objMatch) { newObj.denominator = result.total; newObj.partial = objMatch.total; newObj.total = (objMatch.total / result.total) * 100; @@ -577,10 +576,10 @@ function matchQueries(queryPartial, queryTotal) { basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.querySet = []; - + // Subquery para total_pop_maior_19 com filtros dinâmicos let totalPopMaior19 = req.sql.clone(); - totalPopMaior19.from("pnad_novo") + totalPopMaior19.from("pnad_novo") .field("ano_ref", "year") .field("SUM(peso_domicilio_pessoas_com_cal)", "total") .where("idade_morador_31_03 >= 19") @@ -590,7 +589,7 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) => // Subquery para total_pop_ed_bas_maior_19 com filtros dinâmicos let totalPopEdBasMaior19 = req.sql.clone(); - totalPopEdBasMaior19.from("pnad_novo") + totalPopEdBasMaior19.from("pnad_novo") .field("ano_ref", "year") .field("SUM(peso_domicilio_pessoas_com_cal)", "total") .where("idade_morador_31_03 >= 19") @@ -599,9 +598,8 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) => .group("ano_ref") .order("ano_ref"); - if (req.query.dims && req.query.dims.includes("income_range")) { - totalPopMaior19.where("faixa_rendimento_aux_tx is not null"); - totalPopEdBasMaior19.where("faixa_rendimento_aux_tx is not null"); + if (req.query.dims && req.query.dims.includes("cap_code")) { + totalPopMaior19.where("cod_cap < 99"); } req.querySet.push(totalPopEdBasMaior19); @@ -609,10 +607,10 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) => next(); }, multiQuery, (req, res, next) => { - // The multiple requests are made. Then we need to calculate the percetange. So the function - // below is used - let newObj = matchQueries(req.result[0], req.result[1]); - req.result = newObj; + // The multiple requests are made. Then we need to calculate the percetange. So the function + // below is used + let newObj = matchQueries(req.result[0], req.result[1]); + req.result = newObj; next(); }, id2str.transform(false), response('basic_education_conclusion')); diff --git a/src/libs/routes_v1/courseAggregate.js b/src/libs/routes_v1/courseAggregate.js index 78ae89a71035adf992cdc43f1e8253fc5dbef575..cf9a52f257d5c2cab340003438ab74240ccf9ccd 100644 --- a/src/libs/routes_v1/courseAggregate.js +++ b/src/libs/routes_v1/courseAggregate.js @@ -316,7 +316,7 @@ rqf.addField({ field: 'cod_cine_area_detalhada' } }).addValue({ - name: 'uni_offer_aggregate', + name: 'university', table: 'ies_ens_superior', tableField: ['cod_ies', 'nome_ies'], resultField: ['university_id', 'university_name'], @@ -405,7 +405,6 @@ CourseAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .where('curso_superior_agregado.tp_nivel_acad = 1 AND qtd_cursos = 1') .group('curso_superior_agregado.ano_censo') .order('curso_superior_agregado.ano_censo') - console.log(req.sql.toString()) next(); }, query, id2str.transform(false), response('course_aggregate')); diff --git a/src/libs/routes_v1/enrollment.js b/src/libs/routes_v1/enrollment.js index 872593852ad783b18d3491405184d63132558872..d7c8ef997105df8e45238b85df649d9a0fac61fa 100644 --- a/src/libs/routes_v1/enrollment.js +++ b/src/libs/routes_v1/enrollment.js @@ -454,7 +454,7 @@ rqf.addField({ }, 'filter').addValueToField({ name: 'school', table: 'escola', - tableField: ['nome_escola', 'id'], + tableField: ['nome_escola_atualizado', 'id'], resultField: ['school_name', 'school_id'], where: { relation: '=', @@ -494,7 +494,7 @@ rqf.addField({ }, 'dims').addValueToField({ name: 'school', table: 'escola', - tableField: 'nome_escola', + tableField: 'nome_escola_atualizado', resultField: 'school_name', where: { relation: '=', diff --git a/src/libs/routes_v1/enrollmentAggregate.js b/src/libs/routes_v1/enrollmentAggregate.js index 7e8cd3faece61c7999d16db913c50938de4e51fb..6acfd4478b17f9896644aa8a8480f909f1fdb70b 100644 --- a/src/libs/routes_v1/enrollmentAggregate.js +++ b/src/libs/routes_v1/enrollmentAggregate.js @@ -19,6 +19,7 @@ along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ const express = require('express'); + const { result } = require('lodash'); const enrollmentAggregateApp = express.Router(); @@ -392,7 +393,7 @@ rqf.addField({ foreign: 'estado_id', foreignTable: 'escola' } -}, 'filter').addValueToField({ +}, 'filter').addValue({ name: 'city', table: 'municipio', tableField: ['nome', 'id'], @@ -400,29 +401,15 @@ rqf.addField({ where: { relation: '=', type: 'integer', - field: 'id' + field: 'municipio_id', + table: 'escola' }, join: { primary: 'id', foreign: 'municipio_id', foreignTable: 'escola' } -}, 'dims').addValueToField({ - name: 'city', - table: 'municipio', - tableField: 'nome', - resultField: 'city_name', - where: { - relation: '=', - type: 'integer', - field: 'id' - }, - join: { - primary: 'id', - foreign: 'municipio_id', - foreignTable: 'escola' - } -}, 'filter').addValueToField({ +}).addValueToField({ name: 'school', table: 'escola', tableField: ['nome_escola', 'id'], @@ -512,8 +499,6 @@ rqf.addField({ // Return all cities enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { - console.log(req.query); - if (req.query.dims && req.query.dims.includes('gender')) { req.sql.from('escola') @@ -562,7 +547,7 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('SUM(escola.qt_mat_eja_med_agg)', 'total_eja_med') .field('SUM(escola.qt_mat_eja_fund_fic + escola.qt_mat_eja_med_fic + escola.qt_mat_eja_med_tec)', 'total_tec') .field('SUM(escola.qt_mat_prof_agg)', 'total_prof') - .field('SUM(escola.qt_mat_bas)', 'total_total') + //.field('SUM(escola.qt_mat_bas)', 'total_total') .field('escola.ano_censo', 'year') .group('escola.ano_censo') .order('escola.ano_censo') diff --git a/src/libs/routes_v1/expenses.js b/src/libs/routes_v1/expenses.js new file mode 100644 index 0000000000000000000000000000000000000000..a761b10870886e48a2239768bfca786b11fd670c --- /dev/null +++ b/src/libs/routes_v1/expenses.js @@ -0,0 +1,174 @@ +/* +Copyright (C) 2024 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +const express = require('express'); + +const ExpensesApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const config = require(`${libs}/config`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +const aggregateData = require(`${libs}/middlewares/aggregateData`); + +let rqf = new ReqQueryFields(); + +ExpensesApp.use(cache('15 day')); + +ExpensesApp.get('/years', (req, res, next) => { + req.sql.from('despesas') + .field('DISTINCT despesas.ano_censo', 'year') + .where('despesas.ano_censo is not null') + next(); +}, query, response('years')) + +ExpensesApp.get('/type', (req, res, next) => { + req.result = [] + + req.result.push({ id: 1, name: "Rede Estadual" }); + req.result.push({ id: 2, name: "Rede Municipal" }); + next(); +}, response('type')); + +// rqf.addValue({ +// name: 'location', +// table: 'despesas', +// tableField: 'nome_ente', +// resultField: 'location_id', +// where: { +// relation: '=', +// type: 'integer', +// field: 'localizacao_id' +// } +// }); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'min_year', + table: 'despesas', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'despesas', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'region', + table: 'estado', + tableField: ['nome_ente', 'cod_ibge', 'id', 'regiao_id'], + resultField: ['state_name', 'state_cod_ibge', 'state_id', 'region_id'], + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + }, + join: { + primary: 'id', + foreign: 'cod_ibge', + foreignTable: 'despesas' + } +}) + + +ExpensesApp.get( + '/', + rqf.parse(), + rqf.build(), + (req, res, next) => { + let obj = {}; + let typeFilter = false; + // condição inicial padrão (sem filtro de região, coloca cod_ibge = 0 só para não quebrar a query) + let whereCondition = req.query.filter.includes('region') + ? '' + : 'despesas.cod_ibge = 0'; + let filterId; + const filters = req.query.filter.split(','); + + // Se houver filtro de estado/cidade, ajusta o cod_ibge + filters.forEach((filter) => { + if (filter.includes('state') || filter.includes('city')) { + filterId = Number(filter.split(':')[1].replace(/"/g, '')); + whereCondition = `despesas.cod_ibge = ${filterId}`; + } + }); + + // Interpreta o JSON de filtros; se tiver “type”, ajusta o where para agregar por tipo + if (req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + obj = JSON.parse(jsonString); + + if (obj.type) { + // Se vier só “1” (por exemplo, Unidades Federativas) + if (obj.type.includes('1') && !obj.type.includes('2')) { + whereCondition = 'despesas.cod_ibge >= 11 AND despesas.cod_ibge <= 53'; + } + // Se vier só “2” (Municípios) + else if (obj.type.includes('2') && !obj.type.includes('1')) { + whereCondition = 'despesas.cod_ibge > 53'; + } + typeFilter = true; + } + } + + req.sql + .from('despesas') + .field('despesas.ano_censo', 'year') + .field('SUM(ROUND(despesas.despesas, 2))', 'total') + .where(whereCondition) + .group('despesas.ano_censo') + .order('despesas.ano_censo'); + + next(); + }, + query, + id2str.transform(false), + response('expenses') +); +module.exports = ExpensesApp; \ No newline at end of file diff --git a/src/libs/routes_v1/instructionLevel.js b/src/libs/routes_v1/instructionLevel.js index d3cb7d0c0b8cdf1a766a34326dc6abda81723628..aa08338053a2a0119347770f2b032ede763cf46f 100644 --- a/src/libs/routes_v1/instructionLevel.js +++ b/src/libs/routes_v1/instructionLevel.js @@ -291,7 +291,6 @@ NivelInstrucao.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('nivel_de_instrucao', 'nivel') .field('round(sum(peso_domicilio_pessoas_com_cal), 0)', 'total') .field('ano_ref', 'year') - .where('nivel_de_instrucao <> 99') .where('faixa_etaria <> 99') .group('pnad_novo.ano_ref') .group('pnad_novo.nivel_de_instrucao') diff --git a/src/libs/routes_v1/newPnad.js b/src/libs/routes_v1/newPnad.js index 5bfb7b8a23e5badace6614815f84ba4ae40515b3..e9549404d6f369410b8e9d3c7558bcaa91a8e798 100644 --- a/src/libs/routes_v1/newPnad.js +++ b/src/libs/routes_v1/newPnad.js @@ -518,7 +518,6 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('pnad_novo.ano_ref', 'year') .group('pnad_novo.ano_ref') .order('pnad_novo.ano_ref') - console.log(req.sql.toString()) next(); }, query, id2str.transform(false), response('pnad_novo')); diff --git a/src/libs/routes_v1/populationOutOfSchool.js b/src/libs/routes_v1/populationOutOfSchool.js new file mode 100644 index 0000000000000000000000000000000000000000..d5686c239df8e90974a0e52de3a70b5628f2c431 --- /dev/null +++ b/src/libs/routes_v1/populationOutOfSchool.js @@ -0,0 +1,511 @@ +/* +Copyright (C) 2024 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +const express = require('express'); + +const PopulationOutOfSchoolApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const config = require(`${libs}/config`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +let rqf = new ReqQueryFields(); + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +PopulationOutOfSchoolApp.use(cache('15 day')); + +PopulationOutOfSchoolApp.get('/years', (req, res, next) => { + req.sql.from('pnad_novo') + .field('DISTINCT pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019') + next(); +}, query, response('years')); + +PopulationOutOfSchoolApp.get('/illiteracy', (req, res, next) => { + req.result = [] + + for (let i = 0; i < 2; i++) { + req.result.push({ + id: i, name: id2str.illiteracy(i) + }); + + } + req.result.push({id: 9, name: id2str.illiteracy(9)}); + next(); +}, response('illiteracy')); + +PopulationOutOfSchoolApp.get('/years_of_study', (req, res, next) => { + req.result = [] + + for (let i = 0; i < 17; i++) { + req.result.push({ + id: i, name: id2str.yearsOfStudy(i) + }); + } + + req.result.push({id: 99, name: id2str.yearsOfStudy(99)}); + next(); +}, response('years_of_study')); + +PopulationOutOfSchoolApp.get('/instruction_level', (req, res, next) => { + req.result = [] + + for (let i = 1; i < 8; i++) { + req.result.push({ + id: i, name: id2str.instructionLevel(i) + }); + } + req.result.push({id: 99, name: id2str.instructionLevel(99)}); + next(); +}, response('instruction_level')); + +PopulationOutOfSchoolApp.get('/new_pnad_adm_dependency', (req, res, next) => { + req.result = [] + for (let i = 1; i < 3; i++) { + req.result.push({ + id: i, name: id2str.newPnadAdmDependency(i) + }); + } + req.result.push({id: 99, name: id2str.newPnadAdmDependency(99)}); + next(); +}, response('new_pnad_adm_dependency')); + +PopulationOutOfSchoolApp.get('/region', (req, res, next) => { + req.result = [] + for (let i = 1; i < 6; i++) { + req.result.push({ + id: i, name: id2str.regionCode(i) + }); + } + + next(); +}, response('region')); + +PopulationOutOfSchoolApp.get('/cap_code', (req, res, next) => { + req.result = [] + for (let i = 11; i < 54; i++) { + if (id2str.capitalCode(i) !== 'Não informado') { + req.result.push({ + id: i, name: id2str.capitalCode(i) + }); + } + } + req.result.push({id: 99, name: id2str.capitalCode(99)}); + + next(); +}, response('cap_code')); + +PopulationOutOfSchoolApp.get('/metro_code', (req, res, next) => { + req.result = [] + for (let i = 13; i < 53; i++) { + if (id2str.metroCode(i) !== 'Não informado') { + req.result.push({ + id: i, name: id2str.metroCode(i) + }); + } + } + req.result.push({id: 99, name: id2str.metroCode(99)}); + + next(); +}, response('metro_code')); + +PopulationOutOfSchoolApp.get('/attended_modality', (req, res, next) => { + req.result = [] + for (let i = 1; i < 16; i++) { + req.result.push({ + id: i, name: id2str.attendedModality(i) + }); + } + // Remove the option with id equals 10 => This option exists in the database, a better solution to this would be remove the option from the database + req.result.splice(req.result.findIndex((item) => item.id === 10), 1); + req.result.push({id: 99, name: id2str.attendedModality(99)}); + next(); +}, response('attended_modality')); + +PopulationOutOfSchoolApp.get('/income_range', (req, res, next) => { + req.result = [] + for (let i = 1; i < 8; i++) { + req.result.push({ + id: i, name: id2str.incomeRange(i) + }); + } + req.result.push({id: 9, name: id2str.incomeRange(9)}); + next(); +}, response('income_range')); + +PopulationOutOfSchoolApp.get('/attends_school', (req, res, next) => { + req.result = [] + for (let i = 1; i < 3; i++) { + req.result.push({ + id: i, name: id2str.attendsSchool(i) + }); + } + next(); +}, response('attends_school')); + +PopulationOutOfSchoolApp.get('/gender', (req, res, next) => { + req.result = [] + for (let i = 1; i < 3; i++) { + req.result.push({ + id: i, name: id2str.gender(i) + }); + } + next(); +}, response('gender')); + +PopulationOutOfSchoolApp.get('/new_pnad_ethnic_group', (req, res, next) => { + req.result = [] + for (let i = 1; i < 6; i++) { + req.result.push({ + id: i, name: id2str.ethnicGroupNewPnad(i) + }); + } + req.result.push({id: 9, name: id2str.ethnicGroupNewPnad(9)}); + next(); +}, response('new_pnad_ethnic_group')); + +PopulationOutOfSchoolApp.get('/bolsa_familia', (req, res, next) => { + req.result = [] + for (let i = 1; i < 3; i++) { + req.result.push({ + id: i, name: id2str.attendsSchool(i) + }); + } + req.result.push({id: 9, name: id2str.attendsSchool(9)}); + next(); +}, response('bolsa_familia')); + +PopulationOutOfSchoolApp.get('/modality', (req, res, next) => { + req.result = [] + for (let i = 1; i < 4; i++) { + req.result.push({ + id: i, name: id2str.modality(i) + }); + } + req.result.push({id: 99, name: id2str.modality(99)}); + next(); +}, response('modality')); + +PopulationOutOfSchoolApp.get('/modality_shift', (req, res, next) => { + req.result = [] + for (let i = 1; i < 8; i++) { + req.result.push({ + id: i, name: id2str.modalityShift(i) + }); + } + req.result.push({id: 9, name: id2str.modalityShift(9)}); + req.result.push({id: 99, name: id2str.modalityShift(99)}); + next(); +}, response('modality_shift')); + +PopulationOutOfSchoolApp.get('/state', (req, res, next) => { + req.result = [] + for (let i = 11; i < 54; i++) { + if (id2str.stateName(i) !== 'Não declarada') { + req.result.push({ + id: i, name: id2str.stateName(i) + }); + } + } + req.result.push({id: 99, name: id2str.stateName(99)}); + + next(); +}, response('state')); + +PopulationOutOfSchoolApp.get('/age_range_pop_school', (req, res, next) => { + req.result = [] + for (let i = 1; i < 8; i++) { + req.result.push({ + id: i, name: id2str.ageRangePopSchool(i) + }); + } + + next(); +}, response('age_range_pop_school')); + + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'id', + table: 'pnad_novo', + tableField: 'id', + where: { + relation: '=', + type: 'integer', + field: 'id' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: ['id', 'nome'], + resultField: ['state_id', 'state_nome'], + where: { + relation: '=', + type: 'integer', + field: 'id', + }, + join: { + primary: 'id', + foreign: 'cod_uf', + foreignTable: 'pnad_novo' + } +}).addValue({ + name: 'state_not', + table: 'estado', + tableField: ['nome', 'id'], + resultField: ['state_name', 'state_id'], + where: { + relation: '<>', + type: 'integer', + field: 'cod_uf', + table: 'pnad_novo' + }, + join: { + primary: 'id', + foreign: 'cod_uf', + foreignTable: 'pnad_novo' + } +}).addValue({ + name: 'bolsa_familia', + table: 'pnad_novo', + tableField: 'recebeu_rendimentos_de_programa_bolsa_familia', + resultField: 'bolsa_familia_id', + where: { + relation: '=', + type: 'integer', + field: 'recebeu_rendimentos_de_programa_bolsa_familia' + } +}).addValue({ + name: 'new_pnad_ethnic_group', + table: 'pnad_novo', + tableField: 'cor_raca', + resultField: 'new_pnad_ethnic_group_id', + where: { + relation: '=', + type: 'integer', + field: 'cor_raca' + } +}).addValue({ + name: 'age_range_pop_school', + table: 'pnad_novo', + tableField: 'faixa_etaria_pop_out', + resultField: 'age_range_pop_school_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria_pop_out' + } +}).addValue({ + name: 'income_range', + table: 'pnad_novo', + tableField: 'faixa_rendimento_aux', + resultField: 'income_range_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_rendimento_aux' + } +}).addValue({ + name: 'gender', + table: 'pnad_novo', + tableField: 'sexo', + resultField: 'gender_id', + where: { + relation: '=', + type: 'integer', + field: 'sexo' + } +}).addValue({ + name: 'cap_code', + table: 'pnad_novo', + tableField: 'cod_cap', + resultField: 'cap_code_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_cap' + } +}).addValue({ + name: 'region', + table: 'pnad_novo', + tableField: 'cod_regiao', + resultField: 'region_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_regiao' + } +}).addValue({ + name: 'metro_code', + table: 'pnad_novo', + tableField: 'cod_rm_ride', + resultField: 'metro_code_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_rm_ride' + } +}).addValue({ + name: 'min_year', + table: 'pnad_novo', + tableField: 'ano_ref', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_ref' + } +}).addValue({ + name: 'max_year', + table: 'pnad_novo', + tableField: '', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_ref' + } +}).addValue({ + name: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}); + +function matchQueries(queryPartial, queryTotal) { + let match = []; + queryTotal.forEach((result) => { + let newObj = {}; + let keys = Object.keys(result); + keys.forEach((key) => { + newObj[key] = result[key]; + }); + let index = keys.indexOf('total'); + if(index > -1) keys.splice(index, 1); + let objMatch = null; + + for(let i = 0; i < queryPartial.length; ++i) { + let partial = queryPartial[i]; + let foundMatch = true; + for(let j = 0; j < keys.length; ++j) { + let key = keys[j]; + if(partial[key] !== result[key]) { + foundMatch = false; + break; + } + } + if(foundMatch) { + objMatch = partial; + break; + } + } + + if(objMatch) { + newObj.denominator = result.total; + newObj.pop_total = objMatch.total; + newObj.total = parseFloat(((objMatch.total / result.total) * 100).toFixed(1)); + match.push(newObj); + } + } +) + + return match; +} + +PopulationOutOfSchoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if ("age_range_pop_school" in req.filter || "age_range_pop_school" in req.dims) { + // As we will need to do two requests, they'll be stored in a list + req.querySet = [] + + // Create an object that will store the first request (the sum of all people that attend school) + let attends_school = req.sql.clone(); + attends_school.from('pnad_novo') + .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') + .field('pnad_novo.faixa_etaria_pop_out') + .field('pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019') + .where('pnad_novo.frequenta_escola = 2') + .where('(pnad_novo.faixa_etaria_pop_out IN (1,2)) OR (pnad_novo.faixa_etaria_pop_out = 3 AND pnad_novo.nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos <= 2) OR \ + (pnad_novo.faixa_etaria_pop_out = 4 AND pnad_novo.nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos <= 3) OR \ + (pnad_novo.faixa_etaria_pop_out >= 5 AND pnad_novo.nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos <= 4)') + .group('pnad_novo.ano_ref') + .group('pnad_novo.faixa_etaria_pop_out') + .order('pnad_novo.ano_ref') + .order('pnad_novo.faixa_etaria_pop_out') + req.querySet.push(attends_school); + + + // Then, the second object is created and stores the sum of all people without filters + let full_population = req.sql.clone(); + full_population.from('pnad_novo') + .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') + .field('pnad_novo.faixa_etaria_pop_out') + .field('pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019') + .group('pnad_novo.ano_ref') + .group('pnad_novo.faixa_etaria_pop_out') + .order('pnad_novo.ano_ref') + .order('pnad_novo.faixa_etaria_pop_out') + req.querySet.push(full_population); + } + + next(); +}, multiQuery, (req, res, next) => { + if ("age_range_pop_school" in req.filter || "age_range_pop_school" in req.dims) { + // The multiple requests are made. Then we need to calculate the percetange. So the function + // below is used + let newObj = matchQueries(req.result[0], req.result[1]); + req.result = newObj; + } else { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + next(); +}, id2str.transform(false), response('populationOutOfSchool')); + +module.exports = PopulationOutOfSchoolApp; diff --git a/src/libs/routes_v1/potentialRevenue.js b/src/libs/routes_v1/potentialRevenue.js new file mode 100644 index 0000000000000000000000000000000000000000..3355791612e2f339d01bf6fab489e4ca94c1ea76 --- /dev/null +++ b/src/libs/routes_v1/potentialRevenue.js @@ -0,0 +1,127 @@ +/* +Copyright (C) 2024 Centro de Computacao Cientifica e Software Livre +Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + +This file is part of simcaq-node. + +simcaq-node is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +simcaq-node is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. +*/ + +const express = require('express'); + +const potentialRevenueApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const config = require(`${libs}/config`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +const aggregateData = require(`${libs}/middlewares/aggregateData`); + +let rqf = new ReqQueryFields(); + +potentialRevenueApp.use(cache('15 day')); + +potentialRevenueApp.get('/years', (req, res, next) => { + req.sql.from('receitas') + .field('DISTINCT receitas.ano', 'year') + .where('receitas.ano is not null') + next(); +}, query, response('years')) + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'min_year', + table: 'receitas', + tableField: 'ano', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano' + } +}).addValue({ + name: 'max_year', + table: 'receitas', + tableField: 'ano', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano' + } +}).addValue({ + name: 'region', + table: 'estado', + tableField: ['nome_ente', 'cod_ibge', 'id', 'regiao_id'], + resultField: ['state_name', 'state_cod_ibge', 'state_id', 'region_id'], + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + }, + join: { + primary: 'id', + foreign: 'cod_ibge', + foreignTable: 'receitas' + } +}) + + +potentialRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + + if (req.query.dims && req.query.dims.includes('receita_potencial')) { + let whereCondition = req.query.filter.includes("region") ? "" : "receitas.cod_ibge = 0"; + let filterId; + const filters = req.query.filter.split(","); + filters.forEach((filter) => { + if (filter.includes("state") || filter.includes("city")) { + filterId = Number(filter.split(":")[1].replace(/"/g, "")); + whereCondition = `receitas.cod_ibge = ${filterId}` + } + }) + + req.sql.from('receitas') + .field('receitas.ano', 'year') + .field('receitas.ano', 'total_ano') + .field('receitas.nome_ente', 'total_nome_ente') + .field('receitas.receitas', 'total_receitas') + .field('matriculas_publica', 'total_matriculas_publica') + .field('matriculas_publicas_mais_conveniada', 'total_matriculas_publicas_mais_conveniada') + .where(`${whereCondition}`) + .order('receitas.ano'); + } + + next(); +}, query, aggregateData, id2str.transform(false), response('potentialRevenue')); + +module.exports = potentialRevenueApp; \ No newline at end of file diff --git a/src/libs/routes_v1/rateSchool.js b/src/libs/routes_v1/rateSchool.js index a6a91b7be08919cc1ab12a304ee1832cfddfc84e..37e0b2836d4aa0c2cee304b720605fbbef7d2d88 100644 --- a/src/libs/routes_v1/rateSchool.js +++ b/src/libs/routes_v1/rateSchool.js @@ -257,8 +257,6 @@ function matchQueries(queryTotal, queryPartial) { keys.forEach((key) => { newObj[key] = result[key]; }); - // console.log('NEW OBJ'); - // console.log(newObj); let index = keys.indexOf('total'); if(index > -1) keys.splice(index, 1); let objMatch = null; @@ -280,7 +278,6 @@ function matchQueries(queryTotal, queryPartial) { } if(objMatch) { - // console.log(objMatch); newObj.denominator = result.total; newObj.partial = objMatch.total; newObj.total = (objMatch.total / result.total) * 100; diff --git a/src/libs/routes_v1/school.js b/src/libs/routes_v1/school.js index 54932c51bd78ff629dff89625be3a93c24a8042f..5c5bfdeb3de28a636253d1f841dd215135525d44 100644 --- a/src/libs/routes_v1/school.js +++ b/src/libs/routes_v1/school.js @@ -698,6 +698,7 @@ schoolApp.get('/count', cache('15 day'), rqfCount.parse(), (req, res, next) => { req.sql.where('' + arrangementQuery); } delete req.filter.arrangement + next(); }, rqfCount.build(), query, id2str.transform(), addMissing(rqfCount), response('school')); diff --git a/src/libs/routes_v1/studentCost.js b/src/libs/routes_v1/studentCost.js index fdcf7270790bfcf1c50d26386a76f3196ccc7aa9..5283e54d3800bfe13e357c1ff0ee76c9ea5706ce 100644 --- a/src/libs/routes_v1/studentCost.js +++ b/src/libs/routes_v1/studentCost.js @@ -51,6 +51,14 @@ studentCostApp.get('/years', (req, res, next) => { next(); }, query, response('years')); +studentCostApp.get('/type', (req, res, next) => { + req.result = [] + + req.result.push({ id: 1, name: "Rede Estadual" }); + req.result.push({ id: 2, name: "Rede Municipal" }); + next(); +}, response('type')); + rqf.addField({ name: 'filter', field: false, @@ -98,8 +106,10 @@ rqf.addField({ studentCostApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { if (req.query.dims && req.query.dims.includes('despesas')) { + let obj = {}; let whereCondition = req.query.filter.includes("region") ? "" : "despesas.cod_ibge = 0"; let filterId; + let typeFilter = false; const filters = req.query.filter.split(","); filters.forEach((filter) => { if (filter.includes("state") || filter.includes("city")) { @@ -108,23 +118,33 @@ studentCostApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { } }) - req.sql.from('despesas') - .field('despesas.ano_censo', 'year') - .field('despesas.ano_censo', 'total_ano') - .field('despesas.nome_ente', 'total_nome_ente') - .field('despesas.despesas', 'total_despesas') - .field('matriculas_publica', 'total_matriculas_publica') - .field('matriculas_publicas_mais_conveniada', 'total_matriculas_publicas_mais_conveniada') - .field('gasto_aluno_ano_publica', 'total_gasto_aluno_ano_publica') - .field('gasto_aluno_mes_publica', 'total_gasto_aluno_mes_publica') - .field('gasto_aluno_ano_publica_mais_conveniada', 'total_gasto_aluno_ano_publica_mais_conveniada') - .field('gasto_aluno_mes_publica_mais_conveniada', 'total_gasto_aluno_mes_publica_mais_conveniada') - .where(`${whereCondition}`) - .order('despesas.cod_ibge') - .order('despesas.ano_censo') + if (req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + obj = JSON.parse(jsonString); + if (obj.type) { + if (obj.type.includes("1") && !obj.type.includes("2")) + whereCondition = "despesas.cod_ibge >= 11 and despesas.cod_ibge <= 53" + else if (obj.type.includes("2") && !obj.type.includes("1")) + whereCondition = "despesas.cod_ibge > 53" + + typeFilter = true; + } + } + + if (!typeFilter || (typeFilter && (!obj.state && !obj.city && !obj.region))) { + req.sql.from('despesas') + .field('despesas.ano_censo', 'year') + .field(`SUM(despesas) / SUM(matriculas_publica)`, '(total_gasto_aluno_ano_publica)') + .field(`SUM(despesas) / SUM(matriculas_publica) / 12`, 'total_gasto_aluno_mes_publica') + .field(`SUM(despesas) / SUM(matriculas_publicas_mais_conveniada)`, 'total_gasto_aluno_ano_publica_mais_conveniada') + .field(`SUM(despesas) / SUM(matriculas_publicas_mais_conveniada) / 12`, 'total_gasto_aluno_mes_publica_mais_conveniada') + .where(`${whereCondition}`) + .group('despesas.ano_censo') + .order('despesas.ano_censo') + } } next(); -}, query, aggregateData, id2str.transform(false), response('student_cost')); +}, query, aggregateData, response('student_cost')); module.exports = studentCostApp; diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js index b595fb0086846cef7c7b058abbc34ab23006ceca..f5d5fda0c3fee620fd0c6d1c5036ac9bd5cc4cd9 100644 --- a/src/libs/routes_v1/studentRevenue.js +++ b/src/libs/routes_v1/studentRevenue.js @@ -36,21 +36,29 @@ const aggregateData = require(`${libs}/middlewares/aggregateData`); const id2str = require(`${libs}/middlewares/id2str`); -const config = require(`${libs}/config`); +const config = require(`${libs}/config`); -const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; +const cache = require('apicache').options({ debug: config.debug, statusCodes: { include: [200] } }).middleware; let rqf = new ReqQueryFields(); -studentRevenueApp.use(cache('15 day')); +// studentRevenueApp.use(cache('15 day')); studentRevenueApp.get('/years', (req, res, next) => { req.sql.from('receitas') - .field('DISTINCT receitas.ano', 'year') - .where('receitas.ano is not null') + .field('DISTINCT receitas.ano', 'year') + .where('receitas.ano is not null') next(); }, query, response('years')); +studentRevenueApp.get('/type', (req, res, next) => { + req.result = [] + + req.result.push({ id: 1, name: "Rede Estadual" }); + req.result.push({ id: 2, name: "Rede Municipal" }); + next(); +}, response('type')); + rqf.addField({ name: 'filter', field: false, @@ -96,9 +104,12 @@ rqf.addField({ } }) -studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { +studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { if (req.query.dims && req.query.dims.includes('receitas')) { + let obj = {}; let whereCondition = req.query.filter.includes("region") ? "" : "receitas.cod_ibge = 0"; + + let typeFilter = false; let filterId; const filters = req.query.filter.split(","); filters.forEach((filter) => { @@ -108,20 +119,31 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { } }) - req.sql.from('receitas') - .field('receitas.ano', 'year') - .field('receitas.ano', 'total_ano') - .field('receitas.nome_ente', 'total_nome_ente') - .field('receitas.receitas', 'total_receitas') - .field('matriculas_publica', 'total_matriculas_publica') - .field('matriculas_publicas_mais_conveniada', 'total_matriculas_publicas_mais_conveniada') - .field('receuta_aluno_ano_publica', 'total_receita_aluno_ano_publica') - .field('receita_aluno_mes_publica', 'total_receita_aluno_mes_publica') - .field('receita_aluno_ano_publica_mais_conveniada', 'total_receita_aluno_ano_publica_mais_conveniada') - .field('receita_aluno_mes_publica_mais_conveniada', 'total_receita_aluno_mes_publica_mais_conveniada') - .where(`${whereCondition}`) - .order('receitas.cod_ibge') - .order('receitas.ano') + if (req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + obj = JSON.parse(jsonString); + if (obj.type) { + if (obj.type.includes("1") && !obj.type.includes("2")) { + whereCondition = "receitas.cod_ibge >= 11 and receitas.cod_ibge <= 53" + } + else if (obj.type.includes("2") && !obj.type.includes("1")) { + whereCondition = "receitas.cod_ibge > 53" + } + + typeFilter = true; + } + } + if (!typeFilter || (typeFilter && (!obj.state && !obj.city && !obj.region))) { + req.sql.from('receitas') + .field('receitas.ano', 'year') + .field(`SUM(receitas) / SUM(matriculas_publica)`, '(total_receita_aluno_ano_publica)') + .field(`SUM(receitas) / SUM(matriculas_publica) / 12`, 'total_receita_aluno_mes_publica') + .field(`SUM(receitas) / SUM(matriculas_publicas_mais_conveniada)`, 'total_receita_aluno_ano_publica_mais_conveniada') + .field(`SUM(receitas) / SUM(matriculas_publicas_mais_conveniada) / 12`, 'total_receita_aluno_mes_publica_mais_conveniada') + .where(`${whereCondition}`) + .group('receitas.ano') + .order('receitas.ano') + } } next(); diff --git a/src/libs/routes_v1/studentsReceivingAidTax.js b/src/libs/routes_v1/studentsReceivingAidTax.js index c4509cf84057b5243e9e93f2ae7ccfc992e00a47..2910f434f18a0d3db52707d0c1b9b312c485570c 100644 --- a/src/libs/routes_v1/studentsReceivingAidTax.js +++ b/src/libs/routes_v1/studentsReceivingAidTax.js @@ -120,8 +120,6 @@ studentsReceivingAidTaxApp.get('/cap_code', (req, res, next) => { }); } } - req.result.push({id: 99, name: id2str.capitalCode(99)}); - next(); }, response('cap_code')); @@ -242,7 +240,7 @@ studentsReceivingAidTaxApp.get('/state', (req, res, next) => { next(); }, response('state')); -studentsReceivingAidTaxApp.get('/age_range', (req, res, next) => { +studentsReceivingAidTaxApp.get('/age_range_all', (req, res, next) => { req.result = [] for (let i = 1; i < 12; i++) { req.result.push({ @@ -251,7 +249,7 @@ studentsReceivingAidTaxApp.get('/age_range', (req, res, next) => { } next(); -}, response('age_range')); +}, response('age_range_all')); studentsReceivingAidTaxApp.get('/location', (req, res, next) => { req.result = [] @@ -578,8 +576,24 @@ function matchQueries(queryPartial, queryTotal) { return match; } +function whereCondition(req) { + let where = ""; + + const dims = req.query.dims; + if (dims && dims.includes("income_range")){ + where += `pnad_novo.faixa_rendimento_aux_tx is not null`; + } + + if (dims && dims.includes("cap_code")){ + where += `pnad_novo.cod_cap <> 99`; + } + + return where; +} + studentsReceivingAidTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.querySet = []; + let where = whereCondition(req); // Subquery para total_pop_maior_25 com filtros dinâmicos let totalEstudantesQueRecebemAuxilio = req.sql.clone(); @@ -587,6 +601,7 @@ studentsReceivingAidTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) = .field("ano_ref", "year") .field("SUM(peso_domicilio_pessoas_com_cal)", "total") .where("recebeu_rendimentos_de_programa_bolsa_familia = 1") + .where(`${where}`) .where("frequenta_escola = 1") .where("ano_ref >= 2019") .group("ano_ref") @@ -598,11 +613,12 @@ studentsReceivingAidTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) = totalEstudantes.from("pnad_novo") .field("ano_ref", "year") .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where(`${where}`) .where("frequenta_escola = 1") + .where("ano_ref >= 2019") .group("ano_ref") .order("ano_ref"); - req.querySet.push(totalEstudantesQueRecebemAuxilio); req.querySet.push(totalEstudantes); diff --git a/src/libs/routes_v1/superiorEducationConclusionTax.js b/src/libs/routes_v1/superiorEducationConclusionTax.js index 1f84f7b071048a4a3170244131a66c2ba6174d78..5a21db624318aa0c1b3010d725538740bf2b9f01 100644 --- a/src/libs/routes_v1/superiorEducationConclusionTax.js +++ b/src/libs/routes_v1/superiorEducationConclusionTax.js @@ -102,7 +102,7 @@ superiorEducationConclusionTaxApp.get('/new_pnad_adm_dependency', (req, res, nex superiorEducationConclusionTaxApp.get('/region', (req, res, next) => { req.result = [] - for (let i = 1; i < 6; i++) { + for (let i = 1; i < 5; i++) { req.result.push({ id: i, name: id2str.regionCode(i) }); @@ -120,8 +120,6 @@ superiorEducationConclusionTaxApp.get('/cap_code', (req, res, next) => { }); } } - req.result.push({id: 99, name: id2str.capitalCode(99)}); - next(); }, response('cap_code')); @@ -134,7 +132,6 @@ superiorEducationConclusionTaxApp.get('/metro_code', (req, res, next) => { }); } } - req.result.push({id: 99, name: id2str.metroCode(99)}); next(); }, response('metro_code')); @@ -160,6 +157,7 @@ superiorEducationConclusionTaxApp.get('/income_range', (req, res, next) => { }); } req.result.push({id: 10, name: id2str.incomeRange(10)}); + next(); }, response('income_range')); @@ -242,16 +240,16 @@ superiorEducationConclusionTaxApp.get('/state', (req, res, next) => { next(); }, response('state')); -superiorEducationConclusionTaxApp.get('/age_range', (req, res, next) => { +superiorEducationConclusionTaxApp.get('/age_range_all', (req, res, next) => { req.result = [] - for (let i = 1; i < 12; i++) { + for (let i = 7; i < 12; i++) { req.result.push({ id: i, name: id2str.ageRangeAll(i) }); } next(); -}, response('age_range')); +}, response('age_range_all')); superiorEducationConclusionTaxApp.get('/location', (req, res, next) => { req.result = [] @@ -575,9 +573,28 @@ function matchQueries(queryPartial, queryTotal) { return match; } +function whereCondition(req) { + let where = ""; + let filterId; + //const filters = req.query.filter.split(","); + const dims = req.query.dims; + if (dims && dims.includes("cap_code")){ + where += `pnad_novo.cod_cap <> 99`; + } + if (dims && dims.includes("income_range")){ + where += `pnad_novo.faixa_rendimento_aux_tx is not null`; + } + if (dims && dims.includes("age_range_superior")){ + where += `pnad_novo.faixa_etaria > 6`; + } + + return where; +} + superiorEducationConclusionTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.querySet = []; - + let where = whereCondition(req); + // Subquery para total_pop_maior_25 com filtros dinâmicos let totalPopMaior25ComSuperiorCompleto = req.sql.clone(); totalPopMaior25ComSuperiorCompleto.from("pnad_novo") @@ -586,9 +603,10 @@ superiorEducationConclusionTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, .where("idade_morador_31_03 >= 25") .where("nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos = 7") .where("ano_ref >= 2019") + .where(`${where}`) .group("ano_ref") .order("ano_ref"); - + // Subquery para total_pop_maior_25 com filtros dinâmicos let totalPopMaior25 = req.sql.clone(); @@ -597,14 +615,10 @@ superiorEducationConclusionTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, .field("SUM(peso_domicilio_pessoas_com_cal)", "total") .where("idade_morador_31_03 >= 25") .where("ano_ref >= 2019") + .where(`${where}`) .group("ano_ref") .order("ano_ref"); - // if (req.query.dims && req.query.dims.includes("income_range")) { - // totalPopMaior19.where("faixa_rendimento_aux_tx is not null"); - // totalPopEdBasMaior19.where("faixa_rendimento_aux_tx is not null"); - // } - req.querySet.push(totalPopMaior25ComSuperiorCompleto); req.querySet.push(totalPopMaior25);