Skip to content

Commit

Permalink
Include log feature for the get_polis_api_data function
Browse files Browse the repository at this point in the history
  • Loading branch information
truenomad committed Mar 22, 2024
1 parent d78b2a1 commit d18edd5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
Binary file modified .DS_Store
Binary file not shown.
46 changes: 43 additions & 3 deletions R/get_polis_api_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#' Default is retrieved from the environment variable
#' 'POLIS_API_KEY'. An explicit API key can be provided
#' if required.
#' @param log_results Logical indicating whether to log the results of the API
#' @param log_file_path Path to the directory where the log file will be saved
#' @param updated_dates Logical indicating whether to use the 'LastUpdateDate'
#' @return A data frame containing the requested data aggregated from all pages
#' of the API response. Each row represents a record, and columns
Expand All @@ -45,13 +47,15 @@
#' }
#' @export

get_polis_api_data <- function(min_date,
get_polis_api_data <- function(min_date = "2021-01-01",
max_date = Sys.Date(),
data_type = "cases",
region = "AFRO",
select_vars = NULL,
updated_dates = TRUE,
polis_api_key) {
updated_dates = FALSE,
polis_api_key,
log_results = FALSE,
log_file_path) {

# API Endpoint and URL Construction
api_endpoint <- "https://extranet.who.int/polis/api/v2/"
Expand Down Expand Up @@ -85,5 +89,41 @@ get_polis_api_data <- function(min_date,
# process API response
full_data <- process_api_response(response)

# log results
if (log_results) {

# Check if log file name is provided
if (is.null(log_file_path)) {
warning("No log file name provided. Logging is disabled.")
return(invisible(NULL))
}

# set up log file name
log_file_name <- paste0(log_file_path, "/", "polis_data_update_log.rds")

# Construct the log message
log_message <- data.frame(
Region = tools::toTitleCase(region),
QueryStartDate = as.Date(min_date, format = "%Y-%m-%d"),
QueryEndDate = as.Date(max_date, format = "%Y-%m-%d"),
DataStartDate = min(as.Date(full_data[[date_field]])),
DataEndDate = max(as.Date(full_data[[date_field]])),
PolisDataType = as.character(endpoint_suffix),
NumberOfVariables = ncol(full_data),
NumberOfRows = format(nrow(full_data), big.mark = ",")
)

if (file.exists(log_file_name)) {
log_data <- epiCleanr::import(log_file_name)
log_data <- rbind(log_data, log_message)
} else {
log_data <- log_message
}

# Save log file
epiCleanr::export(log_data, log_file_name)

}

return(full_data)
}
11 changes: 10 additions & 1 deletion R/save_polis_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ save_polis_data <- function(polis_data, polis_path,
cli::cli_process_done( )

# Check existing datasets and keep only the 5 most recent
existing_files <- list.files(polis_path, full.names = TRUE)
existing_files <- list.files(
polis_path, pattern = "\\.rds$", full.names = TRUE)

# Check existing RDS datasets and keep only the 5 most recent
existing_files <- list.files(
polis_path, pattern = "\\.rds$", full.names = TRUE)

# Exclude files that contain 'polis_data_update_log' in the name
existing_files <- grep(
"polis_data_update_log", existing_files, value = TRUE, invert = TRUE)

if (length(existing_files) > 5) {
# Sort files by date, assuming the naming convention holds the date info
Expand Down
12 changes: 9 additions & 3 deletions man/get_polis_api_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d18edd5

Please sign in to comment.