Skip to content

Commit

Permalink
Merge pull request #49 from analytics-ufcg/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JoaquimCMH authored Aug 5, 2020
2 parents e354a52 + 03ec0bb commit bdfb7bf
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 19 deletions.
2 changes: 2 additions & 0 deletions feed-al/DAO.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ create <- function() {
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_municipio.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_licitacao.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_contrato.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_previsao.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_empenho.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_participante.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_pagamento.sql"))
system(paste0("psql -h ", host, " -U ", user, " -d ", db, " -f ", " /feed-al/scripts/create/create_proposta.sql"))
Expand Down
43 changes: 43 additions & 0 deletions feed-al/scripts/create/create_empenho.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
DROP TABLE IF EXISTS empenho;

CREATE TABLE IF NOT EXISTS "empenho" (
"id_empenho" VARCHAR(50),
"id_licitacao" VARCHAR(50),
"cd_municipio" VARCHAR(3),
"cd_u_gestora" INTEGER,
"dt_ano" SMALLINT,
"cd_unid_orcamentaria" VARCHAR(5),
"cd_funcao" VARCHAR(2),
"cd_subfuncao" VARCHAR(3),
"cd_programa" VARCHAR(4),
"cd_acao" VARCHAR(4),
"cd_classificacao" VARCHAR(6),
"cd_cat_economica" VARCHAR(1),
"cd_nat_despesa" VARCHAR(1),
"cd_modalidade" VARCHAR(2),
"cd_elemento" VARCHAR(2),
"cd_sub_elemento" VARCHAR(2),
"tp_licitacao" SMALLINT,
"nu_licitacao" VARCHAR(10),
"nu_empenho" VARCHAR(7),
"tp_empenho" SMALLINT,
"dt_empenho" DATE,
"vl_empenho" DECIMAL,
"cd_credor" VARCHAR(14),
"no_credor" VARCHAR(100),
"tp_credor" SMALLINT,
"de_historico1" VARCHAR,
"de_historico2" VARCHAR,
"de_historico" VARCHAR,
"tp_meta" SMALLINT,
"nu_obra" VARCHAR(8),
"dt_mes_ano" VARCHAR(6),
"dt_mes_ano_referencia" VARCHAR(6),
"tp_fonte_recursos" SMALLINT,
"nu_cpf" VARCHAR(14),
PRIMARY KEY("id_empenho"),
CONSTRAINT empenho_key UNIQUE (nu_empenho, cd_unid_orcamentaria, dt_ano, cd_u_gestora),
FOREIGN KEY("cd_municipio") REFERENCES municipio("cd_municipio") ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY("id_licitacao") REFERENCES licitacao("id_licitacao") ON DELETE CASCADE ON UPDATE CASCADE

);
31 changes: 31 additions & 0 deletions feed-al/scripts/create/create_previsao.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DROP TABLE IF EXISTS previsao;

CREATE TABLE IF NOT EXISTS "previsao" (
"id_contrato" VARCHAR(50),
"cd_u_gestora" INTEGER,
"nu_licitacao" VARCHAR(10),
"nu_contrato" VARCHAR(9),
"dt_ano" SMALLINT,
"data_inicio" DATE,
"nu_cpfcnpj" VARCHAR(14),
"tp_licitacao" SMALLINT,
"vl_total_contrato" DECIMAL,
"n_licitacoes_part" INTEGER,
"n_licitacoes_venceu" INTEGER,
"montante_lic_venceu" DECIMAL,
"perc_vitoria" DECIMAL,
"media_n_licitacoes_part" DECIMAL,
"media_n_licitacoes_venceu" DECIMAL,
"n_propostas" INTEGER,
"media_n_propostas" DECIMAL,
"total_ganho" DECIMAL,
"status_tramita" SMALLINT,
"razao_contrato_por_vl_recebido" DECIMAL,
"media_num_contratos" DECIMAL,
"num_contratos" DECIMAL,
"vig_pred" VARCHAR(1),
"vig_prob_0" DECIMAL,
"vig_prob_1" DECIMAL,
FOREIGN KEY("id_contrato") REFERENCES contrato("id_contrato") ON DELETE CASCADE ON UPDATE CASCADE

);
2 changes: 2 additions & 0 deletions feed-al/scripts/drop/drop_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ DROP TABLE IF EXISTS "pagamento";
DROP TABLE IF EXISTS "estorno_pagamento";
DROP TABLE IF EXISTS "proposta";
DROP TABLE IF EXISTS "participante";
DROP TABLE IF EXISTS "previsao";
DROP TABLE IF EXISTS "contrato";
DROP TABLE IF EXISTS "empenho";
DROP TABLE IF EXISTS "licitacao";
DROP TABLE IF EXISTS "municipio";
2 changes: 2 additions & 0 deletions feed-al/scripts/import/import_data.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
\copy municipio FROM '/data/municipios.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy licitacao FROM '/data/licitacoes.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy contrato FROM '/data/contratos.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy previsao FROM '/data/previsoes.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy empenho FROM PROGRAM 'awk FNR-1 /data/empenhos/*.csv | cat' WITH NULL AS 'NA' DELIMITER ',' CSV;
\copy participante FROM '/data/participantes.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy proposta FROM '/data/propostas.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
\copy pagamento FROM '/data/pagamentos.csv' WITH NULL AS 'NA' DELIMITER ',' CSV HEADER;
Expand Down
2 changes: 1 addition & 1 deletion fetcher/R/cols_constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ COLNAMES_CODIGO_UNIDADE_GESTORA <- c("cd_Ibge"="integer", "cd_Municipio"="charac
"de_Ugestora"="character", "previdencia"="character"
)

COLNAMES_EMPENHOS <- c("cd_UGestora"="character", "dt_Ano"="integer", "cd_UnidOrcamentaria"="character",
COLNAMES_EMPENHOS <- c("cd_UGestora"="integer", "dt_Ano"="integer", "cd_UnidOrcamentaria"="character",
"cd_Funcao"="character", "cd_Subfuncao"="character", "cd_Programa"="character",
"cd_Acao"="character", "cd_classificacao"="character", "cd_CatEconomica"="character",
"cd_NatDespesa"="character", "cd_Modalidade"="character", "cd_Elemento"="character",
Expand Down
4 changes: 2 additions & 2 deletions fetcher/R/sagres.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ fetch_codigo_unidade_gestora <- function(sagres_con) {
#' @return Dataframe contendo informações sobre os empenhos
#' @rdname fetch_empenhos
#' @examples
fetch_empenhos <- function(sagres_con) {
fetch_empenhos <- function(sagres_con, cd_municipio) {
empenhos <- tibble::tibble()
tryCatch({
empenhos <- DBI::dbGetQuery(sagres_con, "SELECT TOP 10 * FROM Empenhos;") %>%
empenhos <- DBI::dbGetQuery(sagres_con, sprintf("SELECT * FROM Empenhos WHERE RIGHT (cd_UGestora, 3) = %s;", cd_municipio)) %>%
assert_dataframe_completo(COLNAMES_EMPENHOS)
},
error = function(e) print(paste0("Erro ao buscar empenhos no Banco Sagres (SQLServer): ", e))
Expand Down
16 changes: 14 additions & 2 deletions fetcher/scripts/fetch_sagres_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ readr::write_csv(codigo_subfuncao, here::here("./data/codigo_subfuncao.csv"))
codigo_unidade_gestora <- fetch_codigo_unidade_gestora(sagres)
readr::write_csv(codigo_unidade_gestora, here::here("./data/codigo_unidade_gestora.csv"))

empenhos <- fetch_empenhos(sagres)
readr::write_csv(empenhos, here::here("./data/empenhos.csv"))
for (cd_municipio in codigo_municipio$cd_Municipio) {
print(sprintf('[%s] empenhos cd_municipio = %s', Sys.time(), cd_municipio))

output_dir = 'data/empenhos/'
if (!dir.exists(output_dir)){
dir.create(output_dir)
}

empenhos <- fetch_empenhos(sagres, cd_municipio)
readr::write_csv(empenhos, here::here(sprintf("./data/empenhos/empenhos_%s.csv", cd_municipio)))

rm(empenhos) # remove o dataframe 'empenhos'
gc() # permite que o R retorne memória pro sistema operacional
}

estorno_pagamento <- fetch_estorno_pagamento(sagres)
readr::write_csv(estorno_pagamento, here::here("./data/estorno_pagamento.csv"))
Expand Down
7 changes: 6 additions & 1 deletion fetcher/tests/test-empenhos.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ tryCatch({sagres <- DBI::dbConnect(odbc::odbc(),
PWD = SQLSERVER_SAGRES19_PASS)
}, error = function(e) print(paste0("Erro ao tentar se conectar ao Banco Sagres (SQLServer): ", e)))

empenhos <- fetch_empenhos(sagres)
empenhos <- fetch_empenhos(sagres, '001')
codigo_municipio <- fetch_codigo_municipio(sagres)

DBI::dbDisconnect(sagres)

Expand All @@ -23,3 +24,7 @@ test_that("Is dataframe", {
test_that("Not Empty", {
expect_true(nrow(empenhos) != 0)
})

test_that("Contains All Cities", {
expect_true(nrow(codigo_municipio) == length(list.files("data/empenhos")))
})
28 changes: 28 additions & 0 deletions transformador/R/sagres.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ generate_participante_id <- function(participantes_df) {
dplyr::select(id_participante, dplyr::everything())
}

#' @title Gera um identificador único para cada empenho
#' @param empenhos_df Dataframe contendo informações sobre os empenhos
#' @return Dataframe contendo informações sobre os empenhos e seus ids
#' @rdname generate_empenho_id
#' @examples
#' empenhos_dt <- generate_empenho_id(empenho_df)
generate_empenho_id <- function(empenho_df) {
empenho_df %<>% .generate_hash_id(c("nu_empenho", "cd_unid_orcamentaria",
"dt_ano", "cd_u_gestora"),
"id_empenho") %>%
dplyr::select(id_empenho, dplyr::everything())
}

#' @title Gera um identificador único para cada proposta
#' @param propostas_df Dataframe contendo informações sobre as propostas
#' @return Dataframe contendo informações sobre as propostas e seus ids
Expand Down Expand Up @@ -91,6 +104,21 @@ process_contrato <- function(contratos_df) {
dplyr::mutate(language = 'portuguese')
}

#' @title Processa dataframe de empenhos
#' @description Manipula tabela pra forma que será utilizada no banco
#' @param empenhos_df Dataframe contendo informações dos empenhos
#' @return Dataframe contendo informações dos empenhos processados
process_empenho <- function(empenhos_df) {
empenhos_df %<>% .extract_cd_municipio("cd_u_gestora") %>%
generate_empenho_id() %>%
dplyr::mutate(nu_empenho = iconv(nu_empenho, "UTF-8", "latin1", sub=''),
de_historico = iconv(de_historico, "UTF-8", "latin1", sub=''),
de_historico1 = iconv(de_historico1, "UTF-8", "latin1", sub=''),
de_historico2 = iconv(de_historico2, "UTF-8", "latin1", sub=''),
no_credor = iconv(no_credor, "UTF-8", "latin1", sub=''),
)
}

#' @title Processa dataframe de licitações
#' @description Manipula tabela pra forma que será utilizada no banco
#' @param licitacoes_df Dataframe contendo informações das licitações
Expand Down
8 changes: 4 additions & 4 deletions transformador/R/tradutor/interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ get_codigo_subelemento <- function() {
translate_codigo_subelemento()
}

#' @title Obtem dados dos empenhos
#' @title Obtem dados dos empenhos pelo codigo do municipio
#' @return Dataframe contendo informações sobre empenhos
#' @rdname get_empenhos
#' @examples
#' empenhos_dt <- get_empenhos()
get_empenhos <- function() {
empenhos_dt <- read_empenhos() %>%
#' empenhos_dt <- get_empenhos_by_municipio()
get_empenhos_by_municipio <- function(cd_municipio) {
empenhos_dt <- read_empenhos_by_municipio(cd_municipio) %>%
translate_empenhos()
}

Expand Down
10 changes: 5 additions & 5 deletions transformador/R/tradutor/read_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ read_codigo_subelemento <- function() {
#' @rdname read_empenhos
#' @examples
#' empenhos_dt <- read_empenhos()
read_empenhos <- function() {
empenhos_df <- readr::read_csv(here::here("../fetcher/data/empenhos.csv"),
read_empenhos_by_municipio <- function(cd_municipio) {
empenhos_df <- readr::read_csv(here::here(sprintf("../fetcher/data/empenhos/empenhos_%s.csv", cd_municipio)),
col_types = list(
.default = readr::col_number(),
cd_UGestora = readr::col_character(),
dt_Ano = readr::col_character(),
cd_UGestora = readr::col_number(),
dt_Ano = readr::col_number(),
cd_UnidOrcamentaria = readr::col_character(),
cd_Funcao = readr::col_character(),
cd_Subfuncao = readr::col_character(),
Expand All @@ -171,7 +171,7 @@ read_empenhos <- function() {
cd_Modalidade = readr::col_character(),
cd_Elemento = readr::col_character(),
cd_SubElemento = readr::col_character(),
tp_Licitacao = readr::col_character(),
tp_Licitacao = readr::col_number(),
nu_Licitacao = readr::col_character(),
nu_Empenho = readr::col_character(),
tp_Empenho = readr::col_character(),
Expand Down
1 change: 1 addition & 0 deletions transformador/data/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*
# exceto o .gitignore
!.gitignore
!previsoes.csv
Loading

0 comments on commit bdfb7bf

Please sign in to comment.