diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js index 15a0f7eef1b7c12775d1011daa20dac763f78bc8..458117dbb59a9e10fdedd39dbd86b83dfd1bda70 100644 --- a/src/libs/routes/classroomCount.js +++ b/src/libs/routes/classroomCount.js @@ -515,6 +515,8 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { obj.school_id = classroom.school_id, obj.school_name = classroom.school_name } + if (req.teacherCalc) + obj.percentage_teacher_career = []; obj.locations = [] // Inserimos a localidade no array de locations da sala @@ -530,6 +532,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { let currentClassroomObj = null; if( !hashSet.has(hash) ) { // Nunca passou por esse municÃpio/escola if (result[result.length - 1] !== undefined) { // Após mudar de cidade, passamos pela cidade anterior e juntamos o valor to_be_built de localizações com o mesmo id + let last_city = result[result.length - 1] let last_locations = result[result.length - 1].locations for (let i = 0; i < last_locations.length - 1; i++) { if (last_locations[i].location_id === last_locations[i+1].location_id) { @@ -542,7 +545,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { last_locations[i].total_classroom_be_built = (last_locations[i].total_classroom_be_built < 0) ? 0 : last_locations[i].total_classroom_be_built; } - if (req.teacherCalc) executeTeacherCalc(last_locations, ti); + if (req.teacherCalc) executeTeacherCalc(last_city, ti); } hashSet.add(hash); result.push(obj); @@ -777,6 +780,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { // Tratamento do último resultado, para remover double location, tirar negativo do to_be_built. if (result[result.length - 1] !== undefined) { // Após mudar de cidade, passamos pela cidade anterior e juntamos o valor to_be_built de localizações com o mesmo id + let last_city = result[result.length - 1] let last_locations = result[result.length - 1].locations for (let i = 0; i < last_locations.length - 1; i++) { if (last_locations[i].location_id === last_locations[i+1].location_id) { @@ -789,7 +793,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { last_locations[i].total_classroom_be_built = (last_locations[i].total_classroom_be_built < 0) ? 0 : last_locations[i].total_classroom_be_built; } - if (req.teacherCalc) executeTeacherCalc(last_locations, ti); + if (req.teacherCalc) executeTeacherCalc(last_city, ti); } } @@ -804,16 +808,17 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { let city = result[i]; let obj = { year: city.year, - name: city.name + name: city.name, + count: 1 } if(req.dims.state) { obj.state_id = city.state_id; obj.state_name = city.state_name; } - + obj.percentage_teacher_career = [] obj.locations = []; - + let hash = '' + city.year; if(req.dims.state) hash += '' + city.state_id; @@ -824,6 +829,16 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { currentObj = obj; } else { // Está ordenado, podemos pegar o último currentObj = reduction[reduction.length - 1]; + currentObj.count++; + } + + if (currentObj.count == 1) + currentObj.percentage_teacher_career = city.percentage_teacher_career + else { + // Incrementa valores percentuais de cada nivel de carreira no objeto atual + currentObj.percentage_teacher_career.forEach((item, ind, thisArr) => { + thisArr[ind].percentage += city.percentage_teacher_career[ind].percentage + }) } // Fazer "merge" do array locations da cidade com o da agregação @@ -958,6 +973,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { ++l; } } + // Reinicia vetor percentage_teacher_career e calcula porcentagens pelos totais currentLocation.total_classroom += cityLocation.total_classroom; currentLocation.total_classroom_be_built += cityLocation.total_classroom_be_built; @@ -969,6 +985,10 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { ++i; } for (let state of reduction){ + state.percentage_teacher_career.forEach((item, ind, thisArr) => { + thisArr[ind].percentage = Number((thisArr[ind].percentage / state.count).toFixed(2)) + }) + delete state.count for (let location of state.locations){ for (let educationLevel of location.education_level){ let total = educationLevel.enrollment.integral_time_total; @@ -982,7 +1002,8 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { req.result = reduction || result; - function executeTeacherCalc(lastLocations, index) { + function executeTeacherCalc(lastCity, index) { + let lastLocations = lastCity.locations let teacherByFormation = []; // Vetor com a porcentagem de professores por formação. let teacherTotal = req.teacher[index].total; @@ -1002,13 +1023,26 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { for (let value of teacherByFormation) { sum += value; } + teacherByFormation[1] += teacherByFormation[0] + teacherByFormation[0] = 0 + let diff = 1 - sum; - // Se for menor/maior que 100 soma/subtrai na P2 + + // Se soma de porcentagens for menor/maior que 100, faz correção if (Math.abs(diff) > 0.0001) { - teacherByFormation[1] += diff; + // Garante que a porcentagem corrigida não ficará negativa + let indDiff = 1; + while (teacherByFormation[indDiff] + diff < 0) indDiff++; + teacherByFormation[indDiff] += diff; } - teacherByFormation[1] += teacherByFormation[0] - teacherByFormation[0] = 0 + + // Cria vetor de porcentagens de carreira dos professores + req.teacherFormation.forEach((formation, i) => { + lastCity.percentage_teacher_career.push({ + formation_level_id: formation.idFormationLevel, + percentage: Number((teacherByFormation[i]*100).toFixed(2)), + }) + }); lastLocations.forEach((location) => { location.education_level.forEach((educationLevel) => {