Skip to content
Snippets Groups Projects
Commit e3742a20 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Merge branch 'issue/422' into issue/423

parents 17206b43 de5e225e
No related branches found
No related tags found
No related merge requests found
......@@ -467,4 +467,333 @@ enrollmentApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
enrollmentApp.get('/download', passport.authenticate('bearer', { session: false }), rqf.parse(), rqf.build(), download('matricula', 'mapping_matricula'));
enrollmentApp.get('/diagnosis', rqf.parse(), (req, res, next) => {
req.dims = {};
req.dims.school_year = true;
req.dims.location = true;
req.dims.adm_dependency_detailed = true;
req.sql.field('COUNT(*)', 'total')
.field("'Brasil'", 'name')
.field('matricula.ano_censo', 'year')
.from('matricula')
.group('matricula.ano_censo')
.order('matricula.ano_censo')
.where('matricula.tipo<=3');
next();
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
let enrollments = req.result;
// Gera a relação etapa de ensino X ano escolar
let educationSchoolYear = {};
for(let i = 10; i < 80; ++i) {
if(id2str.schoolYear(i) !== id2str.schoolYear(99)) {
let educationLevelId = Math.floor(i/10);
educationSchoolYear[i] = {
id: educationLevelId,
name: id2str.educationLevelShort(educationLevelId),
};
}
}
let result = [];
let educationLevelSet = new Set();
let schoolYearSet = new Set();
let i = 0;
while(i < enrollments.length) {
let enrollment = enrollments[i];
let educationLevelHash = '' + enrollment.year + educationSchoolYear[enrollment.school_year_id].id;
let schoolYearHash = '' + enrollment.year + enrollment.school_year_id;
let currentEducation = null;
// Busca ou cria a etapa de ensino adequada
if(educationLevelSet.has(educationLevelHash)) {
let j = 0;
let edu = result[j];
while(j < result.length && (edu.year != enrollment.year || edu.education_level_school_year_id != educationSchoolYear[enrollment.school_year_id].id)) {
++j;
edu = result[j];
}
if(j >= result.length) --j;
edu = result[j];
currentEducation = edu;
} else {
educationLevelSet.add(educationLevelHash);
let obj = {
year: enrollment.year,
name: enrollment.name,
education_level_school_year_id: educationSchoolYear[enrollment.school_year_id].id,
education_level_school_year_name: educationSchoolYear[enrollment.school_year_id].name,
total: 0,
adm_dependencies: [
{
adm_dependency_detailed_id: enrollment.adm_dependency_detailed_id,
adm_dependency_detailed_name: enrollment.adm_dependency_detailed_name,
total: 0
}
],
locations: [
{
location_id: enrollment.location_id,
location_name: enrollment.location_name,
total: 0
}
]
};
result.push(obj);
currentEducation = obj;
}
let currentSchoolYear = null;
// Busca ou cria a série adequada
if(schoolYearSet.has(schoolYearHash)) {
let j = 0;
let edu = result[j];
while(j < result.length && (edu.year != enrollment.year || edu.education_level_school_year_id != enrollment.school_year_id)) {
++j;
edu = result[j];
}
if(j >= result.length) --j;
edu = result[j];
currentSchoolYear = edu;
} else {
schoolYearSet.add(schoolYearHash);
let obj = {
year: enrollment.year,
name: enrollment.name,
education_level_school_year_id: enrollment.school_year_id,
education_level_school_year_name: enrollment.school_year_name,
total: 0,
adm_dependencies: [
{
adm_dependency_detailed_id: enrollment.adm_dependency_detailed_id,
adm_dependency_detailed_name: enrollment.adm_dependency_detailed_name,
total: 0
}
],
locations: [
{
location_id: enrollment.location_id,
location_name: enrollment.location_name,
total: 0
}
]
};
result.push(obj);
currentSchoolYear = obj;
}
// Adiciona ao total
currentEducation.total += enrollment.total;
currentSchoolYear.total += enrollment.total;
// Adiciona ao total da dependência administrativa
let admDependencyIndex = 0;
let admDependency = currentEducation.adm_dependencies[admDependencyIndex];
while (admDependencyIndex < currentEducation.adm_dependencies.length && enrollment.adm_dependency_detailed_id > admDependency.adm_dependency_detailed_id) {
++admDependencyIndex;
admDependency = currentEducation.adm_dependencies[admDependencyIndex];
}
if(admDependencyIndex >= currentEducation.adm_dependencies.length || admDependency.adm_dependency_detailed_id != enrollment.adm_dependency_detailed_id) { // não encontrou
let obj = {
adm_dependency_detailed_id: enrollment.adm_dependency_detailed_id,
adm_dependency_detailed_name: enrollment.adm_dependency_detailed_name,
total: 0
}
currentEducation.adm_dependencies.splice(admDependencyIndex, 0, obj);
admDependency = obj;
}
admDependency.total += enrollment.total;
admDependencyIndex = 0;
admDependency = currentSchoolYear.adm_dependencies[admDependencyIndex];
while (admDependencyIndex < currentSchoolYear.adm_dependencies.length && enrollment.adm_dependency_detailed_id > admDependency.adm_dependency_detailed_id) {
++admDependencyIndex;
admDependency = currentSchoolYear.adm_dependencies[admDependencyIndex];
}
if(admDependencyIndex >= currentSchoolYear.adm_dependencies.length || admDependency.adm_dependency_detailed_id != enrollment.adm_dependency_detailed_id) { // não encontrou
let obj = {
adm_dependency_detailed_id: enrollment.adm_dependency_detailed_id,
adm_dependency_detailed_name: enrollment.adm_dependency_detailed_name,
total: 0
}
currentSchoolYear.adm_dependencies.splice(admDependencyIndex, 0, obj);
admDependency = obj;
}
admDependency.total += enrollment.total;
// Adiciona ao total da localidade
let locationIndex = 0;
let location = currentEducation.locations[locationIndex];
while (locationIndex < currentEducation.locations.length && enrollment.location_id > location.location_id) {
++locationIndex;
location = currentEducation.locations[locationIndex];
}
if(locationIndex >= currentEducation.locations.length || location.location_id != enrollment.location_id) {
let obj = {
location_id: enrollment.location_id,
location_name: enrollment.location_name,
total: 0
}
currentEducation.locations.splice(locationIndex, 0, obj);
location = obj;
}
location.total += enrollment.total;
locationIndex = 0;
location = currentSchoolYear.locations[locationIndex];
while (locationIndex < currentSchoolYear.locations.length && enrollment.location_id > location.location_id) {
++locationIndex;
location = currentSchoolYear.locations[locationIndex];
}
if(locationIndex >= currentSchoolYear.locations.length || location.location_id != enrollment.location_id) {
let obj = {
location_id: enrollment.location_id,
location_name: enrollment.location_name,
total: 0
}
currentSchoolYear.locations.splice(locationIndex, 0, obj);
location = obj;
}
location.total += enrollment.total;
++i;
}
req.result = result;
next();
}, response('enrollment_diagnosis'));
enrollmentApp.get('/projection', rqf.parse(), (req, res, next) => {
req.dims = {};
req.dims.location = true;
req.dims.school_year = true;
req.dims.adm_dependency = true;
req.dims.period = true;
req.filter.adm_dependency = [1,2,3];
req.sql.field('COUNT(*)', 'total')
.field("'Brasil'", 'name')
.field('matricula.ano_censo', 'year')
.from('matricula')
.group('matricula.ano_censo')
.order('matricula.ano_censo')
.where('matricula.tipo<=3');
next();
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
let enrollments = req.result;
// Gera a relação etapa de ensino X ano escolar
let educationSchoolYear = {};
for(let i = 10; i < 80; ++i) {
if(id2str.schoolYear(i) !== id2str.schoolYear(99)) {
let educationLevelId = Math.floor(i/10);
educationSchoolYear[i] = {
id: educationLevelId,
name: id2str.educationLevelShort(educationLevelId),
};
}
}
let result = [];
let educationLevelSet = new Set();
let schoolYearSet = new Set();
let i = 0;
while(i < enrollments.length) {
let enrollment = enrollments[i];
let educationLevelHash = '' + enrollment.year + educationSchoolYear[enrollment.school_year_id].id;
let schoolYearHash = '' + enrollment.year + enrollment.school_year_id;
let currentEducation = null;
// Busca ou cria a etada de ensino adequada
if(educationLevelSet.has(educationLevelHash)) {
let j = 0;
let edu = result[j];
while(j < result.length && (edu.year != enrollment.year || edu.education_level_school_year_id != educationSchoolYear[enrollment.school_year_id].id)) {
++j;
edu = result[j];
}
if((j >= result.length)) --j;
edu = result[j];
currentEducation = edu;
} else {
educationLevelSet.add(educationLevelHash);
let obj = {
year: enrollment.year,
name: enrollment.name,
education_level_school_year_id: educationSchoolYear[enrollment.school_year_id].id,
education_level_school_year_name: educationSchoolYear[enrollment.school_year_id].name,
urban_day_total: 0,
urban_night_total: 0,
rural_day_total: 0,
rural_night_total: 0
};
result.push(obj);
currentEducation = obj;
}
let currentSchoolYear = null;
// Busca ou cria a série adequada
if(schoolYearSet.has(schoolYearHash)) {
let j = 0;
let edu = result[j];
while(j < result.length && (edu.year != enrollment.year || edu.education_level_school_year_id != enrollment.school_year_id)){
++j;
edu = result[j];
}
if(j >= result.length) --j;
edu = result[j];
currentSchoolYear = edu;
} else {
schoolYearSet.add(schoolYearHash);
let obj = {
year: enrollment.year,
name: enrollment.name,
education_level_school_year_id: enrollment.school_year_id,
education_level_school_year_name: enrollment.school_year_name,
urban_day_total: 0,
urban_night_total: 0,
rural_day_total: 0,
rural_night_total: 0
};
result.push(obj);
currentSchoolYear = obj;
}
if(enrollment.location_id == 1) {
if(enrollment.period_id < 3) {
currentEducation.urban_day_total += enrollment.total;
currentSchoolYear.urban_day_total += enrollment.total;
} else {
currentEducation.urban_night_total += enrollment.total;
currentSchoolYear.urban_night_total += enrollment.total;
}
} else {
if(enrollment.period_id < 3) {
currentEducation.rural_day_total += enrollment.total;
currentSchoolYear.rural_day_total += enrollment.total;
} else {
currentEducation.rural_night_total += enrollment.total;
currentSchoolYear.rural_night_total += enrollment.total;
}
}
++i;
}
req.result = result;
next();
}, response('enrollment_projection'));
module.exports = enrollmentApp;
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