Skip to content
Snippets Groups Projects
rateSchool.js 8.02 KiB
Newer Older
Fernando Erd's avatar
Fernando Erd committed
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`);

Fernando Erd's avatar
Fernando Erd committed
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;

Hamer Iboshi's avatar
Hamer Iboshi committed
rateSchoolApp.use(cache('15 day'));

Fernando Erd's avatar
Fernando Erd committed
let rqf = new ReqQueryFields();

Fernando Erd's avatar
Fernando Erd committed
// 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');
Fernando Erd's avatar
Fernando Erd committed
    next();
}, query, response('range'));

rateSchoolApp.get('/years', (req, res, next) => {
    req.sql.from('pnad')
    .field('DISTINCT pnad.ano_censo', 'year');
Fernando Erd's avatar
Fernando Erd committed
    next();
}, query, response('years'));

rateSchoolApp.get('/source', (req, res, next) => {
    req.sql.from('fonte')
    .field('fonte', 'source')
    .where('tabela = \'pnad\'');
Fernando Erd's avatar
Fernando Erd committed
    next();
}, query, response('source'));

Hamer Iboshi's avatar
Hamer Iboshi committed
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'}
    ];
Fernando Erd's avatar
Fernando Erd committed
    next();
Hamer Iboshi's avatar
Hamer Iboshi committed
}, response('ethnic_group_pnad'));
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 = [
        {id: 2, name: 'Masculino'},
        {id: 4, name: 'Feminino'}
    ];
    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'));
Fernando Erd's avatar
Fernando Erd committed

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).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'
    }
Fernando Erd's avatar
Fernando Erd committed
}).addValue({
Hamer Iboshi's avatar
Hamer Iboshi committed
    name: 'ethnic_group_pnad',
Fernando Erd's avatar
Fernando Erd committed
    table: 'pnad',
Fernando Erd's avatar
Fernando Erd committed
    tableField: 'cor_raca',
    resultField: 'ethnic_group_pnad_id',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'integer',
Fernando Erd's avatar
Fernando Erd committed
        field: 'cor_raca'
Hamer Iboshi's avatar
Hamer Iboshi committed
}).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',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'gender_pnad_id',
    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) {
Fernando Erd's avatar
Fernando Erd committed
            newObj.denominator = result.total;
            newObj.partial = objMatch.total;
Fernando Erd's avatar
Fernando Erd committed
            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());
Fernando Erd's avatar
Fernando Erd committed
    if ("age_range" in req.filter) {
        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) => {
Fernando Erd's avatar
Fernando Erd committed
    if ("age_range" in req.filter) {
        let school_attendance_rate = matchQueries(req.result[req.queryIndex.freq_total], req.result[req.queryIndex.freq_nursery]);
        req.result = school_attendance_rate;
    } else {
        req.result = [];
    }
Fernando Erd's avatar
Fernando Erd committed
}, id2str.transform(false), response('rateSchool'));

rateSchoolApp.get('/download', passport.authenticate('bearer', { session: false }), rqf.parse(), rqf.build(), download('pnad', 'mapping_pnad'));
Fernando Erd's avatar
Fernando Erd committed

module.exports = rateSchoolApp;