Skip to content

Commit 68bd0b4

Browse files
author
md
committed
Show statistics for found data & Ask user for permisssion to download data if the size large
1 parent 8373992 commit 68bd0b4

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Imports:
1616
jsonlite,
1717
magrittr,
1818
htmltools,
19-
stringr
19+
stringr,
20+
humanize
2021
Depends:
2122
R (>= 2.10),
2223
Suggests:

R/client.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Client <- R6::R6Class("Client",
284284
#' @importFrom stringr str_detect
285285
#' @export
286286
search = function(query, limit = NULL) {
287-
json_query <- jsonlite::toJSON(query, pretty = TRUE, auto_unbox = TRUE)
287+
json_query <- jsonlite::toJSON(query, pretty = TRUE, auto_unbox = FALSE)
288288
json_query <- strip_off_template_placeholders(json_query)
289289

290290
url <- paste0(self$apiUrl, "/dataaccess/search")
@@ -297,7 +297,12 @@ Client <- R6::R6Class("Client",
297297
paginator <- Paginator$new(self, request_type = "POST")
298298
results <- paginator$run(req, limit)
299299

300-
SearchResults$new(self, results, query$dataset_id)
300+
search_results <- SearchResults$new(self, results, query$dataset_id)
301+
302+
print(paste("Found", search_results$total_count, "files"))
303+
print(paste("Total Size", humanize::natural_size(search_results$total_size)))
304+
305+
search_results
301306
},
302307
error = function(err) {
303308
stop(paste("Search query failed"))

R/search_results.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ SearchResults <- R6::R6Class("SearchResults",
1212
#' @field results Stores the search results data.
1313
results = NULL,
1414

15+
#' @field total_count Stores the total count of results' element.
16+
total_count = NULL,
17+
18+
#' @field total_size Stores the total size of results
19+
total_size = NULL,
20+
1521
#' @description Initializes a new SearchResults object with the specified client, results, and dataset identifier.
1622
#'
1723
#' @param client An object containing the API client used to interact with the dataset.
@@ -23,6 +29,11 @@ SearchResults <- R6::R6Class("SearchResults",
2329
private$client <- client
2430
private$dataset_id <- dataset_id
2531
self$results <- results
32+
33+
self$total_count <- length(sapply(results, FUN = function(x) {
34+
x$id
35+
}))
36+
self$total_size <- sum(sapply(results, function(x) if (!is.null(x$properties$size)) x$properties$size else 0))
2637
},
2738

2839
#' @description
@@ -33,6 +44,10 @@ SearchResults <- R6::R6Class("SearchResults",
3344
#' @return Nothing returned but downloaded files are saved at the specified location.
3445
#' @export
3546
download = function(output_dir = ".", selected_indexes, stop_at_failure = TRUE) {
47+
if (self$total_count == 0 || !private$prompt_user_confirmation(self$total_size)) {
48+
return(NULL)
49+
}
50+
3651
print("[Download] Start")
3752

3853
if (!dir.exists(output_dir)) {
@@ -72,6 +87,7 @@ SearchResults <- R6::R6Class("SearchResults",
7287
}
7388
),
7489
private = list(
90+
LARGE_DOWNLOAD_SIZE = 100 * 1024 * 1024, # 100MB
7591
client = NULL,
7692
dataset_id = NULL,
7793
check_status = function(download_id) {
@@ -120,7 +136,7 @@ SearchResults <- R6::R6Class("SearchResults",
120136
# Extract the file name from the Content-Disposition header
121137
content_disposition <- httr2::resp_headers(resp, "content-disposition")
122138
filename <- if (!is.null(content_disposition)) {
123-
gsub('.*filename="?([^"]+)"?.*', '\\1', content_disposition)
139+
gsub('.*filename="?([^"]+)"?.*', "\\1", content_disposition)
124140
} else {
125141
"downloaded_file"
126142
}
@@ -130,6 +146,22 @@ SearchResults <- R6::R6Class("SearchResults",
130146
writeBin(httr2::resp_body_raw(resp), file_path)
131147

132148
return(NA)
149+
},
150+
prompt_user_confirmation = function(total_size) {
151+
if (total_size >= private$LARGE_DOWNLOAD_SIZE) {
152+
repeat {
153+
cat("The total size is", humanize::natural_size(total_size), ". Do you want to proceed? (Y/N): ")
154+
answer <- tolower(readLines(n = 1))
155+
if (answer %in% c("y", "n")) {
156+
return(answer == "y")
157+
} else {
158+
cat("Invalid input. Please enter 'Y' or 'N'.\n")
159+
return(TRUE)
160+
}
161+
}
162+
} else {
163+
return(TRUE)
164+
}
133165
}
134166
)
135167
)

man/SearchResults.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)