From a209ef26cce151aa137cf4b10ba2cc0f620c31db Mon Sep 17 00:00:00 2001
From: tgcl21 <tgcl21@inf.ufpr.br>
Date: Fri, 28 Feb 2025 09:38:29 -0300
Subject: [PATCH] initial

---
 src/libs/routes_v1/enrollmentRate.js | 282 +++++++++++++++++++++++++++
 1 file changed, 282 insertions(+)
 create mode 100644 src/libs/routes_v1/enrollmentRate.js

diff --git a/src/libs/routes_v1/enrollmentRate.js b/src/libs/routes_v1/enrollmentRate.js
new file mode 100644
index 00000000..1418c196
--- /dev/null
+++ b/src/libs/routes_v1/enrollmentRate.js
@@ -0,0 +1,282 @@
+/*
+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 EnrollmentRateApp = 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 multiQuery = require(`${libs}/middlewares/multiQuery`);
+
+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();
+
+EnrollmentRateApp.use(cache('15 day'));
+
+EnrollmentRateApp.get('/years', (req, res, next) => {
+    req.sql.from('pnad_novo')
+    .field('DISTINCT pnad_novo.ano_ref', 'year')
+    next();
+}, query, response('years'));
+
+EnrollmentRateApp.get('/ageRanges', (req, res, next) => {
+    req.sql.from('pnad_novo')
+    .field('DISTINCT pnad_novo.faixa_etaria', 'age_range')
+    next();
+}, query, response('ageRanges'));
+
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValue({
+    name: 'location',
+    table: 'pnad_novo',
+    tableField: 'situacao_domicilio',
+    resultField: 'location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'situacao_domicilio'
+    }
+}).addValue({
+    name: 'location_escola',
+    table: 'escola',
+    tableField: 'localizacao_id',
+    resultField: 'location_id',
+    where: {
+      relation: '=',
+      type: 'integer',
+      field: 'localizacao_id'
+    }
+}).addValue({
+    name: 'cap_code',
+    table: 'pnad_novo',
+    tableField: 'cod_cap',
+    resultField: 'cap_code_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_cap'
+    }
+}).addValue({
+    name: 'cod_mun',
+    table: 'escola',
+    tableField: 'municipio_id',
+    resultField: 'municipio_id',
+    where: {
+      relation: '=',
+      type: 'integer',
+      field: 'municipio_id'
+    }
+}).addValue({
+    name: 'region',
+    table: 'pnad_novo',
+    tableField: 'cod_regiao',
+    resultField: 'region_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_regiao'
+    }
+}).addValue({
+    name: 'region_escola',
+    table: 'escola',
+    tableField: 'regiao_id',
+    resultField: 'regiao_id',
+    where: {
+      relation: '=',
+      type: 'integer',
+      field: 'regiao_id'
+    }
+}).addValue({
+    name: 'state',
+    table: 'pnad_novo',
+    tableField: 'cod_uf',
+    resultField: 'cod_uf',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_uf',
+    }
+}).addValue({
+    name: 'state_escola',
+    table: 'escola',
+    tableField: 'estado_id',
+    resultField: 'estado_id',
+    where: {
+      relation: '=',
+      type: 'integer',
+      field: 'estado_id'
+    }
+}).addValue({
+    name: 'faixa_etaria',
+    table: 'pnad_novo',
+    tableField: 'faixa_etaria',
+    resultField: 'age_range_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'faixa_etaria'
+    }
+})
+
+
+
+function matchQueries(queryPartial, queryTotal) {
+    let match = [];
+    console.log(queryPartial, queryTotal)
+    queryTotal.forEach((result) => {
+        let found = false;
+        queryPartial.forEach((partial) => {
+            console.log(result.year, partial.year)
+            if (result.year === partial.year) {
+                switch (result.age_range) {
+                    case 1:
+                    console.log("é 1")
+                    match.push({
+                            year: result.year,
+                            age_range: result.age_range,
+                            total: result.total,
+                            total_partial: partial.total_inf_cre,
+                            total_taxa: ((partial.total_inf_cre/result.total) * 100).toFixed(2),
+                        });                        
+                        found = true;
+                        break;
+                    case 2:
+                        match.push({
+                            year: result.year,
+                            age_range: result.age_range,
+                            total: result.total,
+                            total_partial: partial.total_inf_pre,
+                            total_taxa: ((partial.total_inf_pre/result.total) * 100).toFixed(2),
+                        });                        
+                        found = true;
+                        break;
+                    case 3:
+                        match.push({
+                            year: result.year,
+                            age_range: result.age_range,
+                            total: result.total,
+                            total_partial: partial.total_fund_ai,
+                            total_taxa: ((partial.total_fund_ai/result.total) * 100).toFixed(2),
+                        });                        
+                        found = true;
+                        break;
+                    case 4:
+                        match.push({
+                            year: result.year,
+                            age_range: result.age_range,
+                            total: result.total,
+                            total_partial: partial.total_fund_af,
+                            total_taxa: ((partial.total_fund_af/result.total) * 100).toFixed(2),
+                        });                        
+                        found = true;
+                        break;
+                    case 5:
+                        match.push({
+                            year: result.year,
+                            age_range: result.age_range,
+                            total: result.total,
+                            total_partial: partial.total_med,
+                            total_taxa: ((partial.total_med/result.total) * 100).toFixed(2),
+                        });                        
+                        found = true;
+                        break;
+                    default:
+                        break;
+                    
+
+            }
+        }
+        });
+        if (!found) {
+            match.push({
+                year: result.year,
+                age_range: result.age_range,
+                total: result.total,
+                total_taxa: 0,
+            });
+        }   
+    });
+
+    return match;
+}
+
+
+EnrollmentRateApp.get('/', rqf.parse(), rqf.build(),  (req, res, next) => {
+req.querySet = [];
+
+    let padrao = req.sql.clone()
+    .from('escola')
+    .field('escola.ano_censo', 'year')
+    .field('sum(escola.qt_mat_inf_cre)', 'total_inf_cre')
+    .field('sum(escola.qt_mat_inf_pre)', 'total_inf_pre')
+    .field('sum(escola.qt_mat_fund_ai)', 'total_fund_ai')
+    .field('sum(escola.qt_mat_fund_af)', 'total_fund_af')
+    .field('sum(escola.qt_mat_med)', 'total_med')
+    .where('escola.situacao_funcionamento_pareada = 1')
+    .where('(escola.ensino_regular = 1 or escola.ensino_eja = 1 or escola.educacao_profissional = 1)')
+    .where('escola.ano_censo IN (2019, 2022, 2023)')
+    .group('escola.ano_censo');
+
+    let subquery = req.sql.clone()
+    .from('pnad_novo')
+    .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total')
+    .field('pnad_novo.faixa_etaria', 'age_range')
+    .field('pnad_novo.ano_ref', 'year')
+    .where('pnad_novo.ano_ref IN (2019, 2022, 2023) AND pnad_novo.faixa_etaria IN (1, 2, 3, 4, 5)')
+    .group('pnad_novo.ano_ref')
+    .group('pnad_novo.faixa_etaria')
+    .order('pnad_novo.ano_ref')
+    .order('pnad_novo.faixa_etaria');
+
+req.querySet.push(padrao);
+req.querySet.push(subquery);
+next();
+
+}, multiQuery, (req, res, next) => {
+    // The multiple requests are made. Then we need to calculate the percetange. So the function
+    // below is used
+    let newObj = matchQueries(req.result[0], req.result[1]);
+    req.result = newObj;
+next();
+}, id2str.transform(false), response('enrollmentRate'));
+
+module.exports = EnrollmentRateApp;
-- 
GitLab