R/logs.R

Defines functions read_cromwell_log logs.WorkflowIdAndStatus logs.character logs

Documented in logs logs.character logs.WorkflowIdAndStatus read_cromwell_log

#' Get the logs for a workflow
#'
#' Returns paths to the standard out and standard error files that were
#' generated during the execution of all calls in a workflow. A call has one or
#' more standard out and standard error logs, depending on if the call was
#' scattered or not. In the latter case, one log is provided for each instance
#' of the call that has been run.
#'
#' @param x Cromwell API endpoint of \code{WorkflowIdAndStatus} object.
#' @param ... not used
#'
#' @return \code{WorkflowLogs} object containing outputs and id.
#'
#' @examples
#' \dontrun{
#' host <- "http://localhost:8000"
#' workflow <- system.file("extdata/workflow.wdl", package = "CromwellClient")
#' inputs <- system.file("extdata/inputs.json", package = "CromwellClient")
#' job <- submit(host, workflow, inputs)
#' logs(job)
#' }
#'
#' @rdname logs
#' @export
logs <- function(x, ...)
{
	UseMethod("logs", x)
}

#' @param id A workflow ID.
#' @param version Cromwell API Version.
#'
#' @rdname logs
#' @export
logs.character <- function(x, id, version = "v1", ...)
{
	url <- file.path(x, "api/workflows/", version, id, "logs")

	response <- httr::GET(url, cromwellAgent())

	if (httr::http_error(response)) {
		stop(paste0("Cromwell API request failed.
Code: ", httr::status_code(response), "
Message: ", jsonlite::fromJSON(httr::content(response, "text"))$message), call. = FALSE)
	}

	if (httr::http_type(response) != "application/json") {
		stop("API did not return json", call. = FALSE)
	}

	content <- jsonlite::fromJSON(httr::content(response, "text"))
	structure(
		c(list(
			id = content$id,
			host = x,
			version = version
		), content$calls),
		class = "WorkflowLogs"
	)
}

#' @rdname logs
#' @export
logs.WorkflowIdAndStatus <- function(x, ...)
{
	logs(x$host, id = x$id, version = x$version)
}

CROMWELL_LOG_REGEX <- "^(.+),\\d+\\s+INFO\\s+-\\s+\\w+\\s+\\[UUID\\((.+)\\)(.+)\\.(.+):(.+:\\d+)\\]:\\s+Status change from.+to\\s+(Running|Complete)$"
DIFF_TIME_UNITS <- "hours"

#' Read Cromwell log file
#'
#' @rdname logs
#' @export
read_cromwell_log <- function(file) {
  data <- read_lines(file) %>%
    str_match(CROMWELL_LOG_REGEX) %>%
    na.omit()
  
  tibble(
    id = data[, 3],
    workflow = data[, 4],
    task = data[, 5],
    shard = data[, 6],
    state = ifelse(grepl("Running", data[, 7]), "start", "end"),
    time = as.POSIXct(data[, 2])
  ) %>%
    spread(state, time) %>%
    mutate(elapsed = difftime(end, start, units = DIFF_TIME_UNITS))
}
labbcb/CromwellClient documentation built on Aug. 16, 2020, 9:11 p.m.