From abf9f9f5eed1a6771830c09e61bc1d3f2b737e59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Frans=20Pondaco=20Winandy?=
 <jvfpw18@inf.ufpr.br>
Date: Wed, 5 Aug 2020 10:40:51 -0300
Subject: [PATCH] Add disciplines indicator

---
 src/libs/routes/api.js         |   3 +
 src/libs/routes/disciplines.js | 451 +++++++++++++++++++++++++++++++++
 2 files changed, 454 insertions(+)
 create mode 100644 src/libs/routes/disciplines.js

diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index 760ef550..bb690b86 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -124,6 +124,8 @@ const microregion = require(`${libs}/routes/microregion`);
 
 const location = require(`${libs}/routes/location`);
 
+const disciplines = require(`${libs}/routes/disciplines`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API is running' });
 });
@@ -177,4 +179,5 @@ api.use('/school_location', schoolLocation);
 api.use('/mesoregion', mesoregion);
 api.use('/microregion', microregion);
 api.use('/location', location);
+api.use('/disciplines', disciplines);
 module.exports = api;
diff --git a/src/libs/routes/disciplines.js b/src/libs/routes/disciplines.js
new file mode 100644
index 00000000..5810a693
--- /dev/null
+++ b/src/libs/routes/disciplines.js
@@ -0,0 +1,451 @@
+/*
+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 disciplinesApp = express.Router();
+
+const libs = `${process.cwd()}/libs`;
+
+const log = require(`${libs}/log`)(module);
+
+const squel = require('squel');
+
+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 config = require(`${libs}/config`);
+
+const addMissing = require(`${libs}/middlewares/addMissing`);
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]}  }).middleware;
+
+let rqf = new ReqQueryFields();
+
+disciplinesApp.use(cache('15 day'));
+
+// Returns a tuple of start and ending years of the complete enrollments dataset.
+disciplinesApp.get('/year_range', (req, res, next) => {
+    req.sql.from('docente')
+    .field('MIN(docente.ano_censo)', 'start_year')
+    .field('MAX(docente.ano_censo)', 'end_year');
+    next();
+}, query, response('range'));
+
+disciplinesApp.get('/years', (req, res, next) => {
+    req.sql.from('docente').
+    field('DISTINCT docente.ano_censo', 'year');
+    next();
+}, query, response('years'));
+
+disciplinesApp.get('/source', (req, res, next) => {
+    req.sql.from('fonte')
+    .field('fonte', 'source')
+    .where('tabela = \'docente\'');
+    next();
+}, query, response('source'));
+
+disciplinesApp.get('/adm_dependency_detailed', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 8; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.admDependencyPriv(i)
+        });
+    };
+    next();
+}, response('adm_dependency_detailed'));
+
+disciplinesApp.get('/adm_dependency', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 4; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.admDependency(i)
+        });
+    };
+    next();
+}, response('adm_dependency'));
+
+disciplinesApp.get('/education_level_mod', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 12; ++i) {   
+        req.result.push({
+            id: i,
+            name: id2str.educationLevelMod(i)
+        });
+    }
+    req.result.push({
+        id: 99,
+        name: id2str.educationLevelMod(99)
+    });
+    next();
+}, response('education_level_mod'));
+
+disciplinesApp.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'));
+
+disciplinesApp.get('/location', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 2; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.location(i)
+        });
+    };
+    next();
+}, response('location'));
+
+disciplinesApp.get('/rural_location', (req, res, next) => {
+    req.result = [];
+    for (let i = 1; i <= 8; i++) {
+        req.result.push({
+            id: i,
+            name: id2str.ruralLocation(i)
+        });
+    };
+    next();
+}, response('rural_location'));
+
+disciplinesApp.get('/education_type', (req, res, next) => {
+    req.sql.from('docente')
+    .field('DISTINCT nivel_tipo_formacao', 'id')
+    .order('id');
+    next();
+}, query, (req, res, next) => {
+    req.result.forEach((result) => {
+        result.name = id2str.educationType(result.id);
+    });
+    next();
+}, response('education_type'));
+
+disciplinesApp.get('/gender', (req, res, next) => {
+    req.result = [
+        {id: 1, name: 'Masculino'},
+        {id: 2, name: 'Feminino'}
+    ];
+    next();
+}, response('gender'));
+
+
+disciplinesApp.get('/contract_type', (req, res, next) => {
+    req.result = [
+        {id: 1, name: 'Concursado/Efetivo/Estável'},
+        {id: 2, name: 'Contrato temporário'},
+        {id: 3, name: 'Contrato terceirizado'},
+        {id: 4, name: 'Contrato CLT'}
+    ];
+    next();
+}, response('contract_type'));
+
+disciplinesApp.get('/ethnic_group', (req, res, next) => {
+    req.result = [];
+    for(let i = 0; i <=5; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.ethnicGroup(i)
+        });
+    }
+    next();
+}, response('ethnic_group'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValue({
+    name: 'adm_dependency',
+    table: 'docente',
+    tableField: 'dependencia_adm_id',
+    resultField: 'adm_dependency_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_adm_id'
+    }
+}).addValue({
+    name: 'adm_dependency_detailed',
+    table: 'docente',
+    tableField: 'dependencia_adm_priv',
+    resultField: 'adm_dependency_detailed_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_adm_priv'
+    }
+}).addValue({
+    name: 'contract_type',
+    table: 'docente',
+    tableField: 'tipo_contratacao',
+    resultField: 'contract_type_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'tipo_contratacao'
+    }
+}).addValue({
+    name: 'education_level_mod',
+    table: 'docente',
+    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: 'docente',
+    tableField: 'etapa_resumida',
+    resultField: 'education_level_short_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'etapa_resumida'
+    }
+}).addValue({
+    name: 'education_type',
+    table: 'docente',
+    tableField: 'nivel_tipo_formacao',
+    resultField: 'education_type_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'nivel_tipo_formacao'
+    }
+}).addValue({
+    name: 'region',
+    table: 'regiao',
+    tableField: ['nome', 'id'],
+    resultField: ['region_name', 'region_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_regiao_id',
+        foreignTable: 'docente'
+    }
+}).addValue({
+    name: 'mesoregion',
+    table: 'municipio',
+    tableField: ['nome_mesorregiao', 'mesorregiao_id'],
+    resultField: ['mesoregion_name', 'mesoregion_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'mesorregiao_id',
+        table: 'municipio'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+}).addValue({
+    name: 'microregion',
+    table: 'municipio',
+    tableField: ['nome_microrregiao', 'microrregiao_id'],
+    resultField: ['microregion_name', 'microregion_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'microrregiao_id',
+        table: 'municipio'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: ['nome', 'id'],
+    resultField: ['state_name', 'state_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_estado_id',
+        foreignTable: 'docente'
+    }
+}).addValueToField({
+    name: 'city',
+    table: 'municipio',
+    tableField: ['nome', 'id'],
+    resultField: ['city_name', 'city_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+}, 'dims').addValueToField({
+    name: 'city',
+    table: 'municipio',
+    tableField: 'nome',
+    resultField: 'city_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+}, '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: 'docente'
+    }
+}, 'dims').addValueToField({
+    name: 'school',
+    table: 'escola',
+    tableField: 'nome_escola',
+    resultField: 'school_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: ['id', 'ano_censo'],
+        foreign: ['escola_id', 'ano_censo'],
+        foreignTable: 'docente'
+    }
+}, 'filter').addValue({
+    name: 'location',
+    table: 'docente',
+    tableField: 'localizacao_id',
+    resultField: 'location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localizacao_id'
+    }
+}).addValue({
+    name: 'rural_location',
+    table: 'docente',
+    tableField: 'localidade_area_rural',
+    resultField: 'rural_location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localidade_area_rural'
+    }
+}).addValue({
+    name: 'min_year',
+    table: 'docente',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '>=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'max_year',
+    table: 'docente',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '<=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'gender',
+    table: 'docente',
+    tableField: 'sexo',
+    resultField: 'gender_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'sexo'
+    }
+}).addValue({
+    name: 'ethnic_group',
+    table: 'docente',
+    tableField: 'cor_raca',
+    resultField: 'ethnic_group_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cor_raca'
+    }
+});
+
+disciplinesApp.get('/', rqf.parse(), (req, res, next) => {
+    req.sql.field('SUM(n_disc', 'total_disciplines')
+           .field('SUM(n_disc_adequada)', 'total_suitable_disciplines')
+           .field("'Brasil'", 'name')
+           .field('docente.ano_censo', 'year')
+           .from('docente')
+        //    .join('turma', null, 'docente.turma_id=turma.id AND docente.ano_censo=turma.ano_censo')
+           .group('docente.ano_censo')
+           .order('docente.ano_censo')
+           .where('etapas_mod_ensino_segmento_id <> 6 AND etapas_mod_ensino_segmento_id <> 12');
+
+    next();
+}, rqf.build(), query, addMissing(rqf), id2str.transform(), response('disciplines'));
+
+module.exports = disciplinesApp;
-- 
GitLab