From 74c3825a39bb3d86f1829bdc4b6a28acad99a3ed Mon Sep 17 00:00:00 2001 From: Alexis Le-Quoc Date: Sat, 2 May 2015 23:34:33 -0400 Subject: [PATCH] Can now return a data frame --- R/rdog.R | 13 +++++++------ man/dogq.Rd | 12 ++++++------ man/freq.Rd | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/R/rdog.R b/R/rdog.R index 1defa18..106da94 100644 --- a/R/rdog.R +++ b/R/rdog.R @@ -9,16 +9,16 @@ values <- function(l) { #' Compute the frequency based on the interval freq <- function(i) { if (i <= 60) { - return(60/i) + return(60) } else if (60 < i && i <= 3600) { - return(3600/i) + return(24) } else { - return(86400/i) + return(7) } } #' @export -dogq <- function(api_key, application_key, query, from_t, to_t, to_ts = FALSE) { +dogq <- function(api_key, application_key, query, from_t, to_t, as_df = FALSE) { res <- RCurl::getForm("https://app.datadoghq.com/api/v1/query", api_key = api_key, application_key = application_key, from = from_t, to = to_t, query = query) parsed <- jsonlite::fromJSON(res) @@ -39,13 +39,14 @@ dogq <- function(api_key, application_key, query, from_t, to_t, to_ts = FALSE) { # Extract timestamps from the first list They will be identical across all groups timestamps <- to_epoch(pointlist[[1]][, 1]) + print(class(timestamps)) # Collect all series values (accessible as [, 2]) v <- mapply(values, pointlist) # build the time series - if (to_ts) { - df <- ts(v, frequency = freq(interval)) + if (as_df) { + df <- data.frame(timestamps, v) } else { df <- xts::xts(v, order.by = timestamps, frequency = freq(interval)) } diff --git a/man/dogq.Rd b/man/dogq.Rd index c556b14..82b4d6f 100644 --- a/man/dogq.Rd +++ b/man/dogq.Rd @@ -8,7 +8,7 @@ dogq Query metrics via the Datadog API and turn them into time series } \usage{ -dogq(api_key, application_key, query, from_t, to_t, to_ts) +dogq(api_key, application_key, query, from_t, to_t, as_df) } %- maybe also 'usage' for other objects documented here. \arguments{ @@ -27,8 +27,8 @@ dogq(api_key, application_key, query, from_t, to_t, to_ts) \item{to_t}{ The end of the time window for the query, also expressed a POSIX timestamp } - \item{to_ts}{ - Decision to return xts or ts + \item{as_df}{ + Decision to return an xts time series or a data frame } } \details{ @@ -49,7 +49,7 @@ Alexis Le-Quoc ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as -function(api_key, application_key, query, from_t, to_t, to_ts=FALSE) +function(api_key, application_key, query, from_t, to_t, as_df=FALSE) { res <- getForm('https://app.datadoghq.com/api/v1/query', api_key=api_key, application_key=application_key, from=from_t, to=to_t, query=query) parsed <- fromJSON(res) @@ -66,8 +66,8 @@ function(api_key, application_key, query, from_t, to_t, to_ts=FALSE) interval <- min(timeseries$interval) timestamps <- to_epoch(pointlist[[1]][, 1]) v <- mapply(values, pointlist) - if (to_ts) { - df <- ts(v, frequency=60/interval) + if (as_df) { + df <- data.frame(timestamps, v) } else { df <- xts(v, order.by=timestamps, frequency=60/interval) } diff --git a/man/freq.Rd b/man/freq.Rd index 30c694c..b583620 100644 --- a/man/freq.Rd +++ b/man/freq.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/rdog.R \name{freq} \alias{freq}