Newer
Older
/*
Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
This file is part of simcaq-node.
simcaq-node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
simcaq-node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with simcaq-node. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const enrollmentApp = express.Router();
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const query = require(`${libs}/middlewares/query`).query;
const response = require(`${libs}/middlewares/response`);
const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
const id2str = require(`${libs}/middlewares/id2str`);
const addMissing = require(`${libs}/middlewares/addMissing`);
const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
enrollmentApp.use(cache('15 day'));
let rqf = new ReqQueryFields();
// Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset.
enrollmentApp.get('/year_range', (req, res, next) => {
req.sql.from('matricula')
.field('MIN(matricula.ano_censo)', 'start_year')
.field('MAX(matricula.ano_censo)', 'end_year');
next();
}, query, response('range'));
enrollmentApp.get('/years', (req, res, next) => {
req.sql.from('matricula')
enrollmentApp.get('/source', (req, res, next) => {
req.sql.from('fonte')
.field('fonte', 'source')
.where('tabela = \'matricula\'');
next();
}, query, response('source'));
enrollmentApp.get('/location', (req, res, next) => {
req.result.push({
id: i,
name: id2str.location(i)
});
};
next();
enrollmentApp.get('/rural_location', (req, res, next) => {
req.result = [
{id: 1, name: "Urbana"},
{id: 2, name: "Rural"},
{id: 3, name: "Rural - Área de assentamento"},
{id: 4, name: "Rural - Terra indígena"},
{id: 5, name: "Rural - Área remanescente de quilombos"},
{id: 6, name: "Rural - Unidade de uso sustentável"}
];
next();
}, response('rural_location'));
// Returns all school years available
enrollmentApp.get('/school_year', (req, res, next) => {
req.result = [];
for(let i = 11; i <= 71; ++i) {
let obj = {
id: i,
name: id2str.schoolYear(i)
};
if(obj.name !== id2str.schoolYear(99)) {
req.result.push(obj);
}
}
req.result.push({
id: 99,
name: id2str.schoolYear(99)
});
// Returns all school years available
enrollmentApp.get('/education_level', (req, res, next) => {
req.result = [];
for(let i = 1; i <= 74; ++i) {
let obj = {
id: i,
name: id2str.educationLevel(i)
};
if(obj.name !== id2str.educationLevel(99)) {
req.result.push(obj);
}
}
req.result.push({
id: 99,
name: id2str.educationLevel(99)
});
// Returns all school years available
enrollmentApp.get('/education_level_mod', (req, res, next) => {
req.result.push({
id: i,
name: id2str.educationLevelMod(i)
});
}
req.result.push({
id: 99,
name: id2str.educationLevelMod(99)
});
enrollmentApp.get('/education_level_short', (req, res, next) => {
req.result = [
{id: null, name: 'Não classificada'},
{id: 1, name: 'Creche'},
{id: 2, name: 'Pré-Escola'},
{id: 3, name: 'Ensino Fundamental - anos iniciais'},
{id: 4, name: 'Ensino Fundamental - anos finais'},
{id: 5, name: 'Ensino Médio'},
{id: 6, name: 'EJA'},
{id: 7, name: 'EE exclusiva'}
];
next();
}, response('education_level_short'));
enrollmentApp.get('/adm_dependency', (req, res, next) => {
req.result.push({
id: i,
name: id2str.admDependency(i)
});
};
enrollmentApp.get('/adm_dependency_detailed', (req, res, next) => {
req.result.push({
id: i,
name: id2str.admDependencyPriv(i)
});
};
// Return genders
enrollmentApp.get('/gender', (req, res, next) => {
req.result = [
{id: 1, name: 'Masculino'},
{id: 2, name: 'Feminino'}
];
next();
}, response('gender'));
// Return ethnic group
enrollmentApp.get('/ethnic_group', (req, res, next) => {
req.result.push({
id: i,
name: id2str.ethnicGroup(i)
});
}
enrollmentApp.get('/period', (req, res, next) => {
req.result = [];
for(let i = 1; i <= 3; ++i) {
req.result.push({
id: i,
name: id2str.period(i)
});
}
req.result.push({
id: 99,
name: id2str.period(99)
});
enrollmentApp.get('/integral_time', (req, res, next) => {
req.result = [
{id: null, name: 'Não Disponível'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('integral_time'));
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
enrollmentApp.get('/special_class', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('special_class'));
enrollmentApp.get('/age_range_all', (req, res, next) => {
req.result = [
{id: 1, name: '0 a 3 anos'},
{id: 2, name: '4 a 5 anos'},
{id: 3, name: '6 a 10 anos'},
{id: 4, name: '11 a 14 anos'},
{id: 5, name: '15 a 17 anos'},
{id: 6, name: '18 a 24 anos'},
{id: 7, name: '25 a 29 anos'},
{id: 8, name: '30 a 40 anos'},
{id: 9, name: '41 a 50 anos'},
{id: 10, name: '51 a 64 anos'},
{id: 11, name: 'Mais que 64 anos'}
];
next();
}, response('age_range_all'));
rqf.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false
}).addValue({
name: 'adm_dependency',
table: 'matricula',
tableField: 'dependencia_adm_id',
resultField: 'adm_dependency_id',
where: {
relation: '=',
field: 'dependencia_adm_id'
}).addValue({
name: 'adm_dependency_detailed',
table: 'matricula',
tableField: 'dependencia_adm_priv',
resultField: 'adm_dependency_detailed_id',
where: {
relation: '=',
type: 'integer',
field: 'dependencia_adm_priv'
name: 'school_year',
table: 'matricula',
tableField: 'serie_ano_id',
resultField: 'school_year_id',
where: {
relation: '=',
type: 'integer',
field: 'serie_ano_id'
}).addValue({
name: 'education_level',
table: 'matricula',
tableField: 'etapa_ensino_id',
resultField: 'education_level_id',
where: {
relation: '=',
type: 'integer',
field: 'etapa_ensino_id'
}
}).addValue({
name: 'education_level_mod',
table: 'matricula',
tableField: 'etapas_mod_ensino_segmento_id',
resultField: 'education_level_mod_id',
where: {
relation: '=',
type: 'integer',
field: 'etapas_mod_ensino_segmento_id'
}).addValue({
name: 'education_level_short',
table: 'matricula',
tableField: 'etapa_resumida',
resultField: 'education_level_short_id',
where: {
relation: '=',
type: 'integer',
field: 'etapa_resumida'
}
}).addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
foreignTable: 'matricula'
}).addValueToField({
name: 'state',
table: 'estado',
tableField: ['nome', 'id'],
resultField: ['state_name', 'state_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'matricula'
}
}, 'dims').addValueToField({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
foreign: 'estado_id',
foreignTable: 'matricula'
name: 'city',
table: 'municipio',
tableField: ['nome', 'id'],
resultField: ['city_name', 'city_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'matricula'
}
}, 'dims').addValueToField({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
foreign: 'municipio_id',
foreignTable: 'matricula'
}, 'filter').addValueToField({
name: 'school',
table: 'escola',
tableField: ['nome_escola', 'id'],
resultField: ['school_name', 'school_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: ['id', 'ano_censo'],
foreign: ['escola_id', 'ano_censo'],
foreignTable: 'matricula'
}
}, 'dims').addValueToField({
name: 'locale_id',
table: 'matricula',
tableField: 'localizacao_id',
resultField: 'locale_id',
where: {
relation: '=',
type: 'integer',
field: 'localizacao_id'
}
}, 'dims').addValueToField({
name: 'school_id',
table: 'escola',
tableField: 'id',
resultField: 'school_id',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: ['id', 'ano_censo'],
foreign: ['escola_id', 'ano_censo'],
foreignTable: 'matricula'
}
name: 'school',
table: 'escola',
tableField: 'nome_escola',
resultField: 'school_name',
where: {
relation: '=',
type: 'integer',
primary: ['id', 'ano_censo'],
foreign: ['escola_id', 'ano_censo'],
foreignTable: 'matricula'
table: 'matricula',
tableField: 'localizacao_id',
resultField: 'location_id',
where: {
relation: '=',
type: 'integer',
field: 'localizacao_id'
}).addValue({
name: 'rural_location',
table: 'matricula',
tableField: 'localidade_area_rural',
resultField: 'rural_location_id',
where: {
relation: '=',
type: 'integer',
field: 'localidade_area_rural'
}
}).addValue({
name: 'min_year',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
field: 'ano_censo'
}).addValue({
name: 'gender',
table: 'matricula',
tableField: 'sexo',
resultField: 'gender_id',
where: {
relation: '=',
type: 'integer',
field: 'sexo'
}
}).addValue({
name: 'ethnic_group',
table: 'matricula',
tableField: 'cor_raca_id',
resultField: 'ethnic_group_id',
where: {
relation: '=',
table: 'matricula',
tableField: 'turma_turno_id',
resultField: 'period_id',
where: {
relation: '=',
type: 'integer',
tableField: 'tempo_integral',
resultField: 'integral_time_id',
where: {
relation: '=',
}).addValue({
name:'age_range_all',
table: 'matricula',
tableField: 'faixa_etaria_31_03',
resultField: 'age_range_all_id',
where: {
relation: '=',
type: 'integer',
field: 'faixa_etaria_31_03'
}
}).addValue({
name:'special_class',
table: 'matricula',
tableField: 'exclusiva_especial',
resultField: 'special_class_id',
where: {
relation: '=',
type: 'boolean',
field: 'exclusiva_especial'
}
});
enrollmentApp.get('/', rqf.parse(), (req, res, next) => {
req.sql.field('COUNT(*)', 'total')
.field('matricula.ano_censo', 'year')
.from('matricula')
.group('matricula.ano_censo')
.order('matricula.ano_censo')
.where('matricula.tipo<=3');
}, rqf.build(), query, id2str.transform(false), addMissing(rqf), response('enrollment'));
enrollmentApp.get('/diagnosis', rqf.parse(), (req, res, next) => {
req.dims = {};
req.dims.state = true;
req.dims.city = true;
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
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];
if(!educationSchoolYear[enrollment.school_year_id]) {
++i;
continue;
}
let educationLevelHash = '' + enrollment.year + educationSchoolYear[enrollment.school_year_id].id + enrollment.city_id;
let schoolYearHash = '' + enrollment.year + enrollment.school_year_id + enrollment.city_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,
state_id: enrollment.state_id,
state_name: enrollment.state_name,
city_id: enrollment.city_id,
city_name: enrollment.city_name,
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
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,
state_id: enrollment.state_id,
state_name: enrollment.state_name,
city_id: enrollment.city_id,
city_name: enrollment.city_name,
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
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.state = true;
req.dims.city = true;
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
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),
};
}
}
console.log(educationSchoolYear);
let result = [];
let educationLevelSet = new Set();
let schoolYearSet = new Set();
let i = 0;
while(i < enrollments.length) {
let enrollment = enrollments[i];
if(!educationSchoolYear[enrollment.school_year_id]) {
++i;
continue;
}
let educationLevelHash = '' + enrollment.year + educationSchoolYear[enrollment.school_year_id].id + enrollment.city_id;
let schoolYearHash = '' + enrollment.year + enrollment.school_year_id + enrollment.city_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,
state_id: enrollment.state_id,
state_name: enrollment.state_name,
city_id: enrollment.city_id,
city_name: enrollment.city_name,
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
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,
state_id: enrollment.state_id,
state_name: enrollment.state_name,
city_id: enrollment.city_id,
city_name: enrollment.city_name,
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
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'));