Skip to content

Commit

Permalink
better download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Jan 4, 2024
1 parent bab8781 commit 379e86c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Imports:
cli,
dplyr,
httr2,
prettyunits,
purrr,
tibble,
utils
Expand Down
2 changes: 2 additions & 0 deletions R/models.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pull_model <- function(model = NULL, server = NULL) {
httr2::req_body_json(list(name = model)) |>
httr2::req_perform_stream(callback = pgrs, buffer_kb = 0.1)

cli::cli_process_done(.envir = the)
the$str_prgs <- NULL

invisible(show_model(model))
Expand Down Expand Up @@ -61,6 +62,7 @@ delete_model <- function(model, server = NULL) {
httr2::req_url_path_append("/api/delete") |>
httr2::req_method("DELETE") |>
httr2::req_body_json(list(name = model)) |>
httr2::req_error(body = function(resp) httr2::resp_body_json(resp)$error) |>
httr2::req_perform()

cli::cli_progress_message("{cli::col_green(cli::symbol$tick)} {model} removed")
Expand Down
24 changes: 17 additions & 7 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,41 @@ pgrs <- function(resp) {
status <- setdiff(status, the$str_prgs$pb_done)

for (s in status) {

status_message <- purrr::pluck(s, "status")
if (!purrr::pluck_exists(s, "total")) {
cli::cli_progress_step(purrr::pluck(s, "status"), .envir = the)
if (status_message == "success") {
cli::cli_progress_message("{cli::col_green(cli::symbol$tick)} success!")
} else {
cli::cli_progress_step(purrr::pluck(s, "status"), .envir = the)
}
} else {
the$str_prgs$f <- sub("pulling ", "", purrr::pluck(s, "status"))
the$str_prgs$done <- purrr::pluck(s, "completed", .default = 0L)
the$str_prgs$total <- purrr::pluck(s, "total", .default = 0L)
the$str_prgs$done_pct <-
paste(round(the$str_prgs$done / the$str_prgs$total * 100, 0), "%")
the$str_prgs$speed <-
prettyunits::pretty_bytes(
the$str_prgs$done /
(as.integer(Sys.time()) - as.integer(the$str_prgs$pb_start)))
if (!isTRUE(the$str_prgs$pb == the$str_prgs$f)) {
cli::cli_progress_bar(
name = the$str_prgs$f,
type = "download",
format = paste0(
"{cli::pb_spin} Downloading {str_prgs$f} ",
"[{str_prgs$done}/{str_prgs$total}] ETA:{cli::pb_eta}"
"({str_prgs$done_pct} of {prettyunits::pretty_bytes(str_prgs$total)}) ",
"at {str_prgs$speed}/s"
),
format_done = paste0(
"{cli::col_green(cli::symbol$tick)} Downloaded f ",
"in {cli::pb_elapsed}."
"{cli::col_green(cli::symbol$tick)} Downloaded {str_prgs$f}."
),
total = the$str_prgs$total,
.envir = the
)
the$str_prgs$pb <- the$str_prgs$f
the$str_prgs$pb_start <- Sys.time()
} else {
if ( the$str_prgs$total > the$str_prgs$done) {
if (the$str_prgs$total > the$str_prgs$done) {
cli::cli_progress_update(force = TRUE, .envir = the)
} else {
cli::cli_process_done(.envir = the)
Expand Down

0 comments on commit 379e86c

Please sign in to comment.