From ed2c8bc8273fdb3607e07a6e162330292b094c26 Mon Sep 17 00:00:00 2001 From: Victor Picussa <vp16@inf.ufpr.br> Date: Fri, 15 Jun 2018 11:57:16 -0300 Subject: [PATCH] [release_v1.3.0]Fix api.js --- src/libs/routes/api.js | 3 + src/libs/routes/dailyChargeAmount.js | 415 +++++++++++++++++++++++++++ 2 files changed, 418 insertions(+) create mode 100644 src/libs/routes/dailyChargeAmount.js diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 9f8bdc4c..2f9e7f4a 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -64,6 +64,8 @@ const outOfSchool = require(`${libs}/routes/outOfSchool`); const classroomCount = require(`${libs}/routes/classroomCount`); +const dailyChargeAmount = require(`${libs}/routes/dailyChargeAmount`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -98,5 +100,6 @@ api.use('/distribution_factor', distributionFactor); api.use('/siope', siope); api.use('/out_of_school', outOfSchool); api.use('/classroom_count', classroomCount); +api.use('/daily_charge_amount', dailyChargeAmount); module.exports = api; diff --git a/src/libs/routes/dailyChargeAmount.js b/src/libs/routes/dailyChargeAmount.js new file mode 100644 index 00000000..1514b441 --- /dev/null +++ b/src/libs/routes/dailyChargeAmount.js @@ -0,0 +1,415 @@ +const express = require('express'); + +const dailyChargeAmountApp = 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 download = require(`${libs}/middlewares/downloadDatabase`); + +const addMissing = require(`${libs}/middlewares/addMissing`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +let rqf = new ReqQueryFields(); +let rqfCount = new ReqQueryFields(); + +dailyChargeAmountApp.get('/year_range', (req, res, next) => { + req.sql.from('turma') + .field('MIN(turma.ano_censo)', 'start_year') + .field('MAX(turma.ano_censo)', 'end_year'); + next(); +}, query, response('range')); + +dailyChargeAmountApp.get('/years', (req, res, next) => { + req.sql.from('turma') + .field('DISTINCT turma.ano_censo', 'year'); + next(); +}, query, response('years')); + +dailyChargeAmountApp.get('/source', (req, res, next) => { + req.sql.from('fonte') + .field('fonte', 'source') + .where('tabela = \'turma\''); + next(); +}, query, response('source')); + +dailyChargeAmountApp.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')); + +dailyChargeAmountApp.get('/adm_dependency_detailed', cache('15 day'), (req, res, next) => { + req.result = []; + for(let i = 1; i <= 6; ++i) { + req.result.push({ + id: i, + name: id2str.admDependencyPriv(i) + }); + }; + next(); +}, response('adm_dependency_detailed')); + +dailyChargeAmountApp.get('/location', cache('15 day'), (req, res, next) => { + req.result = [ + {id: 1, name: 'Urbana'}, + {id: 2, name: 'Rural'} + ]; + next(); +}, response('location')); + +dailyChargeAmountApp.get('/rural_location', (req, res, next) => { + req.result = [ + {id: 1, name: "Urbana"}, + {id: 2, name: "Rural"}, + {id: 3, name: "Rural - Ãrea de assentamento"}, + {id: 4, name: "Rural - Terra indÃgena"}, + {id: 5, name: "Rural - Ãrea remanescente de quilombos"}, + {id: 6, name: "Rural - Unidade de uso sustentável"} + ]; + next(); +}, response('rural_location')); + +dailyChargeAmountApp.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')); + +dailyChargeAmountApp.get('/average/education_level_mod', (req, res, next) => { + req.result = [ + {id: null, name: 'Não classificada'}, + {id: 1, name: 'Creche'}, + {id: 2, name: 'Pré-Escola'}, + {id: 3, name: 'Educação Infantil Unificada'}, + {id: 4, name: 'Ensino Fundamental - anos iniciais'}, + {id: 5, name: 'Ensino Fundamental - anos finais'}, + {id: 6, name: 'Ensino Médio'}, + {id: 7, name: 'Turma Multisseriadas e Multietapas'}, + {id: 8, name: 'EJA - Ensino Fundamental'}, + {id: 9, name: 'EJA - Ensino Médio'}, + {id: 10, name: 'Educação Profissional'} + ]; + next(); +}, response('education_level_mod')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'region', + table: 'regiao', + tableField: 'nome', + resultField: 'region_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'regiao_id', + foreignTable: 'turma' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'turma' + } +}).addValue({ + name: 'school', + table: 'escola', + tableField: ['nome_escola', 'id'], + resultField: ['school_name', 'school_id'], + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'escola_id', + foreignTable: 'turma' + } +}).addValue({ + name: 'city', + table: 'municipio', + tableField: ['nome', 'id'], + resultField: ['city_name', 'city_id'], + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'turma' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'turma' + } +}).addValue({ + name: 'min_year', + table: 'turma', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + table: 'turma', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'turma', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + table: 'turma', + field: 'ano_censo' + } +}).addValue({ + name: 'location', + table: 'turma', + tableField: 'localizacao_id', + resultField: 'location_id', + where: { + relation: '=', + type: 'integer', + field: 'localizacao_id' + } +}).addValue({ + name: 'adm_dependency', + table: 'turma', + tableField: 'dependencia_adm_id', + resultField: 'adm_dependency_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm_id' + } +}).addValue({ + name: 'adm_dependency_detailed', + table: 'turma', + tableField: 'dependencia_adm_priv', + resultField: 'adm_dependency_detailed_id', + where: { + relation: '=', + type: 'integer', + field: 'dependencia_adm_priv' + } +}).addValue({ + name: 'rural_location', + table: 'turma', + tableField: 'localidade_area_rural', + resultField: 'rural_location_id', + where: { + relation: '=', + type: 'integer', + field: 'localidade_area_rural' + } +}).addValue({ + name: 'education_level_mod', + table: 'turma', + tableField: 'etapas_mod_ensino_segmento_id', + resultField: 'education_level_mod_id', + where: { + relation: '=', + type: 'integer', + field: 'etapas_mod_ensino_segmento_id' + } +}).addValue({ + name: 'period', + table: 'turma', + tableField: 'turma_turno_id', + resultField: 'period_id', + where: { + relation: '=', + type: 'integer', + field: 'turma_turno_id' + } +}).addValue({ + name: 'integral_time', + table: 'turma', + tableField: 'tempo_integral', + resultField: 'integral_time_id', + where: { + relation: '=', + type: 'boolean', + field: 'tempo_integral' + } +}).addValue({ + name: 'education_level_short', + table: 'turma', + tableField: 'etapa_resumida', + resultField: 'education_level_short_id', + where: { + relation: '=', + type: 'integer', + field: 'etapa_resumida' + } +}); + +dailyChargeAmountApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + var status = 0; + if ('integral_time' in req.filter) { + if (req.filter['integral_time'] == '0' + && ('period' in req.filter)) { + if (req.filter['period'].length == 1 + && req.filter['period'][0] == '3') { + status = 1; + } else if (req.filter['period'].length <= 2 + && (req.filter['period'].includes('1') + || req.filter['period'].includes('2')) + && (!req.filter['period'].includes('3'))) { + status = 1; + } + } else if (req.filter['integral_time'] == '1') { + status = 1; + } + } + + if (status) { + req.sql.from('turma') + .field('turma.ano_censo', 'year') + .field('turma.etapa_resumida', 'education_level_short_id') + .field('AVG(turma.duracao_turma)/60.0', 'average_class_duration') + .field('MEDIAN(turma.duracao_turma)/60.0', 'median_class_duration') + .field('STDDEV_POP(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') + .group('turma.ano_censo') + .group('turma.etapa_resumida') + .order('turma.ano_censo') + .order('turma.etapa_resumida') + .where('turma.tipo_turma_id <= 3') + } else { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + + next(); + +}, query, addMissing(rqf), (req, res, next) => { + + function sliced(object) { + return object['education_level_short_id'] > 3; + } + + if ('period' in req.filter || 'period' in req.dims) { + req.filter['period'].forEach((element) => { + if (element == '3') + req.result = req.result.filter(sliced); + }); + } + + next(); +}, id2str.transform(), response('turma')); + +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)) { + if (req.filter['integral_time'] == '0' + && ('period' in req.filter)) { + if (req.filter['period'].length == 1 + && req.filter['period'][0] == '3') { + status = 1; + } else if (req.filter['period'].length <= 2 + && (req.filter['period'].includes('1') + || req.filter['period'].includes('2')) + && (!req.filter['period'].includes('3'))) { + status = 1; + } + } else if (req.filter['integral_time'] == '1') { + status = 1; + } + } + + if (status) { + req.sql.from('turma') + .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') + .group('turma.ano_censo') + .group('turma.etapas_mod_ensino_segmento_id') + .order('turma.ano_censo') + .order('turma.etapas_mod_ensino_segmento_id') + .where('turma.tipo_turma_id <= 3') + } else { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + + next(); + +}, query, addMissing(rqf), (req, res, next) => { + + function sliced(object) { + return object['education_level_mod_id'] > 4; + } + + if ('period' in req.filter || 'period' in req.dims) { + req.filter['period'].forEach((element) => { + if (element == '3') + req.result = req.result.filter(sliced); + }); + } + + next(); +}, id2str.transform(), response('turma')); + +module.exports = dailyChargeAmountApp; -- GitLab