Add a function to pull device IDs.

labels
junos 2021-11-24 18:19:20 +01:00
parent 8b2717122d
commit 3acf6ece14
1 changed files with 30 additions and 2 deletions

View File

@ -109,10 +109,10 @@ pull_data <- function(stream_parameters, device, sensor, sensor_container, colum
#' @description #' @description
#' Gets participants' IDs for specified usernames. #' Gets participants' IDs for specified usernames.
#' #'
#' @param stream_parameters The PHONE_STREAM_PARAMETERS key in config.yaml. If you need specific parameters add them there. #' @param stream_parameters The PHONE_DATA_STREAMS key in config.yaml. If you need specific parameters add them there.
#' @param usernames A vector of usernames #' @param usernames A vector of usernames
#' @param participants_container The name of the database table containing participants data, such as their username. #' @param participants_container The name of the database table containing participants data, such as their username.
#' @return A dataframe with the sensor data for device #' @return A dataframe with participant IDs matching usernames
pull_participants_ids <- function(stream_parameters, usernames, participants_container) { pull_participants_ids <- function(stream_parameters, usernames, participants_container) {
dbEngine <- get_db_engine(stream_parameters$DATABASE_GROUP) dbEngine <- get_db_engine(stream_parameters$DATABASE_GROUP)
@ -133,3 +133,31 @@ pull_participants_ids <- function(stream_parameters, usernames, participants_con
return(participant_data) return(participant_data)
} }
#' @description
#' Gets participants' IDs for specified usernames.
#'
#' @param stream_parameters The PHONE_DATA_STREAMS key in config.yaml. If you need specific parameters add them there.
#' @param participants_ids A vector of numeric participant IDs
#' @param device_id_container The name of the database table which will be used to determine distinct device ID. Ideally, a table that reliably contains data, but not too much.
#' @return A dataframe with a row matching each distinct device ID with a participant ID
pull_participants_device_ids <- function(stream_parameters, participants_ids, device_id_container) {
query_device_id <- tbl(dbEngine, device_id_container) %>%
filter(participant_id %in% !!participants_ids) %>%
group_by(participant_id) %>%
distinct(device_id, .keep_all = FALSE)
message(paste0("Executing the following query to get the distinct device IDs: \n", sql_render(query_device_id)))
device_ids <- query_device_id %>% collect()
dbDisconnect(dbEngine)
if(nrow(device_ids) == 0)
warning(paste("We could not find device IDs for requested participant IDs (", participants_ids, ") in ", device_id_container))
return(device_ids)
}