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 1/9] [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 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 2/9] [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 3/9] [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 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 4/9] [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 5/9] [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 6/9] [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 7/9] [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 8/9] [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 9/9] 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