From c1f56c61e82e504ef662bb10bea725f7c287a8be Mon Sep 17 00:00:00 2001 From: junos Date: Wed, 24 Nov 2021 18:33:06 +0100 Subject: [PATCH] Add a function to pull start and end datetimes. --- src/data/streams/aware_postgresql/container.R | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/data/streams/aware_postgresql/container.R b/src/data/streams/aware_postgresql/container.R index 30e8e5ac..8f4c6a06 100644 --- a/src/data/streams/aware_postgresql/container.R +++ b/src/data/streams/aware_postgresql/container.R @@ -134,7 +134,7 @@ pull_participants_ids <- function(stream_parameters, usernames, participants_con } #' @description -#' Gets participants' IDs for specified usernames. +#' Gets participants' IDs for specified participant IDs #' #' @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 @@ -142,6 +142,7 @@ pull_participants_ids <- function(stream_parameters, usernames, participants_con #' @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) { + dbEngine <- get_db_engine(stream_parameters$DATABASE_GROUP) query_device_id <- tbl(dbEngine, device_id_container) %>% filter(participant_id %in% !!participants_ids) %>% @@ -160,4 +161,46 @@ pull_participants_device_ids <- function(stream_parameters, participants_ids, de return(device_ids) } +#' @description +#' Gets start and end datetimes for specified participant IDs. +#' +#' @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 start_end_date_container The name of the database table which will be used to determine when a participant started and ended their participation. Briefing and debriefing EMAs can be meaningfully used here. +#' @return A dataframe relating participant IDs with their start and end datetimes. + +pull_participants_start_end_dates <- function(stream_parameters, participants_ids, start_end_date_container) { + dbEngine <- get_db_engine(stream_parameters$DATABASE_GROUP) + + query_timestamps <- tbl(dbEngine, start_end_date_container) %>% + filter( + participant_id %in% !!participant_data$id, + double_esm_user_answer_timestamp > 0 + ) %>% + group_by(participant_id) %>% + summarise( + timestamp_min = min(double_esm_user_answer_timestamp, na.rm = TRUE), + timestamp_max = max(double_esm_user_answer_timestamp, na.rm = TRUE) + ) %>% + select(participant_id, timestamp_min, timestamp_max) + + message(paste0("Executing the following query to get the starting and ending datetimes: \n", sql_render(query_timestamps))) + + start_end_timestamps <- query_timestamps %>% collect() + + if(nrow(start_end_timestamps) == 0) + warning(paste("We could not find datetimes for requested participant IDs (", participants_ids, ") in ", device_id_container)) + + start_end_times <- start_end_timestamps %>% + mutate( + datetime_start = as_datetime(timestamp_min/1000, tz = "UTC"), + datetime_end = as_datetime(timestamp_max/1000, tz = "UTC") + ) %>% + select(-c(timestamp_min, timestamp_max)) + + dbDisconnect(dbEngine) + + return(start_end_times) +} +