Select Git revision
-
Fernando Gbur dos Santos authoredFernando Gbur dos Santos authored
newPnad.js 9.09 KiB
/*
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 = [
{id: 0, name: 'Sem instrução e menos de 1 ano de idade'},
{id: 1, name: '1 ano de estudo'},
];
for (let i = 2; i < 17; i++) {
req.result.push({id: i, name: i + ' anos de estudo'});
}
req.result.push({id: 99, name: 'Não se aplica 17 anos ou menos'});
next();
}, response('years_of_study'));
PnadNovoApp.get('/instruction_level', (req, res, next) => {
req.result = [
{id: 1, name: 'Sem instrução e menos de 1 ano de estudo'},
{id: 2, name: 'Fundamental incompleto ou equivalente'},
{id: 3, name: 'Fundamental completo ou equivalente'},
{id: 4, name: 'Médio incompleto ou equivalente'},
{id: 5, name: 'Médio completo ou equivalente'},
{id: 6, name: 'Superior incompleto ou equivalente'},
{id: 7, name: 'Superior completo ou equivalente'},
{id: 8, name: 'Não se aplica - 17 anos ou menos'}
];
next();
}, response('instruction_level'));
PnadNovoApp.get('/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('adm_dependency'));
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: ['nome', 'id'],
resultField: ['state_name', 'state_id'],
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: 'illiteracy',
table: 'pnad_novo',
tableField: 'analfabetismo',
resultField: 'illiteracy_id',
where: {
relation: '=',
type: 'integer',
field: 'analfabetismo'
}
}).addValue({
name: 'years_of_study',
table: 'pnad_novo',
tableField: 'anos_de_estudo',
resultField: 'years_of_study',
where: {
relation: '=',
type: 'integer',
field: 'anos_de_estudo'
}
}).addValue({
name: 'instruction_level',
table: 'pnad_novo',
tableField: 'nivel_de_instrucao',
resultField: 'instruction_level',
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',
where: {
relation: '=',
type: 'integer',
field: 'frequenta_escola'
}
}).addValue({
name: 'modality',
table: 'pnad_novo',
tableField: 'modalidade',
resultField: 'modality',
where: {
relation: '=',
type: 'integer',
field: 'modalidade'
}
}).addValue({
name: 'attended_modality',
table: 'pnad_novo',
tableField: 'nivel_etapa_modalidade_freq',
resultField: 'attended_modality',
where: {
relation: '=',
type: 'integer',
field: 'nivel_etapa_modalidade_freq'
}
}).addValue({
name: 'bolsa_familia',
table: 'pnad_novo',
tableField: 'recebeu_rendimentos_de_programa_bolsa_familia',
resultField: 'bolsa_familia',
where: {
relation: '=',
type: 'integer',
field: 'recebeu_rendimentos_de_programa_bolsa_familia'
}
}).addValue({
name: 'race',
table: 'pnad_novo',
tableField: 'cor_raca',
resultField: 'race',
where: {
relation: '=',
type: 'integer',
field: 'cor_raca'
}
}).addValue({
name: 'income_range',
table: 'pnad_novo',
tableField: 'faixa_rendimento',
resultField: 'income_range',
where: {
relation: '=',
type: 'integer',
field: 'faixa_rendimento'
}
}).addValue({
name: 'gender',
table: 'pnad_novo',
tableField: 'sexo',
resultField: 'gender',
where: {
relation: '=',
type: 'integer',
field: 'sexo'
}
}).addValue({
name: 'cap_code',
table: 'pnad_novo',
tableField: 'cod_cap',
resultField: 'cap_code',
where: {
relation: '=',
type: 'integer',
field: 'cod_cap'
}
}).addValue({
name: 'reg_code',
table: 'pnad_novo',
tableField: 'cod_regiao',
resultField: 'reg_code',
where: {
relation: '=',
type: 'integer',
field: 'cod_regiao'
}
}).addValue({
name: 'metro_code',
table: 'pnad_novo',
tableField: 'cod_rm_ride',
resultField: 'metro_code',
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'
}
}).addValueToField({
name: 'region',
table: 'estado',
tableField: 'regiao_id',
resultField: 'region_id',
where: {
relation: '=',
type: 'integer',
field: 'regiao_id',
table: 'estado'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'municipio'
}
}, 'filter');
PnadNovoApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.sql.from('pnad_novo')
.field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total')
.field('pnad_novo.ano_ref', 'year')
.group('pnad_novo.ano_ref')
.order('pnad_novo.ano_ref')
console.log(req.sql.toString())
next();
}, query, id2str.transform(false),response('pnad_novo'));
module.exports = PnadNovoApp;