R/get_activity.R

Defines functions get_activity

Documented in get_activity

#' Get Current Server Activity
#'
#' @inheritParams api_request
#'
#' @return A `list` with bandwidth + stream info and a session `tbl`.
#' @export
#' @importFrom purrr map
#' @importFrom purrr discard
#' @importFrom plyr rbind.fill
#' @importFrom tibble as_tibble
#' @importFrom tibble tibble
#' @examples
#' \dontrun{
#' get_activity()
#' }
get_activity <- function(url = NULL, apikey = NULL) {

  result <- api_request(url = url, apikey = apikey, cmd = "get_activity")

  if (result$result != "success") {
    warning("Error in 'get_activity': ", result$result)
    return(data.frame())
  }

  info <- result$data[names(result$data) != "sessions"]
  info <- map(info, as.numeric)
  bandwidth <- info[grepl(pattern = "bandwidth", names(info))]
  streams <- info[grepl(pattern = "stream", names(info))]
  sessions <- result$data$sessions

  if (!identical(sessions, list())) {
    sessions <- sessions %>%
      map(discard, is.list) %>%
      map(discard, is.null) %>%
      map(as_tibble) %>%
      plyr::rbind.fill()
  } else {
    sessions <- tibble()
  }

  # Return things compactly
  list(
    streams = streams,
    bandwidth = bandwidth,
    sessions = sessions
  )
}
jemus42/tauturri documentation built on Feb. 7, 2024, 2 a.m.