Skip to content
Snippets Groups Projects
liquidEnrollmentRatio.js 12.8 KiB
Newer Older
const express = require('express');

Fernando Erd's avatar
Fernando Erd committed
const liquidEnrollmentRatioApp = 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 addMissing = require(`${libs}/middlewares/addMissing`);

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;

let rqf = new ReqQueryFields();

Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.use(cache('15 day'));

// Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset.
Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/year_range', (req, res, next) => {
    req.sql.from('pnad')
    .field('DISTINCT pnad.ano_censo', 'year');
    next();
}, query, (req, res, next) => {
    req.oldResult = req.result;

    req.sql = squel.select();

    req.sql.from('matricula')
    .field('DISTINCT matricula.ano_censo', 'year');
    next();
}, query, (req, res, next) => {
    let distinct_years = [];
    let new_result = [];
    for (let i = 0; i < req.oldResult.length; i++) {
        for (let j = 0; j < req.result.length; j++) {
            if(req.oldResult[i].year == req.result[j].year) {
                distinct_years.push(req.oldResult[i]);
            }
        }
    }
    new_result.push({start_year: distinct_years[distinct_years.length -1].year, end_year: distinct_years[0].year});
    req.result = new_result;
    next();
}, response('range'));

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

    req.sql = squel.select();

    req.sql.from('matricula')
    .field('DISTINCT matricula.ano_censo', 'year');
    next();
}, query, (req, res, next) => {
    let new_result = []
    for (let i = 0; i < req.oldResult.length; i++) {
        for (let j = 0; j < req.result.length; j++) {
            if(req.oldResult[i].year == req.result[j].year) {
                new_result.push(req.oldResult[i]);
            }
        }
    }
    req.result = new_result;
    next();
}, response('years'));

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

Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/education_level_basic', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não classificada'},
        {id: 1, name: 'Creche'},
        {id: 2, name: 'Pré-Escola'},
        {id: 4, name: 'Ensino Fundamental - anos iniciais'},
        {id: 5, name: 'Ensino Fundamental - anos finais'},
        {id: 6, name: 'Ensino Médio'}
    ];
    next();
Fernando Erd's avatar
Fernando Erd committed
}, response('education_level_basic'));
Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/gender', (req, res, next) => {
    req.result = [
        {id: 1, name: 'Masculino'},
        {id: 2, name: 'Feminino'}
    ];
    next();
}, response('gender'));

Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/ethnic_group', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Sem declaração'},
        {id: 1, name: 'Branca'},
        {id: 2, name: 'Preta'},
        {id: 3, name: 'Parda'},
        {id: 4, name: 'Amarela'},
        {id: 5, name: 'Indígena'}
    ];
    next();
}, response('ethnic_group'));

Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/location', (req, res, next) => {
    req.result = [
        {id: 1, name: 'Urbana'},
        {id: 2, name: 'Rural'}
    ];
    next();
}, response('location'));

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: '@'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: '@'
    }
}).addValue({
    name: 'ethnic_group',
    table: '@',
    tableField: 'cor_raca_id',
    resultField: 'ethnic_group_id',
    where: {
        relation: '=',
        type: 'integer',
Fernando Erd's avatar
Fernando Erd committed
        field: 'cor_raca_id'
    }
}).addValue({
    name: 'min_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
}).addValue({
    name: 'gender',
Fernando Erd's avatar
Fernando Erd committed
    table: '@',
    tableField: 'sexo',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'gender_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'sexo'
    }
}).addValue({
    name: 'location',
    table: '@',
    tableField: 'localizacao_id',
    resultField: 'location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'localizacao_id'
    }
}).addValue({
Fernando Erd's avatar
Fernando Erd committed
    name: 'education_level_basic',
    table: 'matricula',
    tableField: 'etapas_mod_ensino_segmento_id',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'education_level_basic_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'etapas_mod_ensino_segmento_id'
Fernando Erd's avatar
Fernando Erd committed
function matchQueries(queryTotal, queryPartial) {
    let match = [];
Fernando Erd's avatar
Fernando Erd committed
    queryPartial.forEach((result) => {
Fernando Erd's avatar
Fernando Erd committed
        let newObj = {};
        let keys = Object.keys(result);
        keys.forEach((key) => {
            newObj[key] = result[key];
        });
Fernando Erd's avatar
Fernando Erd committed
        // console.log('NEW OBJ');
        // console.log(newObj);
        // remove total
Fernando Erd's avatar
Fernando Erd committed
        let index = keys.indexOf('total');
        if(index > -1) keys.splice(index, 1);
Fernando Erd's avatar
Fernando Erd committed
        // remove education_level_basic_id
        index = keys.indexOf('education_level_basic_id');
        if(index > -1) keys.splice(index, 1);
        // remove education_level_basic_name
        index = keys.indexOf('education_level_basic_name');
        if(index > -1) keys.splice(index, 1);
Fernando Erd's avatar
Fernando Erd committed
        let objMatch = null;

Fernando Erd's avatar
Fernando Erd committed
        for(let i = 0; i < queryTotal.length; ++i) {
            let total = queryTotal[i];
Fernando Erd's avatar
Fernando Erd committed
            let foundMatch = true;
            for(let j = 0; j < keys.length; ++j) {
                let key = keys[j];
Fernando Erd's avatar
Fernando Erd committed
                if(total[key] !== result[key]) {
Fernando Erd's avatar
Fernando Erd committed
                    foundMatch = false;
                    break;
                }
            }
            if(foundMatch) {
Fernando Erd's avatar
Fernando Erd committed
                objMatch = total;
Fernando Erd's avatar
Fernando Erd committed
                break;
            }
        }

        if(objMatch) {
Fernando Erd's avatar
Fernando Erd committed
            // console.log('MATCH!!!!');
            // console.log(objMatch);
            newObj.total = (result.total / objMatch.total) * 100;
            newObj.partial = result.total;
            newObj.denominator = objMatch.total
Fernando Erd's avatar
Fernando Erd committed
            match.push(newObj);
        }
    });
Fernando Erd's avatar
Fernando Erd committed
    // console.log('TAMANHOS');
    // console.log(queryTotal.length);
    // console.log(queryPartial.length);
    // console.log(match.length);
Fernando Erd's avatar
Fernando Erd committed
    return match;
}


Fernando Erd's avatar
Fernando Erd committed
liquidEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
    req.numerator = {};
    req.denominator = {};
Fernando Erd's avatar
Fernando Erd committed
    let liquidEnrollmentRatioApp = {};

    req.sql.from('matricula')
    .field('count(*)', 'total')
    .field('matricula.ano_censo', 'year')
    .group('matricula.ano_censo')
    .order('matricula.ano_censo')
    .where('matricula.tipo <= 3')

    function ConvertMatricula(result) {
        if (result == 1) {
            return '(matricula.faixa_etaria_31_03 = 1 AND matricula.etapas_mod_ensino_segmento_id = 1)'
        } else if (result == 2) {
            return '(matricula.faixa_etaria_31_03 = 2 AND matricula.etapas_mod_ensino_segmento_id = 2)'
        } else if (result == 4) {
            return '(matricula.faixa_etaria_31_03 = 3 AND matricula.etapas_mod_ensino_segmento_id = 4)'
        } else if (result == 5) {
            return '(matricula.faixa_etaria_31_03 = 4 AND matricula.etapas_mod_ensino_segmento_id = 5)'
        } else if (result == 6) {
            return '(matricula.faixa_etaria_31_03 = 5 AND matricula.etapas_mod_ensino_segmento_id = 6)'
Fernando Erd's avatar
Fernando Erd committed
    if ("education_level_basic" in req.filter) {
Fernando Erd's avatar
Fernando Erd committed
        if (Array.isArray(req.filter.education_level_basic)) {
            var string_query_enrollment = '';
            for(let i = 0; i < req.filter.education_level_basic.length - 1; i++)  {
                string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_basic[i]) + ' OR ';
        string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_basic[req.filter.education_level_basic.length - 1]);
        delete req.filter.education_level_basic;
        req.sql.where(string_query_enrollment);
        req.sql.field('matricula.faixa_etaria_31_03', 'age_range')
        req.sql.group('matricula.faixa_etaria_31_03', 'age_range');
    } else if ( "education_level_basic" in req.dims ) {
        req.sql.field('matricula.faixa_etaria_31_03', 'age_range')
        req.sql.where('(matricula.etapas_mod_ensino_segmento_id = 1  AND matricula.faixa_etaria_31_03 = 1) OR (matricula.etapas_mod_ensino_segmento_id = 2  AND matricula.faixa_etaria_31_03 = 2) OR (matricula.etapas_mod_ensino_segmento_id = 4  AND matricula.faixa_etaria_31_03 = 3) OR (matricula.etapas_mod_ensino_segmento_id = 5  AND matricula.faixa_etaria_31_03 = 4) OR (matricula.etapas_mod_ensino_segmento_id = 6  AND matricula.faixa_etaria_31_03 = 5)');
        req.sql.group('matricula.faixa_etaria_31_03', 'age_range');
    } else {
        res.status(400);
        next({
            status: 400,
            message: 'Wrong/No filter specified'
        });
    }

    next();
Fernando Erd's avatar
Fernando Erd committed
}, rqf.build(), query, id2str.transform(),  (req, res, next) => {
    req.numerator = req.result;
    req.resetSql();
Fernando Erd's avatar
Fernando Erd committed
    req.sql.field('sum(peso)', 'total')
    .field('pnad.ano_censo','year')
    .from('pnad')
    .group('pnad.ano_censo')
    .order('pnad.ano_censo')

    function convertPnad(result) {
        if (result == 1) {
Fernando Erd's avatar
Fernando Erd committed
            return 'pnad.faixa_etaria_31_03 = 1'
        } else if (result == 2) {
Fernando Erd's avatar
Fernando Erd committed
            return 'pnad.faixa_etaria_31_03 = 2'
        } else if (result == 4) {
Fernando Erd's avatar
Fernando Erd committed
            return 'pnad.faixa_etaria_31_03 = 3'
        } else if (result == 5) {
Fernando Erd's avatar
Fernando Erd committed
            return 'pnad.faixa_etaria_31_03 = 4'
        } else if (result == 6) {
Fernando Erd's avatar
Fernando Erd committed
            return 'pnad.faixa_etaria_31_03 = 5'
Fernando Erd's avatar
Fernando Erd committed
    //remove education_level_basic how filter and add faixa_etaria_31_03 in filter
    if ("education_level_basic" in req.filter) {
Fernando Erd's avatar
Fernando Erd committed
        if (Array.isArray(req.filter.education_level_basic)) {
            var string_query = '';
            for(let i = 0; i < req.filter.education_level_basic.length - 1; i++)  {
                string_query = string_query + convertPnad(req.filter.education_level_basic[i]) + ' OR ';
            }
            string_query = string_query + convertPnad(req.filter.education_level_basic[req.filter.education_level_basic.length - 1]);
            req.sql.where(string_query);
        req.sql.field('pnad.faixa_etaria_31_03', 'age_range')
        req.sql.group('pnad.faixa_etaria_31_03', 'age_range');
    } else if ( "education_level_basic" in req.dims ) {
        req.sql.field('pnad.faixa_etaria_31_03','age_range')
        req.sql.where('pnad.faixa_etaria_31_03 = 1 OR pnad.faixa_etaria_31_03 = 2 OR pnad.faixa_etaria_31_03 = 3 OR pnad.faixa_etaria_31_03 = 4 OR pnad.faixa_etaria_31_03 = 5')
        req.sql.group('pnad.faixa_etaria_31_03', 'age_range');
    } else {
        res.status(400);
        next({
            status: 400,
            message: 'Wrong/No filter specified'
        });
    }

    next();
}, rqf.parse(), (req, res, next) => {
Fernando Erd's avatar
Fernando Erd committed
    if ("education_level_basic" in req.filter) {
        delete req.filter.education_level_basic;
    }
    if ("education_level_basic" in req.dims) {
        delete req.dims.education_level_basic;
    }
    next();
Fernando Erd's avatar
Fernando Erd committed
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
    req.denominator = req.result;
    //division to generate req.result final
    req.result = []
Fernando Erd's avatar
Fernando Erd committed
    let liquidEnrollment = matchQueries(req.denominator, req.numerator);
    req.result = liquidEnrollment;
    next();
Fernando Erd's avatar
Fernando Erd committed
}, response('liquidEnrollmentRatio'));
Fernando Erd's avatar
Fernando Erd committed
module.exports = liquidEnrollmentRatioApp;