diff --git a/src/libs/convert/studentCost.js b/src/libs/convert/studentCost.js new file mode 100644 index 0000000000000000000000000000000000000000..d757ec47f43f907e63cfd73a392aaa85c1dab7d6 --- /dev/null +++ b/src/libs/convert/studentCost.js @@ -0,0 +1,42 @@ +/* +Copyright (C) 2024 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/>. +*/ + +module.exports = function studentCost(id) { + switch (id) { + case 1: + return "Ano"; + case 2: + return "Nome Ente" + case 3: + return 'Receitas'; + case 4: + return 'MatrÃculas Públicas'; + case 5: + return 'MatrÃculas Públicas Mais Conveniada'; + case 6: + return 'Gasto Aluno Ano Pública' + case 7: + return 'Gasto Aluno Mês Pública'; + case 8: + return 'Gasto Aluno Ano Pública Mais Conveniada'; + case 9: + return 'Gasto Aluno Mês Pública Mais Conveniada'; + } +}; diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js index cbe55d8c7d075fc66ad98d20b28deb6e1f76c3a6..903fda039fd276f42346152906a1bce7bb12c37e 100644 --- a/src/libs/middlewares/aggregateData.js +++ b/src/libs/middlewares/aggregateData.js @@ -36,7 +36,8 @@ const convert = { total_doc: "totalDoc", education_degree_entity: "educationDegreeEntity", government_agreement: "governmentAgreement", - receitas: "revenue" + receitas: "revenue", + despesas: "studentCost" } function aggregateData(req, res, next) { @@ -64,10 +65,12 @@ function aggregateData(req, res, next) { 'post_graduation_entity', 'contract_type_entity', 'education_degree_entity', - 'receitas' + 'receitas', + 'despesas' ] let id; const fields = req.query.dims.split(','); + console.log(fields); let currentAggregateField; let currentNonAggregateField; diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js index 60ff424f8c0599499d6a7e3cf559a4ab1a595049..730d620e18c1bc1708af165eebd5de1b3a54c931 100644 --- a/src/libs/middlewares/id2str.js +++ b/src/libs/middlewares/id2str.js @@ -127,6 +127,7 @@ const postGraduationEntity = require(`${libs}/convert/postGraduationEntity`); const totalDoc = require(`${libs}/convert/totalDoc`); const educationDegreeEntity = require(`${libs}/convert/educationDegreeEntity`); const revenue = require(`${libs}/convert/revenue`); +const studentCost = require(`${libs}/convert/studentCost`); const ids = { gender_id: gender, @@ -246,7 +247,8 @@ const ids = { special_education_entity: specialEducationEntity, total_doc: totalDoc, education_degree_entity: educationDegreeEntity, - receitas_id: revenue + receitas_id: revenue, + student_cost: studentCost }; function transform(removeId=false) { @@ -389,5 +391,6 @@ module.exports = { specialEducationEntity, totalDoc, educationDegreeEntity, - revenue + revenue, + studentCost }; diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js index 954c9c8208d7349b104bdc5355e2bddb96a3498a..d11f98f5c7f02a508e8bf51b40c9d4554019a905 100644 --- a/src/libs/middlewares/studentRevenueParser.js +++ b/src/libs/middlewares/studentRevenueParser.js @@ -4,7 +4,6 @@ Devido à limitações na tabela de dados, quando ambos campos estão ativos o f "região" funciona incorretamente. */ module.exports = function studentRevenueParser(req, res, next) { - console.log(req.query); if (req.query && req.query.filter) { const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js index 843060180a8ade07714fc6965b16049f2420d797..5331b389e96c49e06b8ee0df8b8868be68f2422a 100644 --- a/src/libs/routes_v1/api.js +++ b/src/libs/routes_v1/api.js @@ -176,6 +176,8 @@ const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`); const studentRevenue = require(`${libs}/routes_v1/studentRevenue`); const studentRevenueParser = require(`${libs}/middlewares/studentRevenueParser`); +const studentCost = require(`${libs}/routes_v1/studentCost`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API v1 is running' }); }); @@ -251,6 +253,7 @@ api.use('/basic_education_conclusion', basicEducationConclusion); api.use('/adjusted_liquid_frequency', adjustedLiquidFrequency); api.use('/iliteracy_rate', iliteracyRate); api.use('/student_revenue', studentRevenueParser, studentRevenue); +api.use('/student_cost', studentRevenueParser, studentCost); //Publication api.use('/publication', publication); diff --git a/src/libs/routes_v1/studentCost.js b/src/libs/routes_v1/studentCost.js new file mode 100644 index 0000000000000000000000000000000000000000..fdcf7270790bfcf1c50d26386a76f3196ccc7aa9 --- /dev/null +++ b/src/libs/routes_v1/studentCost.js @@ -0,0 +1,130 @@ +/* +Copyright (C) 2024 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 studentCostApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const aggregateData = require(`${libs}/middlewares/aggregateData`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const config = require(`${libs}/config`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +let rqf = new ReqQueryFields(); + +studentCostApp.use(cache('15 day')); + +studentCostApp.get('/years', (req, res, next) => { + req.sql.from('despesas') + .field('DISTINCT despesas.ano_censo', 'year') + .where('despesas.ano_censo is not null') + next(); +}, query, response('years')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'min_year', + table: 'despesas', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'despesas', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'region', + table: 'estado', + tableField: ['nome_ente', 'cod_ibge', 'id', 'regiao_id'], + resultField: ['state_name', 'state_cod_ibge', 'state_id', 'region_id'], + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + }, + join: { + primary: 'id', + foreign: 'cod_ibge', + foreignTable: 'despesas' + } +}) + +studentCostApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if (req.query.dims && req.query.dims.includes('despesas')) { + let whereCondition = req.query.filter.includes("region") ? "" : "despesas.cod_ibge = 0"; + let filterId; + const filters = req.query.filter.split(","); + filters.forEach((filter) => { + if (filter.includes("state") || filter.includes("city")) { + filterId = Number(filter.split(":")[1].replace(/"/g, "")); + whereCondition = `despesas.cod_ibge = ${filterId}` + } + }) + + req.sql.from('despesas') + .field('despesas.ano_censo', 'year') + .field('despesas.ano_censo', 'total_ano') + .field('despesas.nome_ente', 'total_nome_ente') + .field('despesas.despesas', 'total_despesas') + .field('matriculas_publica', 'total_matriculas_publica') + .field('matriculas_publicas_mais_conveniada', 'total_matriculas_publicas_mais_conveniada') + .field('gasto_aluno_ano_publica', 'total_gasto_aluno_ano_publica') + .field('gasto_aluno_mes_publica', 'total_gasto_aluno_mes_publica') + .field('gasto_aluno_ano_publica_mais_conveniada', 'total_gasto_aluno_ano_publica_mais_conveniada') + .field('gasto_aluno_mes_publica_mais_conveniada', 'total_gasto_aluno_mes_publica_mais_conveniada') + .where(`${whereCondition}`) + .order('despesas.cod_ibge') + .order('despesas.ano_censo') + } + + next(); +}, query, aggregateData, id2str.transform(false), response('student_cost')); + +module.exports = studentCostApp; diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js index ac1b5bc6aef9e3f162d6b61ceb48463ef47939ab..b595fb0086846cef7c7b058abbc34ab23006ceca 100644 --- a/src/libs/routes_v1/studentRevenue.js +++ b/src/libs/routes_v1/studentRevenue.js @@ -121,6 +121,7 @@ studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('receita_aluno_mes_publica_mais_conveniada', 'total_receita_aluno_mes_publica_mais_conveniada') .where(`${whereCondition}`) .order('receitas.cod_ibge') + .order('receitas.ano') } next();