diff --git a/src/libs/routes_v1/enrollmentRate.js b/src/libs/routes_v1/enrollmentRate.js index 6a87a5f17c3418374e16628f77a8bfd201556d90..aaf2df29b5a4f0137adfcf8be831136db2eda152 100644 --- a/src/libs/routes_v1/enrollmentRate.js +++ b/src/libs/routes_v1/enrollmentRate.js @@ -42,6 +42,9 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i let rqf = new ReqQueryFields(); +let rqfSchool = new ReqQueryFields(); + + EnrollmentRateApp.use(cache('15 day')); EnrollmentRateApp.get('/years', (req, res, next) => { @@ -139,6 +142,26 @@ rqf.addField({ name: 'dims', field: true, where: false +}).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: 'ano_ref', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_ref' + } }).addValue({ name: 'situacao_domicilio', table: 'pnad_novo', @@ -234,96 +257,93 @@ rqf.addField({ 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) { - match.push({ - year: result.year, - age_range: result.age_range, - age_range_name: id2str.ageRange(result.age_range), - location: result.location, - location_name: id2str.location(result.location), - cap_code: result.cap_code, - cap_code_name: id2str.capitalCode(result.cap_code), - region: result.region, - region_name: id2str.regionCode(result.region), - state: result.state, - state_name: id2str.stateName(result.state) - }); + const escolaMap = new Map(); + queryPartial.forEach(partial => { + escolaMap.set(partial.year, partial); + }); - switch (result.age_range) { - case 1: - match.push({ - // total: result.total, - // total_partial: partial.total_inf_cre, - attended_modality_id: 1, - attended_modality_name: id2str.attendedModality(1), - total: ((partial.total_inf_cre/result.total) * 100).toFixed(2), - }); - found = true; - break; - case 2: - match.push({ - attended_modality_id: 2, - attended_modality_name: id2str.attendedModality(2), - total: ((partial.total_inf_pre/result.total) * 100).toFixed(2), - }); - found = true; - break; - case 3: - match.push({ - attended_modality_id: 4, - attended_modality_name: id2str.attendedModality(4), - total: ((partial.total_fund_ai/result.total) * 100).toFixed(2), - }); - found = true; - break; - case 4: - match.push({ - attended_modality_id: 5, - attended_modality_name: id2str.attendedModality(5), - total: ((partial.total_fund_af/result.total) * 100).toFixed(2), - }); - found = true; - break; - case 5: - match.push({ - attended_modality_id: 7, - attended_modality: id2str.attendedModality(7), - total: ((partial.total_med/result.total) * 100).toFixed(2), - }); - found = true; - break; - default: - break; - - - } - } - }); - if (!found) { + queryTotal.forEach(pnad => { + const escolaData = escolaMap.get(pnad.year); + const ageRange = pnad.age_range; + + const modalityMap = { + 1: { id: 1, field: 'total_inf_cre', name: id2str.attendedModality(1) }, + 2: { id: 2, field: 'total_inf_pre', name: id2str.attendedModality(2) }, + 3: { id: 4, field: 'total_fund_ai', name: id2str.attendedModality(4) }, + 4: { id: 5, field: 'total_fund_af', name: id2str.attendedModality(5) }, + 5: { id: 7, field: 'total_med', name: id2str.attendedModality(7) } + }; + + if (escolaData && modalityMap[ageRange]) { + const modality = modalityMap[ageRange]; + const partialValue = escolaData[modality.field] || 0; + + console.log('Partial Value:', partialValue); + console.log('Total Value:', pnad.total); + console.log('Year:', pnad.year); + console.log('Age Range:', ageRange); + console.log('Modality:', modality.name); + console.log('Modality ID:', modality.id); + + match.push({ + year: pnad.year, + age_range: ageRange, + age_range_name: id2str.ageRange(ageRange), + attended_modality_id: modality.id, + attended_modality_name: modality.name, + total: ((partialValue / pnad.total) * 100).toFixed(2), + }); + } else { match.push({ attended_modality_id: 0, attended_modality_name: 'Não informado', - total: result.total, - total_taxa: 0, + year: pnad.year, + age_range: ageRange, + total: 0, + total_population: pnad.total }); - } + } }); return match; } - EnrollmentRateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { -req.querySet = []; + req.querySet = []; + + + let whereCondition = ""; + let filterId; + const filters = req.query.filter.split(","); + filters.forEach((filter) => { + if (filter.includes("min_year")) { + filterId = filter.split(":")[1]; + whereCondition += `pnad_novo.ano_ref >= ${filterId}`; + } + if (filter.includes("max_year")) { + filterId = filter.split(":")[1]; + whereCondition += `pnad_novo.ano_ref <= ${filterId}`; + } + + }) - let padrao = req.sql.clone() - .from('escola') + + // let query_escola = req.sql.clone(); + // query_escola.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 query_escola = squel.select() + query_escola.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') @@ -334,36 +354,45 @@ req.querySet = []; .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') + req.querySet.push(query_escola); + + let query_pnad = req.sql.clone(); + query_pnad.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.cod_uf', 'state') - .field('pnad_novo.situacao_domicilio', 'location') - .field('pnad_novo.cod_cap', 'cap_code') - .field('pnad_novo.cod_regiao', 'region') .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)') + .where(whereCondition) .group('pnad_novo.ano_ref') .group('pnad_novo.faixa_etaria') - .group('pnad_novo.cod_uf') - .group('pnad_novo.situacao_domicilio') - .group('pnad_novo.cod_cap') - .group('pnad_novo.cod_regiao') .order('pnad_novo.ano_ref') .order('pnad_novo.faixa_etaria'); - -req.querySet.push(padrao); -req.querySet.push(subquery); -next(); + req.querySet.push(query_pnad); + + // let query_pnad = squel.select() + // query_pnad.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)') + // .where(`${whereCondition}`) + // .group('pnad_novo.ano_ref') + // .group('pnad_novo.faixa_etaria') + // .order('pnad_novo.ano_ref') + // .order('pnad_novo.faixa_etaria'); + + console.log(query_escola.toString()) + console.log(query_pnad.toString()) + + // req.querySet.push(query_escola); + // req.querySet.push(query_pnad); + 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(); -}, response('enrollmentRate')); +}, id2str.transform(false), response('enrollmentRate')); module.exports = EnrollmentRateApp;