boB Rudis
3 years ago
9 changed files with 202 additions and 7 deletions
@ -1,4 +1,53 @@ |
|||||
|
is_windows <- function() .Platform$OS.type == "windows" |
||||
|
is_mac <- function() Sys.info()[["sysname"]] == "Darwin" |
||||
|
is_linux <- function() Sys.info()[["sysname"]] == "Linux" |
||||
|
|
||||
|
platform <- function() { |
||||
|
if (is_windows()) return("win") |
||||
|
if (is_mac()) return("mac") |
||||
|
if (is_linux()) return("linux") |
||||
|
stop("unknown platform") |
||||
|
} |
||||
|
|
||||
set_names <- function (object = nm, nm) { |
set_names <- function (object = nm, nm) { |
||||
names(object) <- nm |
names(object) <- nm |
||||
object |
object |
||||
|
} |
||||
|
|
||||
|
version_test <- function(zq_path) { |
||||
|
|
||||
|
try( |
||||
|
system2( |
||||
|
command = zq_path, |
||||
|
args = "-version", |
||||
|
stdout = TRUE, |
||||
|
stderr = TRUE |
||||
|
) , |
||||
|
silent = TRUE |
||||
|
) -> res |
||||
|
|
||||
|
((is.null(attributes(res))) && any(grepl("Version", res))) |
||||
|
|
||||
|
} |
||||
|
|
||||
|
find_zq <- function() { |
||||
|
|
||||
|
try_env <- Sys.getenv("ZQ_PATH", unset = NA) |
||||
|
|
||||
|
if (is.na(try_env)) { |
||||
|
if (is_mac()) { |
||||
|
try_env <- "/Applications/Brim.app/Contents/Resources/app/zdeps/zq" |
||||
|
} else if (is_linux()) { |
||||
|
try_env <- "/usr/lib/brim/resources/app/zdeps/zq" |
||||
|
} |
||||
|
} # TODO Windows |
||||
|
|
||||
|
if (!file.exists(try_env)) try_env <- Sys.which("zq") |
||||
|
|
||||
|
if (file.exists(try_env)) { |
||||
|
if (version_test(try_env)) return(try_env) |
||||
|
} |
||||
|
|
||||
|
NA |
||||
|
|
||||
} |
} |
@ -0,0 +1,63 @@ |
|||||
|
#' Execute a zq command line |
||||
|
#' |
||||
|
#' zq is a command-line tool for processing logs. It applies boolean logic to |
||||
|
#' filter each log value, optionally computes analytics and transformations, |
||||
|
#' and returns results that can be consumed programmatically.\cr |
||||
|
#' \cr |
||||
|
#' This function takes command line arguments to be used with `zq` ( |
||||
|
#' in the form [system2()] uses) and reads the results into a |
||||
|
#' data frame (it is actually a `data.table`).\cr |
||||
|
#' \cr |
||||
|
#' If the environment variable `ZQ_PATH` is not set or invalid, this function |
||||
|
#' will attempt to guess the `zq` binary path.\cr |
||||
|
#' \cr |
||||
|
#' Do not specify an output format as `-f ndjson` is added by default. |
||||
|
#' |
||||
|
#' @param args see [system2()] |
||||
|
#' @param parse if `TRUE` (the default) the output of the command line call |
||||
|
#' will be parsed using [ndjson::stream_in()]. There are some combinations |
||||
|
#' of `zq` flags that will never return ndjson output. There are heuristics |
||||
|
#' in place to detect this, but you can deliberately force the function |
||||
|
#' to return raw command line output by setting this to `FALSE`. |
||||
|
#' @return `data.table` (if output is parseable); character vector (if output is |
||||
|
#' either not parseable or `parse` equals `FALSE`); or `NULL` in the |
||||
|
#' event an error occurred when processing the `zq` command line. |
||||
|
#' @export |
||||
|
#' @examples |
||||
|
#' zq_cmd( |
||||
|
#' c( |
||||
|
#' '"* | cut ts,id.orig_h,id.orig_p"', # note the quotes |
||||
|
#' system.file("logs", "conn.log.gz", package = "brimr") |
||||
|
#' ) |
||||
|
#' ) |
||||
|
zq_cmd <- function(args, parse = TRUE) { |
||||
|
|
||||
|
zq_path <- find_zq() |
||||
|
|
||||
|
if (is.na(zq_path)) { |
||||
|
stop( |
||||
|
"Cannot locate 'zq'. Please set the ZQ_PATH to the full path to the ", |
||||
|
"executable or install Brim Desktop and/or zq.", call. = FALSE |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
tf <- tempfile(fileext = ".json") |
||||
|
on.exit(unlink(tf)) |
||||
|
|
||||
|
system2( |
||||
|
command = zq_path, |
||||
|
args = c("-f ", "ndjson", args), |
||||
|
stdout = tf |
||||
|
) -> res |
||||
|
|
||||
|
if (!is.null(res)) { |
||||
|
|
||||
|
one <- readLines(tf, 1) |
||||
|
|
||||
|
if ((parse) && grepl("\\{", one)) return(ndjson::stream_in(tf)) |
||||
|
|
||||
|
readLines(tf) |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Binary file not shown.
@ -0,0 +1,44 @@ |
|||||
|
% Generated by roxygen2: do not edit by hand |
||||
|
% Please edit documentation in R/zq-cmd.R |
||||
|
\name{zq_cmd} |
||||
|
\alias{zq_cmd} |
||||
|
\title{Execute a zq command line} |
||||
|
\usage{ |
||||
|
zq_cmd(args, parse = TRUE) |
||||
|
} |
||||
|
\arguments{ |
||||
|
\item{args}{see \code{\link[=system2]{system2()}}} |
||||
|
|
||||
|
\item{parse}{if \code{TRUE} (the default) the output of the command line call |
||||
|
will be parsed using \code{\link[ndjson:stream_in]{ndjson::stream_in()}}. There are some combinations |
||||
|
of \code{zq} flags that will never return ndjson output. There are heuristics |
||||
|
in place to detect this, but you can deliberately force the function |
||||
|
to return raw command line output by setting this to \code{FALSE}.} |
||||
|
} |
||||
|
\value{ |
||||
|
\code{data.table} (if output is parseable); character vector (if output is |
||||
|
either not parseable or \code{parse} equals \code{FALSE}); or \code{NULL} in the |
||||
|
event an error occurred when processing the \code{zq} command line. |
||||
|
} |
||||
|
\description{ |
||||
|
zq is a command-line tool for processing logs. It applies boolean logic to |
||||
|
filter each log value, optionally computes analytics and transformations, |
||||
|
and returns results that can be consumed programmatically.\cr |
||||
|
\cr |
||||
|
This function takes command line arguments to be used with \code{zq} ( |
||||
|
in the form \code{\link[=system2]{system2()}} uses) and reads the results into a |
||||
|
data frame (it is actually a \code{data.table}).\cr |
||||
|
\cr |
||||
|
If the environment variable \code{ZQ_PATH} is not set or invalid, this function |
||||
|
will attempt to guess the \code{zq} binary path.\cr |
||||
|
\cr |
||||
|
Do not specify an output format as \verb{-f ndjson} is added by default. |
||||
|
} |
||||
|
\examples{ |
||||
|
zq_cmd( |
||||
|
c( |
||||
|
'"* | cut ts,id.orig_h,id.orig_p"', # note the quotes |
||||
|
system.file("logs", "conn.log.gz", package = "brimr") |
||||
|
) |
||||
|
) |
||||
|
} |
Loading…
Reference in new issue