Skip to content
Snippets Groups Projects
Commit 53c12859 authored by tgcl21's avatar tgcl21
Browse files

[FIX] fixed min_year and max_year not working

parent 67d966e5
Branches
No related tags found
No related merge requests found
...@@ -42,6 +42,9 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i ...@@ -42,6 +42,9 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i
let rqf = new ReqQueryFields(); let rqf = new ReqQueryFields();
let rqfSchool = new ReqQueryFields();
EnrollmentRateApp.use(cache('15 day')); EnrollmentRateApp.use(cache('15 day'));
EnrollmentRateApp.get('/years', (req, res, next) => { EnrollmentRateApp.get('/years', (req, res, next) => {
...@@ -139,6 +142,26 @@ rqf.addField({ ...@@ -139,6 +142,26 @@ rqf.addField({
name: 'dims', name: 'dims',
field: true, field: true,
where: false 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({ }).addValue({
name: 'situacao_domicilio', name: 'situacao_domicilio',
table: 'pnad_novo', table: 'pnad_novo',
...@@ -234,83 +257,51 @@ rqf.addField({ ...@@ -234,83 +257,51 @@ rqf.addField({
function matchQueries(queryPartial, queryTotal) { function matchQueries(queryPartial, queryTotal) {
let match = []; 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)
});
switch (result.age_range) { const escolaMap = new Map();
case 1: queryPartial.forEach(partial => {
match.push({ escolaMap.set(partial.year, partial);
// 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;
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),
}); });
if (!found) { } else {
match.push({ match.push({
attended_modality_id: 0, attended_modality_id: 0,
attended_modality_name: 'Não informado', attended_modality_name: 'Não informado',
total: result.total, year: pnad.year,
total_taxa: 0, age_range: ageRange,
total: 0,
total_population: pnad.total
}); });
} }
}); });
...@@ -318,12 +309,41 @@ function matchQueries(queryPartial, queryTotal) { ...@@ -318,12 +309,41 @@ function matchQueries(queryPartial, queryTotal) {
return match; return match;
} }
EnrollmentRateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { EnrollmentRateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.querySet = []; req.querySet = [];
let padrao = req.sql.clone()
.from('escola') 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 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('escola.ano_censo', 'year')
.field('sum(escola.qt_mat_inf_cre)', 'total_inf_cre') .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_inf_pre)', 'total_inf_pre')
...@@ -334,36 +354,45 @@ req.querySet = []; ...@@ -334,36 +354,45 @@ req.querySet = [];
.where('(escola.ensino_regular = 1 or escola.ensino_eja = 1 or escola.educacao_profissional = 1)') .where('(escola.ensino_regular = 1 or escola.ensino_eja = 1 or escola.educacao_profissional = 1)')
.where('escola.ano_censo IN (2019, 2022, 2023)') .where('escola.ano_censo IN (2019, 2022, 2023)')
.group('escola.ano_censo'); .group('escola.ano_censo');
req.querySet.push(query_escola);
let subquery = req.sql.clone() let query_pnad = req.sql.clone();
.from('pnad_novo') query_pnad.from('pnad_novo')
.field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total')
.field('pnad_novo.faixa_etaria', 'age_range') .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') .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('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.ano_ref')
.group('pnad_novo.faixa_etaria') .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.ano_ref')
.order('pnad_novo.faixa_etaria'); .order('pnad_novo.faixa_etaria');
req.querySet.push(query_pnad);
req.querySet.push(padrao);
req.querySet.push(subquery); // 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(); next();
}, multiQuery, (req, res, 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]); let newObj = matchQueries(req.result[0], req.result[1]);
req.result = newObj; req.result = newObj;
next(); next();
}, response('enrollmentRate')); }, id2str.transform(false), response('enrollmentRate'));
module.exports = EnrollmentRateApp; module.exports = EnrollmentRateApp;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment