Skip to content

Commit

Permalink
read session logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dipterix committed Feb 2, 2024
1 parent ce90681 commit 031c0e9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ravedash
Type: Package
Title: Dashboard System for Reproducible Visualization of 'iEEG'
Version: 0.1.3.23
Version: 0.1.3.24
Authors@R: c(
person("Zhengjia", "Wang", email = "dipterix.wang@gmail.com", role = c("aut", "cre", "cph"))
)
Expand Down Expand Up @@ -40,7 +40,7 @@ Suggests:
httr,
rmarkdown,
testthat (>= 3.0.0)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
URL: https://dipterix.org/ravedash/
BugReports: https://github.com/dipterix/ravedash/issues
VignetteBuilder: knitr
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ S3method(logger_error_condition,tar_condition_targets)
S3method(names,ravedash_shiny_icons)
S3method(print,"rave-dash-session")
S3method(print,ravedash_printable)
S3method(print,ravedash_session_log_string)
S3method(remove_session,"rave-dash-session")
S3method(remove_session,default)
S3method(use_session,"rave-dash-session")
Expand Down Expand Up @@ -85,6 +86,7 @@ export(remove_session)
export(run_analysis_button)
export(safe_observe)
export(session_getopt)
export(session_log)
export(session_setopt)
export(set_card_badge)
export(set_card_url_scheme)
Expand Down
82 changes: 82 additions & 0 deletions R/core-app.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ ensure_template <- function(path, use_cache = TRUE){
#' @param modules selected module ID to launch; used to only show a subset of
#' modules; default is \code{NULL} (select all modules); hidden modules are
#' always selected
#' @param max_lines maximum number of log entries to return; default is 200
#' @param page_title session web page title and logo text; can have length
#' of either one (page title and logo text are the same); or length of two,
#' with page title be the first element and logo text be the second.
Expand Down Expand Up @@ -875,3 +876,84 @@ shutdown_session <- function(
}
shiny::stopApp(returnValue = returnValue)
}


#' @rdname rave-session
#' @export
session_log <- function(x, max_lines = 200, modules = NULL) {
n <- as.integer(max_lines)
if(n <= 0) { n <- 5000L }
if(missing(x) || is.null(x)) {
x <- list_session(order = "descend")
if(!length(x)) {
return(structure(character(0L), class = "ravedash_session_log_string", max_lines = n, session_id = NULL))
}
x <- x[[1]]
}
session <- use_session(x)
log_dir <- file.path(session$app_path, "logs")
if(!dir.exists(log_dir)) {
return(structure(character(0L), class = "ravedash_session_log_string", max_lines = n, session_id = session$session_id))
}
all_modules <- list.files(log_dir, pattern = "\\.log", all.files = FALSE, full.names = FALSE, recursive = FALSE, include.dirs = FALSE)
modules <- all_modules
if(length(modules)) {
modules <- gsub("\\(.log|)$", ".log", x = modules, ignore.case = TRUE)
modules <- c(modules[modules %in% all_modules], "ravedash.log")
}
modules <- unique(modules)
modules <- modules[file.exists(file.path(log_dir, modules))]
if(!length(modules)) {
return(structure(character(0L), class = "ravedash_session_log_string", max_lines = n, session_id = session$session_id))
}
logs <- lapply(modules, function(module) {
s <- trimws(readLines(file.path(log_dir, module)))
if(length(s) > n) {
s <- s[ -seq_len(length(s) - n) ]
}
timestamp <- substring(gsub("^(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)[ ]{0, }", "", s), 1, 19)
timestamp <- strptime(timestamp, format = "%Y-%m-%d %H:%M:%S")
nas <- dipsaus::deparse_svec( which(is.na(timestamp)), concatenate = FALSE)

for(ns_idx in nas) {
idx <- dipsaus::parse_svec(ns_idx)
if(length(idx)) {
midx <- idx[[1]]
if(midx > 1) {
timestamp[ idx ] <- timestamp[[ midx - 1 ]]
}
}
}
data.frame(
time = timestamp,
string = s
)
})
logs_combined <- do.call("rbind", logs)
timestamps <- logs_combined$time[!is.na(logs_combined$time)]

if(length(timestamps) > n) {
tmp <- timestamps[[ order(timestamps, decreasing = TRUE)[[n]] ]]
logs <- lapply(logs, function(log) {
log[log$time >= tmp, ]
})
logs_combined <- do.call("rbind", logs)
}
return(structure(logs_combined$string[order(logs_combined$time, decreasing = FALSE)],
class = "ravedash_session_log_string", max_lines = n, session_id = session$session_id))

}

#' @export
print.ravedash_session_log_string <- function(x, ...) {
x_ <- unclass(x)
session_id <- attr(x_, "session_id")
if(is.null(session_id)) {
cat("<RAVE Module Session Logs> (empty session ID)\n")
return(invisible(x))
}
cat(sprintf("<RAVE Module Session Logs> (%s)\n", session_id))
cat(x_, sep = "\n")
cat(sprintf("<Max lines: %s>\n", attr(x_, "max_lines")))
return(invisible(x))
}
5 changes: 5 additions & 0 deletions man/rave-session.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 031c0e9

Please sign in to comment.