Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Last active February 15, 2022 20:53
Show Gist options
  • Save dantonnoriega/7c4ee7e9e37b72a52452f9d851435def to your computer and use it in GitHub Desktop.
Save dantonnoriega/7c4ee7e9e37b72a52452f9d851435def to your computer and use it in GitHub Desktop.
a script for determining the number of active users in some RSC instance
# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- "https://some-rsc-url.com/"
# Save RSC API key to ~/.Renviron as RSC_API_KEY
# - https://docs.rstudio.com/connect/1.7.4/user/cookbook.html
connectAPIKey <- Sys.getenv("RSC_API_KEY")
# Request a page of up to 25 usage records.
resp <- httr::GET(
file.path(connectServer, "__api__/v1/instrumentation/shiny/usage?limit=100"),
httr::add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- httr::content(resp, encoding = 'UTF-8')
results <- payload$results
# Continue to page through additional records
# while we have a "next" reference
while(!is.null(payload$paging[["next"]])) {
resp <- httr::GET(
payload$paging[["next"]],
httr::add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- httr::content(resp, encoding = 'UTF-8')
results <- append(results, payload$results)
}
# as.data.table has better handling of NULL list elements
# - results in ERROR: as.data.frame(list(value = 3, endpoint = NULL))
# - works but drops NULL: data.table::as.data.table(list(value = 3, endpoint = NULL))
l = lapply(results, data.table::as.data.table)
d = data.table::rbindlist(l, T, T)
# get content details ---------------
# API Details
# - https://docs.rstudio.com/connect/1.7.4/api/#getContent
content_guids = d[, .N, by = content_guid][order(-N)]
content_guids[, paths := paste0("/v1/experimental/content/", content_guid)]
urls <- paste0(connectServer, "__api__", content_guids$paths)
rsc_get_content <- function(x, api_key) {
httr::content(
httr::GET(
x,
httr::add_headers(
Authorization = paste("Key", api_key))))
}
content_details <-
data.table::rbindlist(
lapply(
lapply(urls, rsc_get_content, api_key = connectAPIKey),
data.table::as.data.table
), use.names = T, fill = T)
# get user details ---------------
# API Details
# - https://docs.rstudio.com/connect/1.7.4/api/#getUser
user_guids = d[, .N, by = user_guid][order(-N)]
user_guids[, paths := paste0("/v1/users/", user_guid)]
urls <- paste0(connectServer, "__api__", user_guids$paths)
rsc_get_content <- function(x, api_key) {
httr::content(
httr::GET(
x,
httr::add_headers(
Authorization = paste("Key", api_key))))
}
user_details <-
data.table::rbindlist(
lapply(
lapply(urls, rsc_get_content, api_key = connectAPIKey),
data.table::as.data.table
), use.names = T, fill = T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment