Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@ export(download_single_folder_asset_fr_df)
export(download_single_session_asset_fr_df)
export(download_video)
export(download_volume_zip)
export(get_category_by_id)
export(get_db_stats)
export(get_file_duration)
export(get_folder_by_id)
export(get_folder_file)
export(get_funder_by_id)
export(get_institution_avatar)
export(get_institution_by_id)
export(get_permission_levels)
export(get_release_levels)
export(get_session_by_id)
export(get_session_by_name)
export(get_session_file)
export(get_supported_file_types)
export(get_tag_by_id)
export(get_user_avatar)
export(get_user_by_id)
export(get_volume_by_id)
export(get_volume_collaborator_by_id)
export(get_volume_record_by_id)
export(list_asset_formats)
export(list_authorized_investigators)
export(list_categories)
export(list_folder_assets)
export(list_institution_affiliates)
export(list_institutions)
export(list_session_activity)
export(list_session_assets)
export(list_user_affiliates)
Expand All @@ -44,6 +54,7 @@ export(list_volume_folders)
export(list_volume_funding)
export(list_volume_info)
export(list_volume_links)
export(list_volume_records)
export(list_volume_session_assets)
export(list_volume_sessions)
export(list_volume_tags)
Expand Down
9 changes: 9 additions & 0 deletions R/CONSTANTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ API_USER_SPONSORSHIPS <- "/users/%s/sponsorships/"
API_USER_AFFILIATES <- "/users/%s/affiliates/"
API_USER_AVATAR <- "/users/%s/avatar/"
API_USERS_HISTORY <- "/users/%s/history/"
API_INSTITUTIONS_LIST <- "/institutions/"
API_INSTITUTIONS <- "/institutions/%s/"
API_INSTITUTION_AFFILIATES <- "/institutions/%s/affiliates/"
API_INSTITUTION_AVATAR <- "/institutions/%s/avatar/"
Expand All @@ -21,9 +22,12 @@ API_VOLUME_TAGS <- "/volumes/%s/tags/"
API_VOLUME_LINKS <- "/volumes/%s/links/"
API_VOLUME_FUNDINGS <- "/volumes/%s/fundings/"
API_VOLUME_COLLABORATORS <- "/volumes/%s/collaborators/"
API_VOLUME_COLLABORATOR_DETAIL <- "/volumes/%s/collaborators/%s/"
API_VOLUME_HISTORY <- "/volumes/%s/history/"
API_VOLUME_SESSIONS <- "/volumes/%s/sessions/"
API_VOLUME_FOLDERS <- "/volumes/%s/folders/"
API_VOLUME_RECORDS <- "/volumes/%s/records/"
API_VOLUME_RECORD_DETAIL <- "/volumes/%s/records/%s/"
API_SESSION_DETAIL <- "/volumes/%s/sessions/%s/"
API_SESSION_FILES <- "/volumes/%s/sessions/%s/files/"
API_SESSION_FILE_DETAIL <- "/volumes/%s/sessions/%s/files/%s/"
Expand All @@ -32,6 +36,7 @@ API_SESSION_DOWNLOAD_LINK <- "/volumes/%s/sessions/%s/download-link/"
API_SESSION_CSV_DOWNLOAD_LINK <- "/volumes/%s/sessions/%s/csv-download-link/"
API_FOLDER_DETAIL <- "/volumes/%s/folders/%s/"
API_FOLDER_FILES <- "/volumes/%s/folders/%s/files/"
API_FOLDER_FILES_DETAIL <- "/volumes/%s/folders/%s/files/%s/"
API_FOLDER_DOWNLOAD_LINK <- "/volumes/%s/folders/%s/download-link/"
API_FOLDER_FILE_DOWNLOAD_LINK <- "/volumes/%s/folders/%s/files/%s/download-link/"
API_VOLUME_DOWNLOAD_LINK <- "/volumes/%s/download-link/"
Expand All @@ -40,6 +45,10 @@ API_SEARCH_VOLUMES <- "/search/volumes/"
API_SEARCH_USERS <- "/search/users/"
API_SEARCH_INSTITUTIONS <- "/search/institutions/"
API_FUNDERS <- "/funders/"
API_FUNDER_DETAIL <- "/funders/%s/"
API_TAG_DETAIL <- "/tags/%s/"
API_CATEGORIES <- "/categories/"
API_CATEGORY_DETAIL <- "/categories/%s/"

RETRY_LIMIT <- 3
RETRY_WAIT_TIME <- 1 # seconds
Expand Down
90 changes: 90 additions & 0 deletions R/get_category_by_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' @eval options::as_params()
#' @name options_params
#'
NULL

#' Get Category Information By ID
#'
#' @description Retrieve detailed information about a specific category from
#' Databrary using its unique identifier. Categories include nested metrics
#' that define data collection fields.
#'
#' @param category_id Numeric category identifier. Must be a positive integer.
#' @param rq An `httr2` request object. Defaults to `NULL`.
#'
#' @return A list with the category's metadata including id, name, description,
#' and nested metrics, or `NULL` if the category is not found or inaccessible.
#'
#' @inheritParams options_params
#'
#' @examples
#' \donttest{
#' \dontrun{
#' # Get details for a specific category
#' get_category_by_id(category_id = 1)
#'
#' # Get category information with verbose output
#' get_category_by_id(category_id = 1, vb = TRUE)
#' }
#' }
#' @export
get_category_by_id <- function(
category_id = 1,
vb = options::opt("vb"),
rq = NULL
) {
# Validate category_id
assertthat::assert_that(is.numeric(category_id))
assertthat::assert_that(length(category_id) == 1)
assertthat::assert_that(category_id > 0)
assertthat::assert_that(
category_id == floor(category_id),
msg = "category_id must be an integer"
)

# Validate vb
assertthat::assert_that(length(vb) == 1)
assertthat::assert_that(is.logical(vb))

# Validate rq
assertthat::assert_that(is.null(rq) || inherits(rq, "httr2_request"))

# Perform API call
category <- perform_api_get(
path = sprintf(API_CATEGORY_DETAIL, category_id),
rq = rq,
vb = vb
)

if (is.null(category)) {
if (vb) {
message("Category ", category_id, " not found or inaccessible.")
}
return(NULL)
}

# Process metrics if present
metrics <- NULL
if (!is.null(category$metrics) && length(category$metrics) > 0) {
metrics <- lapply(category$metrics, function(metric) {
list(
metric_id = metric$id,
metric_name = metric$name,
metric_type = metric$type,
metric_release = metric$release,
metric_options = metric$options,
metric_assumed = metric$assumed,
metric_description = metric$description,
metric_required = metric$required
)
})
}

# Return structured list
list(
category_id = category$id,
category_name = category$name,
category_description = category$description,
metrics = metrics
)
}
112 changes: 80 additions & 32 deletions R/get_db_stats.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @eval options::as_params()
#' @name options_params
#'
#'
NULL

#' Get Stats About Databrary.
Expand All @@ -11,11 +11,11 @@ NULL
#' @param type Type of Databrary report to run "institutions", "people", "data"
#' @param rq An `httr2` request object.
#'
#' @returns A data frame with the requested data or NULL if there is
#' @returns A data frame with the requested data or NULL if there is
#' no new information.
#'
#' @inheritParams options_params
#'
#'
#' @examples
#' \donttest{
#' get_db_stats()
Expand All @@ -24,33 +24,34 @@ NULL
#' get_db_stats("places") # Information about the newest institutions.
#' }
#' @export
get_db_stats <- function(type = "stats",
vb = options::opt("vb"),
rq = NULL) {
get_db_stats <- function(type = "stats", vb = options::opt("vb"), rq = NULL) {
# Check parameters
assertthat::assert_that(length(type) == 1)
assertthat::assert_that(is.character(type))
assertthat::assert_that(
type %in% c(
"institutions",
"places",
"people",
"researchers",
"investigators",
"datasets",
"data",
"volumes",
"stats",
"numbers"
)
type %in%
c(
"institutions",
"places",
"people",
"researchers",
"investigators",
"datasets",
"data",
"volumes",
"stats",
"numbers"
)
)

assertthat::assert_that(length(vb) == 1)
assertthat::assert_that(is.logical(vb))

assertthat::assert_that(is.null(rq) |
("httr2_request" %in% class(rq)))


assertthat::assert_that(
is.null(rq) |
("httr2_request" %in% class(rq))
)

if (is.null(rq)) {
if (vb) {
message("\nNULL request object. Will generate default.")
Expand All @@ -63,23 +64,70 @@ get_db_stats <- function(type = "stats",
rq = rq,
vb = vb
)

if (is.null(stats)) {
message("Cannot access requested resource on Databrary. Exiting.")
return(NULL)
}

if (type %in% c("stats", "numbers")) {
# Map new API field names to output
tibble::tibble(
date = Sys.time(),
investors = stats$authorized_users,
datasets_total = stats$total_volumes,
datasets_shared = stats$public_volumes,
n_files = stats$total_files,
hours = stats$total_duration_hours,
TB = stats$total_storage_tb
institutions = if (!is.null(stats$institutions)) {
stats$institutions
} else {
NA_integer_
},
affiliates = if (!is.null(stats$affiliates)) {
stats$affiliates
} else {
NA_integer_
},
investigators = if (!is.null(stats$investigators)) {
stats$investigators
} else {
NA_integer_
},
hours_of_recordings = if (!is.null(stats$hours_of_recordings)) {
stats$hours_of_recordings
} else {
NA_integer_
},
# Legacy fields (may not be present in new API)
authorized_users = if (!is.null(stats$authorized_users)) {
stats$authorized_users
} else {
NA_integer_
},
total_volumes = if (!is.null(stats$total_volumes)) {
stats$total_volumes
} else {
NA_integer_
},
public_volumes = if (!is.null(stats$public_volumes)) {
stats$public_volumes
} else {
NA_integer_
},
total_files = if (!is.null(stats$total_files)) {
stats$total_files
} else {
NA_integer_
},
total_duration_hours = if (!is.null(stats$total_duration_hours)) {
stats$total_duration_hours
} else {
NA_real_
},
total_storage_tb = if (!is.null(stats$total_storage_tb)) {
stats$total_storage_tb
} else {
NA_real_
}
)
} else {
tibble::as_tibble(stats$recent_activity)
# For other types, return the raw stats as a tibble
tibble::as_tibble(stats)
}
}
Loading