From a9d8d026b543918fefdf2bb06178cad11fb3cc02 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 7 Nov 2024 11:21:49 -0300
Subject: [PATCH 01/36] =?UTF-8?q?feat:=20add=20indicador=20Taxa=20de=20con?=
 =?UTF-8?q?clus=C3=A3o=20da=20Educa=C3=A7=C3=A3o=20B=C3=A1sica?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/libs/routes_v1/api.js                     |   4 +-
 .../routes_v1/basicEducationConclusion.js     | 600 ++++++++++++++++++
 2 files changed, 603 insertions(+), 1 deletion(-)
 create mode 100644 src/libs/routes_v1/basicEducationConclusion.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index bffc8b5d..12d89282 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -157,6 +157,8 @@ const email = require(`${libs}/routes_v1/email`);
 
 const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`)
 
+const basicEducationConclusion = require(`${libs}/routes_v1/basicEducationConclusion`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -223,7 +225,7 @@ api.use('/employeesAggregate', employeesAggregate);
 api.use('/course_aggregate', courseAggregate);
 api.use('/federativeEntity', federativeEntity);
 api.use('/uni_offer_aggregate', uniLocalOfferAggregate);
-
+api.use('/basic_education_conclusion', basicEducationConclusion);
 
 //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 00000000..d9ca2b3a
--- /dev/null
+++ b/src/libs/routes_v1/basicEducationConclusion.js
@@ -0,0 +1,600 @@
+/*
+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')
+    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: 9, name: id2str.incomeRange(9)});
+    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'));
+
+
+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;
+            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")
+        .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
+        .where("idade_morador_31_03 >= 19")
+        .where("ano_ref >= 2019")
+        .group("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")
+        .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");
+
+    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;
-- 
GitLab


From d96901756d585f4c7107150e53585280b014060a Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Fri, 8 Nov 2024 11:54:37 -0300
Subject: [PATCH 02/36] feat: fix year range problem, add location options get

---
 src/libs/routes_v1/basicEducationConclusion.js | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/libs/routes_v1/basicEducationConclusion.js b/src/libs/routes_v1/basicEducationConclusion.js
index d9ca2b3a..44436759 100644
--- a/src/libs/routes_v1/basicEducationConclusion.js
+++ b/src/libs/routes_v1/basicEducationConclusion.js
@@ -47,6 +47,7 @@ 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'));
 
@@ -252,6 +253,17 @@ basicEducationConclusion.get('/age_range_all', (req, res, next) => {
     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',
@@ -569,7 +581,7 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) =>
     // Subquery para total_pop_maior_19 com filtros dinâmicos
     let totalPopMaior19 = req.sql.clone();
         totalPopMaior19.from("pnad_novo")
-        .field("ano_ref")
+        .field("ano_ref", "year")
         .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
         .where("idade_morador_31_03 >= 19")
         .where("ano_ref >= 2019")
@@ -578,7 +590,7 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) =>
     // Subquery para total_pop_ed_bas_maior_19 com filtros dinâmicos
     let totalPopEdBasMaior19 = req.sql.clone();
         totalPopEdBasMaior19.from("pnad_novo")
-        .field("ano_ref")
+        .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)")
-- 
GitLab


From 3fb81f8a65223a045fafca92667d6b3bfb83d06b Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 11 Nov 2024 10:28:45 -0300
Subject: [PATCH 03/36] fix: sort subqueries by year

---
 src/libs/routes_v1/basicEducationConclusion.js | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/libs/routes_v1/basicEducationConclusion.js b/src/libs/routes_v1/basicEducationConclusion.js
index 44436759..c45cb6e5 100644
--- a/src/libs/routes_v1/basicEducationConclusion.js
+++ b/src/libs/routes_v1/basicEducationConclusion.js
@@ -585,7 +585,8 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) =>
         .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
         .where("idade_morador_31_03 >= 19")
         .where("ano_ref >= 2019")
-        .group("ano_ref");
+        .group("ano_ref")
+        .order("ano_ref");
 
     // Subquery para total_pop_ed_bas_maior_19 com filtros dinâmicos
     let totalPopEdBasMaior19 = req.sql.clone();
@@ -595,7 +596,8 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) =>
         .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");
+        .group("ano_ref")
+        .order("ano_ref");
 
     req.querySet.push(totalPopEdBasMaior19);
     req.querySet.push(totalPopMaior19);
-- 
GitLab


From dd8b92a0a2623ee0f571c30a1c0ea8d26ff4e660 Mon Sep 17 00:00:00 2001
From: fgs21 <fgs21@inf.ufpr.br>
Date: Tue, 12 Nov 2024 09:51:13 -0300
Subject: [PATCH 04/36] [ADD] First base route created

---
 src/libs/routes_v1/api.js          |   4 +-
 src/libs/routes_v1/yearsOfStudy.js | 536 +++++++++++++++++++++++++++++
 2 files changed, 539 insertions(+), 1 deletion(-)
 create mode 100644 src/libs/routes_v1/yearsOfStudy.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index bffc8b5d..c1a480d7 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -157,6 +157,8 @@ const email = require(`${libs}/routes_v1/email`);
 
 const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`)
 
+const yearsOfStudy = require(`${libs}/routes_v1/yearsOfStudy`)
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -223,7 +225,7 @@ 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)
 
 //Publication 
 api.use('/publication', publication);
diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
new file mode 100644
index 00000000..cc76ca34
--- /dev/null
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -0,0 +1,536 @@
+/*
+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: '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: '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'
+    }
+});
+
+PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+    
+    let mean = squel.select()
+    .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', 'mean')
+    .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', 'pnad_novo.ano_ref = media.ano_ref')
+    .where('pnad_novo.ano_ref >= 2019')
+    .group('pnad_novo.ano_ref')
+    .group('media.media_anos_estudo')
+    console.log(req.sql.toString())
+    next();
+}, query, id2str.transform(false), response('years_of_study'));
+
+module.exports = PnadNovoApp;
-- 
GitLab


From 8e64602da3cecaf18f9fd745013fa4f00a3adf49 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 14 Nov 2024 09:50:54 -0300
Subject: [PATCH 05/36] feat: faixa de renda showing right results

---
 src/libs/convert/incomeRange.js                |  2 ++
 src/libs/routes_v1/basicEducationConclusion.js | 12 +++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/libs/convert/incomeRange.js b/src/libs/convert/incomeRange.js
index fee5189b..a38446aa 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/routes_v1/basicEducationConclusion.js b/src/libs/routes_v1/basicEducationConclusion.js
index c45cb6e5..fb745299 100644
--- a/src/libs/routes_v1/basicEducationConclusion.js
+++ b/src/libs/routes_v1/basicEducationConclusion.js
@@ -159,7 +159,7 @@ basicEducationConclusion.get('/income_range', (req, res, next) => {
             id: i, name: id2str.incomeRange(i)
         });
     }
-    req.result.push({id: 9, name: id2str.incomeRange(9)});
+    req.result.push({id: 10, name: id2str.incomeRange(10)});
     next();
 }, response('income_range'));
 
@@ -426,12 +426,12 @@ rqf.addField({
 }).addValue({
     name: 'income_range',
     table: 'pnad_novo',
-    tableField: 'faixa_rendimento_aux',
+    tableField: 'faixa_rendimento_aux_tx',
     resultField: 'income_range_id',
     where: {
         relation: '=',
         type: 'integer',
-        field: 'faixa_rendimento_aux'
+        field: 'faixa_rendimento_aux_tx'
     }
 }).addValue({
     name: 'gender',
@@ -599,8 +599,14 @@ basicEducationConclusion.get('/', rqf.parse(), rqf.build(), (req, res, next) =>
         .group("ano_ref")
         .order("ano_ref");
 
+    if (req.query.dims && req.query.dims.includes("income_range")) {
+        totalPopMaior19.where("faixa_rendimento_aux_tx is not null");
+        totalPopEdBasMaior19.where("faixa_rendimento_aux_tx is not null");
+    }
+
     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
-- 
GitLab


From 12097c5b7e2666e89cda933401b187d0a0d38750 Mon Sep 17 00:00:00 2001
From: tgcl21 <tgcl21@inf.ufpr.br>
Date: Thu, 14 Nov 2024 10:18:21 -0300
Subject: [PATCH 06/36] [ADD] Instruction level route

---
 docker-compose.yml                     |  11 ++
 entrypoint.sh                          |   5 +
 gulpfile.babel.js                      |  22 +--
 gulpfile.template.js                   |  20 +++
 src/libs/routes_v1/aggregateClass.js   |  50 +++++++
 src/libs/routes_v1/api.js              |   6 +
 src/libs/routes_v1/city.js             |   1 +
 src/libs/routes_v1/instructionLevel.js | 177 +++++++++++++++++++++++++
 8 files changed, 277 insertions(+), 15 deletions(-)
 create mode 100644 docker-compose.yml
 create mode 100644 entrypoint.sh
 create mode 100644 gulpfile.template.js
 create mode 100644 src/libs/routes_v1/aggregateClass.js
 create mode 100644 src/libs/routes_v1/instructionLevel.js

diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 00000000..85266ff9
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,11 @@
+services:
+  simcaq-node:
+    container_name: simcaq-node
+    build: .
+    ports:
+      - '3000:3000'
+    develop:
+      watch:
+        - action: sync
+          path: .
+          target: /API
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100644
index 00000000..04db5192
--- /dev/null
+++ b/entrypoint.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+echo "Starting simcaq-node"
+gulp watch &> /dev/null &
+cd build
+NODE_ENV=production gulp run
diff --git a/gulpfile.babel.js b/gulpfile.babel.js
index c661d5af..1d0724e1 100644
--- a/gulpfile.babel.js
+++ b/gulpfile.babel.js
@@ -43,12 +43,12 @@ gulp.task('compile', ['lint'], () => {
     gulp.src('src/**/*.js')
     .pipe(cache.filter())       // cache source files
     .pipe(babel())      // compile only modified files
-    // .pipe(cache.cache())        // cache compiled files
+    .pipe(cache.cache())        // cache compiled files
     .pipe(gulp.dest('build'));  // move compiled files to build directory
 });
 
 gulp.task('build', ['compile'], () => {
-    var filesToCopy = [ 'config.json', 'package.json' ];
+    var filesToCopy = [ 'config.json', 'package.json', '.eslintignore', '.eslintrc.json' ];
     // copy configuration file to build directory
     gulp.src(filesToCopy)
     .pipe(gulp.dest('build'));
@@ -93,24 +93,16 @@ gulp.task('test', ['pre-test'], () => {
     });
 });
 
-gulp.task('watch', ['compile'], () => {
+gulp.task('watch', () => {
     console.log('Watching source directory for changes');
-    gulp.watch('src/**/*.js').on('change', () => {
+    return gulp.watch('src/**/*.js').on('change', () => {
         console.log('Recompiling source');
         gulp.start('compile');
         console.log('Source recompilation done');
     });
 });
 
-gulp.task('run', () => {
-    process.chdir('build');
-    nodemon({
-        script: 'server.js',
-        tasks: ['watch'],
-        ignore: ["test/test.js", "gulpfile.babel.js"],
-        ext: 'js html json',
-        env: { 'NODE_ENV': process.env.NODE_ENV }
-    });
+gulp.task('default', () => {
+    console.log("Não execuatar apenas gulp, execute da forma:");
+    console.log("\t\tgulp <task>");
 });
-
-gulp.task('default', ['run']);
diff --git a/gulpfile.template.js b/gulpfile.template.js
new file mode 100644
index 00000000..3de679df
--- /dev/null
+++ b/gulpfile.template.js
@@ -0,0 +1,20 @@
+const gulp = require('gulp');
+
+const nodemon = require('gulp-nodemon');
+
+gulp.task('run', () => {
+    // process.chdir('build');
+    nodemon({
+        script: 'server.js',
+        // tasks: ['watch'],
+        ignore: ["test/test.js", "gulpfile.babel.js"],
+        ext: 'js html json',
+        env: { 'NODE_ENV': process.env.NODE_ENV }
+    });
+});
+
+gulp.task('default', () => {
+    console.log("Não execuatar apenas gulp, execute da forma:");
+    console.log("\t\tgulp <task>");
+});
+
diff --git a/src/libs/routes_v1/aggregateClass.js b/src/libs/routes_v1/aggregateClass.js
new file mode 100644
index 00000000..8a186bda
--- /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 53f9e4df..dc011610 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -145,6 +145,10 @@ const newPnad = require(`${libs}/routes_v1/newPnad`);
 
 const email = require(`${libs}/routes_v1/email`);
 
+const aggregateClass = require(`${libs}/routes_v1/aggregateClass`);
+
+const instructionLevel = require(`${libs}/routes_v1/instructionLevel`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -205,6 +209,8 @@ 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)
 
 //Publication 
 api.use('/publication', publication);
diff --git a/src/libs/routes_v1/city.js b/src/libs/routes_v1/city.js
index fc697a9f..26eabd0f 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/instructionLevel.js b/src/libs/routes_v1/instructionLevel.js
new file mode 100644
index 00000000..2e2b0847
--- /dev/null
+++ b/src/libs/routes_v1/instructionLevel.js
@@ -0,0 +1,177 @@
+/*
+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'));
+
+
+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: 'locality_area',
+    table: 'pnad_novo',
+    tableField: 'tipo_de_area',
+    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: 'uf',
+    table: 'pnad_novo',
+    tableField: 'cod_uf',
+    resultField: 'cod_uf_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_uf'
+    }
+})
+
+NivelInstrucao.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+    req.sql.from('pnad_novo')
+    .field('nivel_de_instrucao', 'nivel')
+    .field('sum(peso_domicilio_pessoas_com_cal)', 'total')
+    .field('ano_ref', 'ano')
+    .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())
+    next();
+}, query, id2str.transform(false), response('pnad_novo'));
+
+module.exports = NivelInstrucao;
-- 
GitLab


From 0b487d8c73548fb0ed92b42db99d4f2545647846 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 18 Nov 2024 11:16:23 -0300
Subject: [PATCH 07/36] feat: iliteracy rate

---
 src/libs/routes_v1/api.js           |   3 +
 src/libs/routes_v1/iliteracyRate.js | 613 ++++++++++++++++++++++++++++
 2 files changed, 616 insertions(+)
 create mode 100644 src/libs/routes_v1/iliteracyRate.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index 12d89282..51edf1ba 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -159,6 +159,8 @@ const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate
 
 const basicEducationConclusion = require(`${libs}/routes_v1/basicEducationConclusion`);
 
+const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -226,6 +228,7 @@ api.use('/course_aggregate', courseAggregate);
 api.use('/federativeEntity', federativeEntity);
 api.use('/uni_offer_aggregate', uniLocalOfferAggregate);
 api.use('/basic_education_conclusion', basicEducationConclusion);
+api.use('/iliteracy_rate', iliteracyRate);
 
 //Publication 
 api.use('/publication', publication);
diff --git a/src/libs/routes_v1/iliteracyRate.js b/src/libs/routes_v1/iliteracyRate.js
new file mode 100644
index 00000000..468e18e2
--- /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 < 8; i++) {
+        req.result.push({
+            id: i, name: id2str.incomeRange(i)
+        });
+    }
+    req.result.push({id: 10, name: id2str.incomeRange(10)});
+    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 = 1; 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_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;
+}
+
+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;
-- 
GitLab


From beb4d51e0c546f9db663d48491f8c891490c81ad Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 18 Nov 2024 12:03:02 -0300
Subject: [PATCH 08/36] fix: age range all options

---
 src/libs/routes_v1/iliteracyRate.js | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/libs/routes_v1/iliteracyRate.js b/src/libs/routes_v1/iliteracyRate.js
index 468e18e2..25b97d5d 100644
--- a/src/libs/routes_v1/iliteracyRate.js
+++ b/src/libs/routes_v1/iliteracyRate.js
@@ -154,12 +154,11 @@ iliteracyRate.get('/attended_modality', (req, res, next) => {
 
 iliteracyRate.get('/income_range', (req, res, next) => {
     req.result = []
-    for (let i = 1; i < 8; i++) {
+    for (let i = 1; i < 10; i++) {
         req.result.push({
             id: i, name: id2str.incomeRange(i)
         });
     }
-    req.result.push({id: 10, name: id2str.incomeRange(10)});
     next();
 }, response('income_range'));
 
@@ -244,7 +243,7 @@ iliteracyRate.get('/state', (req, res, next) => {
 
 iliteracyRate.get('/age_range_all', (req, res, next) => {
     req.result = []
-    for (let i = 1; i < 12; i++) {
+    for (let i = 5; i < 12; i++) {
         req.result.push({
             id: i, name: id2str.ageRangeAll(i)
         });
@@ -426,12 +425,12 @@ rqf.addField({
 }).addValue({
     name: 'income_range',
     table: 'pnad_novo',
-    tableField: 'faixa_rendimento_aux_tx',
+    tableField: 'faixa_rendimento_aux',
     resultField: 'income_range_id',
     where: {
         relation: '=',
         type: 'integer',
-        field: 'faixa_rendimento_aux_tx'
+        field: 'faixa_rendimento_aux'
     }
 }).addValue({
     name: 'gender',
@@ -568,6 +567,7 @@ function matchQueries(queryPartial, queryTotal) {
             newObj.denominator = result.total;
             newObj.partial = objMatch.total;
             newObj.total = (objMatch.total / result.total) * 100;
+            newObj.total = newObj.total.toFixed(1);
             match.push(newObj);
         }
     });
-- 
GitLab


From 6764b0a5d6d8100fb24b0cf1d6ddee9119219364 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 21 Nov 2024 12:20:39 -0300
Subject: [PATCH 09/36] feat: adjusted liquid frequency indicator

---
 src/libs/routes_v1/adjustedLiquidFrequency.js | 336 ++++++++++++++++++
 src/libs/routes_v1/api.js                     |   3 +
 2 files changed, 339 insertions(+)
 create mode 100644 src/libs/routes_v1/adjustedLiquidFrequency.js

diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js
new file mode 100644
index 00000000..b90443ba
--- /dev/null
+++ b/src/libs/routes_v1/adjustedLiquidFrequency.js
@@ -0,0 +1,336 @@
+/*
+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('/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: '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: '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: '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'
+    }
+});
+
+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;
+}
+
+
+adjustedLiquidFrequency.get('/', rqf.parse(), rqf.build(), (req, res, next) => {    
+    let totalPop = req.sql.clone();
+    totalPop.field("ano_ref")
+    .field("faixa_etaria")
+    .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
+    .from("pnad_novo")
+    .group("ano_ref")
+    .group("faixa_etaria");
+
+    // Aplicando filtros dinâmicos à subquery total_pop
+    totalPop = rqf.buildQuery(req, totalPop);
+
+    // Subquery: total_apoio_freq
+    let totalApoioFreq = req.sql.clone();
+        totalApoioFreq.field("pnad_novo.ano_ref")
+        .field("pnad_novo.apoio_frequencia_ajustada")
+        .field("total_pop.total")
+        .field("SUM(pnad_novo.peso_domicilio_pessoas_com_cal)", "total_freq")
+        .from("pnad_novo")
+        .join(
+            totalPop, 
+            "total_pop", 
+            "total_pop.ano_ref = pnad_novo.ano_ref AND total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada"
+        )
+        .group("pnad_novo.ano_ref")
+        .group("pnad_novo.apoio_frequencia_ajustada")
+        .group("total_pop.total");
+
+    req.sql.field("ano_ref")
+        .field("apoio_frequencia_ajustada")
+        .field("round((total_freq / total) * 100,1)", "total")
+        .from(totalApoioFreq, "total_apoio_freq")
+        .where("ano_ref >= 2019")
+        .order("ano_ref")
+        .order("apoio_frequencia_ajustada");
+
+        next();
+}, query, id2str.transform(false), response('adjusted_liquid_frequency'));
+
+module.exports = adjustedLiquidFrequency;
diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index 12d89282..6b6e1c99 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -159,6 +159,8 @@ const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate
 
 const basicEducationConclusion = require(`${libs}/routes_v1/basicEducationConclusion`);
 
+const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequency`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -226,6 +228,7 @@ api.use('/course_aggregate', courseAggregate);
 api.use('/federativeEntity', federativeEntity);
 api.use('/uni_offer_aggregate', uniLocalOfferAggregate);
 api.use('/basic_education_conclusion', basicEducationConclusion);
+api.use('/adjusted_liquid_frequency', adjustedLiquidFrequency);
 
 //Publication 
 api.use('/publication', publication);
-- 
GitLab


From 39a20296e86f523e659596c8da66cd89a7b99432 Mon Sep 17 00:00:00 2001
From: edsaibert <es23@inf.ufpr.br>
Date: Tue, 26 Nov 2024 11:44:55 -0300
Subject: [PATCH 10/36] add indicador taxa educacao conclusao educacao superior

---
 src/libs/routes_v1/api.js                     |  12 +
 .../superiorEducationConclusionTax.js         | 621 ++++++++++++++++++
 2 files changed, 633 insertions(+)
 create mode 100644 src/libs/routes_v1/superiorEducationConclusionTax.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index ba036951..e12b4f56 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -149,8 +149,16 @@ const enrollmentAggregate = require(`${libs}/routes_v1/enrollmentAggregate`);
 
 const employeesAggregate = require(`${libs}/routes_v1/employeesAggregate`);
 
+const courseAggregate = require(`${libs}/routes_v1/courseAggregate`);
+
+const federativeEntity = require(`${libs}/routes_v1/federativeEntity`);
+
 const email = require(`${libs}/routes_v1/email`);
 
+const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`)
+
+const superiorEducationConclusionTax = require(`${libs}/routes_v1/superiorEducationConclusionTax`)
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -214,6 +222,10 @@ api.use('/new_pnad', newPnad);
 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('/superior_education_conclusion_tax', superiorEducationConclusionTax)
 
 
 //Publication 
diff --git a/src/libs/routes_v1/superiorEducationConclusionTax.js b/src/libs/routes_v1/superiorEducationConclusionTax.js
new file mode 100644
index 00000000..1f84f7b0
--- /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;
-- 
GitLab


From ba15c353eec46eed5a6314d158bfb1812f71d2b2 Mon Sep 17 00:00:00 2001
From: tgcl21 <tgcl21@inf.ufpr.br>
Date: Wed, 27 Nov 2024 09:49:04 -0300
Subject: [PATCH 11/36] =?UTF-8?q?[ADD]=20nivel=20de=20instru=C3=A7=C3=A3o?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/libs/middlewares/id2str.js         |   1 +
 src/libs/routes_v1/instructionLevel.js | 101 +++++-
 src/libs/routes_v1/rateSchoolNew.js    | 411 +++++++++++++++++++++++++
 3 files changed, 500 insertions(+), 13 deletions(-)
 create mode 100644 src/libs/routes_v1/rateSchoolNew.js

diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js
index 02273a8b..77706527 100644
--- a/src/libs/middlewares/id2str.js
+++ b/src/libs/middlewares/id2str.js
@@ -230,6 +230,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;
diff --git a/src/libs/routes_v1/instructionLevel.js b/src/libs/routes_v1/instructionLevel.js
index 2e2b0847..c8b27886 100644
--- a/src/libs/routes_v1/instructionLevel.js
+++ b/src/libs/routes_v1/instructionLevel.js
@@ -42,6 +42,24 @@ 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'));
 
 rqf.addField({
     name: 'filter',
@@ -61,9 +79,10 @@ rqf.addField({
         field: 'id'
     }
 }).addValue({
-    name: 'locality_area',
+    name: 'location',
     table: 'pnad_novo',
     tableField: 'tipo_de_area',
+    resultField: 'location_id',
     where: {
         relation: '=',
         type: 'integer',
@@ -150,27 +169,83 @@ rqf.addField({
         field: 'sexo'
     }
 }).addValue({
-    name: 'uf',
+    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: 'cod_uf',
-    resultField: 'cod_uf_id',
+    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'
+    }
 })
 
 NivelInstrucao.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
-    req.sql.from('pnad_novo')
-    .field('nivel_de_instrucao', 'nivel')
-    .field('sum(peso_domicilio_pessoas_com_cal)', 'total')
-    .field('ano_ref', 'ano')
-    .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())
+    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'));
 
diff --git a/src/libs/routes_v1/rateSchoolNew.js b/src/libs/routes_v1/rateSchoolNew.js
new file mode 100644
index 00000000..237edfa1
--- /dev/null
+++ b/src/libs/routes_v1/rateSchoolNew.js
@@ -0,0 +1,411 @@
+/*
+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 rateSchoolNewApp = express.Router();
+
+const libs = `${process.cwd()}/libs`;
+
+const query = require(`${libs}/middlewares/query`).query;
+
+const response = require(`${libs}/middlewares/response`);
+
+const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
+
+const multiQuery = require(`${libs}/middlewares/multiQuery`);
+
+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();
+
+rateSchoolNewApp.use(cache('15 day'));
+
+rateSchoolNewApp.get('/years', (req, res, next) => {
+    req.sql.from('pnad_novo')
+    .field('DISTINCT pnad_novo.ano_ref', 'year')
+    .where('pnad_novo.ano_ref> 2018')
+    next();
+}, query, response('years'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.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'));
+
+
+rateSchoolNewApp.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'));
+
+rateSchoolNewApp.get('/age_range', (req, res, next) => {
+    req.result = [
+        {id: 1, name: '0 a 3 anos'},
+        {id: 2, name: '4 a 5 anos'},
+        {id: 3, name: '6 a 10 anos'},
+        {id: 4, name: '11 a 14 anos'},
+        {id: 5, name: '15 a 17 anos'},
+        {id: 6, name: '18 a 24 anos'}
+    ];
+    next();
+}, response('age_range'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValue({
+    name: 'id',
+    table: 'pnad_novo',
+    tableField: 'id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    }
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: ['id', 'nome'],
+    resultField: ['state_id', 'state_nome'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id',
+    },
+    join: {
+        primary: 'id',
+        foreign: 'cod_uf',
+        foreignTable: 'pnad_novo'
+    }
+}).addValue({
+    name: 'state_not',
+    table: 'estado',
+    tableField: ['nome', 'id'],
+    resultField: ['state_name', 'state_id'],
+    where: {
+        relation: '<>',
+        type: 'integer',
+        field: 'cod_uf',
+        table: 'pnad_novo'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'cod_uf',
+        foreignTable: 'pnad_novo'
+    }
+}).addValue({
+    name: 'bolsa_familia',
+    table: 'pnad_novo',
+    tableField: 'recebeu_rendimentos_de_programa_bolsa_familia',
+    resultField: 'bolsa_familia_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'recebeu_rendimentos_de_programa_bolsa_familia'
+    }
+}).addValue({
+    name: 'new_pnad_ethnic_group',
+    table: 'pnad_novo',
+    tableField: 'cor_raca',
+    resultField: 'new_pnad_ethnic_group_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cor_raca'
+    }
+}).addValue({
+    name: 'age_range',
+    table: 'pnad_novo',
+    tableField: 'faixa_etaria',
+    resultField: 'age_range_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: 'cap_code',
+    table: 'pnad_novo',
+    tableField: 'cod_cap',
+    resultField: 'cap_code_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_cap'
+    }
+}).addValue({
+    name: 'region',
+    table: 'pnad_novo',
+    tableField: 'cod_regiao',
+    resultField: 'region_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_regiao'
+    }
+}).addValue({
+    name: 'metro_code',
+    table: 'pnad_novo',
+    tableField: 'cod_rm_ride',
+    resultField: 'metro_code_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_rm_ride'
+    }
+}).addValue({
+    name: 'min_year',
+    table: 'pnad_novo',
+    tableField: 'ano_ref',
+    resultField: 'year',
+    where: {
+        relation: '>=',
+        type: 'integer',
+        field: 'ano_ref'
+    }
+}).addValue({
+    name: 'max_year',
+    table: 'pnad_novo',
+    tableField: '',
+    resultField: 'year',
+    where: {
+        relation: '<=',
+        type: 'integer',
+        field: 'ano_ref'
+    }
+}).addValue({
+    name: 'income_range',
+    table: 'pnad_novo',
+    tableField: 'faixa_rendimento_aux',
+    resultField: 'income_range_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'faixa_rendimento_aux'
+    }
+});
+
+/* 
+    The queries are matched and the total is calculated.
+    This function is necessary to match the results of the
+    2 queries with the right pair. In some queries, the result
+    doesn't exist in one of the 2, so, we need to run this algorithm
+    to match them right.
+*/
+
+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;
+}
+
+rateSchoolNewApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+    
+    if ("age_range" in req.filter || "age_range" in req.dims) {
+        // As we will need to do two requests, they'll be stored in a list
+        req.querySet = []
+        
+        // Create an object that will store the first request (the sum of all people that attend school)
+        // and are below a certain age (in this case, 24 yeas)
+        let attends_school = req.sql.clone();
+        attends_school.from('pnad_novo')
+        .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total')
+        .field('pnad_novo.faixa_etaria')
+        .field('pnad_novo.ano_ref', 'year')
+        .where('pnad_novo.ano_ref >= 2019 AND frequenta_escola = 1')
+        .where('pnad_novo.faixa_etaria < 7')
+        .group('pnad_novo.ano_ref')
+        .group('pnad_novo.faixa_etaria')
+        .order('pnad_novo.ano_ref')
+        .order('pnad_novo.faixa_etaria')
+        req.querySet.push(attends_school);
+
+        // Then, the second object is created and stores the sum of all people below a certain age (24 years)
+        let full_population = req.sql.clone();
+        full_population.from('pnad_novo')
+        .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total')
+        .field('pnad_novo.faixa_etaria')
+        .field('pnad_novo.ano_ref', 'year')
+        .where('pnad_novo.ano_ref >= 2019')
+        .where('pnad_novo.faixa_etaria < 7')
+        .group('pnad_novo.ano_ref')
+        .group('pnad_novo.faixa_etaria')
+        .order('pnad_novo.ano_ref')
+        .order('pnad_novo.faixa_etaria')
+        req.querySet.push(full_population);
+    }
+
+    next();
+}, multiQuery, (req, res, next) => {
+    if ("age_range" in req.filter || "age_range" in req.dims) {
+        // The multiple requests are made. Then we need to calculate the percetange. So the function
+        // below is used
+        let newObj = matchQueries(req.result[0], req.result[1]);
+        req.result = newObj;
+    } else {
+        res.status(400);
+        next({
+            status: 400,
+            message: 'Wrong/No filter specified'
+        });
+    }
+    next();
+}, id2str.transform(false), response('rateSchoolNew'));
+
+module.exports = rateSchoolNewApp;
-- 
GitLab


From b672b91b97119617985462401522663b7a91fa4f Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Wed, 11 Dec 2024 11:00:14 -0300
Subject: [PATCH 12/36] maybe working

---
 src/libs/routes_v1/adjustedLiquidFrequency.js | 54 ++++++++++++-------
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js
index b90443ba..a682dac2 100644
--- a/src/libs/routes_v1/adjustedLiquidFrequency.js
+++ b/src/libs/routes_v1/adjustedLiquidFrequency.js
@@ -182,6 +182,16 @@ rqf.addField({
         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',
@@ -294,23 +304,24 @@ function matchQueries(queryPartial, queryTotal) {
 }
 
 
-adjustedLiquidFrequency.get('/', rqf.parse(), rqf.build(), (req, res, next) => {    
-    let totalPop = req.sql.clone();
-    totalPop.field("ano_ref")
-    .field("faixa_etaria")
-    .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
-    .from("pnad_novo")
-    .group("ano_ref")
-    .group("faixa_etaria");
+adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, 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")
+        .group("ano_ref")
+        .group("faixa_etaria");
 
     // Aplicando filtros dinâmicos à subquery total_pop
     totalPop = rqf.buildQuery(req, totalPop);
 
     // Subquery: total_apoio_freq
-    let totalApoioFreq = req.sql.clone();
-        totalApoioFreq.field("pnad_novo.ano_ref")
-        .field("pnad_novo.apoio_frequencia_ajustada")
-        .field("total_pop.total")
+    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(
@@ -322,15 +333,20 @@ adjustedLiquidFrequency.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
         .group("pnad_novo.apoio_frequencia_ajustada")
         .group("total_pop.total");
 
-    req.sql.field("ano_ref")
-        .field("apoio_frequencia_ajustada")
-        .field("round((total_freq / total) * 100,1)", "total")
+    // Aplicando filtros à subquery total_apoio_freq
+    totalApoioFreq = rqf.buildQuery(req, totalApoioFreq);
+
+    // Query principal
+    req.sql = squel.select()
+        .field("total_apoio_freq.ano_ref")
+        .field("total_apoio_freq.apoio_frequencia_ajustada")
+        .field("ROUND((total_apoio_freq.total_freq / total_apoio_freq.total) * 100, 1)", "total")
         .from(totalApoioFreq, "total_apoio_freq")
-        .where("ano_ref >= 2019")
-        .order("ano_ref")
-        .order("apoio_frequencia_ajustada");
+        .where("total_apoio_freq.ano_ref >= 2019") // Corrigido para usar alias correto
+        .order("total_apoio_freq.ano_ref")
+        .order("total_apoio_freq.apoio_frequencia_ajustada");
 
-        next();
+    next();
 }, query, id2str.transform(false), response('adjusted_liquid_frequency'));
 
 module.exports = adjustedLiquidFrequency;
-- 
GitLab


From cadf6ea36719ef98afbc5a1137672e2920506c26 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 12 Dec 2024 11:11:01 -0300
Subject: [PATCH 13/36] Fix api

---
 src/libs/routes_v1/api.js | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index 72fc3ddc..296c69fc 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -163,8 +163,6 @@ const aggregateClass = require(`${libs}/routes_v1/aggregateClass`);
 
 const instructionLevel = require(`${libs}/routes_v1/instructionLevel`);
 
-const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`)
-
 const basicEducationConclusion = require(`${libs}/routes_v1/basicEducationConclusion`);
 
 const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`);
-- 
GitLab


From 5ee3f0ce2177cfb9d44a810f12bde738b49ba7f6 Mon Sep 17 00:00:00 2001
From: edsaibert <es23@inf.ufpr.br>
Date: Wed, 15 Jan 2025 09:35:16 -0300
Subject: [PATCH 14/36] students receiving aid tax

---
 src/libs/routes_v1/api.js                     |   3 +
 src/libs/routes_v1/studentsReceivingAidTax.js | 616 ++++++++++++++++++
 2 files changed, 619 insertions(+)
 create mode 100644 src/libs/routes_v1/studentsReceivingAidTax.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index bffc8b5d..88249dae 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -157,6 +157,8 @@ const email = require(`${libs}/routes_v1/email`);
 
 const uniLocalOfferAggregate = require(`${libs}/routes_v1/uniLocalOfferAggregate`)
 
+const studentsReceivingAidTax = require(`${libs}/routes_v1/studentsReceivingAidTax`)
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -223,6 +225,7 @@ api.use('/employeesAggregate', employeesAggregate);
 api.use('/course_aggregate', courseAggregate);
 api.use('/federativeEntity', federativeEntity);
 api.use('/uni_offer_aggregate', uniLocalOfferAggregate);
+api.use('/students_receiving_aid_tax', studentsReceivingAidTax);
 
 
 //Publication 
diff --git a/src/libs/routes_v1/studentsReceivingAidTax.js b/src/libs/routes_v1/studentsReceivingAidTax.js
new file mode 100644
index 00000000..3603888e
--- /dev/null
+++ b/src/libs/routes_v1/studentsReceivingAidTax.js
@@ -0,0 +1,616 @@
+/*
+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.partial = objMatch.total;
+            newObj.total = (objMatch.total / result.total) * 100;
+            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;
-- 
GitLab


From 6490dffba23c2b91aed1744be1ff45e8b2a499f7 Mon Sep 17 00:00:00 2001
From: edsaibert <es23@inf.ufpr.br>
Date: Wed, 15 Jan 2025 11:35:28 -0300
Subject: [PATCH 15/36] rounding match queries

---
 docker-compose.yml                            | 11 ++++++++++
 entrypoint.sh                                 |  5 +++++
 gulpfile.babel.js                             | 22 ++++++-------------
 gulpfile.template.js                          | 20 +++++++++++++++++
 src/libs/routes_v1/studentsReceivingAidTax.js |  3 +++
 5 files changed, 46 insertions(+), 15 deletions(-)
 create mode 100644 docker-compose.yml
 create mode 100644 entrypoint.sh
 create mode 100644 gulpfile.template.js

diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 00000000..85266ff9
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,11 @@
+services:
+  simcaq-node:
+    container_name: simcaq-node
+    build: .
+    ports:
+      - '3000:3000'
+    develop:
+      watch:
+        - action: sync
+          path: .
+          target: /API
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100644
index 00000000..04db5192
--- /dev/null
+++ b/entrypoint.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+echo "Starting simcaq-node"
+gulp watch &> /dev/null &
+cd build
+NODE_ENV=production gulp run
diff --git a/gulpfile.babel.js b/gulpfile.babel.js
index c661d5af..1d0724e1 100644
--- a/gulpfile.babel.js
+++ b/gulpfile.babel.js
@@ -43,12 +43,12 @@ gulp.task('compile', ['lint'], () => {
     gulp.src('src/**/*.js')
     .pipe(cache.filter())       // cache source files
     .pipe(babel())      // compile only modified files
-    // .pipe(cache.cache())        // cache compiled files
+    .pipe(cache.cache())        // cache compiled files
     .pipe(gulp.dest('build'));  // move compiled files to build directory
 });
 
 gulp.task('build', ['compile'], () => {
-    var filesToCopy = [ 'config.json', 'package.json' ];
+    var filesToCopy = [ 'config.json', 'package.json', '.eslintignore', '.eslintrc.json' ];
     // copy configuration file to build directory
     gulp.src(filesToCopy)
     .pipe(gulp.dest('build'));
@@ -93,24 +93,16 @@ gulp.task('test', ['pre-test'], () => {
     });
 });
 
-gulp.task('watch', ['compile'], () => {
+gulp.task('watch', () => {
     console.log('Watching source directory for changes');
-    gulp.watch('src/**/*.js').on('change', () => {
+    return gulp.watch('src/**/*.js').on('change', () => {
         console.log('Recompiling source');
         gulp.start('compile');
         console.log('Source recompilation done');
     });
 });
 
-gulp.task('run', () => {
-    process.chdir('build');
-    nodemon({
-        script: 'server.js',
-        tasks: ['watch'],
-        ignore: ["test/test.js", "gulpfile.babel.js"],
-        ext: 'js html json',
-        env: { 'NODE_ENV': process.env.NODE_ENV }
-    });
+gulp.task('default', () => {
+    console.log("Não execuatar apenas gulp, execute da forma:");
+    console.log("\t\tgulp <task>");
 });
-
-gulp.task('default', ['run']);
diff --git a/gulpfile.template.js b/gulpfile.template.js
new file mode 100644
index 00000000..3de679df
--- /dev/null
+++ b/gulpfile.template.js
@@ -0,0 +1,20 @@
+const gulp = require('gulp');
+
+const nodemon = require('gulp-nodemon');
+
+gulp.task('run', () => {
+    // process.chdir('build');
+    nodemon({
+        script: 'server.js',
+        // tasks: ['watch'],
+        ignore: ["test/test.js", "gulpfile.babel.js"],
+        ext: 'js html json',
+        env: { 'NODE_ENV': process.env.NODE_ENV }
+    });
+});
+
+gulp.task('default', () => {
+    console.log("Não execuatar apenas gulp, execute da forma:");
+    console.log("\t\tgulp <task>");
+});
+
diff --git a/src/libs/routes_v1/studentsReceivingAidTax.js b/src/libs/routes_v1/studentsReceivingAidTax.js
index 3603888e..c4509cf8 100644
--- a/src/libs/routes_v1/studentsReceivingAidTax.js
+++ b/src/libs/routes_v1/studentsReceivingAidTax.js
@@ -566,8 +566,11 @@ function matchQueries(queryPartial, queryTotal) {
 
         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);
         }
     });
-- 
GitLab


From aad36ec1943204fa5b98953f25c7d45ad3ab72cf Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 16 Jan 2025 11:25:50 -0300
Subject: [PATCH 16/36] feat: dimensions for all necessary data

---
 src/libs/routes_v1/adjustedLiquidFrequency.js | 109 +++++++-----------
 1 file changed, 44 insertions(+), 65 deletions(-)

diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js
index a682dac2..bb02e81e 100644
--- a/src/libs/routes_v1/adjustedLiquidFrequency.js
+++ b/src/libs/routes_v1/adjustedLiquidFrequency.js
@@ -142,16 +142,6 @@ rqf.addField({
         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: 'bolsa_familia',
     table: 'pnad_novo',
@@ -265,57 +255,32 @@ rqf.addField({
     }
 });
 
-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;
-}
-
-
 adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => {    
+    const dimensions = req.dims || {}; // Obter dimensões solicitadas pela requisição
+
+    
     // Subquery: total_pop
     let totalPop = squel.select()
-        .field("ano_ref")
-        .field("faixa_etaria")
-        .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
-        .from("pnad_novo")
-        .group("ano_ref")
-        .group("faixa_etaria");
-
-    // Aplicando filtros dinâmicos à subquery total_pop
+    .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()
@@ -326,26 +291,40 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => {
         .from("pnad_novo")
         .join(
             totalPop, 
-            "total_pop", 
-            "total_pop.ano_ref = pnad_novo.ano_ref AND total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada"
+            "total_pop",
+            joinQuery 
         )
         .group("pnad_novo.ano_ref")
         .group("pnad_novo.apoio_frequencia_ajustada")
         .group("total_pop.total");
 
-    // Aplicando filtros à subquery total_apoio_freq
+    // Aplicar filtros dinâmicos à subquery total_apoio_freq
     totalApoioFreq = rqf.buildQuery(req, totalApoioFreq);
 
-    // Query principal
-    req.sql = squel.select()
+    // Query Principal
+    let mainQuery = squel.select()
+        .from(totalApoioFreq, "total_apoio_freq")
         .field("total_apoio_freq.ano_ref")
-        .field("total_apoio_freq.apoio_frequencia_ajustada")
+        .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")
-        .from(totalApoioFreq, "total_apoio_freq")
-        .where("total_apoio_freq.ano_ref >= 2019") // Corrigido para usar alias correto
+        .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'));
 
-- 
GitLab


From 3b74666e40b75651c2d8fcf36f780c8178a2d2a1 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Fri, 17 Jan 2025 11:09:38 -0300
Subject: [PATCH 17/36] feat: require mandatory field to be on dimensions

---
 src/libs/routes_v1/adjustedLiquidFrequency.js | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js
index bb02e81e..5f2ce8e8 100644
--- a/src/libs/routes_v1/adjustedLiquidFrequency.js
+++ b/src/libs/routes_v1/adjustedLiquidFrequency.js
@@ -83,6 +83,16 @@ adjustedLiquidFrequency.get('/new_pnad_ethnic_group', (req, res, next) => {
     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++) {
@@ -258,6 +268,10 @@ rqf.addField({
 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()
-- 
GitLab


From 761be77cf8529c0f2aab7866487a0a770d1f1bc8 Mon Sep 17 00:00:00 2001
From: Fernando Gbur <fgs21@inf.ufpr.br>
Date: Mon, 20 Jan 2025 10:12:03 -0300
Subject: [PATCH 18/36] [ADD] Trying to make the querry work right

---
 src/libs/routes_v1/yearsOfStudy.js | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index cc76ca34..7cfde4d6 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -519,17 +519,16 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     .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')
-    
+    .group('pnad_novo.ano_ref');
+
     req.sql.from('pnad_novo')
     .field('pnad_novo.ano_ref', 'year')
     .field('media.media_anos_estudo', 'mean')
     .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', 'pnad_novo.ano_ref = media.ano_ref')
     .where('pnad_novo.ano_ref >= 2019')
-    .group('pnad_novo.ano_ref')
-    .group('media.media_anos_estudo')
-    console.log(req.sql.toString())
+    .group('pnad_novo.ano_ref');
+
     next();
 }, query, id2str.transform(false), response('years_of_study'));
 
-- 
GitLab


From 27e73cf423fedefdf54cbf3aac26e99cceff3fb4 Mon Sep 17 00:00:00 2001
From: Fernando Gbur <fgs21@inf.ufpr.br>
Date: Mon, 20 Jan 2025 10:29:11 -0300
Subject: [PATCH 19/36] [ADD] Adding baloolan's years of study file

---
 src/libs/routes_v1/yearsOfStudy.js | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index 7cfde4d6..573cd83c 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -514,21 +514,22 @@ rqf.addField({
 
 PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     
-    let mean = squel.select()
+    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');
-
+    .group('pnad_novo.ano_ref')
+    
     req.sql.from('pnad_novo')
     .field('pnad_novo.ano_ref', 'year')
     .field('media.media_anos_estudo', 'mean')
-    .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')
+    .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', 'pnad_novo.ano_ref = media.ano_ref')
-    .where('pnad_novo.ano_ref >= 2019')
-    .group('pnad_novo.ano_ref');
-
+    .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')
+    console.log(req.sql.toString())
     next();
 }, query, id2str.transform(false), response('years_of_study'));
 
-- 
GitLab


From 80d1964c6e774c04ac59e949c4b09fe7a182143e Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 20 Jan 2025 11:40:48 -0300
Subject: [PATCH 20/36] Fix: ano_ref as year on query

---
 src/libs/routes_v1/adjustedLiquidFrequency.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js
index 5f2ce8e8..5b79740d 100644
--- a/src/libs/routes_v1/adjustedLiquidFrequency.js
+++ b/src/libs/routes_v1/adjustedLiquidFrequency.js
@@ -318,7 +318,7 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => {
     // Query Principal
     let mainQuery = squel.select()
         .from(totalApoioFreq, "total_apoio_freq")
-        .field("total_apoio_freq.ano_ref")
+        .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")
-- 
GitLab


From 10d97e1cccdca0d05b943a8805bba354703c5ea8 Mon Sep 17 00:00:00 2001
From: Fernando Gbur dos Santos <fernando_gbur@hotmail.com>
Date: Wed, 22 Jan 2025 11:17:52 -0300
Subject: [PATCH 21/36] [ADD] Trying to fix the join problem

---
 src/libs/routes_v1/yearsOfStudy.js | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index cc76ca34..f3cf6ec3 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -300,14 +300,14 @@ rqf.addField({
         foreignTable: 'pnad_novo'
     }
 }).addValue({
-    name: 'years_of_study',
+    name: 'location',
     table: 'pnad_novo',
-    tableField: 'anos_de_estudo',
-    resultField: 'years_of_study_id',
+    tableField: 'situacao_domicilio',
+    resultField: 'location_id',
     where: {
         relation: '=',
         type: 'integer',
-        field: 'anos_de_estudo'
+        field: 'situacao_domicilio'
     }
 }).addValue({
     name: 'instruction_level',
@@ -378,7 +378,7 @@ rqf.addField({
         relation: '=',
         type: 'integer',
         field: 'turno_nivel_etapa'
-    }
+    }               
 }).addValue({
     name: 'bolsa_familia',
     table: 'pnad_novo',
@@ -482,7 +482,7 @@ rqf.addField({
 }).addField({
     name: 'search',
     field: false,
-    where: true
+    where: true 
 }).addValueToField({
     name: 'name',
     table: 'pnad_novo',
@@ -514,6 +514,15 @@ rqf.addField({
 
 PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     
+    let teste = "pnad_novo.ano_ref = media.ano_ref"
+    if (req.dims) {
+        for (i in req.dims) {
+            teste += ` and pnad_novo.${i.table} = media.${i.tableField}`
+        }
+    }
+
+    console.log(teste)
+    
     let mean = squel.select()
     .from('pnad_novo')
     .field('pnad_novo.ano_ref', 'ano_ref')
@@ -525,7 +534,7 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     .field('pnad_novo.ano_ref', 'year')
     .field('media.media_anos_estudo', 'mean')
     .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', 'pnad_novo.ano_ref = media.ano_ref')
+    .join(mean, 'media', teste)
     .where('pnad_novo.ano_ref >= 2019')
     .group('pnad_novo.ano_ref')
     .group('media.media_anos_estudo')
-- 
GitLab


From a0c46dd2e4c38adb97dfe0e10fcb10dd58cc3c3f Mon Sep 17 00:00:00 2001
From: Fernando Gbur dos Santos <fernando_gbur@hotmail.com>
Date: Wed, 22 Jan 2025 11:41:55 -0300
Subject: [PATCH 22/36] [ADD] Adding the i variable

---
 src/libs/routes_v1/yearsOfStudy.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index d3dc226a..d2f71a31 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -516,7 +516,7 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     
     let teste = "pnad_novo.ano_ref = media.ano_ref"
     if (req.dims) {
-        for (i in req.dims) {
+        for (let i in req.dims) {
             teste += ` and pnad_novo.${i.table} = media.${i.tableField}`
         }
     }
-- 
GitLab


From b0297a2b56d1c912e1669cb64864cf8ad1c4539e Mon Sep 17 00:00:00 2001
From: Fernando Gbur dos Santos <fernando_gbur@hotmail.com>
Date: Wed, 22 Jan 2025 11:54:38 -0300
Subject: [PATCH 23/36] [TEST] Still trying to make the query work

---
 src/libs/routes_v1/yearsOfStudy.js | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index d2f71a31..ad83b9ec 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -515,14 +515,13 @@ rqf.addField({
 PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     
     let teste = "pnad_novo.ano_ref = media.ano_ref"
+    console.log(req.dims)
     if (req.dims) {
         for (let i in req.dims) {
             teste += ` and pnad_novo.${i.table} = media.${i.tableField}`
         }
     }
 
-    console.log(teste)
-
     let mean = req.sql.clone()
     .from('pnad_novo')
     .field('pnad_novo.ano_ref', 'ano_ref')
-- 
GitLab


From 5266041461fd19b923d3df08e814c863edc9d866 Mon Sep 17 00:00:00 2001
From: Fernando Gbur dos Santos <fernando_gbur@hotmail.com>
Date: Wed, 22 Jan 2025 12:03:54 -0300
Subject: [PATCH 24/36] [TEST] Trying to get the correct variable

---
 src/libs/routes_v1/yearsOfStudy.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index ad83b9ec..bdb95066 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -518,7 +518,7 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     console.log(req.dims)
     if (req.dims) {
         for (let i in req.dims) {
-            teste += ` and pnad_novo.${i.table} = media.${i.tableField}`
+            teste += ` and pnad_novo.${rqf.f`${i}`.table} = media.${rqf.f`${i}`.tableField}`
         }
     }
 
-- 
GitLab


From 8182ab5fdf70443da4ea0467cf5d1dc418178bea Mon Sep 17 00:00:00 2001
From: Fernando Gbur dos Santos <fernando_gbur@hotmail.com>
Date: Wed, 22 Jan 2025 12:13:06 -0300
Subject: [PATCH 25/36] [TEST] Trying again

---
 src/libs/routes_v1/yearsOfStudy.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index bdb95066..dc8f11c9 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -518,7 +518,7 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     console.log(req.dims)
     if (req.dims) {
         for (let i in req.dims) {
-            teste += ` and pnad_novo.${rqf.f`${i}`.table} = media.${rqf.f`${i}`.tableField}`
+            teste += ` and pnad_novo.${rqf[i].table} = media.${rqf[i].tableField}`
         }
     }
 
-- 
GitLab


From fad04d90b0070e5050321193e58714e6f2c5df7b Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Tue, 28 Jan 2025 11:28:37 -0300
Subject: [PATCH 26/36] feat: all dimensions working

---
 src/libs/routes_v1/yearsOfStudy.js | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index dc8f11c9..0a84f206 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -472,7 +472,7 @@ rqf.addField({
 }).addValue({
     name: 'max_year',
     table: 'pnad_novo',
-    tableField: '',
+    tableField: 'ano_ref',
     resultField: 'year',
     where: {
         relation: '<=',
@@ -513,14 +513,21 @@ rqf.addField({
 });
 
 PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+    const dimensions = req.dims || {};
     
-    let teste = "pnad_novo.ano_ref = media.ano_ref"
-    console.log(req.dims)
-    if (req.dims) {
-        for (let i in req.dims) {
-            teste += ` and pnad_novo.${rqf[i].table} = media.${rqf[i].tableField}`
-        }
-    }
+    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')
@@ -528,16 +535,16 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
     .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', 'mean')
     .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', teste)
+    .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')
-    console.log(req.sql.toString())
+
     next();
 }, query, id2str.transform(false), response('years_of_study'));
 
-- 
GitLab


From 00b7dbc2f326c93a010776fda2e1837e1396e030 Mon Sep 17 00:00:00 2001
From: ems19 <ems19@inf.ufpr.br>
Date: Fri, 14 Feb 2025 10:00:03 -0300
Subject: [PATCH 27/36] [FIX] Unused and wrong files removed

---
 .gitignore                             |  4 +++
 docker-compose.yml                     | 11 ------
 entrypoint.sh                          |  5 ---
 gulpfile.babel.js                      | 23 ++++++++----
 gulpfile.template.js                   | 20 -----------
 src/libs/routes_v1/schoolProfessors.js | 50 ++++++++++++++++++++++++++
 6 files changed, 70 insertions(+), 43 deletions(-)
 delete mode 100644 docker-compose.yml
 delete mode 100644 entrypoint.sh
 delete mode 100644 gulpfile.template.js
 create mode 100644 src/libs/routes_v1/schoolProfessors.js

diff --git a/.gitignore b/.gitignore
index 2d98beea..2fa21185 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/docker-compose.yml b/docker-compose.yml
deleted file mode 100644
index 85266ff9..00000000
--- a/docker-compose.yml
+++ /dev/null
@@ -1,11 +0,0 @@
-services:
-  simcaq-node:
-    container_name: simcaq-node
-    build: .
-    ports:
-      - '3000:3000'
-    develop:
-      watch:
-        - action: sync
-          path: .
-          target: /API
diff --git a/entrypoint.sh b/entrypoint.sh
deleted file mode 100644
index 04db5192..00000000
--- a/entrypoint.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/bin/bash
-echo "Starting simcaq-node"
-gulp watch &> /dev/null &
-cd build
-NODE_ENV=production gulp run
diff --git a/gulpfile.babel.js b/gulpfile.babel.js
index 1d0724e1..7c8b7418 100644
--- a/gulpfile.babel.js
+++ b/gulpfile.babel.js
@@ -43,12 +43,12 @@ gulp.task('compile', ['lint'], () => {
     gulp.src('src/**/*.js')
     .pipe(cache.filter())       // cache source files
     .pipe(babel())      // compile only modified files
-    .pipe(cache.cache())        // cache compiled files
+    // .pipe(cache.cache())        // cache compiled files
     .pipe(gulp.dest('build'));  // move compiled files to build directory
 });
 
 gulp.task('build', ['compile'], () => {
-    var filesToCopy = [ 'config.json', 'package.json', '.eslintignore', '.eslintrc.json' ];
+    var filesToCopy = [ 'config.json', 'package.json' ];
     // copy configuration file to build directory
     gulp.src(filesToCopy)
     .pipe(gulp.dest('build'));
@@ -93,16 +93,25 @@ gulp.task('test', ['pre-test'], () => {
     });
 });
 
-gulp.task('watch', () => {
+gulp.task('watch', ['compile'], () => {
     console.log('Watching source directory for changes');
-    return gulp.watch('src/**/*.js').on('change', () => {
+    gulp.watch('src/**/*.js').on('change', () => {
         console.log('Recompiling source');
         gulp.start('compile');
         console.log('Source recompilation done');
     });
 });
 
-gulp.task('default', () => {
-    console.log("Não execuatar apenas gulp, execute da forma:");
-    console.log("\t\tgulp <task>");
+gulp.task('run', () => {
+    process.chdir('build');
+    nodemon({
+        script: 'server.js',
+        tasks: ['watch'],
+        ignore: ["test/test.js", "gulpfile.babel.js"],
+        ext: 'js html json',
+        env: { 'NODE_ENV': process.env.NODE_ENV }
+    });
 });
+
+gulp.task('default', ['run']);
+
diff --git a/gulpfile.template.js b/gulpfile.template.js
deleted file mode 100644
index 3de679df..00000000
--- a/gulpfile.template.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const gulp = require('gulp');
-
-const nodemon = require('gulp-nodemon');
-
-gulp.task('run', () => {
-    // process.chdir('build');
-    nodemon({
-        script: 'server.js',
-        // tasks: ['watch'],
-        ignore: ["test/test.js", "gulpfile.babel.js"],
-        ext: 'js html json',
-        env: { 'NODE_ENV': process.env.NODE_ENV }
-    });
-});
-
-gulp.task('default', () => {
-    console.log("Não execuatar apenas gulp, execute da forma:");
-    console.log("\t\tgulp <task>");
-});
-
diff --git a/src/libs/routes_v1/schoolProfessors.js b/src/libs/routes_v1/schoolProfessors.js
new file mode 100644
index 00000000..8e80d2d1
--- /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'));
+
-- 
GitLab


From 3abfa1b80cff5086e65cc490d8b1b70937a51d2e Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 17 Feb 2025 11:19:28 -0300
Subject: [PATCH 28/36] feat: add student revenue API endpoint

---
 src/libs/routes_v1/api.js            |   3 +
 src/libs/routes_v1/studentRevenue.js | 132 +++++++++++++++++++++++++++
 2 files changed, 135 insertions(+)
 create mode 100644 src/libs/routes_v1/studentRevenue.js

diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index 9bbd5e93..b9af6769 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -173,6 +173,8 @@ const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequen
 
 const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`);
 
+const studentRevenue = require(`${libs}/routes_v1/studentRevenue`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
 });
@@ -247,6 +249,7 @@ 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', studentRevenue);
 
 //Publication 
 api.use('/publication', publication);
diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js
new file mode 100644
index 00000000..ecc79844
--- /dev/null
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -0,0 +1,132 @@
+/*
+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 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('pnad_novo')
+    .field('DISTINCT receitas.ano', 'year')
+    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: 'city',
+    table: 'receitas',
+    tableField: 'cod_ibge',
+    resultField: 'city',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_ibge'
+    }
+})
+
+studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+
+    if (req.query.dims && req.query.dims.includes('receitas')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('receitas.receitas', 'total_receitas')
+    }
+    if (req.query.dims && req.query.dims.includes('matriculas_publicas')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('matriculas_publica', 'matriculas_publica_total')
+    }
+    if (req.query.dims && req.query.dims.includes('matriculas_publicas_mais_conveniada')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('matriculas_publicas_mais_conveniada', 'matriculas_publicas_mais_conveniada_total')
+    }
+    if (req.query.dims && req.query.dims.includes('receita_aluno_ano_publica')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('receuta_aluno_ano_publica', 'receita_aluno_ano_publica_total')
+    }
+    if (req.query.dims && req.query.dims.includes('receita_aluno_mes_publica')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('receita_aluno_mes_publica', 'receita_aluno_mes_publica_total')
+    }
+    if (req.query.dims && req.query.dims.includes('receita_aluno_ano_publica_mais_conveniada')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('receita_aluno_ano_publica_mais_conveniada', 'receita_aluno_ano_publica_mais_conveniada_total')
+    }
+    if (req.query.dims && req.query.dims.includes('receita_aluno_mes_publica_mais_conveniada')) {
+        req.sql.from('receitas')
+        .field('receitas.ano', 'year')
+        .field('receita_aluno_mes_publica_mais_conveniada', 'receita_aluno_mes_publica_mais_conveniada_total')
+    }
+
+    next();
+}, query, id2str.transform(false), response('student_revenue'));
+
+module.exports = studentRevenueApp;
-- 
GitLab


From 28d0694690a5ff8c396cb9c8006ee42b8fe91bb5 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Wed, 19 Feb 2025 11:52:00 -0300
Subject: [PATCH 29/36] feat: implement revenue conversion and update student
 revenue route

---
 src/libs/convert/revenue.js           | 38 +++++++++++++++++++++++++
 src/libs/middlewares/aggregateData.js |  7 +++--
 src/libs/middlewares/id2str.js        |  7 +++--
 src/libs/routes_v1/studentRevenue.js  | 40 ++++++---------------------
 src/libs/routes_v1/yearsOfStudy.js    |  2 +-
 5 files changed, 58 insertions(+), 36 deletions(-)
 create mode 100644 src/libs/convert/revenue.js

diff --git a/src/libs/convert/revenue.js b/src/libs/convert/revenue.js
new file mode 100644
index 00000000..c4f5865a
--- /dev/null
+++ b/src/libs/convert/revenue.js
@@ -0,0 +1,38 @@
+/*
+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 'Receitas';
+        case 2:
+        return 'Matrículas Públicas';
+        case 3:
+        return 'Matrículas Públicas Mais Conveniada';
+        case 4:
+        return 'Receita Aluno Ano Pública'
+        case 5:
+        return 'Receita Aluno Mês Pública';
+        case 6:
+        return 'Receita Aluno Ano Pública Mais Conveniada';
+        case 7:
+        return 'Receita Aluno Mês Pública Mais Conveniada';
+    }
+};
diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js
index eed373dd..adf21fba 100644
--- a/src/libs/middlewares/aggregateData.js
+++ b/src/libs/middlewares/aggregateData.js
@@ -35,7 +35,8 @@ const convert = {
     contract_type_entity: 'contractType',
     total_doc: "totalDoc",
     education_degree_entity: "educationDegreeEntity",
-    government_agreement: "governmentAgreement"
+    government_agreement: "governmentAgreement",
+    receitas: "revenue"
 }
 
 function aggregateData(req, res, next) {
@@ -62,7 +63,8 @@ function aggregateData(req, res, next) {
             'age_range_entity',
             'post_graduation_entity',
             'contract_type_entity',
-            'education_degree_entity'
+            'education_degree_entity',
+            'receitas'
         ]
         let id;
         const fields = req.query.dims.split(',');
@@ -78,6 +80,7 @@ function aggregateData(req, res, next) {
                 for (const property in r) {
                     // Dados agregados são identificados com a substring 'total_' em sua chave
                     if (property.includes('total_')) {
+                        console.log(property);
                         let data = {
                             total: r[property],
                             year: r.year,
diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js
index d6c763c9..60ff424f 100644
--- a/src/libs/middlewares/id2str.js
+++ b/src/libs/middlewares/id2str.js
@@ -126,6 +126,7 @@ 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 ids = {
     gender_id: gender,
@@ -244,7 +245,8 @@ const ids = {
     post_graduation_entity: postGraduationEntity,
     special_education_entity: specialEducationEntity,
     total_doc: totalDoc,
-    education_degree_entity: educationDegreeEntity
+    education_degree_entity: educationDegreeEntity,
+    receitas_id: revenue
 };
 
 function transform(removeId=false) {
@@ -386,5 +388,6 @@ module.exports = {
     postGraduationEntity,
     specialEducationEntity,
     totalDoc,
-    educationDegreeEntity
+    educationDegreeEntity,
+    revenue
 };
diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js
index ecc79844..93ae6997 100644
--- a/src/libs/routes_v1/studentRevenue.js
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -32,6 +32,8 @@ 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`); 
@@ -94,39 +96,15 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
         req.sql.from('receitas')
         .field('receitas.ano', 'year')
         .field('receitas.receitas', 'total_receitas')
-    }
-    if (req.query.dims && req.query.dims.includes('matriculas_publicas')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('matriculas_publica', 'matriculas_publica_total')
-    }
-    if (req.query.dims && req.query.dims.includes('matriculas_publicas_mais_conveniada')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('matriculas_publicas_mais_conveniada', 'matriculas_publicas_mais_conveniada_total')
-    }
-    if (req.query.dims && req.query.dims.includes('receita_aluno_ano_publica')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('receuta_aluno_ano_publica', 'receita_aluno_ano_publica_total')
-    }
-    if (req.query.dims && req.query.dims.includes('receita_aluno_mes_publica')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('receita_aluno_mes_publica', 'receita_aluno_mes_publica_total')
-    }
-    if (req.query.dims && req.query.dims.includes('receita_aluno_ano_publica_mais_conveniada')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('receita_aluno_ano_publica_mais_conveniada', 'receita_aluno_ano_publica_mais_conveniada_total')
-    }
-    if (req.query.dims && req.query.dims.includes('receita_aluno_mes_publica_mais_conveniada')) {
-        req.sql.from('receitas')
-        .field('receitas.ano', 'year')
-        .field('receita_aluno_mes_publica_mais_conveniada', 'receita_aluno_mes_publica_mais_conveniada_total')
+        .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')
     }
 
     next();
-}, query, id2str.transform(false), response('student_revenue'));
+}, query, aggregateData, id2str.transform(false), response('student_revenue'));
 
 module.exports = studentRevenueApp;
diff --git a/src/libs/routes_v1/yearsOfStudy.js b/src/libs/routes_v1/yearsOfStudy.js
index 0a84f206..a87d833a 100644
--- a/src/libs/routes_v1/yearsOfStudy.js
+++ b/src/libs/routes_v1/yearsOfStudy.js
@@ -538,7 +538,7 @@ PnadNovoApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
 
     req.sql.from('pnad_novo')
     .field('pnad_novo.ano_ref', 'year')
-    .field('media.media_anos_estudo', 'mean')
+    .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')
-- 
GitLab


From ee0fd06ac54ca24c712cedc5ce2109601ac9296f Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 24 Feb 2025 11:32:22 -0300
Subject: [PATCH 30/36] feat: update revenue conversion logic and enhance
 student revenue API with new filters

---
 src/libs/convert/revenue.js          | 14 ++++++++------
 src/libs/routes_v1/studentRevenue.js | 26 +++++++++++++++-----------
 2 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/src/libs/convert/revenue.js b/src/libs/convert/revenue.js
index c4f5865a..9278e280 100644
--- a/src/libs/convert/revenue.js
+++ b/src/libs/convert/revenue.js
@@ -21,18 +21,20 @@ along with simcaq-node.  If not, see <https://www.gnu.org/licenses/>.
 module.exports = function regionCode(id) {
     switch (id) {
         case 1:
-        return 'Receitas';
+        return "Nome Ente"
         case 2:
-        return 'Matrículas Públicas';
+        return 'Receitas';
         case 3:
-        return 'Matrículas Públicas Mais Conveniada';
+        return 'Matrículas Públicas';
         case 4:
-        return 'Receita Aluno Ano Pública'
+        return 'Matrículas Públicas Mais Conveniada';
         case 5:
-        return 'Receita Aluno Mês Pública';
+        return 'Receita Aluno Ano Pública'
         case 6:
-        return 'Receita Aluno Ano Pública Mais Conveniada';
+        return 'Receita Aluno Mês Pública';
         case 7:
+        return 'Receita Aluno Ano Pública Mais Conveniada';
+        case 8:
         return 'Receita Aluno Mês Pública Mais Conveniada';
     }
 };
diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js
index 93ae6997..efce47c5 100644
--- a/src/libs/routes_v1/studentRevenue.js
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -45,8 +45,9 @@ let rqf = new ReqQueryFields();
 studentRevenueApp.use(cache('15 day'));
 
 studentRevenueApp.get('/years', (req, res, next) => {
-    req.sql.from('pnad_novo')
+    req.sql.from('receitas')
     .field('DISTINCT receitas.ano', 'year')
+    .where('receitas.ano is not null')
     next();
 }, query, response('years'));
 
@@ -78,23 +79,24 @@ rqf.addField({
         type: 'integer',
         field: 'ano'
     }
-}).addValue({
-    name: 'city',
-    table: 'receitas',
-    tableField: 'cod_ibge',
-    resultField: 'city',
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'cod_ibge'
-    }
 })
 
 studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
 
     if (req.query.dims && req.query.dims.includes('receitas')) {
+        let whereCondition = "";
+        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.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')
@@ -102,6 +104,8 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
         .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')
     }
 
     next();
-- 
GitLab


From fae5308ff17244d26035581305dc1a341a2db5d0 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 24 Feb 2025 11:48:50 -0300
Subject: [PATCH 31/36] refactor: remove unnecessary console log

---
 src/libs/middlewares/aggregateData.js | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js
index adf21fba..cbe55d8c 100644
--- a/src/libs/middlewares/aggregateData.js
+++ b/src/libs/middlewares/aggregateData.js
@@ -80,7 +80,6 @@ function aggregateData(req, res, next) {
                 for (const property in r) {
                     // Dados agregados são identificados com a substring 'total_' em sua chave
                     if (property.includes('total_')) {
-                        console.log(property);
                         let data = {
                             total: r[property],
                             year: r.year,
-- 
GitLab


From da4a8a319a5e0c02c4b43251360694059c3ca3e1 Mon Sep 17 00:00:00 2001
From: tgcl21 <tgcl21@inf.ufpr.br>
Date: Wed, 26 Feb 2025 11:31:27 -0300
Subject: [PATCH 32/36] fixed instruction level filters

---
 src/libs/routes_v1/instructionLevel.js | 78 ++++++++++++++++++++++----
 1 file changed, 68 insertions(+), 10 deletions(-)

diff --git a/src/libs/routes_v1/instructionLevel.js b/src/libs/routes_v1/instructionLevel.js
index c8b27886..d3cb7d0c 100644
--- a/src/libs/routes_v1/instructionLevel.js
+++ b/src/libs/routes_v1/instructionLevel.js
@@ -61,6 +61,64 @@ NivelInstrucao.get('/instruction_level', (req, res, next) => {
     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,
@@ -78,16 +136,6 @@ rqf.addField({
         type: 'integer',
         field: 'id'
     }
-}).addValue({
-    name: 'location',
-    table: 'pnad_novo',
-    tableField: 'tipo_de_area',
-    resultField: 'location_id',
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'id'
-    }
 }).addValue({
     name: 'bolsa_familia',
     table: 'pnad_novo',
@@ -223,6 +271,16 @@ rqf.addField({
         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) => {
-- 
GitLab


From 1998c40928f1aa3ce1486bfc43b4887f7ec2bf50 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Thu, 27 Feb 2025 10:12:16 -0300
Subject: [PATCH 33/36] feat: add studentRevenueParser middleware to handle
 city and region filter logic

---
 src/libs/middlewares/studentRevenueParser.js | 23 ++++++++++++++++++++
 src/libs/routes_v1/api.js                    |  3 ++-
 src/libs/routes_v1/studentRevenue.js         | 18 +++++++++++++--
 3 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 src/libs/middlewares/studentRevenueParser.js

diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js
new file mode 100644
index 00000000..954c9c82
--- /dev/null
+++ b/src/libs/middlewares/studentRevenueParser.js
@@ -0,0 +1,23 @@
+/*
+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) {
+    console.log(req.query);
+
+    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/api.js b/src/libs/routes_v1/api.js
index b9af6769..84306018 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -174,6 +174,7 @@ const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequen
 const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`);
 
 const studentRevenue = require(`${libs}/routes_v1/studentRevenue`);
+const studentRevenueParser = require(`${libs}/middlewares/studentRevenueParser`);
 
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v1 is running' });
@@ -249,7 +250,7 @@ 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', studentRevenue);
+api.use('/student_revenue', studentRevenueParser, studentRevenue);
 
 //Publication 
 api.use('/publication', publication);
diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js
index efce47c5..4b8c334e 100644
--- a/src/libs/routes_v1/studentRevenue.js
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -79,12 +79,26 @@ rqf.addField({
         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 = "";
+        let whereCondition = req.query.filter.includes("region") ? "" : "receitas.cod_ibge = 0";
         let filterId;
         const filters = req.query.filter.split(",");
         filters.forEach((filter) => {
-- 
GitLab


From 23bbea5babfcc7c5b87a9d82b5fa6582ce6b4315 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Tue, 11 Mar 2025 10:12:30 -0300
Subject: [PATCH 34/36] fix: add year for student revenue data

---
 src/libs/convert/revenue.js          | 16 +++++++++-------
 src/libs/routes_v1/studentRevenue.js |  1 +
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/libs/convert/revenue.js b/src/libs/convert/revenue.js
index 9278e280..18cfeb8c 100644
--- a/src/libs/convert/revenue.js
+++ b/src/libs/convert/revenue.js
@@ -21,20 +21,22 @@ along with simcaq-node.  If not, see <https://www.gnu.org/licenses/>.
 module.exports = function regionCode(id) {
     switch (id) {
         case 1:
-        return "Nome Ente"
+        return "Ano";
         case 2:
-        return 'Receitas';
+        return "Nome Ente"
         case 3:
-        return 'Matrículas Públicas';
+        return 'Receitas';
         case 4:
-        return 'Matrículas Públicas Mais Conveniada';
+        return 'Matrículas Públicas';
         case 5:
-        return 'Receita Aluno Ano Pública'
+        return 'Matrículas Públicas Mais Conveniada';
         case 6:
-        return 'Receita Aluno Mês Pública';
+        return 'Receita Aluno Ano Pública'
         case 7:
-        return 'Receita Aluno Ano Pública Mais Conveniada';
+        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/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js
index 4b8c334e..ac1b5bc6 100644
--- a/src/libs/routes_v1/studentRevenue.js
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -110,6 +110,7 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
 
         req.sql.from('receitas')
         .field('receitas.ano', 'year')
+        .field('receitas.ano', 'total_ano')
         .field('receitas.nome_ente', 'total_nome_ente')
         .field('receitas.receitas', 'total_receitas')
         .field('matriculas_publica', 'total_matriculas_publica')
-- 
GitLab


From 271e7d8cb89206aa582b3283ba9a98e24e910194 Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Tue, 11 Mar 2025 11:29:04 -0300
Subject: [PATCH 35/36] feat: add studentCost route

---
 src/libs/convert/studentCost.js              |  42 ++++++
 src/libs/middlewares/aggregateData.js        |   7 +-
 src/libs/middlewares/id2str.js               |   7 +-
 src/libs/middlewares/studentRevenueParser.js |   1 -
 src/libs/routes_v1/api.js                    |   3 +
 src/libs/routes_v1/studentCost.js            | 130 +++++++++++++++++++
 src/libs/routes_v1/studentRevenue.js         |   1 +
 7 files changed, 186 insertions(+), 5 deletions(-)
 create mode 100644 src/libs/convert/studentCost.js
 create mode 100644 src/libs/routes_v1/studentCost.js

diff --git a/src/libs/convert/studentCost.js b/src/libs/convert/studentCost.js
new file mode 100644
index 00000000..d757ec47
--- /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 cbe55d8c..903fda03 100644
--- a/src/libs/middlewares/aggregateData.js
+++ b/src/libs/middlewares/aggregateData.js
@@ -36,7 +36,8 @@ const convert = {
     total_doc: "totalDoc",
     education_degree_entity: "educationDegreeEntity",
     government_agreement: "governmentAgreement",
-    receitas: "revenue"
+    receitas: "revenue",
+    despesas: "studentCost"
 }
 
 function aggregateData(req, res, next) {
@@ -64,10 +65,12 @@ function aggregateData(req, res, next) {
             'post_graduation_entity',
             'contract_type_entity',
             'education_degree_entity',
-            'receitas'
+            'receitas',
+            'despesas'
         ]
         let id;
         const fields = req.query.dims.split(',');
+        console.log(fields);
         let currentAggregateField;
         let currentNonAggregateField;
     
diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js
index 60ff424f..730d620e 100644
--- a/src/libs/middlewares/id2str.js
+++ b/src/libs/middlewares/id2str.js
@@ -127,6 +127,7 @@ 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,
@@ -246,7 +247,8 @@ const ids = {
     special_education_entity: specialEducationEntity,
     total_doc: totalDoc,
     education_degree_entity: educationDegreeEntity,
-    receitas_id: revenue
+    receitas_id: revenue,
+    student_cost: studentCost
 };
 
 function transform(removeId=false) {
@@ -389,5 +391,6 @@ module.exports = {
     specialEducationEntity,
     totalDoc,
     educationDegreeEntity,
-    revenue
+    revenue,
+    studentCost
 };
diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js
index 954c9c82..d11f98f5 100644
--- a/src/libs/middlewares/studentRevenueParser.js
+++ b/src/libs/middlewares/studentRevenueParser.js
@@ -4,7 +4,6 @@ Devido à limitações na tabela de dados, quando ambos campos estão ativos o f
 "região" funciona incorretamente. 
 */
 module.exports = function studentRevenueParser(req, res, next) {
-    console.log(req.query);
 
     if (req.query && req.query.filter) {
         const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`;
diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js
index 84306018..5331b389 100644
--- a/src/libs/routes_v1/api.js
+++ b/src/libs/routes_v1/api.js
@@ -176,6 +176,8 @@ 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' });
 });
@@ -251,6 +253,7 @@ 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/studentCost.js b/src/libs/routes_v1/studentCost.js
new file mode 100644
index 00000000..fdcf7270
--- /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
index ac1b5bc6..b595fb00 100644
--- a/src/libs/routes_v1/studentRevenue.js
+++ b/src/libs/routes_v1/studentRevenue.js
@@ -121,6 +121,7 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
         .field('receita_aluno_mes_publica_mais_conveniada', 'total_receita_aluno_mes_publica_mais_conveniada')
         .where(`${whereCondition}`)
         .order('receitas.cod_ibge')
+        .order('receitas.ano')
     }
 
     next();
-- 
GitLab


From 86bc69842f429233cdd6dfcdef63c1614567900f Mon Sep 17 00:00:00 2001
From: Lewis <lgtg20@inf.ufpr.br>
Date: Mon, 17 Mar 2025 10:40:35 -0300
Subject: [PATCH 36/36] fix: update aggregateData middleware to handle base
 fields correctly

---
 src/libs/middlewares/aggregateData.js | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js
index 903fda03..ab7dfdd9 100644
--- a/src/libs/middlewares/aggregateData.js
+++ b/src/libs/middlewares/aggregateData.js
@@ -67,10 +67,10 @@ function aggregateData(req, res, next) {
             'education_degree_entity',
             'receitas',
             'despesas'
-        ]
+        ];
+        const baseFields = ["school"]
         let id;
         const fields = req.query.dims.split(',');
-        console.log(fields);
         let currentAggregateField;
         let currentNonAggregateField;
     
@@ -92,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)
-- 
GitLab