From 7f3c3d5fe03ef92173fabe095958611fd7a09bab Mon Sep 17 00:00:00 2001 From: fgs21 <fgs21@inf.ufpr.br> Date: Thu, 5 Sep 2024 09:36:25 -0300 Subject: [PATCH 1/2] [FIX] Trying to match the 2 queries right --- src/libs/routes_v1/rateSchoolNew.js | 53 ++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/libs/routes_v1/rateSchoolNew.js b/src/libs/routes_v1/rateSchoolNew.js index a6ae0218..c6ca6a00 100644 --- a/src/libs/routes_v1/rateSchoolNew.js +++ b/src/libs/routes_v1/rateSchoolNew.js @@ -308,20 +308,49 @@ rqf.addField({ } }); -// The queries are matched and the total is calculated -function matchQueries(attendsSchoolObj, populationObj) { - let match = []; - - for (let i = 0; i < attendsSchoolObj.length; i++) { - let newObj = attendsSchoolObj[i]; - - newObj.total = (attendsSchoolObj[i].total / populationObj[i].total) * 100; +/* + The queries are matched and the total is calculated. + This function is necessary to match the results of the + 2 queries with the right pair. In some queries, the result + doesn't exist in one of the 2, so, we need to run this algorithm + to match them right. +*/ - newObj.age_range_id = newObj.faixa_etaria; - delete newObj.faixa_etaria; +function matchQueries(queryTotal, queryPartial) { + 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; + } + } - match.push(newObj); - } + if(objMatch) { + newObj.denominator = result.total; + newObj.partial = objMatch.total; + newObj.total = (objMatch.total / result.total) * 100; + match.push(newObj); + } + }); return match; } -- GitLab From 927e8e15f269825bdd3bed973f0cd7a125cec9a4 Mon Sep 17 00:00:00 2001 From: fgs21 <fgs21@inf.ufpr.br> Date: Thu, 5 Sep 2024 09:52:47 -0300 Subject: [PATCH 2/2] [FIX] matchQueries() parameters switched --- src/libs/routes_v1/rateSchoolNew.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/routes_v1/rateSchoolNew.js b/src/libs/routes_v1/rateSchoolNew.js index c6ca6a00..237edfa1 100644 --- a/src/libs/routes_v1/rateSchoolNew.js +++ b/src/libs/routes_v1/rateSchoolNew.js @@ -316,7 +316,7 @@ rqf.addField({ to match them right. */ -function matchQueries(queryTotal, queryPartial) { +function matchQueries(queryPartial, queryTotal) { let match = []; queryTotal.forEach((result) => { let newObj = {}; -- GitLab