Skip to content
Snippets Groups Projects
Commit 03de2702 authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

finish route

parent 79dad9e5
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!109Enrollment liquid ratio
Pipeline #
This commit is part of merge request !109. Comments created here will be created in the context of that merge request.
module.exports = function educationLevelBasic(id) {
switch (id) {
case 1:
return 'Creche';
case 2:
return 'Pré-Escola';
case 4:
return 'Ensino Fundamental - anos iniciais';
case 5:
return 'Ensino Fundamental - anos finais';
case 6:
return 'Ensino Médio';
default:
return 'Não classificada';
}
};
...@@ -23,12 +23,14 @@ const ageRange = require(`${libs}/convert/ageRange`); ...@@ -23,12 +23,14 @@ const ageRange = require(`${libs}/convert/ageRange`);
const genderPnad = require(`${libs}/convert/genderPnad`); const genderPnad = require(`${libs}/convert/genderPnad`);
const fifthHouseholdIncome = require(`${libs}/convert/fifthHouseholdIncome`); const fifthHouseholdIncome = require(`${libs}/convert/fifthHouseholdIncome`);
const extremesHouseholdIncome = require(`${libs}/convert/extremesHouseholdIncome`); const extremesHouseholdIncome = require(`${libs}/convert/extremesHouseholdIncome`);
const educationLevelBasic = require(`${libs}/convert/educationLevelBasic`);
const ids = { const ids = {
gender_id: gender, gender_id: gender,
period_id: period, period_id: period,
school_year_id: schoolYear, school_year_id: schoolYear,
education_level_id: educationLevel, education_level_id: educationLevel,
education_level_basic_id: educationLevelBasic,
education_level_mod_id: educationLevelMod, education_level_mod_id: educationLevelMod,
education_level_short_id: educationLevelShort, education_level_short_id: educationLevelShort,
adm_dependency_id: admDependency, adm_dependency_id: admDependency,
...@@ -102,6 +104,7 @@ module.exports = { ...@@ -102,6 +104,7 @@ module.exports = {
period, period,
schoolYear, schoolYear,
educationLevel, educationLevel,
educationLevelBasic,
educationLevelMod, educationLevelMod,
educationLevelShort, educationLevelShort,
admDependency, admDependency,
......
const express = require('express'); const express = require('express');
const glossEnrollmentRatioApp = express.Router(); const liquidEnrollmentRatioApp = express.Router();
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
...@@ -30,11 +30,11 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i ...@@ -30,11 +30,11 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i
let rqf = new ReqQueryFields(); let rqf = new ReqQueryFields();
glossEnrollmentRatioApp.use(cache('15 day')); liquidEnrollmentRatioApp.use(cache('15 day'));
// Complete range of the enrollments dataset. // Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset. // Returns a tuple of start and ending years of the complete enrollments dataset.
glossEnrollmentRatioApp.get('/year_range', (req, res, next) => { liquidEnrollmentRatioApp.get('/year_range', (req, res, next) => {
req.sql.from('pnad') req.sql.from('pnad')
.field('DISTINCT pnad.ano_censo', 'year'); .field('DISTINCT pnad.ano_censo', 'year');
next(); next();
...@@ -61,7 +61,7 @@ glossEnrollmentRatioApp.get('/year_range', (req, res, next) => { ...@@ -61,7 +61,7 @@ glossEnrollmentRatioApp.get('/year_range', (req, res, next) => {
next(); next();
}, response('range')); }, response('range'));
glossEnrollmentRatioApp.get('/years', (req, res, next) => { liquidEnrollmentRatioApp.get('/years', (req, res, next) => {
req.sql.from('pnad') req.sql.from('pnad')
.field('DISTINCT pnad.ano_censo', 'year'); .field('DISTINCT pnad.ano_censo', 'year');
next(); next();
...@@ -86,14 +86,14 @@ glossEnrollmentRatioApp.get('/years', (req, res, next) => { ...@@ -86,14 +86,14 @@ glossEnrollmentRatioApp.get('/years', (req, res, next) => {
next(); next();
}, response('years')); }, response('years'));
glossEnrollmentRatioApp.get('/source', (req, res, next) => { liquidEnrollmentRatioApp.get('/source', (req, res, next) => {
req.sql.from('fonte') req.sql.from('fonte')
.field('fonte', 'source') .field('fonte', 'source')
.where('tabela = \'pnad\''); .where('tabela = \'pnad\'');
next(); next();
}, query, response('source')); }, query, response('source'));
glossEnrollmentRatioApp.get('/education_level_short', (req, res, next) => { liquidEnrollmentRatioApp.get('/education_level_basic', (req, res, next) => {
req.result = [ req.result = [
{id: null, name: 'Não classificada'}, {id: null, name: 'Não classificada'},
{id: 1, name: 'Creche'}, {id: 1, name: 'Creche'},
...@@ -103,9 +103,9 @@ glossEnrollmentRatioApp.get('/education_level_short', (req, res, next) => { ...@@ -103,9 +103,9 @@ glossEnrollmentRatioApp.get('/education_level_short', (req, res, next) => {
{id: 6, name: 'Ensino Médio'} {id: 6, name: 'Ensino Médio'}
]; ];
next(); next();
}, response('education_level_short')); }, response('education_level_basic'));
glossEnrollmentRatioApp.get('/gender', (req, res, next) => { liquidEnrollmentRatioApp.get('/gender', (req, res, next) => {
req.result = [ req.result = [
{id: 1, name: 'Masculino'}, {id: 1, name: 'Masculino'},
{id: 2, name: 'Feminino'} {id: 2, name: 'Feminino'}
...@@ -113,7 +113,7 @@ glossEnrollmentRatioApp.get('/gender', (req, res, next) => { ...@@ -113,7 +113,7 @@ glossEnrollmentRatioApp.get('/gender', (req, res, next) => {
next(); next();
}, response('gender')); }, response('gender'));
glossEnrollmentRatioApp.get('/ethnic_group', (req, res, next) => { liquidEnrollmentRatioApp.get('/ethnic_group', (req, res, next) => {
req.result = [ req.result = [
{id: 0, name: 'Sem declaração'}, {id: 0, name: 'Sem declaração'},
{id: 1, name: 'Branca'}, {id: 1, name: 'Branca'},
...@@ -125,7 +125,7 @@ glossEnrollmentRatioApp.get('/ethnic_group', (req, res, next) => { ...@@ -125,7 +125,7 @@ glossEnrollmentRatioApp.get('/ethnic_group', (req, res, next) => {
next(); next();
}, response('ethnic_group')); }, response('ethnic_group'));
glossEnrollmentRatioApp.get('/location', (req, res, next) => { liquidEnrollmentRatioApp.get('/location', (req, res, next) => {
req.result = [ req.result = [
{id: 1, name: 'Urbana'}, {id: 1, name: 'Urbana'},
{id: 2, name: 'Rural'} {id: 2, name: 'Rural'}
...@@ -224,10 +224,10 @@ rqf.addField({ ...@@ -224,10 +224,10 @@ rqf.addField({
field: 'localizacao_id' field: 'localizacao_id'
} }
}).addValue({ }).addValue({
name: 'education_level_short', name: 'education_level_basic',
table: 'matricula', table: 'matricula',
tableField: 'etapa_resumida', tableField: 'etapa_resumida',
resultField: 'education_level_short_id', resultField: 'education_level_basic_id',
where: { where: {
relation: '=', relation: '=',
type: 'integer', type: 'integer',
...@@ -237,48 +237,62 @@ rqf.addField({ ...@@ -237,48 +237,62 @@ rqf.addField({
function matchQueries(queryTotal, queryPartial) { function matchQueries(queryTotal, queryPartial) {
let match = []; let match = [];
queryTotal.forEach((result) => { queryPartial.forEach((result) => {
let newObj = {}; let newObj = {};
let keys = Object.keys(result); let keys = Object.keys(result);
keys.forEach((key) => { keys.forEach((key) => {
newObj[key] = result[key]; newObj[key] = result[key];
}); });
// console.log('NEW OBJ');
// console.log(newObj);
// remove total
let index = keys.indexOf('total'); let index = keys.indexOf('total');
if(index > -1) keys.splice(index, 1); if(index > -1) keys.splice(index, 1);
// remove education_level_basic_id
index = keys.indexOf('education_level_basic_id');
if(index > -1) keys.splice(index, 1);
// remove education_level_basic_name
index = keys.indexOf('education_level_basic_name');
if(index > -1) keys.splice(index, 1);
let objMatch = null; let objMatch = null;
for(let i = 0; i < queryPartial.length; ++i) { for(let i = 0; i < queryTotal.length; ++i) {
let partial = queryPartial[i]; let total = queryTotal[i];
let foundMatch = true; let foundMatch = true;
for(let j = 0; j < keys.length; ++j) { for(let j = 0; j < keys.length; ++j) {
let key = keys[j]; let key = keys[j];
if(partial[key] !== result[key]) { if(total[key] !== result[key]) {
foundMatch = false; foundMatch = false;
break; break;
} }
} }
if(foundMatch) { if(foundMatch) {
objMatch = partial; objMatch = total;
break; break;
} }
} }
if(objMatch) { if(objMatch) {
newObj.total = (objMatch.total / result.total) * 100; // console.log('MATCH!!!!');
newObj.partial = objMatch.total; // console.log(objMatch);
newObj.denominator = result.total newObj.total = (result.total / objMatch.total) * 100;
newObj.partial = result.total;
newObj.denominator = objMatch.total
match.push(newObj); match.push(newObj);
} }
}); });
// console.log('TAMANHOS');
// console.log(queryTotal.length);
// console.log(queryPartial.length);
// console.log(match.length);
return match; return match;
} }
glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => { liquidEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
req.numerator = {}; req.numerator = {};
req.denominator = {}; req.denominator = {};
let glossEnrollmentRatioApp = {}; let liquidEnrollmentRatioApp = {};
log.debug(req.sql.toParam()); log.debug(req.sql.toParam());
req.sql.from('matricula') req.sql.from('matricula')
...@@ -300,13 +314,13 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => { ...@@ -300,13 +314,13 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
return 'matricula.faixa_etaria_31_03 = 5' return 'matricula.faixa_etaria_31_03 = 5'
} }
} }
if ("education_level_short" in req.filter) { if ("education_level_basic" in req.filter) {
if (Array.isArray(req.filter.education_level_short)) if (Array.isArray(req.filter.education_level_basic))
var string_query_enrollment = ''; var string_query_enrollment = '';
for(let i = 0; i < req.filter.education_level_short.length - 1; i++) { for(let i = 0; i < req.filter.education_level_basic.length - 1; i++) {
string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_short[i]) + ' OR '; string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_basic[i]) + ' OR ';
} }
string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_short[req.filter.education_level_short.length - 1]); string_query_enrollment = string_query_enrollment + ConvertMatricula(req.filter.education_level_basic[req.filter.education_level_basic.length - 1]);
req.sql.where(string_query_enrollment); req.sql.where(string_query_enrollment);
} else { } else {
res.status(400); res.status(400);
...@@ -317,7 +331,7 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => { ...@@ -317,7 +331,7 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
} }
next(); next();
}, rqf.build(), query, (req, res, next) => { }, rqf.build(), query, id2str.transform(), (req, res, next) => {
req.numerator = req.result; req.numerator = req.result;
req.resetSql(); req.resetSql();
log.debug(req.sql.toParam()); log.debug(req.sql.toParam());
...@@ -341,26 +355,29 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => { ...@@ -341,26 +355,29 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
} }
} }
//remove education_level_short how filter and add faixa_etaria_31_03 in filter //remove education_level_basic how filter and add faixa_etaria_31_03 in filter
if ("education_level_short" in req.filter) { if ("education_level_basic" in req.filter) {
if (Array.isArray(req.filter.education_level_short)) if (Array.isArray(req.filter.education_level_basic))
var string_query = ''; var string_query = '';
for(let i = 0; i < req.filter.education_level_short.length - 1; i++) { for(let i = 0; i < req.filter.education_level_basic.length - 1; i++) {
string_query = string_query + convertPnad(req.filter.education_level_short[i]) + ' OR '; string_query = string_query + convertPnad(req.filter.education_level_basic[i]) + ' OR ';
} }
string_query = string_query + convertPnad(req.filter.education_level_short[req.filter.education_level_short.length - 1]); string_query = string_query + convertPnad(req.filter.education_level_basic[req.filter.education_level_basic.length - 1]);
req.sql.where(string_query); req.sql.where(string_query);
} else { } else {
req.sql.where(convertPnad(req.filter.education_level_short)); req.sql.where(convertPnad(req.filter.education_level_basic));
} }
next(); next();
}, rqf.parse(), (req, res, next) => { }, rqf.parse(), (req, res, next) => {
if ("education_level_short" in req.filter) { if ("education_level_basic" in req.filter) {
delete req.filter.education_level_short; delete req.filter.education_level_basic;
}
if ("education_level_basic" in req.dims) {
delete req.dims.education_level_basic;
} }
next(); next();
}, rqf.build(), query, (req, res, next) => { }, rqf.build(), query, id2str.transform(), (req, res, next) => {
req.denominator = req.result; req.denominator = req.result;
log.debug(req.numerator); log.debug(req.numerator);
log.debug(req.denominator); log.debug(req.denominator);
...@@ -368,9 +385,9 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => { ...@@ -368,9 +385,9 @@ glossEnrollmentRatioApp.get('/', rqf.parse(),(req, res, next) => {
//division to generate req.result final //division to generate req.result final
req.result = [] req.result = []
log.debug(Object.keys(req.dims)) log.debug(Object.keys(req.dims))
let school_place = matchQueries(req.denominator, req.numerator); let liquidEnrollment = matchQueries(req.denominator, req.numerator);
req.result = school_place; req.result = liquidEnrollment;
next(); next();
}, response('liquuidEnrollmentRatio')); }, response('liquuidEnrollmentRatio'));
module.exports = glossEnrollmentRatioApp; module.exports = liquidEnrollmentRatioApp;
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