R/keen-query.R

#' Query Keen core-events-production
#'
#' Lets users query the core-events-production database from within R
#'
#' `keen_query` returns a data.frame with the results of a Keen query. Currently requests that generate over 10,000 records will fail.
#'
#' @param analysis_type A character string. The type of analysis you want to do in Keen.
#' @param event_collection A character string. Which Event Collection database you want to look in.
#' @param timeframe An optional character string. The timeframe you want records from.
#' @param groupby An optional character string. Variable you want output grouped by (multiple grouping variables not yet supported).
#' @param filters An option list of lists. Additional filters you want added to your query.
#' @param keep_lists Do you want the returned dataframe to include columns of type list?
#' @export
#' @import jsonlite
#' @import httr

keen_query <- function(analysis_type, event_collection, timeframe = NULL, groupby = NULL, filters = NULL, keep_lists = TRUE){
  requireNamespace("httr")
  requireNamespace("jsonlite")

  read_key        <- Sys.getenv("keen_read_key")
  project_id      <- Sys.getenv("keen_project_id")
  url             <- "https://api.keen.io"
  path            <- paste0("3.0/projects/", project_id, "/queries/", analysis_type)


  body <- if(is.na(filters) & !is.null(timeframe) & !is.null(groupby)) {list(timeframe = timeframe, group_by = groupby)} else
    if(is.na(filters) & is.null(timeframe) & !is.null(groupby)) {list(group_by = groupby)} else
      if(is.na(filters) & is.null(timeframe) & is.null(groupby)) {list()} else
        if(!is.na(filters) & !is.null(timeframe) & !is.null(groupby)) {list(timeframe = timeframe, groupby = groupby, filters = filters)} else
          if(!is.na(filters) & !is.null(timeframe) & is.null(groupby)) {list(timeframe = timeframe, filters = filters)} else
            if(!is.na(filters) & is.null(timeframe) & is.null(groupby)) {list(filters = filters)} else
            if(!is.na(filters) & is.null(timeframe) & !is.null(groupby)) {list(group_by = groupby, filters = filters)} else
              if(is.na(filters) & !is.null(timeframe) & is.null(groupby)) {list(timeframe = timeframe)}
  body <- append(body, list(event_collection = event_collection))


  response        <- POST(url = url,
                          path = path,
                          add_headers(
                            'Content-Type' = 'application/json',
                            Authorization = read_key),
                          body = body,
                          encode = "json"
  )


  df <- httr::content(response, as = "text", encoding = "UTF-8") %>%
    jsonlite::fromJSON(flatten = TRUE)

  df <- df$result

  if(keep_lists == TRUE){return(df)} else {return(purrr::discard(df, is.list))}
}

keen_query(analysis_type = "count", event_collection = "webtext_interactions",
           filters = list(
             list(
               property_name = "enrollment.section.course.academic_term.id",
               operator = "eq",
               property_value = 778),
             list(
               property_name = "enrollment.section.course.institutional_course.id",
               operator = "eq",
               property_value = 248
             )
                     ))
plduffy/rsoomo documentation built on May 26, 2019, 12:32 a.m.