diff --git a/src/data/streams/aware_postgresql/container.R b/src/data/streams/aware_postgresql/container.R index 2aa9c9bb..69ebbf66 100644 --- a/src/data/streams/aware_postgresql/container.R +++ b/src/data/streams/aware_postgresql/container.R @@ -29,6 +29,7 @@ library(RPostgres) # :1:10: fatal error: libpq-fe.h: No such file or directory # compilation terminated. +library(dbplyr) library(yaml) #' @description @@ -105,3 +106,30 @@ pull_data <- function(stream_parameters, device, sensor, sensor_container, colum return(sensor_data) } +#' @description +#' 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 usernames A vector of usernames +#' @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 + +pull_participants_ids <- function(stream_parameters, usernames, participants_container) { + dbEngine <- get_db_engine(stream_parameters$DATABASE_GROUP) + + query_participant_id <- tbl(dbEngine, participants_container) %>% + filter(username %in% usernames) %>% + select(username, id) + + message(paste0("Executing the following query to get the participant's id: \n", sql_render(query_participant_id))) + + participant_data <- query_participant_id %>% collect() + + dbDisconnect(dbEngine) + + if(nrow(participant_data) == 0) + warning(paste("We could not find requested usernames (", usernames, ") in ", participants_container)) + + return(participant_data) +} +