inst/hbcdtlfb/www/R/functions/save_AWS.R

# Save TLFB data on AWS and notify.
save_aws <- function(
    data,
    data_table_routine,
    id,
    v,
    submit_comments = "",
    submit_rating = "",
    complete = FALSE,
    last_action = "add_substance",
    session) {
  
  # Notify user.
  show_toast(
    title = "In Progress...", 
    text = "",
    type = "info", 
    timer = 10000,
    position = "center", 
    width = "50%"
  )
  
  ifelse(complete,
         {
           # Upload _COMPLETE file.
           name_file <- glue("{id}_v{v}_COMPLETE.rds")
           session_data <- data |> 
             filter(what == "Substance")
           
           tlfb_data_session <- list(
             meta = "Format TLFB App without Events",
             shiny_session_start_time = shiny_session_start_time,
             session_user = session$user,
             last_save_time = Sys.time(),
             id = id,
             v = v,
             data = session_data,
             data_routine = data_table_routine,
             submit_comments = submit_comments,
             submit_rating = submit_rating,
             complete = complete
           )
           
           file <- file.path(tempdir(), name_file)
           saveRDS(tlfb_data_session, file = file)
           
           upload_success <- aws.s3::put_object(
             file = file, 
             object = name_file,
             bucket = "hbcd-tlfb", 
             base_url = "storjshare.io", 
             region = "gateway.us1"
           )
           
           # Remove _PARTIAL file.
           if(upload_success) {
             aws.s3::delete_object(
               file = file, 
               object = glue("{id}_v{v}_PARTIAL.rds"),
               bucket = "hbcd-tlfb", 
               base_url = "storjshare.io", 
               region = "gateway.us1"
             )
           }
           
           # Create a _DATA file that contains data in an easy to use format.
           name_file <- glue("{id}_v{v}_DATA.rds")
           session_data <- data |> 
             filter(what == "Substance") |> 
             transmute(day = start, substance, frequency = as.numeric(frequency)) |> 
             group_by(day, substance) |> 
             summarise(total_frequency = sum(frequency),
                       .groups = "drop")
           
           tlfb_data_session <- list(
             meta = "Format TLFB Data without Events",
             shiny_session_start_time = shiny_session_start_time,
             session_user = session$user,
             last_save_time = Sys.time(),
             id = id,
             v = v,
             data = session_data,
             submit_comments = submit_comments,
             submit_rating = submit_rating,
             complete = complete
           )
           
           file <- file.path(tempdir(), name_file)
           saveRDS(tlfb_data_session, file = file)
           
           upload_data <- aws.s3::put_object(
             file = file, 
             object = name_file,
             bucket = "hbcd-tlfb", 
             base_url = "storjshare.io", 
             region = "gateway.us1"
           )
         },
         # Upload _PARTIAL file.
         {
           name_file <- glue("{id}_v{v}_PARTIAL.rds")
           session_data <- data |> 
             filter(what == "Substance")
           
           tlfb_data_session <- list(
             meta = "Format TLFB Data without Events",
             shiny_session_start_time = shiny_session_start_time,
             session_user = session$user,
             last_save_time = Sys.time(),
             id = id,
             v = v,
             data = session_data,
             data_routine = data_table_routine,
             submit_comments = submit_comments,
             submit_rating = submit_rating,
             complete = complete
           )
           
           file <- file.path(tempdir(), name_file)
           saveRDS(tlfb_data_session, file = file)
           
           upload_success <- aws.s3::put_object(
             file = file, 
             object = name_file,
             bucket = "hbcd-tlfb", 
             base_url = "storjshare.io", 
             region = "gateway.us1"
           )
         }
  )
  
  # Notify user.
  if(last_action == "add_substance") {
    if(upload_success) show_toast(
      title = "Substance added",
      text = "",
      type = "success",
      position = "center", 
      width = "50%"
    )
    if(!upload_success) show_toast(
      title = "Couldn't connect to server", 
      text = "Please report to data team.",
      type = "error", 
      position = "center", 
      width = "50%", 
      timer = 10000
    )
  }
  
  if(last_action == "add_event") {
    if(upload_success) show_toast(
      title = "Event added", 
      text = "",
      type = "success", 
      position = "center", 
      width = "50%"
    )
    if(!upload_success) show_toast(
      title = "Couldn't connect to server", 
      text = "Please report to data team.",
      type = "error", 
      position = "center", 
      width = "50%", 
      timer = 10000
    )
  }
  
  if(last_action == "delete") {
    if(upload_success) show_toast(
      title = "Substance(s) removed", 
      text = "",
      type = "success", 
      position = "center", 
      width = "50%"
    )
    if(!upload_success) show_toast(
      title = "Couldn't connect to server", 
      text = "Please report to data team.",
      type = "error", 
      position = "center", 
      width = "50%", 
      timer = 10000
    )
  }
  
  if(last_action == "submit") {
    if(upload_success) {
      show_toast(
        title = "Submitted. You will be disconnected from the app.", 
        text = "",
        type = "success", 
        position = "center",
        width = "50%"
      )
      Sys.sleep(3)
      stopApp("Data submitted.")
    }
    
    if(!upload_success) 
      show_toast(
        title = "Couldn't connect to server", 
        text = "Please report to data team.",
        type = "error", 
        position = "center", 
        width = "50%", 
        timer = 10000
      )
  }
  
  if(last_action == "edit_substance") {
    if(upload_success) {
      show_toast(
        title = "Record edited", 
        text = "",
        type = "success", 
        position = "center",
        width = "50%"
      )
    }
    
    if(!upload_success) 
      show_toast(
        title = "Couldn't connect to server", 
        text = "Please report to data team.",
        type = "error", 
        position = "center", 
        width = "50%", 
        timer = 10000
      )
  }
  
  if(last_action == "delete_substance") {
    if(upload_success) {
      show_toast(
        title = "Record deleted",
        text = "",
        type = "success", 
        position = "center",
        width = "50%"
      )
    }
    
    if(!upload_success) 
      show_toast(
        title = "Couldn't connect to server", 
        text = "Please report to data team.",
        type = "error", 
        position = "center", 
        width = "50%", 
        timer = 10000
      )
  }
}
ucsd-dsm/hbcd-tlfb documentation built on July 10, 2022, 5:46 p.m.