Skip to content

Commit

Permalink
addded information on total file size being downloaded as an information
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed Feb 15, 2024
1 parent 6ea036a commit 0add3e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion R/ct_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ ct_get_data <- function(type = "goods",
} else{
resp <- ct_perform_request(req,
requests_per_second = requests_per_second,
verbose = verbose
verbose = verbose,
bulk = bulk
)
}

Expand Down
13 changes: 10 additions & 3 deletions R/ct_perform_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @noRd
#' @returns JSON data from comtrade, data.frame with results or error codes.
#' @inheritParams ct_get_data
ct_perform_request <- function(req, requests_per_second, verbose = FALSE) {
ct_perform_request <- function(req, requests_per_second, verbose = FALSE, bulk) {
if (verbose) {
cli::cli_inform(c("i" = "Performing request, which can take a few seconds, depending on the amount of data queried.")) # nolint
}
Expand All @@ -40,10 +40,17 @@ ct_perform_request <- function(req, requests_per_second, verbose = FALSE) {
) |>
httr2::req_perform()

if (verbose) {
cli::cli_inform(c("v" = "Got a response object from UN Comtrade. Use `process = F` if there is an error after this step to find issues with the response object.")) # nolint
if(bulk){
if (verbose) {
cli::cli_inform(c("v" = "Got a response object from UN Comtrade."))
}
} else {
if (verbose) {
cli::cli_inform(c("v" = "Got a response object from UN Comtrade. Use `process = F` if there is an error after this step to find issues with the response object.")) # nolint
}
}


return(resp)
}

Expand Down
27 changes: 27 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,30 @@ replace_month <- function(date_str) {
)
stringr::str_replace_all(date_str, months)
}


#' convert file size from comtrade
#'
#' @noRd
convert_file_size <- function(file_sizes) {
units <- c(KB = 1024, MB = 1024^2, GB = 1024^3, TB = 1024^4)
sapply(file_sizes, function(x) {
parts <- strsplit(x, " ")[[1]]
number <- as.numeric(parts[1])
unit <- units[toupper(parts[2])]
number * unit
})
}

#' format file size from comtrade
#'
#' @noRd
format_file_size <- function(size_in_bytes) {
units <- c("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
if (size_in_bytes == 0) {
return("0 B")
}
i <- floor(log(size_in_bytes, 1024))
p <- size_in_bytes / 1024^i
paste0(format(p, digits = 3, nsmall = 1), " ", units[i + 1])
}

0 comments on commit 0add3e3

Please sign in to comment.