diff --git a/src/libs/middlewares/query.js b/src/libs/middlewares/query.js index b724d93dd8e36709033eb171cceaafe9b6ff7659..3b734957fe255458687f0cffaf25cd94c1af3997 100644 --- a/src/libs/middlewares/query.js +++ b/src/libs/middlewares/query.js @@ -10,6 +10,8 @@ function query(req, res, next) { execute(sql.text, sql.values, (err, result) => { if(err) { log.error(err.stack); + console.log(sql.text); + console.log(sql.values); next(new Error('Request could not be satisfied due to a database error.')); } else { req.result = result; diff --git a/src/libs/routes_v2/aux b/src/libs/routes_v2/aux new file mode 100644 index 0000000000000000000000000000000000000000..c0b4cc5f3bc4c89fa92681674e7f80e6a623c8c4 --- /dev/null +++ b/src/libs/routes_v2/aux @@ -0,0 +1 @@ +SELECT COUNT(*) AS "total", simcaq_matricula_agregada.ano_censo AS "year", estado.nome AS "state_name", estado.id AS "state_id", municipio.nome AS "city_name", municipio.id AS "city_id", simcaq_matricula_agregada.etapa_resumida AS "education_level_short_id", simcaq_matricula_agregada.tempo_integral AS "integral_time_id" FROM simcaq_matricula_agregada INNER JOIN estado ON (simcaq_matricula_agregada.estado_id=estado.id) INNER JOIN municipio ON (simcaq_matricula_agregada.municipio_id=municipio.id) WHERE (simcaq_matricula_agregada.ano_censo >= 2021 ) AND (simcaq_matricula_agregada.ano_censo <= 2021 ) GROUP BY simcaq_matricula_agregada.ano_censo, estado.nome, estado.id, municipio.nome, municipio.id, simcaq_matricula_agregada.etapa_resumida, simcaq_matricula_agregada.tempo_integral ORDER BY simcaq_matricula_agregada.ano_censo ASC, estado.nome ASC, estado.id ASC, municipio.nome ASC, municipio.id ASC, simcaq_matricula_agregada.etapa_resumida ASC, simcaq_matricula_agregada.tempo_integral ASC diff --git a/src/libs/routes_v2/classroomCount.js b/src/libs/routes_v2/classroomCount.js index 0af3282819ca80e45b0a5ae6f443ba620fadb61c..b4ba338ab42373bc730eb1bffec93e2a6d1f4647 100644 --- a/src/libs/routes_v2/classroomCount.js +++ b/src/libs/routes_v2/classroomCount.js @@ -54,21 +54,20 @@ rqf.addField({ }).addValueToField({ name: 'city', table: 'municipio', - tableField: 'nome', - resultField: 'city_name', + tableField: ['nome', 'id'], + resultField: ['city_name', 'city_id'], where: { relation: '=', type: 'integer', - field: 'municipio_id', - table: '@' + field: 'id' }, join: { primary: 'id', foreign: 'municipio_id', foreignTable: '@' } -}, 'filter').addValueToField({ - name: 'city', +}, 'dims').addValueToField({ + name: 'cityTeacher', table: 'municipio', tableField: ['nome', 'id'], resultField: ['city_name', 'city_id'], @@ -79,26 +78,10 @@ rqf.addField({ }, join: { primary: 'id', - foreign: 'municipio_id', + foreign: 'escola_cidade_id', foreignTable: '@' } }, 'dims').addValueToField({ - name: 'state', - table: 'estado', - tableField: 'nome', - resultField: 'state_name', - where: { - relation: '=', - type: 'integer', - field: 'estado_id', - table: '@' - }, - join: { - primary: 'id', - foreign: 'estado_id', - foreignTable: '@' - } -}, 'filter').addValueToField({ name: 'state', table: 'estado', tableField: ['nome', 'id'], @@ -115,9 +98,9 @@ rqf.addField({ } }, 'dims').addValueToField({ name: 'school', - table: 'escola', - tableField: ['nome_escola', 'id', 'dependencia_adm_id'], // Dado de dependencia administrativa sempre deve ser retornado com escola - resultField: ['school_name', 'school_id', 'adm_dependency_id'], + table: 'escola_agregada', + tableField: ['nome_escola', 'id'], + resultField: ['school_name', 'school_id'], where: { relation: '=', type: 'integer', @@ -163,39 +146,9 @@ rqf.addField({ type: 'integer', field: 'ano_censo' } -}).addValue({ - name: 'school_year', - table: '@', - tableField: 'serie_ano_id', - resultField: 'school_year_id', - where: { - relation: '=', - type: 'integer', - field: 'serie_ano_id' - } -}).addValue({ - name: 'location', - table: '@', - tableField: 'localizacao_id', - resultField: 'location_id', - where: { - relation: '=', - type: 'integer', - field: 'localizacao_id' - } -}).addValue({ - name: 'period', - table: '@', - tableField: 'turma_turno_id', - resultField: 'period_id', - where: { - relation: '=', - type: 'integer', - field: 'turma_turno_id' - } }).addValue({ name: 'school_building', - table: 'escola', + table: '@', tableField: 'local_func_predio_escolar', resultField: 'school_building', where: { @@ -205,7 +158,7 @@ rqf.addField({ } }).addValue({ name: 'night_time', - table: 'matricula', + table: '@', tableField: 'periodo_noturno', resultField: 'night_time', where: { @@ -215,7 +168,7 @@ rqf.addField({ } }).addValue({ name: 'formation_level', - table: 'docente_por_formacao', + table: '@', tableField: 'tipo_formacao', resultField: 'formation_level', where: { @@ -235,7 +188,7 @@ rqf.addField({ } }, 'filter') .addValue({ name: 'integral_time', - table: 'matricula', + table: '@', tableField: 'tempo_integral', resultField: 'integral_time_id', where: { @@ -245,7 +198,7 @@ rqf.addField({ } }).addValue({ name: 'education_level_short', - table: 'matricula', + table: '@', tableField: 'etapa_resumida', resultField: 'education_level_short_id', where: { @@ -253,6 +206,26 @@ rqf.addField({ type: 'integer', field: 'etapa_resumida' } +}).addValue({ + name: 'location', + table: '@', + tableField: 'localizacao_id', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'localizacao_id' + } +}).addValue({ + name: 'school_year', + table: '@', + tableField: 'ano_censo', + resultField: 'school_year', + where: { + relation: '=', + type: 'integer', + field: 'ano_censo' + } }); classroomCountApp.post('/', rqf.parse(), (req, res, next) => { @@ -276,11 +249,11 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { req.sql.field('sum(dia_total)', 'total_day') .field('sum(noite_total)', 'total_night') .field("'Brasil'", 'name') - .field('matricula_por_localizacao.ano_censo', 'year') - .from('matricula_por_localizacao') - .where('matricula_por_localizacao.serie_ano_id < 15') - .group('matricula_por_localizacao.ano_censo') - .order('matricula_por_localizacao.ano_censo') + .field('simcaq_matricula_por_localizacao.ano_censo', 'year') + .from('simcaq_matricula_por_localizacao') + .where('simcaq_matricula_por_localizacao.serie_ano_id < 15') + .group('simcaq_matricula_por_localizacao.ano_censo') + .order('simcaq_matricula_por_localizacao.ano_censo') next(); }, rqf.build(), query, id2str.transform(), (req, res, next) => { @@ -310,7 +283,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { }; } } - + delete req.dims; delete req.filter; req.resetSql(); @@ -322,15 +295,15 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { req.dims.location = true; req.dims.school_building = true; - req.sql.field('SUM(escola.qtde_salas_utilizadas_dentro)', 'total') + req.sql.field('SUM(escola_agregada.qtde_salas_utilizadas_dentro)', 'total') .field("'Brasil'", 'name') - .field('escola.ano_censo', 'year') - .from('escola') - .group('escola.ano_censo') - .order('escola.ano_censo') - .where('escola.situacao_de_funcionamento = 1') - .where('escola.dependencia_adm_id < 4') - .where('escola.ensino_regular = 1 OR escola.ensino_eja = 1 OR escola.educacao_profissional = 1'); + .field('escola_agregada.ano_censo', 'year') + .from('escola_agregada') + .group('escola_agregada.ano_censo') + .order('escola_agregada.ano_censo') + .where('escola_agregada.situacao_funcionamento_pareada = 1') + .where('escola_agregada.dependencia_adm_id < 4') + .where('escola_agregada.ensino_regular = 1 OR escola_agregada.ensino_eja = 1 OR escola_agregada.educacao_profissional = 1'); next(); }, rqf.build(), query, id2str.transform(), (req, res, next) => { @@ -379,15 +352,14 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { req.teacherCalc = true; } - req.dims.state = true; - req.dims.city = true; + req.dims.cityTeacher = true; req.dims.formation_level = true; - req.sql.field('count(distinct docente_por_formacao.id_docente)', 'total') + req.sql.field('SUM(simcaq_docente_agregada.num_docentes)', 'total') .field("'Brasil'", 'name') - .field('docente_por_formacao.ano_censo', 'year') - .from('docente_por_formacao') - .group('docente_por_formacao.ano_censo') - .order('docente_por_formacao.ano_censo'); + .field('simcaq_docente_agregada.ano_censo', 'year') + .from('simcaq_docente_agregada') + .group('simcaq_docente_agregada.ano_censo') + .order('simcaq_docente_agregada.ano_censo'); next(); }, rqf.build(), query, id2str.transform(), (req, res, next) => { @@ -401,13 +373,13 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { req.dims.state = true; req.dims.city = true; req.dims.education_level_short = true; - req.dims.integral_time = true; + //req.dims.integral_time = true; req.sql.field('COUNT(*)', 'total') - .field('matricula.ano_censo', 'year') - .from('matricula') - .group('matricula.ano_censo') - .order('matricula.ano_censo') - .where('((matricula.tipo<=3 OR matricula.tipo IS NULL) AND (matricula.tipo_atendimento_turma IS NULL OR matricula.tipo_atendimento_turma <= 2) AND matricula.turma_turno_id <> 99)'); + .field('simcaq_matricula_agregada.ano_censo', 'year') + .from('simcaq_matricula_agregada') + .group('simcaq_matricula_agregada.ano_censo') + .order('simcaq_matricula_agregada.ano_censo'); + //.where('((simcaq_matricula_agregada.tipo<=3 OR simcaq_matricula_agregada.tipo IS NULL) AND (simcaq_matricula_agregada.tipo_atendimento_turma IS NULL OR simcaq_matricula_agregada.tipo_atendimento_turma <= 2) AND simcaq_matricula_agregada.turma_turno_id <> 99)'); next(); }, rqf.build() ,query, id2str.transform(), (req, res, next) => { // constrói objeto de tempo integral e calcula diagnósticos @@ -566,7 +538,6 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { ti++; if (ti === req.teacher.length) { - console.log(classroom[id_attribute], "not found") while (classroom[id_attribute] === enrollments[j][id_attribute]) enrollments.splice(j, 1) ti = old_ti;