Connect API activity logs

Hi,

Has anyone used the API to access log info in a meaningful way?
Below is our initial attempt to collect the logs and also the Shiny usage info with a user appended. It would be helpful to have the app names available too. I think the logs have the app id and the shiny stuff has the content_guid. Any chance of getting decodes for both added to the API in future releases?

if(!success) {install.packages("pacman")
  require(pacman)
}

pacman::p_load(jsonlite, httr, curl, dplyr, data.table)

connectServer <- ""
apiKey <- ""
# set up environment
# Note that the connectServer string must have a trailing slash

# get audit logs
authHeader <- add_headers(Authorization = paste("Key", apiKey))
resp <- GET(
  paste0(connectServer, "__api__/v1/audit_logs?ascOrder=true&limit=500"),
  authHeader
)
payload <- content(resp)
# print first 25!
# print(payload$result)
# now step through the remaining audit logs

log <- rbindlist(payload$result)

while(!is.null(payload$paging[["next"]])) {
  resp <- GET(payload$paging[["next"]], authHeader)
  payload <- content(resp)
  # print the next 25
  # print(payload$result)
  
  log <- rbind(log, rbindlist(payload$result))
 
}

# Get Shiny App Usage
authHeader <- add_headers(Authorization = paste("Key", apiKey))
resp <- GET("localhost:3939/__api__/v1/instrumentation/shiny/usage?ascOrder=true&limit=500",
            authHeader)

payload <- content(resp)

shiny_usage <- rbindlist(payload$result)

while(!is.null(payload$paging[["next"]])) {
  resp <- GET(payload$paging[["next"]], authHeader)
  payload <- content(resp)
  # print the next 25
  # print(payload$result)
  shiny_usage <- rbind(shiny_usage, rbindlist(payload$result))
}

# Get Users
users <- GET("localhost:3939/__api__/v1/users?page_size=500", 
             add_headers(Authorization = paste("Key", apiKey)))

user_info <- fromJSON(content(users, "text"))

user_info <- data.frame(user_info$results)

user_info

# Join Shiny Usage and Users
shiny_usage <- left_join(shiny_usage, user_info[c("username","guid")], by = c("user_guid"="guid"))

@jvail - great question! In the upcoming 1.7.0 release (planned for later this week), we've made public an API endpoint for decoding the app information. We've also added information for R Markdown and static content.

Example code is available here: https://github.com/sol-eng/connect-usage

Subscribe to the RStudio blog to get the release announcement with further information and upgrade instructions, and please let us know if the new API endpoints meet your needs :slight_smile:

2 Likes

Great! This looks very promising. Looking forward to the new release.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.