Newer
Older
const express = require('express');
const rateSchoolApp = express.Router();
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
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 download = require(`${libs}/middlewares/downloadDatabase`);
const passport = require('passport');
const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
// Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset.
rateSchoolApp.get('/year_range', (req, res, next) => {
req.sql.from('pnad')
.field('MIN(pnad.ano_censo)', 'start_year')
.field('MAX(pnad.ano_censo)', 'end_year');
next();
}, query, response('range'));
rateSchoolApp.get('/years', (req, res, next) => {
req.sql.from('pnad')
.field('DISTINCT pnad.ano_censo', 'year');
next();
}, query, response('years'));
rateSchoolApp.get('/source', (req, res, next) => {
req.sql.from('fonte')
.field('fonte', 'source')
.where('tabela = \'pnad\'');
rateSchoolApp.get('/ethnic_group_pnad', (req, res, next) => {
req.result = [
{id: 0, name: 'Indígena'},
{id: 1, name: 'Branca e amarela'},
{id: 2, name: 'Preta e parda'},
{id: 9, name: 'Sem declaração'}
];
rateSchoolApp.get('/age_range', (req, res, next) => {
req.result = [
{id: 1, name: '0-3'},
{id: 2, name: '4-5'},
{id: 3, name: '6-10'},
{id: 4, name: '11-14'},
{id: 5, name: '15-17'},
{id: 6, name: '18-24'}
];
next();
}, response('age_range'));
rateSchoolApp.get('/gender', (req, res, next) => {
req.result = [
];
next();
}, response('gender'));
rateSchoolApp.get('/location', (req, res, next) => {
req.result = [
{id: 1, name: 'Urbana'},
{id: 2, name: 'Rural'}
];
next();
}, response('location'));
rateSchoolApp.get('/fifth_household_income', (req, res, next) => {
req.result = [
{id: 1, name: '20% menores'},
{id: 2, name: '2o quinto'},
{id: 3, name: '3o quinto'},
{id: 4, name: '4o quinto'},
{id: 5, name: '20% maiores'},
{id: -1, name: 'Sem declaração'}
];
next();
},response('fifth_household_income'));
rateSchoolApp.get('/extremes_household_income', (req, res, next) => {
req.result = [
{id: 1, name: '10% menores'},
{id: 2, name: '10% maiores'},
{id: -1, name: 'Sem declaração'}
];
next();
}, response('extremes_household_income'));
rqf.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false

Hamer Iboshi
committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
}).addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'regiao_id',
foreignTable: 'pnad'
}
}).addValue({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'pnad'
}
resultField: 'ethnic_group_pnad_id',
}).addValue({
name: 'min_year',
table: 'pnad',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
table: 'pnad',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
table: 'pnad',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
table: 'pnad',
field: 'ano_censo'
}
}).addValue({
name: 'age_range',
table: 'pnad',
tableField: 'faixa_etaria_31_03',
resultField: 'age_range_id',
where: {
relation: '=',
type: 'integer',
field: 'faixa_etaria_31_03'
}
}).addValue({
name: 'gender',
table: 'pnad',
tableField: 'sexo',
where: {
relation: '=',
type: 'integer',
field: 'sexo'
}
}).addValue({
name: 'location',
table: 'pnad',
tableField: 'localizacao_id',
resultField: 'location_id',
where: {
relation: '=',
type: 'integer',
field: 'localizacao_id'
}
}).addValue({
name: 'extremes_household_income',
table: 'pnad',
tableField: 'extremos_nivel_rendimento',
resultField: 'extremes_household_income_id',
where: {
relation: '=',
type: 'integer',
field: 'extremos_nivel_rendimento'
}
}).addValue({
name: 'fifth_household_income',
table: 'pnad',
tableField: 'quintil_nivel_rendimento',
resultField: 'fifth_household_income_id',
where: {
relation: '=',
type: 'integer',
field: 'quintil_nivel_rendimento'
}
function matchQueries(queryTotal, queryPartial) {
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.partial = objMatch.total;
newObj.total = (objMatch.total / result.total) * 100;
match.push(newObj);
}
});
return match;
}
rateSchoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.querySet = [];
req.queryIndex = {};
log.debug(req.sql.toParam());
if ("age_range" in req.filter || "age_range" in req.dims) {
let freq_total = req.sql.clone();
freq_total.field('sum(pnad.peso)', 'total')
.field('pnad.ano_censo','year')
.from('pnad')
.group('pnad.ano_censo')
.order('pnad.ano_censo')
req.queryIndex.freq_total = req.querySet.push(freq_total) - 1;
let freq_nursery = req.sql.clone();
freq_nursery.field('sum(pnad.peso)', 'total')
.field('pnad.ano_censo','year')
.from('pnad')
.group('pnad.ano_censo')
.order('pnad.ano_censo')
.where('pnad.frequenta_escola_creche = 2')
req.queryIndex.freq_nursery = req.querySet.push(freq_nursery) - 1;
}
next();
}, multiQuery, (req, res, next) => {
if ("age_range" in req.filter || "age_range" in req.dims) {
log.debug(req.result[req.queryIndex.freq_total]);
log.debug(req.result[req.queryIndex.freq_nursery])
let school_attendance_rate = matchQueries(req.result[req.queryIndex.freq_total], req.result[req.queryIndex.freq_nursery]);
req.result = school_attendance_rate;
} else {
res.status(400);
next({
status: 400,
message: 'Wrong/No filter specified'
});
}, id2str.transform(false), response('rateSchool'));
rateSchoolApp.get('/download', passport.authenticate('bearer', { session: false }), rqf.parse(), rqf.build(), download('pnad', 'mapping_pnad'));