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 71895b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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))))))
}
20 changes: 18 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,17 @@ 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")
}

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 +74,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)))
}
}

0 comments on commit 71895b4

Please sign in to comment.