|
| 1 | +library(dplyr) |
| 2 | + |
| 3 | +#' Search for Posteriors containing wildcards for filtering |
| 4 | +#' @param pft_id PFT Id (character) |
| 5 | +#' @param offset |
| 6 | +#' @param limit |
| 7 | +#' @return Information about Posteriors based on pft |
| 8 | +#' @author Nihar Sanda |
| 9 | +#* @get / |
| 10 | +searchPosteriors <- function(req, pft_id = NA, host_id = NA, |
| 11 | + offset = 0, limit = 50, res) { |
| 12 | + if (!limit %in% c(10, 20, 50, 100, 500)) { |
| 13 | + res$status <- 400 |
| 14 | + return(list(error = "limit parameter must be 10, 20, 50, 100, or 500")) |
| 15 | + } |
| 16 | + |
| 17 | + posteriors <- tbl(global_db_pool, "posteriors") %>% |
| 18 | + select(everything()) |
| 19 | + |
| 20 | + posteriors <- tbl(global_db_pool, "dbfiles") %>% |
| 21 | + select(file_name, |
| 22 | + file_path, |
| 23 | + container_type, |
| 24 | + id = container_id, |
| 25 | + machine_id) %>% |
| 26 | + inner_join(posteriors, by = "id") %>% |
| 27 | + filter(container_type == "Posterior") %>% |
| 28 | + select(-container_type) |
| 29 | + |
| 30 | + posteriors <- tbl(global_db_pool, "machines") %>% |
| 31 | + select(hostname, machine_id = id) %>% |
| 32 | + inner_join(posteriors, by = "machine_id") |
| 33 | + |
| 34 | + posteriors <- tbl(global_db_pool, "pfts") %>% |
| 35 | + select(pft_name = name, pft_id = id) %>% |
| 36 | + inner_join(posteriors, by = "pft_id") |
| 37 | + |
| 38 | + if (!is.na(pft_id)) { |
| 39 | + posteriors <- posteriors %>% |
| 40 | + filter(pft_id == !!pft_id) |
| 41 | + } |
| 42 | + |
| 43 | + if (!is.na(host_id)) { |
| 44 | + posteriors <- posteriors %>% |
| 45 | + filter(machine_id == !!host_id) |
| 46 | + } |
| 47 | + |
| 48 | + qry_res <- posteriors %>% |
| 49 | + select(-pft_id, -machine_id) %>% |
| 50 | + distinct() %>% |
| 51 | + arrange(id) %>% |
| 52 | + collect() |
| 53 | + |
| 54 | + if (nrow(qry_res) == 0 || as.numeric(offset) >= nrow(qry_res)) { |
| 55 | + res$status <- 404 |
| 56 | + return(list(error = "Posterior(s) not found")) |
| 57 | + } else { |
| 58 | + has_next <- FALSE |
| 59 | + has_prev <- FALSE |
| 60 | + if (nrow(qry_res) > (as.numeric(offset) + as.numeric(limit))) { |
| 61 | + has_next <- TRUE |
| 62 | + } |
| 63 | + if (as.numeric(offset) != 0) { |
| 64 | + has_prev <- TRUE |
| 65 | + } |
| 66 | + |
| 67 | + start_idx <- as.numeric(offset) + 1 |
| 68 | + end_idx <- min((as.numeric(offset) + as.numeric(limit)), nrow(qry_res)) |
| 69 | + qry_res <- qry_res[start_idx:end_idx, ] |
| 70 | + |
| 71 | + result <- list(posteriors = qry_res) |
| 72 | + result$count <- nrow(qry_res) |
| 73 | + if (has_next) { |
| 74 | + if (grepl("offset=", req$QUERY_STRING, fixed = TRUE)) { |
| 75 | + result$next_page <- paste0( |
| 76 | + req$rook.url_scheme, "://", |
| 77 | + req$HTTP_HOST, |
| 78 | + "/api/posteriors", |
| 79 | + req$PATH_INFO, |
| 80 | + substr(req$QUERY_STRING, |
| 81 | + 0, |
| 82 | + stringr::str_locate(req$QUERY_STRING, "offset=")[[2]]), |
| 83 | + (as.numeric(limit) + as.numeric(offset)), |
| 84 | + "&limit=", |
| 85 | + limit |
| 86 | + ) |
| 87 | + } else { |
| 88 | + result$next_page <- paste0( |
| 89 | + req$rook.url_scheme, "://", |
| 90 | + req$HTTP_HOST, |
| 91 | + "/api/posteriors", |
| 92 | + req$PATH_INFO, |
| 93 | + substr(req$QUERY_STRING, |
| 94 | + 0, |
| 95 | + stringr::str_locate(req$QUERY_STRING, "limit=")[[2]] - 6), |
| 96 | + "offset=", |
| 97 | + (as.numeric(limit) + as.numeric(offset)), |
| 98 | + "&limit=", |
| 99 | + limit |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | + if (has_prev) { |
| 104 | + result$prev_page <- paste0( |
| 105 | + req$rook.url_scheme, "://", |
| 106 | + req$HTTP_HOST, |
| 107 | + "/api/workflows", |
| 108 | + req$PATH_INFO, |
| 109 | + substr(req$QUERY_STRING, |
| 110 | + 0, |
| 111 | + stringr::str_locate(req$QUERY_STRING, "offset=")[[2]]), |
| 112 | + max(0, (as.numeric(offset) - as.numeric(limit))), |
| 113 | + "&limit=", |
| 114 | + limit |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + return(result) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +################################################################################ |
| 123 | + |
| 124 | +#' Download the posterior specified by the id |
| 125 | +#' @param id Posterior id (character) |
| 126 | +#' @param filename Optional filename specified if the id points to a folder |
| 127 | +#' instead of file (character). If this is passed with an id that actually |
| 128 | +#' points to a file, this name will be ignored |
| 129 | +#' @return Posterior file specified by user |
| 130 | +#' @author Nihar Sanda |
| 131 | +#* @serializer contentType list(type="application/octet-stream") |
| 132 | +#* @get /<posterior_id> |
| 133 | +downloadPosterior <- function(posterior_id, filename = "", req, res) { |
| 134 | + db_hostid <- PEcAn.DB::dbHostInfo(global_db_pool)$hostid |
| 135 | + |
| 136 | + # This is just for temporary testing due to the existing issue in dbHostInfo() |
| 137 | + db_hostid <- ifelse(db_hostid == 99, 99000000001, db_hostid) |
| 138 | + |
| 139 | + posterior <- tbl(global_db_pool, "dbfiles") %>% |
| 140 | + select(file_name, file_path, container_id, machine_id, container_type) %>% |
| 141 | + filter(machine_id == !!db_hostid) %>% |
| 142 | + filter(container_type == "Posterior") %>% |
| 143 | + filter(container_id == !!posterior_id) %>% |
| 144 | + collect() |
| 145 | + |
| 146 | + if (filename != "") { |
| 147 | + posterior <- posterior %>% |
| 148 | + filter(file_name == !!filename) |
| 149 | + } |
| 150 | + |
| 151 | + if (nrow(posterior) == 0) { |
| 152 | + res$status <- 404 |
| 153 | + return("Posterior not found") |
| 154 | + } |
| 155 | + |
| 156 | + # Generate the full file path using the file_path & file_name |
| 157 | + filepath <- file.path(posterior$file_path, posterior$file_name) |
| 158 | + |
| 159 | + if (length(filepath) > 1 || dir.exists(filepath)) { |
| 160 | + # Don't know which file to send. Return 400 Bad Request error |
| 161 | + # TODO provide an endpoint to list the available files from one posterior |
| 162 | + # (maybe `/posteriors/{posterior_id}/files`?) |
| 163 | + res$status <- 400 |
| 164 | + return("Multiple matches. Please specify filename") |
| 165 | + } |
| 166 | + |
| 167 | + # If the file doesn't exist, return 404 error |
| 168 | + if (!file.exists(filepath)) { |
| 169 | + res$status <- 404 |
| 170 | + return("Posterior file not found") |
| 171 | + } |
| 172 | + |
| 173 | + # Read the data in binary form & return it |
| 174 | + bin <- readBin(filepath, "raw", n = file.info(filepath)$size) |
| 175 | + return(bin) |
| 176 | +} |
0 commit comments