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 1/2] [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 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 2/2] =?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