diff --git a/.gitignore b/.gitignore index 2d98beead909ceb03087bc85133de73ca5a3bde4..2fa21185e28b55b01382a70bdf4fbd61f652e8f0 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ package-lock.json Dockerfile DockerfileAntigo src/libs/db/postgres.js +docker-compose.yml +entrypoint.sh +gulpfile.template.js + diff --git a/gulpfile.babel.js b/gulpfile.babel.js index c661d5af261b6a5a8baf5232592d01ef7cb48b28..7c8b741852d94433abf9034871069a8d0594e315 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -114,3 +114,4 @@ gulp.task('run', () => { }); gulp.task('default', ['run']); + diff --git a/src/libs/convert/incomeRange.js b/src/libs/convert/incomeRange.js index fee5189b4a4100e42940108ce3f173e655ef13ee..a38446aa56ac3012edbe034bf1037772085004f8 100644 --- a/src/libs/convert/incomeRange.js +++ b/src/libs/convert/incomeRange.js @@ -36,5 +36,7 @@ module.exports = function incomeRange(id) { return 'Mais de 5 salários mÃnimos'; case 9: return 'Não informado ou ignorado'; + case 10: + return "Ignorado" } }; diff --git a/src/libs/convert/revenue.js b/src/libs/convert/revenue.js new file mode 100644 index 0000000000000000000000000000000000000000..18cfeb8c0e2cf05ba10a15e4e4801f0327562ddc --- /dev/null +++ b/src/libs/convert/revenue.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 "Nome Ente" + case 3: + return 'Receitas'; + 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'; + } +}; diff --git a/src/libs/convert/studentCost.js b/src/libs/convert/studentCost.js new file mode 100644 index 0000000000000000000000000000000000000000..d757ec47f43f907e63cfd73a392aaa85c1dab7d6 --- /dev/null +++ b/src/libs/convert/studentCost.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 studentCost(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'; + case 6: + return 'Gasto Aluno Ano Pública' + case 7: + return 'Gasto Aluno Mês Pública'; + case 8: + return 'Gasto Aluno Ano Pública Mais Conveniada'; + case 9: + return 'Gasto Aluno Mês Pública Mais Conveniada'; + } +}; diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js index eed373ddbbfd98c4a30fc4615823bb5d91a01fc4..ab7dfdd957843df41e5fffabbad43b579d36fe51 100644 --- a/src/libs/middlewares/aggregateData.js +++ b/src/libs/middlewares/aggregateData.js @@ -35,7 +35,9 @@ const convert = { contract_type_entity: 'contractType', total_doc: "totalDoc", education_degree_entity: "educationDegreeEntity", - government_agreement: "governmentAgreement" + government_agreement: "governmentAgreement", + receitas: "revenue", + despesas: "studentCost" } function aggregateData(req, res, next) { @@ -62,8 +64,11 @@ function aggregateData(req, res, next) { 'age_range_entity', 'post_graduation_entity', 'contract_type_entity', - 'education_degree_entity' - ] + 'education_degree_entity', + 'receitas', + 'despesas' + ]; + const baseFields = ["school"] let id; const fields = req.query.dims.split(','); let currentAggregateField; @@ -87,7 +92,9 @@ function aggregateData(req, res, next) { if (currentNonAggregateField) { data[`${currentNonAggregateField}_id`] = r[`${currentNonAggregateField}_id`]; - data[`${currentNonAggregateField}_name`] = id2str[convert[currentNonAggregateField]](r[`${currentNonAggregateField}_id`]); + data[`${currentNonAggregateField}_name`] = baseFields.includes(currentNonAggregateField) + ? r[`${currentNonAggregateField}_name`] + : id2str[convert[currentNonAggregateField]](r[`${currentNonAggregateField}_id`]); } newResult.push(data) diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js index f366d90a30c8d5c57955856040c78d610b3392b3..730d620e18c1bc1708af165eebd5de1b3a54c931 100644 --- a/src/libs/middlewares/id2str.js +++ b/src/libs/middlewares/id2str.js @@ -126,6 +126,8 @@ 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 revenue = require(`${libs}/convert/revenue`); +const studentCost = require(`${libs}/convert/studentCost`); const ids = { gender_id: gender, @@ -244,7 +246,9 @@ const ids = { post_graduation_entity: postGraduationEntity, special_education_entity: specialEducationEntity, total_doc: totalDoc, - education_degree_entity: educationDegreeEntity + education_degree_entity: educationDegreeEntity, + receitas_id: revenue, + student_cost: studentCost }; function transform(removeId=false) { @@ -254,6 +258,7 @@ function transform(removeId=false) { } // Para cada objeto do resultado req.result.forEach((obj) => { + console.log(obj), Object.keys(obj).forEach((key) => { // Se não há uma função especificada, retorna if(typeof ids[key] === 'undefined') return; @@ -385,5 +390,7 @@ module.exports = { postGraduationEntity, specialEducationEntity, totalDoc, - educationDegreeEntity + educationDegreeEntity, + revenue, + studentCost }; diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js new file mode 100644 index 0000000000000000000000000000000000000000..d11f98f5c7f02a508e8bf51b40c9d4554019a905 --- /dev/null +++ b/src/libs/middlewares/studentRevenueParser.js @@ -0,0 +1,22 @@ +/* +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 studentRevenueParser(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/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js new file mode 100644 index 0000000000000000000000000000000000000000..5b79740d1cee31efb2b14da8ca2977ca64e44dae --- /dev/null +++ b/src/libs/routes_v1/adjustedLiquidFrequency.js @@ -0,0 +1,345 @@ +/* +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 adjustedLiquidFrequency = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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(); + +adjustedLiquidFrequency.use(cache('15 day')); + +adjustedLiquidFrequency.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')); + +adjustedLiquidFrequency.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: 10, name: id2str.incomeRange(10)}); + next(); +}, response('income_range')); + +adjustedLiquidFrequency.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')); + +adjustedLiquidFrequency.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')); + +adjustedLiquidFrequency.get('/age_range_all', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 6; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + next() +}, response('age_range_all')); + +adjustedLiquidFrequency.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')); + +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: 'income_range', + table: 'pnad_novo', + tableField: 'faixa_rendimento_aux_tx', + resultField: 'income_range_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_rendimento_aux_tx' + } +}).addValue({ + name: 'age_range_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).addValue({ + name: 'gender', + table: 'pnad_novo', + tableField: 'sexo', + resultField: 'gender_id', + where: { + relation: '=', + type: 'integer', + field: 'sexo' + } +}).addValue({ + name: 'region', + table: 'pnad_novo', + tableField: 'cod_regiao', + resultField: 'region_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_regiao' + } +}).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' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { + const dimensions = req.dims || {}; // Obter dimensões solicitadas pela requisição + + if (!dimensions.age_range_all) { + req.result = { error: "age_range_all should be selected" }; + next(); + } + + // Subquery: total_pop + let totalPop = squel.select() + .field("ano_ref") + .field("faixa_etaria") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .from("pnad_novo") + .where("faixa_etaria <= 6") + .group("ano_ref") + .group("faixa_etaria"); + + + // Aplicar filtros dinâmicos à subquery total_pop + totalPop = rqf.buildQuery(req, totalPop); + + const joinQuery = ` + total_pop.ano_ref = pnad_novo.ano_ref AND + total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada + ${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" : ""} + ` + + // Subquery: total_apoio_freq + let totalApoioFreq = squel.select() + .field("pnad_novo.ano_ref", "ano_ref") + .field("pnad_novo.apoio_frequencia_ajustada", "apoio_frequencia_ajustada") + .field("total_pop.total", "total") + .field("SUM(pnad_novo.peso_domicilio_pessoas_com_cal)", "total_freq") + .from("pnad_novo") + .join( + totalPop, + "total_pop", + joinQuery + ) + .group("pnad_novo.ano_ref") + .group("pnad_novo.apoio_frequencia_ajustada") + .group("total_pop.total"); + + // Aplicar filtros dinâmicos à subquery total_apoio_freq + totalApoioFreq = rqf.buildQuery(req, totalApoioFreq); + + // Query Principal + let mainQuery = squel.select() + .from(totalApoioFreq, "total_apoio_freq") + .field("total_apoio_freq.ano_ref", "year") + .field("total_apoio_freq.apoio_frequencia_ajustada", "age_range_all_id") + .field("ROUND((total_apoio_freq.total_freq / total_apoio_freq.total) * 100, 1)", "total") + .where("total_apoio_freq.ano_ref >= 2019") + .order("total_apoio_freq.ano_ref") + .order("total_apoio_freq.apoio_frequencia_ajustada"); + + if (dimensions.gender) + mainQuery.field("total_apoio_freq.gender_id") + + if (dimensions.bolsa_familia) + mainQuery.field("total_apoio_freq.bolsa_familia_id") + + if (dimensions.new_pnad_ethnic_group) + mainQuery.field("total_apoio_freq.new_pnad_ethnic_group_id") + + if (dimensions.income_range) + mainQuery.field("total_apoio_freq.income_range_id") + + req.sql = mainQuery; + + next(); +}, query, id2str.transform(false), response('adjusted_liquid_frequency')); + +module.exports = adjustedLiquidFrequency; diff --git a/src/libs/routes_v1/aggregateClass.js b/src/libs/routes_v1/aggregateClass.js new file mode 100644 index 0000000000000000000000000000000000000000..8a186bda7b3e90a49fdf79e607bfe358da5e45e1 --- /dev/null +++ b/src/libs/routes_v1/aggregateClass.js @@ -0,0 +1,50 @@ +/* +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/>. +*/ + +const express = require('express'); + +const aggregateClassApp = 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 config = require(`${libs}/config`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +let rqf = new ReqQueryFields(); + +aggregateClassApp.use(cache('15 day')); + +aggregateClassApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + req.sql.from('turmas_agregadas2022') + .field('id'); + + next(); +}, query, response('aggregateClass')); + +module.exports = aggregateClassApp; diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js index bffc8b5d0ff428652d958d41d521ad1992007b85..5331b389e96c49e06b8ee0df8b8868be68f2422a 100644 --- a/src/libs/routes_v1/api.js +++ b/src/libs/routes_v1/api.js @@ -157,6 +157,27 @@ const email = require(`${libs}/routes_v1/email`); const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`) +const yearsOfStudy = require(`${libs}/routes_v1/yearsOfStudy`) + +const studentsReceivingAidTax = require(`${libs}/routes_v1/studentsReceivingAidTax`) + +const superiorEducationConclusionTax = require(`${libs}/routes_v1/superiorEducationConclusionTax`) + +const aggregateClass = require(`${libs}/routes_v1/aggregateClass`); + +const instructionLevel = require(`${libs}/routes_v1/instructionLevel`); + +const basicEducationConclusion = require(`${libs}/routes_v1/basicEducationConclusion`); + +const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequency`); + +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`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API v1 is running' }); }); @@ -217,13 +238,22 @@ api.use('/universityLocalOffer', universityLocalOffer); api.use('/message', message); api.use('/course_students', courseStudents); api.use('/new_pnad', newPnad); +api.use('/aggregate_class', aggregateClass); +api.use('/instruction_level', instructionLevel) api.use('/rate_school_new', rateSchoolNew) api.use('/enrollmentAggregate', enrollmentAggregate); api.use('/employeesAggregate', employeesAggregate); api.use('/course_aggregate', courseAggregate); api.use('/federativeEntity', federativeEntity); api.use('/uni_offer_aggregate', uniLocalOfferAggregate); - +api.use('/years_of_study', yearsOfStudy) +api.use('/students_receiving_aid_tax', studentsReceivingAidTax); +api.use('/superior_education_conclusion_tax', superiorEducationConclusionTax) +api.use('/basic_education_conclusion', basicEducationConclusion); +api.use('/adjusted_liquid_frequency', adjustedLiquidFrequency); +api.use('/iliteracy_rate', iliteracyRate); +api.use('/student_revenue', studentRevenueParser, studentRevenue); +api.use('/student_cost', studentRevenueParser, studentCost); //Publication api.use('/publication', publication); diff --git a/src/libs/routes_v1/basicEducationConclusion.js b/src/libs/routes_v1/basicEducationConclusion.js new file mode 100644 index 0000000000000000000000000000000000000000..fb7452990b4a1be888247802cea0e9e424af606c --- /dev/null +++ b/src/libs/routes_v1/basicEducationConclusion.js @@ -0,0 +1,620 @@ +/* +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 basicEducationConclusion = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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(); + +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') + next(); +}, query, response('years')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +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: 99, name: id2str.capitalCode(99)}); + + next(); +}, response('cap_code')); + +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: 99, name: id2str.metroCode(99)}); + + next(); +}, response('metro_code')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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: 10, name: id2str.incomeRange(10)}); + next(); +}, response('income_range')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.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')); + +basicEducationConclusion.get('/age_range_all', (req, res, next) => { + req.result = [] + for (let i = 1; i < 12; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + + next(); +}, response('age_range_all')); + +basicEducationConclusion.get('/location', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.location(i) + }); + } + + next(); +}, response('location')); + + +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: 'years_of_study', + table: 'pnad_novo', + tableField: 'anos_de_estudo', + resultField: 'years_of_study_id', + where: { + relation: '=', + type: 'integer', + field: 'anos_de_estudo' + } +}).addValue({ + name: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_de_instrucao' + } +}).addValue({ + name: 'new_pnad_adm_dependency', + table: 'pnad_novo', + tableField: 'dependencia_adm', + resultField: 'new_pnad_adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm' + } +}).addValue({ + name: 'attends_school', + table: 'pnad_novo', + tableField: 'frequenta_escola', + resultField: 'attends_school_id', + where: { + relation: '=', + type: 'integer', + field: 'frequenta_escola' + } +}).addValue({ + name: 'modality', + table: 'pnad_novo', + tableField: 'modalidade', + resultField: 'modality_id', + where: { + relation: '=', + type: 'integer', + field: 'modalidade' + } +}).addValue({ + name: 'attended_modality', + table: 'pnad_novo', + tableField: 'nivel_etapa_modalidade_freq', + resultField: 'attended_modality_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_etapa_modalidade_freq' + } +}).addValue({ + name: 'illiteracy', + table: 'pnad_novo', + tableField: 'analfabetismo', + resultField: 'illiteracy_id', + where: { + relation: '=', + type: 'integer', + field: 'analfabetismo' + } +}).addValue({ + name: 'modality_shift', + table: 'pnad_novo', + tableField: 'turno_nivel_etapa', + resultField: 'modality_shift_id', + where: { + relation: '=', + type: 'integer', + field: 'turno_nivel_etapa' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).addValue({ + name: 'income_range', + table: 'pnad_novo', + tableField: 'faixa_rendimento_aux_tx', + resultField: 'income_range_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_rendimento_aux_tx' + } +}).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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}).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' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +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.partial = objMatch.total; + newObj.total = (objMatch.total / result.total) * 100; + match.push(newObj); + } + }); + + return match; +} + +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") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("idade_morador_31_03 >= 19") + .where("ano_ref >= 2019") + .group("ano_ref") + .order("ano_ref"); + + // Subquery para total_pop_ed_bas_maior_19 com filtros dinâmicos + let totalPopEdBasMaior19 = req.sql.clone(); + totalPopEdBasMaior19.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("idade_morador_31_03 >= 19") + .where("nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos IN (5, 6, 7)") + .where("ano_ref >= 2019") + .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(totalPopEdBasMaior19); + req.querySet.push(totalPopMaior19); + + 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; + next(); +}, id2str.transform(false), response('basic_education_conclusion')); + + +module.exports = basicEducationConclusion; diff --git a/src/libs/routes_v1/city.js b/src/libs/routes_v1/city.js index fc697a9f5d146c48c5e87c49ec54572566b22f82..26eabd0ff7f2bb6979094e53a0ee99ee32a20716 100644 --- a/src/libs/routes_v1/city.js +++ b/src/libs/routes_v1/city.js @@ -144,6 +144,7 @@ cityApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('municipio.latitude', 'latitude') .field('municipio.mesorregiao_id', 'mesoregion_id') .field('municipio.microrregiao_id', 'microregion_id'); + console.log(req.sql.toString()); next(); }, query, response('city')); diff --git a/src/libs/routes_v1/iliteracyRate.js b/src/libs/routes_v1/iliteracyRate.js new file mode 100644 index 0000000000000000000000000000000000000000..25b97d5dbaf3236653632ed6ad6b779209a97811 --- /dev/null +++ b/src/libs/routes_v1/iliteracyRate.js @@ -0,0 +1,613 @@ +/* +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 iliteracyRate = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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(); + +iliteracyRate.use(cache('15 day')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.get('/income_range', (req, res, next) => { + req.result = [] + for (let i = 1; i < 10; i++) { + req.result.push({ + id: i, name: id2str.incomeRange(i) + }); + } + next(); +}, response('income_range')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.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')); + +iliteracyRate.get('/age_range_all', (req, res, next) => { + req.result = [] + for (let i = 5; i < 12; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + + next(); +}, response('age_range_all')); + +iliteracyRate.get('/location', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.location(i) + }); + } + + next(); +}, response('location')); + + +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: 'years_of_study', + table: 'pnad_novo', + tableField: 'anos_de_estudo', + resultField: 'years_of_study_id', + where: { + relation: '=', + type: 'integer', + field: 'anos_de_estudo' + } +}).addValue({ + name: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_de_instrucao' + } +}).addValue({ + name: 'new_pnad_adm_dependency', + table: 'pnad_novo', + tableField: 'dependencia_adm', + resultField: 'new_pnad_adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm' + } +}).addValue({ + name: 'attends_school', + table: 'pnad_novo', + tableField: 'frequenta_escola', + resultField: 'attends_school_id', + where: { + relation: '=', + type: 'integer', + field: 'frequenta_escola' + } +}).addValue({ + name: 'modality', + table: 'pnad_novo', + tableField: 'modalidade', + resultField: 'modality_id', + where: { + relation: '=', + type: 'integer', + field: 'modalidade' + } +}).addValue({ + name: 'attended_modality', + table: 'pnad_novo', + tableField: 'nivel_etapa_modalidade_freq', + resultField: 'attended_modality_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_etapa_modalidade_freq' + } +}).addValue({ + name: 'illiteracy', + table: 'pnad_novo', + tableField: 'analfabetismo', + resultField: 'illiteracy_id', + where: { + relation: '=', + type: 'integer', + field: 'analfabetismo' + } +}).addValue({ + name: 'modality_shift', + table: 'pnad_novo', + tableField: 'turno_nivel_etapa', + resultField: 'modality_shift_id', + where: { + relation: '=', + type: 'integer', + field: 'turno_nivel_etapa' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}).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' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +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.partial = objMatch.total; + newObj.total = (objMatch.total / result.total) * 100; + newObj.total = newObj.total.toFixed(1); + match.push(newObj); + } + }); + + return match; +} + +iliteracyRate.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + req.querySet = []; + + // Subquery para total_pop_maior_19 com filtros dinâmicos + let totalAnalfabetismo = req.sql.clone(); + totalAnalfabetismo.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("analfabetismo = 1") + .where("ano_ref >= 2019") + .group("ano_ref") + .order("ano_ref"); + + // Subquery para total_pop_ed_bas_maior_19 com filtros dinâmicos + let totalPop = req.sql.clone(); + totalPop.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("analfabetismo = 0 or analfabetismo = 1") + .where("ano_ref >= 2019") + .group("ano_ref") + .order("ano_ref"); + + req.querySet.push(totalAnalfabetismo); + req.querySet.push(totalPop); + + 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; + next(); +}, id2str.transform(false), response('iliteracy_rate')); + +module.exports = iliteracyRate; diff --git a/src/libs/routes_v1/instructionLevel.js b/src/libs/routes_v1/instructionLevel.js new file mode 100644 index 0000000000000000000000000000000000000000..d3cb7d0c0b8cdf1a766a34326dc6abda81723628 --- /dev/null +++ b/src/libs/routes_v1/instructionLevel.js @@ -0,0 +1,310 @@ +/* +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 NivelInstrucao = 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(); + +NivelInstrucao.use(cache('15 day')); + +NivelInstrucao.get('/years', (req, res, next) => { + req.sql.from('pnad_novo') + .field('DISTINCT pnad_novo.ano_ref', 'year') + next(); +}, query, response('years')); + +NivelInstrucao.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')); + +NivelInstrucao.get('/age_range_all', (req, res, next) => { + req.result = [] + + for (let i = 1; i < 9; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + + } + req.result.push({id: 99, name: id2str.ageRangeAll(99)}); + next(); +}, response('age_range_all')); + +NivelInstrucao.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')); + +NivelInstrucao.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')); + +NivelInstrucao.get('/location', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.location(i) + }); + } + + next(); +}, response('location')); + +NivelInstrucao.get('/ethnic_group', (req, res, next) => { + req.result = []; + for(let i = 0; i <=5; ++i) { + req.result.push({ + id: i, + name: id2str.ethnicGroup(i) + }); + } + next(); +}, response('ethnic_group')); + + +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: '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: 'cap_code', + table: 'pnad_novo', + tableField: 'cod_cap', + resultField: 'cap_code_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_cap' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).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: '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: 'gender', + table: 'pnad_novo', + tableField: 'sexo', + resultField: 'gender_id', + where: { + relation: '=', + type: 'integer', + field: 'sexo' + } +}).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: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'cod_uf' + } +}).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: '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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}) + +NivelInstrucao.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if ("instruction_level" in req.filter || "instruction_level" in req.dims) { + // The multiple requests are made. Then we need to calculate the percetange. So the function + // below is used + req.sql.from('pnad_novo') + .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') + console.log(req.sql.toString()) + } + else { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + next(); +}, query, id2str.transform(false), response('pnad_novo')); + +module.exports = NivelInstrucao; diff --git a/src/libs/routes_v1/schoolProfessors.js b/src/libs/routes_v1/schoolProfessors.js new file mode 100644 index 0000000000000000000000000000000000000000..8e80d2d10d2af39758ddd8809ed42eedcc6aa40f --- /dev/null +++ b/src/libs/routes_v1/schoolProfessors.js @@ -0,0 +1,50 @@ +/* +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 SchoolProfessorsApp = 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(); + +SchoolProfessorsApp.use(cache('15 day')); + +SchoolProfessorsApp.get('/', (req, res, next) => { + req.sql.from('pnad_novo') + .field('sum(num_doc_ed_basica)', 'escola_agregada') + next(); +}, query, response('years')); + diff --git a/src/libs/routes_v1/studentCost.js b/src/libs/routes_v1/studentCost.js new file mode 100644 index 0000000000000000000000000000000000000000..fdcf7270790bfcf1c50d26386a76f3196ccc7aa9 --- /dev/null +++ b/src/libs/routes_v1/studentCost.js @@ -0,0 +1,130 @@ +/* +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 studentCostApp = 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 aggregateData = require(`${libs}/middlewares/aggregateData`); + +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(); + +studentCostApp.use(cache('15 day')); + +studentCostApp.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')); + +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' + } +}) + +studentCostApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if (req.query.dims && req.query.dims.includes('despesas')) { + let whereCondition = req.query.filter.includes("region") ? "" : "despesas.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 = `despesas.cod_ibge = ${filterId}` + } + }) + + 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') + } + + next(); +}, query, aggregateData, id2str.transform(false), response('student_cost')); + +module.exports = studentCostApp; diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js new file mode 100644 index 0000000000000000000000000000000000000000..b595fb0086846cef7c7b058abbc34ab23006ceca --- /dev/null +++ b/src/libs/routes_v1/studentRevenue.js @@ -0,0 +1,130 @@ +/* +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 studentRevenueApp = 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 aggregateData = require(`${libs}/middlewares/aggregateData`); + +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(); + +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') + 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' + } +}) + +studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if (req.query.dims && req.query.dims.includes('receitas')) { + 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') + .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') + } + + next(); +}, query, aggregateData, id2str.transform(false), response('student_revenue')); + +module.exports = studentRevenueApp; diff --git a/src/libs/routes_v1/studentsReceivingAidTax.js b/src/libs/routes_v1/studentsReceivingAidTax.js new file mode 100644 index 0000000000000000000000000000000000000000..c4509cf84057b5243e9e93f2ae7ccfc992e00a47 --- /dev/null +++ b/src/libs/routes_v1/studentsReceivingAidTax.js @@ -0,0 +1,619 @@ +/* +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 studentsReceivingAidTaxApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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(); + +studentsReceivingAidTaxApp.use(cache('15 day')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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: 10, name: id2str.incomeRange(10)}); + next(); +}, response('income_range')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.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')); + +studentsReceivingAidTaxApp.get('/age_range', (req, res, next) => { + req.result = [] + for (let i = 1; i < 12; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + + next(); +}, response('age_range')); + +studentsReceivingAidTaxApp.get('/location', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.location(i) + }); + } + + next(); +}, response('location')); + + +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: 'years_of_study', + table: 'pnad_novo', + tableField: 'anos_de_estudo', + resultField: 'years_of_study_id', + where: { + relation: '=', + type: 'integer', + field: 'anos_de_estudo' + } +}).addValue({ + name: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_de_instrucao' + } +}).addValue({ + name: 'new_pnad_adm_dependency', + table: 'pnad_novo', + tableField: 'dependencia_adm', + resultField: 'new_pnad_adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm' + } +}).addValue({ + name: 'attends_school', + table: 'pnad_novo', + tableField: 'frequenta_escola', + resultField: 'attends_school_id', + where: { + relation: '=', + type: 'integer', + field: 'frequenta_escola' + } +}).addValue({ + name: 'modality', + table: 'pnad_novo', + tableField: 'modalidade', + resultField: 'modality_id', + where: { + relation: '=', + type: 'integer', + field: 'modalidade' + } +}).addValue({ + name: 'attended_modality', + table: 'pnad_novo', + tableField: 'nivel_etapa_modalidade_freq', + resultField: 'attended_modality_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_etapa_modalidade_freq' + } +}).addValue({ + name: 'illiteracy', + table: 'pnad_novo', + tableField: 'analfabetismo', + resultField: 'illiteracy_id', + where: { + relation: '=', + type: 'integer', + field: 'analfabetismo' + } +}).addValue({ + name: 'modality_shift', + table: 'pnad_novo', + tableField: 'turno_nivel_etapa', + resultField: 'modality_shift_id', + where: { + relation: '=', + type: 'integer', + field: 'turno_nivel_etapa' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).addValue({ + name: 'income_range', + table: 'pnad_novo', + tableField: 'faixa_rendimento_aux_tx', + resultField: 'income_range_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_rendimento_aux_tx' + } +}).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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}).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' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +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.denominator = Number(newObj.denominator.toFixed(2)); + newObj.partial = objMatch.total; + newObj.partial = Number(newObj.total.toFixed(2)); + newObj.total = (objMatch.total / result.total) * 100; + newObj.total = Number(newObj.total.toFixed(2)); + match.push(newObj); + } + }); + + return match; +} + +studentsReceivingAidTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + req.querySet = []; + + // Subquery para total_pop_maior_25 com filtros dinâmicos + let totalEstudantesQueRecebemAuxilio = req.sql.clone(); + totalEstudantesQueRecebemAuxilio.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("recebeu_rendimentos_de_programa_bolsa_familia = 1") + .where("frequenta_escola = 1") + .where("ano_ref >= 2019") + .group("ano_ref") + .order("ano_ref"); + + + // Subquery para total_pop_maior_25 com filtros dinâmicos + let totalEstudantes = req.sql.clone(); + totalEstudantes.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("frequenta_escola = 1") + .group("ano_ref") + .order("ano_ref"); + + + req.querySet.push(totalEstudantesQueRecebemAuxilio); + req.querySet.push(totalEstudantes); + + 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; + next(); +}, id2str.transform(false), response('studentsReceivingAidTax')); + + +module.exports = studentsReceivingAidTaxApp; diff --git a/src/libs/routes_v1/superiorEducationConclusionTax.js b/src/libs/routes_v1/superiorEducationConclusionTax.js new file mode 100644 index 0000000000000000000000000000000000000000..1f84f7b071048a4a3170244131a66c2ba6174d78 --- /dev/null +++ b/src/libs/routes_v1/superiorEducationConclusionTax.js @@ -0,0 +1,621 @@ +/* +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 superiorEducationConclusionTaxApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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(); + +superiorEducationConclusionTaxApp.use(cache('15 day')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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: 10, name: id2str.incomeRange(10)}); + next(); +}, response('income_range')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.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')); + +superiorEducationConclusionTaxApp.get('/age_range', (req, res, next) => { + req.result = [] + for (let i = 1; i < 12; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + + next(); +}, response('age_range')); + +superiorEducationConclusionTaxApp.get('/location', (req, res, next) => { + req.result = [] + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.location(i) + }); + } + + next(); +}, response('location')); + + +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: 'years_of_study', + table: 'pnad_novo', + tableField: 'anos_de_estudo', + resultField: 'years_of_study_id', + where: { + relation: '=', + type: 'integer', + field: 'anos_de_estudo' + } +}).addValue({ + name: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_de_instrucao' + } +}).addValue({ + name: 'new_pnad_adm_dependency', + table: 'pnad_novo', + tableField: 'dependencia_adm', + resultField: 'new_pnad_adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm' + } +}).addValue({ + name: 'attends_school', + table: 'pnad_novo', + tableField: 'frequenta_escola', + resultField: 'attends_school_id', + where: { + relation: '=', + type: 'integer', + field: 'frequenta_escola' + } +}).addValue({ + name: 'modality', + table: 'pnad_novo', + tableField: 'modalidade', + resultField: 'modality_id', + where: { + relation: '=', + type: 'integer', + field: 'modalidade' + } +}).addValue({ + name: 'attended_modality', + table: 'pnad_novo', + tableField: 'nivel_etapa_modalidade_freq', + resultField: 'attended_modality_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_etapa_modalidade_freq' + } +}).addValue({ + name: 'illiteracy', + table: 'pnad_novo', + tableField: 'analfabetismo', + resultField: 'illiteracy_id', + where: { + relation: '=', + type: 'integer', + field: 'analfabetismo' + } +}).addValue({ + name: 'modality_shift', + table: 'pnad_novo', + tableField: 'turno_nivel_etapa', + resultField: 'modality_shift_id', + where: { + relation: '=', + type: 'integer', + field: 'turno_nivel_etapa' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).addValue({ + name: 'income_range', + table: 'pnad_novo', + tableField: 'faixa_rendimento_aux_tx', + resultField: 'income_range_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_rendimento_aux_tx' + } +}).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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}).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' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +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.partial = objMatch.total; + newObj.total = (objMatch.total / result.total) * 100; + match.push(newObj); + } + }); + + return match; +} + +superiorEducationConclusionTaxApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + req.querySet = []; + + // Subquery para total_pop_maior_25 com filtros dinâmicos + let totalPopMaior25ComSuperiorCompleto = req.sql.clone(); + totalPopMaior25ComSuperiorCompleto.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("idade_morador_31_03 >= 25") + .where("nivel_de_instruc_mais_elevad_para_o_fundam_com_duracao_9_anos = 7") + .where("ano_ref >= 2019") + .group("ano_ref") + .order("ano_ref"); + + + // Subquery para total_pop_maior_25 com filtros dinâmicos + let totalPopMaior25 = req.sql.clone(); + totalPopMaior25.from("pnad_novo") + .field("ano_ref", "year") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .where("idade_morador_31_03 >= 25") + .where("ano_ref >= 2019") + .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); + + 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; + next(); +}, id2str.transform(false), response('superiorEducationConclusionTax')); + + +module.exports = superiorEducationConclusionTaxApp; diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js new file mode 100644 index 0000000000000000000000000000000000000000..a87d833ada08033abb2c7925d7f0c0b8bf33e636 --- /dev/null +++ b/src/libs/routes_v1/yearsOfStudy.js @@ -0,0 +1,551 @@ +/* +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 PnadNovoApp = 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(); + +PnadNovoApp.use(cache('15 day')); + +PnadNovoApp.get('/years', (req, res, next) => { + req.sql.from('pnad_novo') + .field('DISTINCT pnad_novo.ano_ref', 'year') + next(); +}, query, response('years')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.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')); + +PnadNovoApp.get('/age_range_all', (req, res, next) => { + req.result = [] + for (let i = 1; i < 12; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAll(i) + }); + } + + next(); +}, response('age_range_all')); + + +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: 'location', + table: 'pnad_novo', + tableField: 'situacao_domicilio', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'situacao_domicilio' + } +}).addValue({ + name: 'instruction_level', + table: 'pnad_novo', + tableField: 'nivel_de_instrucao', + resultField: 'instruction_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_de_instrucao' + } +}).addValue({ + name: 'new_pnad_adm_dependency', + table: 'pnad_novo', + tableField: 'dependencia_adm', + resultField: 'new_pnad_adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm' + } +}).addValue({ + name: 'attends_school', + table: 'pnad_novo', + tableField: 'frequenta_escola', + resultField: 'attends_school_id', + where: { + relation: '=', + type: 'integer', + field: 'frequenta_escola' + } +}).addValue({ + name: 'modality', + table: 'pnad_novo', + tableField: 'modalidade', + resultField: 'modality_id', + where: { + relation: '=', + type: 'integer', + field: 'modalidade' + } +}).addValue({ + name: 'attended_modality', + table: 'pnad_novo', + tableField: 'nivel_etapa_modalidade_freq', + resultField: 'attended_modality_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_etapa_modalidade_freq' + } +}).addValue({ + name: 'illiteracy', + table: 'pnad_novo', + tableField: 'analfabetismo', + resultField: 'illiteracy_id', + where: { + relation: '=', + type: 'integer', + field: 'analfabetismo' + } +}).addValue({ + name: 'modality_shift', + table: 'pnad_novo', + tableField: 'turno_nivel_etapa', + resultField: 'modality_shift_id', + where: { + relation: '=', + type: 'integer', + field: 'turno_nivel_etapa' + } +}).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_all', + table: 'pnad_novo', + tableField: 'faixa_etaria', + resultField: 'age_range_all_id', + where: { + relation: '=', + type: 'integer', + field: 'faixa_etaria' + } +}).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: 'ano_ref', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_ref' + } +}).addField({ + name: 'search', + field: false, + where: true +}).addValueToField({ + name: 'name', + table: 'pnad_novo', + tableField: 'nome', + where: { + relation: 'LIKE', + type: 'string', + field: 'nome' + } +}, 'search').addValue({ + name: 'mesoregion', + table: 'pnad_novo', + tableField: 'mesorregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id' + } +}).addValue({ + name: 'microregion', + table: 'pnad_novo', + tableField: 'microrregiao_id', + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id' + } +}); + +PnadNovoApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + const dimensions = req.dims || {}; + + let joinQuery = ` + pnad_novo.ano_ref = media.ano_ref + ${dimensions.gender ? " AND media.gender_id = pnad_novo.sexo" : ""} + ${dimensions.bolsa_familia ? "AND media.bolsa_familia_id = pnad_novo.recebeu_rendimentos_de_programa_bolsa_familia" : ""} + ${dimensions.new_pnad_ethnic_group ? "AND media.new_pnad_ethnic_group_id = pnad_novo.cor_raca" : ""} + ${dimensions.income_range ? "AND media.income_range_id = pnad_novo.faixa_rendimento_aux_tx" : ""} + ${dimensions.age_range_all ? "AND media.age_range_all_id = pnad_novo.faixa_etaria" : ""} + ${dimensions.location ? "AND media.location_id = pnad_novo.situacao_domicilio" : ""} + ${dimensions.metro_code ? "AND media.metro_code_id = pnad_novo.cod_rm_ride" : ""} + ${dimensions.cap_code ? "AND media.cap_code_id = pnad_novo.cod_cap" : ""} + ${dimensions.region ? "AND media.region_id = pnad_novo.cod_regiao" : ""} + ${dimensions.state ? "AND media.state_id = pnad_novo.cod_uf" : ""} + ` + + let mean = req.sql.clone() + .from('pnad_novo') + .field('pnad_novo.ano_ref', 'ano_ref') + .field('SUM(pnad_novo.anos_de_estudo * pnad_novo.peso_domicilio_pessoas_com_cal) / SUM(pnad_novo.peso_domicilio_pessoas_com_cal)', 'media_anos_estudo') + .where('pnad_novo.anos_de_estudo <> 99 and pnad_novo.faixa_etaria >= 6 and pnad_novo.ano_ref >= 2019') + .group('pnad_novo.ano_ref') + + req.sql.from('pnad_novo') + .field('pnad_novo.ano_ref', 'year') + .field('media.media_anos_estudo', 'average') + .field('SQRT(SUM(POWER(pnad_novo.anos_de_estudo - media.media_anos_estudo, 2) * pnad_novo.peso_domicilio_pessoas_com_cal) / SUM(pnad_novo.peso_domicilio_pessoas_com_cal))', 'std_dev') + .join(mean, 'media', joinQuery) + .where('pnad_novo.ano_ref >= 2019 AND pnad_novo.faixa_etaria >= 6 AND pnad_novo.anos_de_estudo <> 99') + .group('pnad_novo.ano_ref') + .group('media.media_anos_estudo') + + next(); +}, query, id2str.transform(false), response('years_of_study')); + +module.exports = PnadNovoApp;