Skip to content

Commit

Permalink
Can now return a data frame
Browse files Browse the repository at this point in the history
  • Loading branch information
alq666 committed May 3, 2015
1 parent cdf651c commit 74c3825
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
13 changes: 7 additions & 6 deletions R/rdog.R
Expand Up @@ -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)
Expand All @@ -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))
}
Expand Down
12 changes: 6 additions & 6 deletions man/dogq.Rd
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion 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}
Expand Down

0 comments on commit 74c3825

Please sign in to comment.