Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions R/alert_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ setCriteria <- function(rule=NULL, values=NULL,

criteria
}


#fouralert ---------------------------------------------------------------------
#'@title Define conditions to issue a four level alert Green-Yellow-Orange-Red.
#'@description Yellow is raised when environmental conditions required for
Expand Down Expand Up @@ -326,8 +324,6 @@ pipe_infodengue <- function(cities, cid10="A90", datarelatorio, finalday = Sys.D
casoscli <- casos %>%
left_join(cli, by = join_by(cidade == geocodigo, SE))

casoscli$tweet <- NA # legacy

# para cada cidade ...

res <- mclapply(cidades, calc.alerta,
Expand Down Expand Up @@ -486,7 +482,7 @@ tabela_historico <- function(obj, iniSE, lastSE, type = "all", versao = Sys.Date
varnames <-c("data_iniSE", "SE", "CID10","casos", "casos_est",
"casos_est_min", "casos_est_max", "municipio_geocodigo", "casprov",
"p_rt1", "p_inc100k", "Localidade_id", "nivel", "id", "versao_modelo",
"municipio_nome", "tweet", "Rt", "pop", "temp_min","temp_med",
"municipio_nome", "Rt", "pop", "temp_min","temp_med",
"temp_max","umid_min","umid_med","umid_max", "receptivo",
"transmissao", "nivel_inc")

Expand Down Expand Up @@ -614,7 +610,7 @@ write_alerta<-function(d, writetofile = FALSE, datasource = con, arq = "output.s

dcolumns <- c("SE", "data_iniSE", "casos_est", "casos_est_min", "casos_est_max",
"casos","casprov","municipio_geocodigo","p_rt1","p_inc100k","Localidade_id",
"nivel","id","versao_modelo","municipio_nome","Rt", "pop", "tweet",
"nivel","id","versao_modelo","municipio_nome","Rt", "pop",
"receptivo","transmissao","nivel_inc","temp_min","temp_med","temp_max",
"umid_min","umid_med","umid_max")

Expand All @@ -639,7 +635,7 @@ write_alerta<-function(d, writetofile = FALSE, datasource = con, arq = "output.s
# ------ sql command
varnamesforsql <- c("\"SE\"", "\"data_iniSE\"", "casos_est", "casos_est_min", "casos_est_max",
"casos","casprov","municipio_geocodigo","p_rt1","p_inc100k","\"Localidade_id\"",
"nivel","id","versao_modelo","municipio_nome", "tweet", "\"Rt\"", "pop",
"nivel","id","versao_modelo","municipio_nome", "\"Rt\"", "pop",
"tempmin","tempmed","tempmax","umidmin","umidmed","umidmax",
"receptivo", "transmissao","nivel_inc")

Expand All @@ -655,7 +651,7 @@ write_alerta<-function(d, writetofile = FALSE, datasource = con, arq = "output.s
"casos","casprov","municipio_geocodigo","p_rt1","p_inc100k","Localidade_id","nivel","id")], collapse=","),",'",
as.character(vetor$versao_modelo),"','",
as.character(vetor$municipio_nome),"',",
str_c(vetor[1,c("tweet","Rt","pop","temp_min","temp_med","temp_max","umid_min","umid_med","umid_max")], collapse = ","), ",",
str_c(vetor[1,c("Rt","pop","temp_min","temp_med","temp_max","umid_min","umid_med","umid_max")], collapse = ","), ",",
str_c(vetor[1,c("receptivo","transmissao","nivel_inc")], collapse = ",")
)

Expand Down Expand Up @@ -697,5 +693,3 @@ write_alerta<-function(d, writetofile = FALSE, datasource = con, arq = "output.s
}

}


119 changes: 119 additions & 0 deletions tests/testthat/test-remove-tweet-historical-alerts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
library(assertthat)
library(dplyr)
library(purrr)
library(stringr)

source(testthat::test_path("..", "..", "R", "utility_tools.R"))
source(testthat::test_path("..", "..", "R", "alert_functions.R"))

historical_alert_input <- function(include_tweet = TRUE) {
data <- data.frame(
cidade = 3304557,
Localidade_id = 0,
SE = 202401,
nome = "Rio de Janeiro",
CID10 = "A90",
casos = 10,
tcasesmed = 12,
tcasesICmin = 8,
tcasesICmax = 16,
cas_prov = 9,
p1 = 0.5,
localidade = NA,
Rt = 1.2,
pop = 1000000,
temp_min = 20,
temp_med = 25,
temp_max = 30,
umid_min = 40,
umid_med = 50,
umid_max = 60
)
if (include_tweet) data$tweet <- 42

structure(
list(
data = data,
indices = data.frame(cytrue = 1, cotrue = 1, level = 2)
),
class = "alerta"
)
}

test_that("tabela_historico excludes legacy tweet input", {
alert_environment <- environment(tabela_historico)
original_read_parameters <- get("read.parameters", envir = alert_environment)
original_SE2date <- get("SE2date", envir = alert_environment)
assign("read.parameters", function(...) {
data.frame(
municipio_geocodigo = 3304557,
limiar_preseason = 5,
limiar_epidemico = 15
)
}, envir = alert_environment)
on.exit(assign("read.parameters", original_read_parameters,
envir = alert_environment), add = TRUE)
assign("SE2date", function(se) {
data.frame(SE = se, ini = as.Date("2024-01-01"))
}, envir = alert_environment)
on.exit(assign("SE2date", original_SE2date, envir = alert_environment),
add = TRUE)

with_tweet <- tabela_historico(historical_alert_input(TRUE))
without_tweet <- tabela_historico(historical_alert_input(FALSE))

expect_false("tweet" %in% names(with_tweet))
expect_false("tweet" %in% names(without_tweet))
expect_true(all(c("CID10", "casos_est", "Rt", "nivel_inc") %in% names(with_tweet)))
})

historical_alert_row <- function(cid10) {
data.frame(
SE = 202401,
data_iniSE = as.Date("2024-01-01"),
casos_est = 12,
casos_est_min = 8,
casos_est_max = 16,
casos = 10,
casprov = 9,
municipio_geocodigo = 3304557,
p_rt1 = 0.5,
p_inc100k = 1.2,
Localidade_id = 0,
nivel = 2,
id = "33045570202401",
versao_modelo = "2024-01-01",
municipio_nome = "Rio de Janeiro",
Rt = 1.2,
pop = 1000000,
receptivo = 1,
transmissao = 1,
nivel_inc = 1,
temp_min = 20,
temp_med = 25,
temp_max = 30,
umid_min = 40,
umid_med = 50,
umid_max = 60,
CID10 = cid10
)
}

test_that("write_alerta SQL omits tweet for every disease table", {
expected_tables <- c(
A90 = "Historico_alerta",
"A92.0" = "Historico_alerta_chik",
"A92.8" = "Historico_alerta_zika"
)

for (cid10 in names(expected_tables)) {
sql_file <- tempfile(fileext = ".sql")
write_alerta(historical_alert_row(cid10), writetofile = TRUE, arq = sql_file)
sql <- paste(readLines(sql_file, warn = FALSE), collapse = "\n")

expect_false(grepl("\\btweet\\b", sql, ignore.case = TRUE))
expect_match(sql, expected_tables[[cid10]], fixed = TRUE)
expect_match(sql, "casos_est", fixed = TRUE)
expect_match(sql, "\\\"Rt\\\"")
}
})