diff --git a/src/libs/convert/especialEducation.js b/src/libs/convert/especialEducation.js new file mode 100644 index 0000000000000000000000000000000000000000..4634978459f9e89bc473560a14119b0f196d946e --- /dev/null +++ b/src/libs/convert/especialEducation.js @@ -0,0 +1,31 @@ +/* +Copyright (C) 2022 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 especialEducation(id) { + switch (id) { + case 1: + return 'Inclusiva'; + case 2: + return 'Exclusiva'; + default: + return 'Não definido'; + } +}; + diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js index 265e973c57d344445df91a9f58afff880ce6a6a0..a721bb1b8d06adf4d48d6ff9251c7a2b0a0f0c9f 100644 --- a/src/libs/middlewares/aggregateData.js +++ b/src/libs/middlewares/aggregateData.js @@ -1,5 +1,12 @@ const id2str = require(`./id2str`); +/* + Middleware que tem como função formatar dados agregados. + Dados agregados não podem ser "puxados" do banco de dados da mesma maneira que os dados tradicioanis, dessa maneira é + necessário formatá-los para que sigam o padrão. +*/ + +// Faz o mapeamento dos filtros com seus respectivos "id2str" const convert = { "adm_dependency_detailed": "admDependencyPriv", "age_range": "ageRangeAggregate", @@ -7,22 +14,28 @@ const convert = { "ethnic_group": "ethnicGroup", "education_level_mod_agg": "educationLevelModAgg", 'integral_time_agg': 'integralTime', - 'period_agg': 'period' + 'period_agg': 'period', + 'modality_integral_time': 'educationLevelBasic', + 'especial_education': 'especialEducation' } function aggregateData(req, res, next) { const newResult = [] - const aggregateFields = ['gender', 'age_range', 'ethnic_group', 'education_level_mod_agg', 'integral_time_agg', 'period_agg'] + const aggregateFields = ['gender', 'age_range', 'ethnic_group', 'education_level_mod_agg', 'integral_time_agg', 'period_agg', 'modality_integral_time', 'especial_education'] let id; const fields = req.query.dims.split(','); let currentAggregateField; let currentNonAggregateField; + + // Verifica se o filtro passado está presente nos filtros agregados fields.forEach(field => {if (aggregateFields.includes(field)) currentAggregateField = field; else currentNonAggregateField = field}); + console.log(convert[currentAggregateField]) if (currentAggregateField) { - console.log(currentAggregateField); req.result.forEach((r) => { + // Alguns filtros começam com o id = 0 outros id = 1 id = ['ethnic_group', 'integral_time_agg'].includes(currentAggregateField) ? 0 : 1; for (const property in r) { + // Dados agregados são identificados com a substring "total_" em sua chave if (property.includes("total_")) { let data = { total: r[property], diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js index 0a48e4ce06fd9ea078b8756e8c199bc70963f538..06f24488923dba974e4b1fe5ad8eafe10a92d0a4 100644 --- a/src/libs/middlewares/id2str.js +++ b/src/libs/middlewares/id2str.js @@ -116,7 +116,7 @@ const metroCode = require(`${libs}/convert/metroCode`); const modalityShift = require(`${libs}/convert/modalityShift`); const incomeRange = require(`${libs}/convert/incomeRange`); const ageRangeAggregate = require(`${libs}/convert/ageRangeAggregate`); - +const especialEducation = require(`${libs}/convert/especialEducation`); const ids = { gender_id: gender, @@ -225,7 +225,8 @@ const ids = { region_id: regionCode, metro_code_id: metroCode, modality_shift_id: modalityShift, - income_range_id: incomeRange + income_range_id: incomeRange, + especial_education: especialEducation }; function transform(removeId=false) { @@ -356,5 +357,6 @@ module.exports = { regionCode, metroCode, modalityShift, - incomeRange + incomeRange, + especialEducation }; diff --git a/src/libs/routes_v1/enrollmentAggregate.js b/src/libs/routes_v1/enrollmentAggregate.js index feae26810174f762d9a497ff21e1bd20dfc532b5..e7de586c71cc895ea5fbe79c369b521c80e20893 100644 --- a/src/libs/routes_v1/enrollmentAggregate.js +++ b/src/libs/routes_v1/enrollmentAggregate.js @@ -49,6 +49,182 @@ enrollmentAggregateApp.use(cache('15 day')); let rqf = new ReqQueryFields(); +enrollmentAggregateApp.get('/years', (req, res, next) => { + req.sql.from('escola') + .field('DISTINCT escola.ano_censo', 'year') + .where('escola.ano_censo >= 2021') + next(); +}, query, response('years')); + +enrollmentAggregateApp.get('/adm_dependency', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 5; i++) { + req.result.push({ + id: i, name: id2str.admDependency(i) + }); + } + next(); +}, response('adm_dependency')); + +enrollmentAggregateApp.get('/adm_dependency_detailed', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 9; i++) { + req.result.push({ + id: i, name: id2str.admDependencyPriv(i) + }); + } + next(); +}, response('adm_dependency_detailed')); + +enrollmentAggregateApp.get('/education_level_mod', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 13; i++) { + req.result.push({ + id: i, name: id2str.educationLevelMod(i) + }); + } + next(); +}, response('education_level_mod')); + +enrollmentAggregateApp.get('/integral_time', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 3; i++) { + req.result.push({ + id: i, name: id2str.integralTime(i) + }); + } + next(); +}, response('education_level_mod')); + +enrollmentAggregateApp.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')); + +enrollmentAggregateApp.get('/diff_location', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 4; i++) { + req.result.push({ + id: i, name: id2str.diffLocation(i) + }); + } + + req.result.push({ + id: 8, name: 'Ãrea onde se localizam povos e comunidades tradicionais' + }) + + next(); +}, response('diff_location')); + +enrollmentAggregateApp.get('/modality_integral_time', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 7; i++) { + req.result.push({ + id: i, name: id2str.educationLevelBasic(i) + }); + } + + next(); +}, response('modality_integral_time')); + +enrollmentAggregateApp.get('/gender', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.gender(i) + }); + } + + next(); +}, response('gender')); + +enrollmentAggregateApp.get('/age_range', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 7; i++) { + req.result.push({ + id: i, name: id2str.ageRangeAggregate(i) + }); + } + + next(); +}, response('age_range')); + +enrollmentAggregateApp.get('/ethnic_group', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 6; i++) { + req.result.push({ + id: i, name: id2str.ethnicGroup(i) + }); + } + + next(); +}, response('ethnic_group')); + +enrollmentAggregateApp.get('/period_agg', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 5; i++) { + req.result.push({ + id: i, name: id2str.period(i) + }); + } + + next(); +}, response('period_agg')); + +enrollmentAggregateApp.get('/region', (req, res, next) => { + req.result = [] + + for (let i = 0; i <= 5; i++) { + req.result.push({ + id: i, name: id2str.regionCode(i) + }); + } + + next(); +}, response('period_agg')); + +enrollmentAggregateApp.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) + }); + } + } + + next(); +}, response('state')); + +enrollmentAggregateApp.get('/especial_education', (req, res, next) => { + req.result = [] + + for (let i = 1; i <= 2; i++) { + req.result.push({ + id: i, name: id2str.especialEducation(i) + }); + } + + next(); +}, response('especial_education')); + + rqf.addField({ name: 'filter', field: false, @@ -299,6 +475,16 @@ rqf.addField({ type: 'integer', field: 'localizacao_id' } +}).addValue({ + name: 'diff_location', + table: 'escola', + tableField: 'localizacao_diferenciada_par', + resultField: 'diff_location_id', + where: { + relation: '=', + type: 'integer', + field: 'localizacao_diferenciada_par' + } }); // Return all cities @@ -356,7 +542,6 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .order('escola.ano_censo'); } else if (req.query.dims && req.query.dims.includes('integral_time_agg')) { - req.sql.from('turma,escola') .field('SUM(CASE WHEN turma.tempo_integral=0 then turma.num_matricula ELSE 0 END)', 'total_int_nao') .field('SUM(CASE WHEN turma.tempo_integral=1 then turma.num_matricula ELSE 0 END)', 'total_int') @@ -367,7 +552,6 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .where('turma.tipo_atendimento_id <= 2 and turma.escola_id = escola.id and turma.ano_censo = escola.ano_censo'); } else if (req.query.dims && req.query.dims.includes('period_agg')) { - req.sql.from('turma,escola') .field('SUM(CASE WHEN turma.turma_turno_id=1 then turma.num_matricula ELSE 0 END)', 'total_qt_mat_bas_matutino') .field('SUM(CASE WHEN turma.turma_turno_id=2 then turma.num_matricula ELSE 0 END)', 'total_qt_mat_bas_vespertino') @@ -380,7 +564,6 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .where('turma.tipo_atendimento_id <= 2 and turma.escola_id = escola.id and turma.ano_censo = escola.ano_censo'); } else if (req.query.dims && req.query.dims.includes('modality_integral_time')) { - req.sql.from('escola') .field('SUM(escola.qt_mat_inf_cre_int)', 'total_qt_mat_inf_cre_int') .field('SUM(escola.qt_mat_inf_pre_int)', 'total_qt_mat_inf_pre_int') @@ -391,6 +574,14 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .group('escola.ano_censo') .order('escola.ano_censo') } + else if (req.query.dims && req.query.dims.includes('especial_education')) { + req.sql.from('escola') + .field('SUM(escola.qt_mat_esp_cc)', 'total_qt_mat_esp_cc') + .field('SUM(escola.qt_mat_esp_ce)', 'total_qt_mat_esp_ce') + .field('escola.ano_censo', 'year') + .group('escola.ano_censo') + .order('escola.ano_censo') + } else { req.sql.from('escola') .field('SUM(escola.qt_mat_bas)', 'total') @@ -399,6 +590,6 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .order('escola.ano_censo'); } next(); -}, query, aggregateData, response('enrollment_aggregate')); +}, query, aggregateData, id2str.transform(false), response('enrollment_aggregate')); module.exports = enrollmentAggregateApp;