R/stash_outgoing.R

Defines functions stash_outgoing

Documented in stash_outgoing

#' Download from S3 and push to Google Drive
#'
#' @param prefix file prefix - "HFR_Tableau","HFR_Submission", "Mechanism",
#'  "Detailed"
#' @param outputfolder folder path to store file
#' @param gdrive whether to upload to Google Drive, default = FALSE
#'
#' @export
#'
#' @examples
#' \dontrun{
#' stash_outgoing("HFR_Tableau", "out/joint") }
stash_outgoing <- function(prefix = c("HFR_Tableau","HFR_Submission", "Mechanism", "Detailed"),
                           outputfolder,
                           gdrive = FALSE){

  objs <- grabr::s3_objects(
    bucket = "gov-usaid",
    prefix = glue::glue("ddc/uat/processed/hfr/outgoing/{prefix}")
  )

  latest_file <- objs %>%
    dplyr::filter(last_modified == max(last_modified)) %>%
    dplyr::pull(key)

  print(basename(latest_file))

  savename <- ifelse(prefix == "HFR_Tableau", "HFR_Tableau_SQLview.csv", basename(latest_file) %>% stringr::str_remove("_[:digit:].*(?=\\.csv)"))

  grabr::s3_download(
    bucket = "gov-usaid",
    object = latest_file,
    filepath = file.path(outputfolder, savename)
  )

  tab <- dplyr::case_when(prefix == "HFR_Submission" ~ "Submission_Status",
                          prefix == "Mechanism" ~ "Mechanism_Detailed_Error_Output",
                          prefix == "Detailed" ~ "Detailed_Error_Output")

  if(!is.na(tab) && googlesheets4::gs4_has_token() && gdrive == TRUE) {
    file.path(outputfolder, basename(latest_file)) %>%
      readr::read_csv(col_type = c(.default =  "c")) %>%
      googlesheets4::sheet_write(googlesheets4::as_sheets_id(Wavelength::hfr_gs_statrep), tab)
    usethis::ui_info(glue::glue("{basename(latest_file)} uploaded to Google Drive - {usethis::ui_field(hfr_gs_statrep)}"))
  } else if (googlesheets4::gs4_has_token()==FALSE){
    usethis::ui_warn(glue::glue("No credential loaded, not uploaded to Google Drive"))
  } else {
    usethis::ui_done("STATUS - Completed")
  }
}
USAID-OHA-SI/Wavelength documentation built on March 24, 2023, 10:07 a.m.