How can I get the names associated with the user guids in Rstudio connect api?

353 views Asked by At

How can I get the names associated with the user guids

> ## ACTION REQUIRED: Change the server URL below to your server's URL
> Sys.setenv("CONNECT_SERVER"  = "https://rstudio.xx.com/") 
> ## ACTION REQUIRED: Make sure to have your API key ready
> Sys.setenv("CONNECT_API_KEY" = rstudioapi::askForPassword("Enter Connect Token:")) 

> library(ggplot2)
> library(dplyr)
> library(connectapi)
> library(purrr)
> client <- connect()
Defining Connect with server: https://rstudio.hartvillegroup.com/
> # Get and clean the Shiny usage data
> shiny_rsc <- get_usage_shiny(
+   client,
+   from = lubridate::today() - lubridate::ddays(60), 
+   limit = Inf
+ ) %>%
+   filter(!is.na(ended)) %>%
+   mutate(session_duration = ended - started)
> 
> glimpse(shiny_rsc)
Observations: 129
Variables: 6
$ content_guid     <chr> "a261edd3-fa51-4878-8afc-b7fe662a6c37", "a261edd3-fa51-4878...
$ user_guid        <chr> "e96068bd-5e9f-4a5a-ab87-483f084f13fa", NA, NA, NA, NA, NA,...
$ started          <dttm> 2021-07-06 14:39:24, 2021-07-06 15:08:47, 2021-07-06 21:40...
$ ended            <dttm> 2021-07-06 14:49:17, 2021-07-06 16:15:44, 2021-07-06 22:20...
$ data_version     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
$ session_duration <drtn> 593 secs, 4017 secs, 2424 secs, 4329 secs, 2642 secs, 3286.

As you see the user_guid are identifiers. how do I link them with actual user names? or is that always encrypted.

1

There are 1 answers

5
r2evans On

It's not giving you and encrypted username, it's the GUID/UUID key from the user info table in RStudio's instrumentation API.

Using connectapi:

library(connectapi)
client <- connect("https://...", api_key = apikey)
get_users(client)

where the last field is "guid" which should match the user_guid in your question. Note that you may need to extend limit=... to get all users. The output is the same as the httr-direct API call, below.


If you (or somebody) is choosing to not use the connectapi package (it's not on CRAN yet, for instance), then the below function along with the httr package (and data.table for my personal use, but that dependency can be removed) will get you exactly the same data. (For reference, see https://docs.rstudio.com/connect/api/#get-/v1/users .)

library(data.table)
getpages <- function(url, page_number = 1, page_size = 25, page_limit = Inf, apikey = "") {
  needqm <- !grepl("\\?", url)
  firstpass <- TRUE
  results <- list()
  res0 <- NULL
  while (page_number <= page_limit &&
           (firstpass || (length(res0) > 0 && nrow(res0) > 0))) {
    firstpass <- FALSE
    url0 <- sprintf("%s%spage_number=%d&page_size=%d",
                    url, if (needqm) "?" else "&", page_number, page_size)
    res <- httr::GET(url0, httr::add_headers(Authorization = paste("Key", apikey)))
    if (isTRUE(res$status_code == 200L)) {
      res0 <- suppressWarnings(rbindlist(httr::content(res)$results, use.names = TRUE, fill = TRUE))
      results <- c(results, list(res0))
    } else {
      warning("returned status_code of: ", res$status_code)
      break
    }
    page_number <- page_number + 1L
  }
  out <- rbindlist(results, use.names = TRUE, fill = TRUE)
  out[]
}

userinfo <- getpages("https://rsc.mydomain.com/__api__/v1/users", apikey = "....")
head(userinfo)
#                  email username first_name last_name user_role         created_time         updated_time               active_time confirmed locked                                 guid
#                 <char>   <char>     <char>    <char>    <char>               <char>               <char>                    <char>    <lgcl> <lgcl>                               <char>
# 1: [email protected]    user1     first1     last1    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE 33fdbc12-167f-42e6-b119-816fb46e7de1
# 2: [email protected]    user2     first2     last2    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE becd98ce-472e-4c62-a7c7-63f856ea72d3
# 3: [email protected]    user3     first3     last3    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE 9cc751bb-351d-4613-9b39-af35547a971b
# 4: [email protected]    user4     first4     last4    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE 10276566-6922-4c4b-83b1-30dbe4b8f136
# 5: [email protected]    user5     first5     last5    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE 81c2b393-b862-4615-bbcd-3c2e1d2c990b
# 6: [email protected]    user6     first6     last6    viewer 2019-04-12T23:08:39Z 2019-06-11T09:28:41Z 2019-06-15T08:05:17-04:00      TRUE  FALSE 7803fa60-9906-49f3-a26b-157d15a33ddb