diff --git a/CHANGELOG.md b/CHANGELOG.md index a96cd52b986189a1832994fb2d3f67ee8fafabfa..4b0317bb5519bae66cb782bb71c6018f050a0a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ 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.15.0 - 2020-07-16 +## Added +- Route location +## Changed +- Dim income_level in route pibpercapita now uses a different table, pib_quintis +- Population now uses ibge_pib table + ## 1.14.2 - 2020-07-14 ## Changed - Courses route diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 45bdea870e8ec629ba3b8e877a62f0fed29bdeba..760ef5501bde072fd1a2ddfe733af452b5c0a9e8 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -122,6 +122,8 @@ const mesoregion = require(`${libs}/routes/mesoregion`); const microregion = require(`${libs}/routes/microregion`); +const location = require(`${libs}/routes/location`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -174,4 +176,5 @@ api.use('/course_count', courseCount); api.use('/school_location', schoolLocation); api.use('/mesoregion', mesoregion); api.use('/microregion', microregion); +api.use('/location', location); module.exports = api; diff --git a/src/libs/routes/location.js b/src/libs/routes/location.js new file mode 100644 index 0000000000000000000000000000000000000000..d493c452f8dfa1c16a5731d5088fbdf700ff5b08 --- /dev/null +++ b/src/libs/routes/location.js @@ -0,0 +1,135 @@ +const express = require('express'); + +const locationApp = 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 id2str = require(`${libs}/middlewares/id2str`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const request = require(`request`); + +const config = require(`${libs}/config`); + +const passport = require('passport'); + +const addMissing = require(`${libs}/middlewares/addMissing`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +locationApp.use(cache('15 day')) + +let rqf = new ReqQueryFields(); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'id', + table: 'localizacao_escolas', + tableField: 'id', + where: { + relation: '=', + type: 'integer', + field: 'id' + } +}).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: 'municipio_id', + foreignTable: '@' + } +}).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: 'municipio_id', + foreignTable: '@' + } +}).addValueToField({ + name: 'city', + table: 'municipio', + tableField: ['nome', 'id'], + resultField: ['city_name', 'city_id'], + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: '@' + } +}, 'dims'); + +locationApp.get('/school', rqf.parse(), (req, res, next) => { + req.dims.city=true; + req.dims.mesoregion=true; + req.dims.microregion=true; + + req.sql.from('localizacao_escolas') + .field('localizacao_escolas.nome', 'name') + .field('localizacao_escolas.id', 'id') + .field('localizacao_escolas.latitude', 'latitude') + .field('localizacao_escolas.longitude', 'longitude') + .group('localizacao_escolas.nome') + .group('localizacao_escolas.id') + .group('localizacao_escolas.latitude') + .group('localizacao_escolas.longitude'); + next(); +}, rqf.build(), query, id2str.transform(), response('location')); + +locationApp.get('/campi', rqf.parse(), (req, res, next) => { + req.dims.city=true; + req.dims.mesoregion=true; + req.dims.microregion=true; + + req.sql.from('localizacao_campi') + .field('localizacao_campi.nome', 'name') + .field('localizacao_campi.id', 'id') + .field('localizacao_campi.ies_id', 'ies_id') + .field('localizacao_campi.latitude', 'latitude') + .field('localizacao_campi.longitude', 'longitude') + .group('localizacao_campi.nome') + .group('localizacao_campi.id') + .group('localizacao_campi.ies_id') + .group('localizacao_campi.latitude') + .group('localizacao_campi.longitude'); + next(); +}, rqf.build(), query, id2str.transform(), response('location')); + +module.exports = locationApp; \ No newline at end of file diff --git a/src/libs/routes/pibpercapita.js b/src/libs/routes/pibpercapita.js index a98386df3afbd6200a100e6d33a5bd56f6244ac6..c5c77a924ecaa848ab8f6cfb38e25d4bf8455121 100644 --- a/src/libs/routes/pibpercapita.js +++ b/src/libs/routes/pibpercapita.js @@ -112,12 +112,12 @@ rqf.addField({ relation: '=', type: 'integer', field: 'estado_id', - table: 'ibge_pib' + table: '@' }, join: { primary: 'id', foreign: 'estado_id', - foreignTable: 'ibge_pib' + foreignTable: '@' } }).addValue({ name: 'region', @@ -137,7 +137,7 @@ rqf.addField({ } }).addValue({ name: 'min_year', - table: 'ibge_pib', + table: '@', tableField: 'ano_censo', resultField: 'year', where: { @@ -147,7 +147,7 @@ rqf.addField({ } }).addValue({ name: 'max_year', - table: 'ibge_pib', + table: '@', tableField: 'ano_censo', resultField: 'year', where: { @@ -165,17 +165,56 @@ rqf.addField({ type: 'integer', field: 'nivel_renda_per_capita' } -}); +}).addValue({ + name: 'income_level_brasil', + table: 'ibge_pib', + tableField: 'nivel_renda_brasil', + resultField: 'income_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_renda_brasil' + } +}).addValue({ + name: 'income_level_uf', + table: 'ibge_pib', + tableField: 'nivel_renda_uf', + resultField: 'income_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_renda_uf' + } +});; + +pibpercapitaApp.get('/', rqf.parse(), (req, res, next) => { + if ('income_level' in req.dims) { // Retorna os quintis + delete req.dims.income_level + + req.sql.from('pib_quintis') + .field('pib_quintis.valor', 'total') + .field('pib_quintis.tipo', 'income_level_id') + .field('pib_quintis.ano_censo', 'year') + .group('pib_quintis.ano_censo') + .group('pib_quintis.valor') + .group('pib_quintis.tipo') + .order('pib_quintis.tipo') + + if (!('state' in req.filter)) { + req.sql.where('pib_quintis.estado_id = 0') + } + if ('city' in req.filter) { + req.sql.join('ibge_pib', null, 'ibge_pib.nivel_renda_brasil=pib_quintis.tipo AND ibge_pib.ano_censo=pib_quintis.ano_censo') + } + } -pibpercapitaApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { - if (("city" in req.dims) || ("city" in req.filter)) { + else if (("city" in req.dims) || ("city" in req.filter)) { req.sql.from('ibge_pib') .field('ibge_pib.pib_per_capita', 'total') .field('ibge_pib.ano_censo', 'year') .group('ibge_pib.ano_censo') .group('ibge_pib.pib_per_capita') .order('ibge_pib.ano_censo') - console.log("CiTy") } else { req.sql.from('ibge_pib') .field('SUM(ibge_pib.pib)/SUM(ibge_pib.populacao)', 'total') @@ -184,7 +223,7 @@ pibpercapitaApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .order('ibge_pib.ano_censo') } next(); -}, query, (req, res, next) => { +}, rqf.build(), query, (req, res, next) => { req.result.forEach((i) => { let value = parseFloat(i.total); let isnum = /^\d+$/.test(value); @@ -205,7 +244,7 @@ pibpercapitaApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { // console.log(i.total); }); next(); - }, addMissing(rqf), id2str.transform(false), response("pibpercapita")); + }, id2str.transform(false), response("pibpercapita")); module.exports = pibpercapitaApp; diff --git a/src/libs/routes/population.js b/src/libs/routes/population.js index 9488570010d0ecc8783cd9bf32b45ffac1be8367..fca3b0d6b309aba53aa0d9ff8e47eaf7d6f85d74 100644 --- a/src/libs/routes/population.js +++ b/src/libs/routes/population.js @@ -47,30 +47,30 @@ let rqf = new ReqQueryFields(); populationApp.use(cache('15 day')); 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'); + 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')); populationApp.get('/years', (req, res, next) => { - req.sql.from('ibge_populacao'). - field('DISTINCT ibge_populacao.ano_censo', 'year'); + req.sql.from('ibge_pib'). + field('DISTINCT ibge_pib.ano_censo', 'year'); next(); }, query, response('years')); -populationApp.get('/city_size', (req, res, next) => { - req.result = [ - {id: 1, name: "0 - 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')); +// populationApp.get('/city_size', (req, res, next) => { +// req.result = [ +// {id: 1, name: "0 - 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', @@ -89,12 +89,12 @@ rqf.addField({ relation: '=', type: 'integer', field: 'municipio_id', - table: 'ibge_populacao' + table: 'ibge_pib' }, join: { primary: 'id', foreign: 'municipio_id', - foreignTable: 'ibge_populacao' + foreignTable: 'ibge_pib' } }).addValue({ name: 'state', @@ -105,12 +105,12 @@ rqf.addField({ relation: '=', type: 'integer', field: 'estado_id', - table: 'ibge_populacao' + table: 'ibge_pib' }, join: { primary: 'id', foreign: 'estado_id', - foreignTable: 'ibge_populacao' + foreignTable: 'ibge_pib' } }).addValue({ name: 'region', @@ -121,16 +121,16 @@ rqf.addField({ relation: '=', type: 'integer', field: 'regiao_id', - table: 'ibge_populacao' + table: 'ibge_pib' }, join: { primary: 'id', foreign: 'regiao_id', - foreignTable: 'ibge_populacao' + foreignTable: 'ibge_pib' } }).addValue({ name: 'min_year', - table: 'ibge_populacao', + table: 'ibge_pib', tableField: 'ano_censo', resultField: 'year', where: { @@ -140,7 +140,7 @@ rqf.addField({ } }).addValue({ name: 'max_year', - table: 'ibge_populacao', + table: 'ibge_pib', tableField: 'ano_censo', resultField: 'year', where: { @@ -148,24 +148,14 @@ rqf.addField({ 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) => { - 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') + req.sql.from('ibge_pib') + .field('SUM(ibge_pib.populacao)', 'total') + .field('ibge_pib.ano_censo', 'year') + .group('ibge_pib.ano_censo') + .order('ibge_pib.ano_censo') next(); }, query, addMissing(rqf), id2str.transform(false), response('population'));