Skip to content
Snippets Groups Projects
Commit aad36ec1 authored by lgtg20's avatar lgtg20
Browse files

feat: dimensions for all necessary data

parent b672b91b
No related branches found
No related tags found
4 merge requests!449Homologa,!444dev -> hom,!440New indicators,!439Issue 935
...@@ -142,16 +142,6 @@ rqf.addField({ ...@@ -142,16 +142,6 @@ rqf.addField({
foreign: 'cod_uf', foreign: 'cod_uf',
foreignTable: 'pnad_novo' 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({ }).addValue({
name: 'bolsa_familia', name: 'bolsa_familia',
table: 'pnad_novo', table: 'pnad_novo',
...@@ -265,57 +255,32 @@ rqf.addField({ ...@@ -265,57 +255,32 @@ rqf.addField({
} }
}); });
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(), (req, res, next) => { adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => {
const dimensions = req.dims || {}; // Obter dimensões solicitadas pela requisição
// Subquery: total_pop // Subquery: total_pop
let totalPop = squel.select() let totalPop = squel.select()
.field("ano_ref") .field("ano_ref")
.field("faixa_etaria") .field("faixa_etaria")
.field("SUM(peso_domicilio_pessoas_com_cal)", "total") .field("SUM(peso_domicilio_pessoas_com_cal)", "total")
.from("pnad_novo") .from("pnad_novo")
.group("ano_ref") .where("faixa_etaria <= 6")
.group("faixa_etaria"); .group("ano_ref")
.group("faixa_etaria");
// Aplicando filtros dinâmicos à subquery total_pop
// Aplicar filtros dinâmicos à subquery total_pop
totalPop = rqf.buildQuery(req, totalPop); totalPop = rqf.buildQuery(req, totalPop);
const joinQuery = `
total_pop.ano_ref = pnad_novo.ano_ref AND
total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada
${dimensions.gender ? " AND total_pop.gender_id = pnad_novo.sexo" : ""}
${dimensions.bolsa_familia ? "AND total_pop.bolsa_familia_id = pnad_novo.recebeu_rendimentos_de_programa_bolsa_familia" : ""}
${dimensions.new_pnad_ethnic_group ? "AND total_pop.new_pnad_ethnic_group_id = pnad_novo.cor_raca" : ""}
${dimensions.income_range ? "AND total_pop.income_range_id = pnad_novo.faixa_rendimento_aux_tx" : ""}
`
// Subquery: total_apoio_freq // Subquery: total_apoio_freq
let totalApoioFreq = squel.select() let totalApoioFreq = squel.select()
...@@ -326,26 +291,40 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { ...@@ -326,26 +291,40 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => {
.from("pnad_novo") .from("pnad_novo")
.join( .join(
totalPop, totalPop,
"total_pop", "total_pop",
"total_pop.ano_ref = pnad_novo.ano_ref AND total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada" joinQuery
) )
.group("pnad_novo.ano_ref") .group("pnad_novo.ano_ref")
.group("pnad_novo.apoio_frequencia_ajustada") .group("pnad_novo.apoio_frequencia_ajustada")
.group("total_pop.total"); .group("total_pop.total");
// Aplicando filtros à subquery total_apoio_freq // Aplicar filtros dinâmicos à subquery total_apoio_freq
totalApoioFreq = rqf.buildQuery(req, totalApoioFreq); totalApoioFreq = rqf.buildQuery(req, totalApoioFreq);
// Query principal // Query Principal
req.sql = squel.select() let mainQuery = squel.select()
.from(totalApoioFreq, "total_apoio_freq")
.field("total_apoio_freq.ano_ref") .field("total_apoio_freq.ano_ref")
.field("total_apoio_freq.apoio_frequencia_ajustada") .field("total_apoio_freq.apoio_frequencia_ajustada", "age_range_all_id")
.field("ROUND((total_apoio_freq.total_freq / total_apoio_freq.total) * 100, 1)", "total") .field("ROUND((total_apoio_freq.total_freq / total_apoio_freq.total) * 100, 1)", "total")
.from(totalApoioFreq, "total_apoio_freq") .where("total_apoio_freq.ano_ref >= 2019")
.where("total_apoio_freq.ano_ref >= 2019") // Corrigido para usar alias correto
.order("total_apoio_freq.ano_ref") .order("total_apoio_freq.ano_ref")
.order("total_apoio_freq.apoio_frequencia_ajustada"); .order("total_apoio_freq.apoio_frequencia_ajustada");
if (dimensions.gender)
mainQuery.field("total_apoio_freq.gender_id")
if (dimensions.bolsa_familia)
mainQuery.field("total_apoio_freq.bolsa_familia_id")
if (dimensions.new_pnad_ethnic_group)
mainQuery.field("total_apoio_freq.new_pnad_ethnic_group_id")
if (dimensions.income_range)
mainQuery.field("total_apoio_freq.income_range_id")
req.sql = mainQuery;
next(); next();
}, query, id2str.transform(false), response('adjusted_liquid_frequency')); }, query, id2str.transform(false), response('adjusted_liquid_frequency'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment