Skip to content

Commit

Permalink
cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Aug 29, 2024
1 parent d76b8fa commit 35e4451
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
base64enc,
cachem,
docopt,
jsonlite,
logger,
Expand Down
15 changes: 10 additions & 5 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ target_post_dataset <- function(req, res) {
if (length(missing_cols) > 0) {
res$status <- 400L
msg <- paste("Missing required columns:",
paste(missing_cols, collapse = ", "))
paste(missing_cols, collapse = ", "))
return(bad_request_response(msg))
}

Expand Down Expand Up @@ -101,9 +101,9 @@ target_get_trace <- function(name,
filters <- strsplit(filter, "+", fixed = TRUE)[[1]]
logger::log_info(paste("Filtering by variables:", paste(filters,
collapse = ", ")))
for (f in filters) {
dat <- apply_filter(f, dat, cols)
}
for (f in filters) {
dat <- apply_filter(f, dat, cols)
}
}
dat <- dat[dat["biomarker"] == biomarker, ]
if (length(disaggregate) > 0) {
Expand Down Expand Up @@ -191,5 +191,10 @@ get_or_create_session_id <- function(req) {
}

generate_session_id <- function() {
rawToChar(as.raw(sample(c(65:90, 97:122), 10, replace = TRUE)))
tolower(rawToChar(sample(c(as.raw(sample(c(65:90, 97:122),
5,
replace = TRUE)),
as.raw(sample(48:57,
5,
replace = TRUE))))))
}
22 changes: 20 additions & 2 deletions R/router.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build_routes <- function(cookie_key = plumber::random_cookie_key()) {
build_routes <- function(cookie_key = plumber::random_cookie_key(),
cache = cachem::cache_mem(max_age = 60)) {
if (!dir.exists("uploads")) {
dir.create("uploads")
}
Expand All @@ -11,12 +12,19 @@ build_routes <- function(cookie_key = plumber::random_cookie_key()) {
res$setHeader("Access-Control-Allow-Origin", req$HTTP_ORIGIN)
res$setHeader("Access-Control-Allow-Credentials", "true")

Check warning on line 13 in R/router.R

View check run for this annotation

Codecov / codecov/patch

R/router.R#L13

Added line #L13 was not covered by tests
}

if (!is.null(req$session$id)) {
id <- as.character(req$session$id)
cache$set(id, TRUE)
}
prune_inactive_sessions(cache)
value
})

pr$registerHooks(plumber::session_cookie(cookie_key,
name = "serovizr",
path = "/"))
path = "/",
expiration = 60))

pr$handle(get_root())
pr$handle(get_version())
Expand Down Expand Up @@ -68,3 +76,13 @@ get_trace <- function() {
filter = "string"),
returning = porcelain::porcelain_returning_json("DataSeries"))
}

prune_inactive_sessions <- function(cache) {
active_sessions <- cache$keys()
subdirectories <- list.files("uploads")
old_sessions <- setdiff(subdirectories, active_sessions)
if (length(old_sessions) > 0) {
logger::log_info("Cleaning up expired sessions")
lapply(old_sessions, function(x) fs::dir_delete(file.path("uploads", x)))
}
}
2 changes: 1 addition & 1 deletion tests/testthat/test-router.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ test_that("requests without trailing slash are redirected", {
router <- build_routes()
res_api <- router$request("GET", "/version")
expect_equal(res_api$status, 307)
})
})

0 comments on commit 35e4451

Please sign in to comment.