This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2021-10-22_haley_permission_query_speed.R
86 lines (63 loc) · 2.51 KB
/
2021-10-22_haley_permission_query_speed.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Description: test fastest way to query permissions on a Synapse entity.
# Author: Haley Hunter-Zinck
# Date: 2021-
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(synapser)
synLogin()
# synapse
# functions ----------------------------
#' Check whether a string represents a user name.
#'
#' @param personage_name user or other Synapse entity name
#' @return TRUE if name relates to a Synapse user; otherwise FALSE
#' @example is_user("my_user_name")
is_user <- function(personage_name) {
res <- tryCatch(length(synGetUserProfile(personage_name)),
error = function(cond) {return(0)})
return(as.logical(res))
}
#' Get the user Synapse ID number from the user's Synapse user name.
#'
#' @param user_name Synapse user name
#' @return Synapse user ID number
#' @example get_user_id("my_user_name")
get_user_id <- function(user_name) {
if (is_user(user_name)) {
return(synGetUserProfile(user_name)$ownerId)
}
return(NA)
}
get_permissions_synapser <- function(synapse_id, personage_name) {
return(synGetPermissions(synapse_id, personage_name))
}
get_permissions_rest <- function(synapse_id, personage_id) {
acl <- tryCatch({
synRestGET(glue("/entity/{synapse_id}/acl"))
}, error = function(cond) {
return(synRestGET(tail(strsplit(cond[[1]], split = " ")[[1]], 1)))
})
personage_ids <- unlist(lapply(acl$resourceAccess, function(x) {return(x$principalId)}))
return(acl$resourceAccess[[which(personage_ids == personage_id)]]$accessType)
}
# main ----------------------------
synapse_id <- "syn26342236"
personage_name = "hhz"
personage_id <- get_user_id(personage_name)
tic_synapser_name <- as.double(Sys.time())
res_synapser_name <- get_permissions_synapser(synapse_id, personage_name)
toc_synapser_name <- as.double(Sys.time())
tic_synapser_id <- as.double(Sys.time())
res_synapser_id <- get_permissions_synapser(synapse_id, personage_id)
toc_synapser_id <- as.double(Sys.time())
tic_rest <- as.double(Sys.time())
res_rest <- get_permissions_rest(synapse_id, personage_id)
toc_rest <- as.double(Sys.time())
# close out ----------------------------
print(glue("Identical results: {identical(sort(unlist(res_synapser_id)), sort(unlist(res_rest)))}"))
print(glue("Runtime (synapser, name): {toc_synapser_name - tic_synapser_name}"))
print(glue("Runtime (synapser, ID): {toc_synapser_id - tic_synapser_id}"))
print(glue("Runtime (REST API): {toc_rest - tic_rest}"))
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))