diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 53bf96bbf4b4f14a7853e272c747839523e54d14..c78f2e1c7d8a971ae7d7e43f22b246cc7f55e4fd 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -30,7 +30,11 @@ const classroom = require('./classroom'); const teacher = require('./teacher'); -const idhmr = require('./idhmr'); +const idhme = require('./idhme'); + +const pibpercapita = require('./pibpercapita') + +const population = require('./population') const idhm = require('./idhm'); @@ -52,5 +56,8 @@ api.use('/classroom', cache('15 day'), classroom); api.use('/teacher', cache('1 day'), teacher); api.use('/idhmr', cache('1 day'), idhmr); api.use('/idhm', cache('1 day'), idhm); +api.use('/idhme', cache('15 day'), idhme); +api.use('/pibpercapita', cache('1 day'), pibpercapita); +api.use('/population', cache('1 day'), population); module.exports = api; diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js new file mode 100644 index 0000000000000000000000000000000000000000..1a3c581ca4de0dc2a202ec44953549e391a6fb73 --- /dev/null +++ b/src/libs/routes/idhme.js @@ -0,0 +1,130 @@ +const express = require('express'); + +const idhmeApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const response = require(`${libs}/middlewares/response`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +let rqf = new ReqQueryFields(); + +idhmeApp.get('/year_range', (req, res, next) => { + req.sql.from('adh_idh') + .field('MIN(adh_idh.ano_censo)', 'start_year') + .field('MAX(adh_idh.ano_censo)', 'end_year'); + next(); +}, query, (req, res, next) => { + req.sql.from('adh_idh_uf') + .field('MIN(adh_idh_uf.ano_censo)', 'start_year') + .field('MAX(adh_idh_uf.ano_censo)', 'end_year'); + req.old_result = req.result; + next(); +}, query, (req, res, next) => { + // console.log(req.old_result[0].start_year); + // console.log(req.result[0].start_year); + if (req.old_result[0].start_year < req.result[0].start_year) { + req.result[0].start_year = req.old_result[0].start_year; + } + if (req.old_result[0].end_year > req.result[0].end_year) { + req.result[0].end_year = req.old_result[0].old_result; + } + next(); +}, query, response('range')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addValue({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'adh_idh' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'adh_idh' + } +}).addValue({ + name: 'min_year', + table: '@', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + table: '@', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: '@', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + table: '@', + field: 'ano_censo' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'adh_idh_uf' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'adh_idh_uf' + } +}); + +idhmeApp.get('/', rqf.parse(), (req, res, next) => { + if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + if ("state" in req.filter) { + // console.log("sim"); + req.sql.from('adh_idh_uf') + .field('adh_idh_uf.idhm_e', 'total') + .field('adh_idh_uf.ano_censo', 'year') + .field('adh_idh_uf.estado_id', 'state_id'); + } else if ("city" in req.filter) { + req.sql.from('adh_idh') + .field('adh_idh.idhm_e', 'IDHME') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.municipio_id', 'city_id'); + } else { + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + next(); +}, rqf.build(), query, response('idhme')); + +module.exports = idhmeApp; diff --git a/src/libs/routes/pibpercapita.js b/src/libs/routes/pibpercapita.js new file mode 100644 index 0000000000000000000000000000000000000000..e0a6409362c378cd11a1fe4aff0e4e89dd08e8f1 --- /dev/null +++ b/src/libs/routes/pibpercapita.js @@ -0,0 +1,122 @@ +const express = require('express'); + +const pibpercapitaApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +let rqf = new ReqQueryFields(); + +pibpercapitaApp.get('/year_range', (req, res, next) => { + req.sql.from('ibge_pib') + .field('MIN(ibge_pib.ano_censo)', 'start_year') + .field('MAX(ibge_pib.ano_censo)', 'end_year'); + next(); +}, query, response('range')); + +pibpercapitaApp.get('/income_level', (req, res, next) => { + req.result = [ + {id: 1, name: "1º quintil – 20% menores"}, + {id: 2, name: "2º quintil"}, + {id: 3, name: "3º quintil"}, + {id: 4, name: "4º quintil"}, + {id: 5, name: "5º quintil – 20% maiores"}, + ]; + next(); +}, response('income_level')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'ibge_pib' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'ibge_pib' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'ibge_pib' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'ibge_pib' + } +}).addValue({ + name: 'min_year', + table: 'ibge_pib', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'ibge_pib', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'income_level', + table: 'ibge_pib', + tableField: 'nivel_renda_per_capita', + resultField: 'income_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_renda_per_capita' + } +}); + +pibpercapitaApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + req.sql.from('ibge_pib') + .field('SUM(ibge_pib.pib)/SUM(ibge_pib.populacao)', 'total') + .field('ibge_pib.ano_censo', 'year') + .group('ibge_pib.ano_censo') + .order('ibge_pib.ano_censo') + + next(); +}, query, id2str.transform(true), response('pibpercapita')); + +module.exports = pibpercapitaApp; diff --git a/src/libs/routes/population.js b/src/libs/routes/population.js new file mode 100644 index 0000000000000000000000000000000000000000..f2a89783ddad50859cdffeeb38a8d7b76f89a507 --- /dev/null +++ b/src/libs/routes/population.js @@ -0,0 +1,141 @@ +const express = require('express'); + +const populationApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +let rqf = new ReqQueryFields(); + +populationApp.get('/year_range', (req, res, next) => { + req.sql.from('ibge_populacao') + .field('MIN(ibge_populacao.ano_censo)', 'start_year') + .field('MAX(ibge_populacao.ano_censo)', 'end_year'); + next(); +}, query, response('range')); + +populationApp.get('/city_size', (req, res, next) => { + req.result = [ + {id: 1, name: "até 5000"}, + {id: 2, name: "5001 - 10000"}, + {id: 3, name: "10001 - 20000"}, + {id: 4, name: "20001 - 50000"}, + {id: 5, name: "50001 - 100000"}, + {id: 6, name: "100001 - 500000"}, + {id: 7, name: "mais que 500000"} + ]; + next(); +}, response('city_size')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'ibge_populacao' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'ibge_populacao' + } +}).addValue({ + name: 'region', + table: 'regiao', + tableField: 'nome', + resultField: 'region_name', + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + table: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'regiao_id', + foreignTable: 'ibge_populacao' + } +}).addValue({ + name: 'min_year', + table: 'ibge_populacao', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'ibge_populacao', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'city_size', + table: 'ibge_populacao', + tableField: 'porte', + resultField: 'city_size_id', + where: { + relation: '=', + type: 'integer', + field: 'porte' + } +}); + +populationApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + log.debug(req.dims); + req.sql.from('ibge_populacao') + .field('SUM(ibge_populacao.populacao)', 'total') + .field('ibge_populacao.ano_censo', 'year') + .group('ibge_populacao.ano_censo') + .order('ibge_populacao.ano_censo') + + next(); +}, query, id2str.transform(true), response('population')); + +module.exports = populationApp; diff --git a/src/libs/routes/teacher.js b/src/libs/routes/teacher.js index 12cd08c12d98571bffd986478491a06de023e111..29fdb4db8b171894cf15b6049afa34a0913eb5d0 100644 --- a/src/libs/routes/teacher.js +++ b/src/libs/routes/teacher.js @@ -309,7 +309,7 @@ teacherApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field("'Brasil'", 'name') .field('docente.ano_censo', 'year') .from('docente') - .join('turma', null, 'docente.turma_id=turma.id') + .join('turma', null, 'docente.turma_id=turma.id AND docente.ano_censo=turma.ano_censo') .group('docente.ano_censo') .order('docente.ano_censo') .where('(docente.tipo_docente = 1 OR docente.tipo_docente = 5) AND (turma.tipo_turma_id <= 3)'); diff --git a/src/test/idhme.js b/src/test/idhme.js new file mode 100644 index 0000000000000000000000000000000000000000..285160819ada0b77cde946c97ba56ffa0bcc48fd --- /dev/null +++ b/src/test/idhme.js @@ -0,0 +1,80 @@ +process.env.NODE_ENV = 'test'; + +const chai = require('chai'); + +const dirtyChai = require('dirty-chai'); + +chai.use(dirtyChai); + +const chaiXml = require('chai-xml'); + +chai.use(chaiXml); + +const chaiHttp = require('chai-http'); + +const assert = chai.assert; + +const expect = chai.expect; + +const should = chai.should(); // actually call the function + +const libs = `${process.cwd()}/libs`; + +const server = require(`${libs}/app`); + +chai.use(chaiHttp); +describe('request idhme', () => { + it('should list the year range', (done) => { + chai.request(server) + .get('/api/v1/idhme/year_range') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('start_year'); + res.body.result[0].should.have.property('end_year'); + done(); + }); + }); + + it('should list idhme with valid filters', (done) => { + chai.request(server) + .get('/api/v1/idhme?filter=min_year:2000,state:41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('total'); + res.body.result[0].should.have.property('year'); + res.body.result[0].should.have.property('state_id'); + done(); + }); + }); + + it('should list idhme with invalid filters', (done) => { + chai.request(server) + .get('/api/v1/idhme?filter=foo:2010,bar:41') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }); + }); + + it('should return 400 with no filters', (done) => { + chai.request(server) + .get('/api/v1/idhme') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }) + }); + +});