diff --git a/CHANGELOG.md b/CHANGELOG.md index 3926308420a01368d1a529dcfe028773a9ed2e80..6d3dbb013cec3a98716cbe86e5644659c987e7c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.9.0 - 2018-11-24 +## Changed +- Fix school infrastructure results when partial or needed schools query return empty +- Block 2 or more dimensions in daily_charge_amount/average + ## 1.8.3 - 2018-11-24 ### Changed - Fixed reulst return in classroom count diff --git a/src/libs/middlewares/query.js b/src/libs/middlewares/query.js index ae002df2fb13727152497dc9ad61c1371d98d194..b724d93dd8e36709033eb171cceaafe9b6ff7659 100644 --- a/src/libs/middlewares/query.js +++ b/src/libs/middlewares/query.js @@ -6,6 +6,7 @@ const db = require(`${libs}/db/query_exec`); function query(req, res, next) { let sql = req.sql.toParam(); log.info(`Executando query ${req.sql.toString()}`); + sql.text = sql.text.replace(/`/g, ""); execute(sql.text, sql.values, (err, result) => { if(err) { log.error(err.stack); diff --git a/src/libs/routes/dailyChargeAmount.js b/src/libs/routes/dailyChargeAmount.js index 54ef5beabc0aa84f611acb61e3a6412285838d79..d9a02d779ad0355683769910b40780d8bac77c3d 100644 --- a/src/libs/routes/dailyChargeAmount.js +++ b/src/libs/routes/dailyChargeAmount.js @@ -284,6 +284,7 @@ rqf.addField({ table: 'turma', tableField: 'etapas_mod_ensino_segmento_id', resultField: 'education_level_mod_id', + dontGroup: true, where: { relation: '=', type: 'integer', @@ -380,7 +381,7 @@ dailyChargeAmountApp.get('/', rqf.parse(), (req, res, next) => { next(); }, id2str.transform(), response('turma')); -dailyChargeAmountApp.get('/average', rqf.parse(), (req, res, next) => { +dailyChargeAmountApp.get('/average', rqf.parse(), rqf.build(), (req, res, next) => { var status = 0; if (('education_level_mod' in req.filter || 'education_level_mod' in req.dims) && ('integral_time' in req.filter)) { @@ -401,11 +402,34 @@ dailyChargeAmountApp.get('/average', rqf.parse(), (req, res, next) => { } if (status) { - req.sql.from('turma') + let baseQ = req.sql.clone(); + + let tableR = baseQ.clone(); + tableR.from('turma') + .field('duracao_turma') + .field('ROW_NUMBER() OVER(PARTITION BY etapas_mod_ensino_segmento_id ORDER BY duracao_turma)', 'rowno') + .where('tipo_turma_id <= 3') + + let tableG = baseQ.clone(); + tableG.from('turma') + .field('1+COUNT(*)', 'counter') + .where('tipo_turma_id <= 3') + .group('etapas_mod_ensino_segmento_id') + + let joinRG = squel.select(); + joinRG.from(tableR, 'R') + .field('R.education_level_mod_id') + .field('AVG(1.0*R.duracao_turma)/60', 'median_value') + .join(tableG, 'G', 'R.education_level_mod_id = G.education_level_mod_id AND R.rowNo BETWEEN G.counter/2 AND G.counter/2+G.counter%2') + .group('R.education_level_mod_id') + + req.sql + .from('turma') + .from(joinRG, 'm') .field('turma.ano_censo', 'year') .field('turma.etapas_mod_ensino_segmento_id', 'education_level_mod_id') .field('AVG(turma.duracao_turma)/60.0', 'average_class_duration') - .field('MEDIAN(turma.duracao_turma)/60.0', 'median_class_duration') + .field('AVG(m.median_value)', 'median_class_duration') .field('STDDEV_SAMP(turma.duracao_turma)/60.0', 'std_class_duration') .field('QUANTILE(turma.duracao_turma, 0.25)/60.0', 'fstqt_class_duration') .field('QUANTILE(turma.duracao_turma, 0.75)/60.0', 'thdqt_class_duration') @@ -413,7 +437,7 @@ dailyChargeAmountApp.get('/average', rqf.parse(), (req, res, next) => { .group('turma.etapas_mod_ensino_segmento_id') .order('turma.ano_censo') .order('turma.etapas_mod_ensino_segmento_id') - .where('turma.tipo_turma_id <= 3') + .where('turma.tipo_turma_id <= 3 and m.education_level_mod_id = turma.etapas_mod_ensino_segmento_id') } else { res.status(400); next({ @@ -423,6 +447,6 @@ dailyChargeAmountApp.get('/average', rqf.parse(), (req, res, next) => { } next(); -}, rqf.build(), query, addMissing(rqf), id2str.transform(), response('turma')); +}, query, addMissing(rqf), id2str.transform(), response('turma')); module.exports = dailyChargeAmountApp; diff --git a/src/libs/routes/schoolInfrastructure.js b/src/libs/routes/schoolInfrastructure.js index 0c617883cba003bc894bc09be1ddb7a3bd1fa327..65c4677b4381b106b044f9e4fe129ce8691c0686 100644 --- a/src/libs/routes/schoolInfrastructure.js +++ b/src/libs/routes/schoolInfrastructure.js @@ -259,6 +259,11 @@ function matchQueries(queryTotal, queryPartial, queryNeeded, zeroPercentage=fals } } + if(queryPartial.length == 0) { + partialMatch = JSON.parse(JSON.stringify(result)); + partialMatch.total = 0; + } + for(let i = 0; i < queryNeeded.length; ++i) { let needed = queryNeeded[i]; let foundMatch = true; @@ -275,6 +280,11 @@ function matchQueries(queryTotal, queryPartial, queryNeeded, zeroPercentage=fals } } + if(queryNeeded.length == 0) { + needMatch = JSON.parse(JSON.stringify(result)); + needMatch.total = 0; + } + if(partialMatch && needMatch) { newObj.percentage = (partialMatch.total / result.total) * 100; if(zeroPercentage) newObj.percentage = 0; @@ -340,11 +350,11 @@ infrastructureApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.queryIndex.allLibrariesReadingRoom = req.queryIndex.allCountrySchools; let haveLibrariesReadingRoom = allCountrySchools.clone(); - haveLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = 1'); + haveLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = true'); req.queryIndex.haveLibrariesReadingRoom = req.querySet.push(haveLibrariesReadingRoom) - 1; let needLibrariesReadingRoom = allCountrySchools.clone(); - needLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = 0'); + needLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = false'); req.queryIndex.needLibrariesReadingRoom = req.querySet.push(needLibrariesReadingRoom) - 1; // Laboratório de informática @@ -366,11 +376,11 @@ infrastructureApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.queryIndex.allScienceLab = req.querySet.push(allScienceLab) - 1; let haveScienceLab = allScienceLab.clone(); - haveScienceLab.where('escola.lab_ciencias = 1'); + haveScienceLab.where('escola.lab_ciencias = true'); req.queryIndex.haveScienceLab = req.querySet.push(haveScienceLab) - 1; let needScienceLab = allScienceLab.clone(); - needScienceLab.where('escola.lab_ciencias = 0'); + needScienceLab.where('escola.lab_ciencias = false'); req.queryIndex.needScienceLab = req.querySet.push(needScienceLab) - 1; // Parque infantil