Skip to content
Snippets Groups Projects
Select Git revision
  • teachers-ies
  • homologa protected
  • development protected
  • instruction_level_fix2
  • enrollment_rate
  • master default protected
  • hotfix-enrollment-aggregate
  • instruction_level_fixes
  • docentes-ies-ente-federativo
  • receita-potencial
  • #974-receita-potencial
  • db-conn
  • years-of-study-mean
  • new-indicators
  • issue_935
  • instruction_number
  • issue_958
  • issue_953
  • issues_indicadores_agregados
  • courseAggregate
  • v1.16.0
  • v1.15.1
  • v1.14.2
  • v1.14.1
  • v1.14.0
  • v1.14
  • v1.9.0
  • v1.8.3
  • v1.8.2
  • v1.8.1
  • v1.8.0
  • v1.7.0
  • v1.6.1
  • v1.6.0
  • v1.5.0
  • v1.4.2
  • v1.4.1
  • v1.4.0
  • v1.3.3
  • v1.3.2
40 results

adjustedLiquidFrequency.js

Blame
  • adjustedLiquidFrequency.js 8.79 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 adjustedLiquidFrequency = express.Router();
    
    const libs = `${process.cwd()}/libs`;
    
    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 cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
    
    let rqf = new ReqQueryFields();
    
    adjustedLiquidFrequency.use(cache('15 day'));
    
    adjustedLiquidFrequency.get('/years', (req, res, next) => {
        req.sql.from('pnad_novo')
        .field('DISTINCT pnad_novo.ano_ref', 'year')
        .where('pnad_novo.ano_ref >= 2019')
        next();
    }, query, response('years'));
    
    adjustedLiquidFrequency.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: 10, name: id2str.incomeRange(10)});
        next();
    }, response('income_range'));
    
    adjustedLiquidFrequency.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'));
    
    adjustedLiquidFrequency.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'));
    
    adjustedLiquidFrequency.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'));
    
    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: '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: 'income_range',
        table: 'pnad_novo',
        tableField: 'faixa_rendimento_aux_tx',
        resultField: 'income_range_id',
        where: {
            relation: '=',
            type: 'integer',
            field: 'faixa_rendimento_aux_tx'
        }
    }).addValue({
        name: 'gender',
        table: 'pnad_novo',
        tableField: 'sexo',
        resultField: 'gender_id',
        where: {
            relation: '=',
            type: 'integer',
            field: 'sexo'
        }
    }).addValue({
        name: 'region',
        table: 'pnad_novo',
        tableField: 'cod_regiao',
        resultField: 'region_id',
        where: {
            relation: '=',
            type: 'integer',
            field: 'cod_regiao'
        }
    }).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'
        }
    });
    
    function matchQueries(queryPartial, queryTotal) {
        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.denominator = result.total;
                newObj.partial = objMatch.total;
                newObj.total = (objMatch.total / result.total) * 100;
                match.push(newObj);
            }
        });
        return match;
    }
    
    
    adjustedLiquidFrequency.get('/', rqf.parse(), rqf.build(), (req, res, next) => {    
        let totalPop = req.sql.clone();
        totalPop.field("ano_ref")
        .field("faixa_etaria")
        .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
        .from("pnad_novo")
        .group("ano_ref")
        .group("faixa_etaria");
    
        // Aplicando filtros dinâmicos à subquery total_pop
        totalPop = rqf.buildQuery(req, totalPop);
    
        // Subquery: total_apoio_freq
        let totalApoioFreq = req.sql.clone();
            totalApoioFreq.field("pnad_novo.ano_ref")
            .field("pnad_novo.apoio_frequencia_ajustada")
            .field("total_pop.total")
            .field("SUM(pnad_novo.peso_domicilio_pessoas_com_cal)", "total_freq")
            .from("pnad_novo")
            .join(
                totalPop, 
                "total_pop", 
                "total_pop.ano_ref = pnad_novo.ano_ref AND total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada"
            )
            .group("pnad_novo.ano_ref")
            .group("pnad_novo.apoio_frequencia_ajustada")
            .group("total_pop.total");
    
        req.sql.field("ano_ref")
            .field("apoio_frequencia_ajustada")
            .field("round((total_freq / total) * 100,1)", "total")
            .from(totalApoioFreq, "total_apoio_freq")
            .where("ano_ref >= 2019")
            .order("ano_ref")
            .order("apoio_frequencia_ajustada");
    
            next();
    }, query, id2str.transform(false), response('adjusted_liquid_frequency'));
    
    module.exports = adjustedLiquidFrequency;