From de9db2a0ad99dda2391e022926e69dd26f220d62 Mon Sep 17 00:00:00 2001 From: Anh Nguyet Vu <32753274+anngvu@users.noreply.github.com> Date: Wed, 12 Jul 2023 13:40:06 -0600 Subject: [PATCH] Use no-email-list (#68) * Add log output sink for dev * Add no-email check * Refactor into wrapper fun for easier testing * Add unit test * Update README * Ignore logs --- .gitignore | 1 + monitor_annotations/README.md | 9 ++++-- monitor_annotations/helpers.R | 36 +++++++++++++++++++++++ monitor_annotations/monitor_annotations.R | 31 ++++++++----------- monitor_annotations/tests/unit_tests.R | 18 ++++++++++++ 5 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 .gitignore create mode 100644 monitor_annotations/tests/unit_tests.R diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a988499 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.log diff --git a/monitor_annotations/README.md b/monitor_annotations/README.md index 546a297..8ba97cc 100644 --- a/monitor_annotations/README.md +++ b/monitor_annotations/README.md @@ -8,9 +8,9 @@ It does the following: - How each studyFileView is checked: - If no files, don't do anything - If files present, check for core required annotations (e.g. `assay`) -- Make list of the subset of files without required annotations +- Reference which folders have files without required annotations - Get user id(s) who uploaded the data -- Send email to user with list of files +- Send email to user with folders referenced ### Secrets and env vars @@ -42,6 +42,11 @@ DIGEST_SUBSCRIBERS=1111111;2222222 ### Testing Notes +#### With Docker image + - Build the image with e.g. `docker build -t ghcr.io/nfosi/jobs-monitor-annotations .` (or pull current/pre-build image if available) - Create an envfile `envfile-monitor-anno` as above and run `docker run --env-file envfile-monitor-anno ghcr.io/nf-osi/jobs-monitor-annotations` +#### Unit tests + +- See tests in `tests` folder \ No newline at end of file diff --git a/monitor_annotations/helpers.R b/monitor_annotations/helpers.R index 2c35ccb..d4d627d 100644 --- a/monitor_annotations/helpers.R +++ b/monitor_annotations/helpers.R @@ -229,4 +229,40 @@ make_active_study_reminder_list <- function(study_tab_id, fileview_tab_id) { return(reminder_list_by_study) } +#' Send reminder messages +#' +#' @param todo The "todo" list, which is generated by `make_active_study_reminder_list` +#' @param dcc_user DCC account +#' @param test_user Test user account, which may be different from DCC user. Only used in test mode. +#' @param no_email_list_users List of users to skip emails +#' @param sleep_interval Number of seconds to wait between emails (throttling). +send_message_list <- function(todo, + dcc_user = NULL, + test_user = NULL, + no_email_list_users = NULL, + dry_run = TRUE, + sleep_interval = 6) { + for(project in names(todo)) { + for(user in names(todo[[project]][["naf"]]) ) { + # Check user against no-email-list + if(user %in% no_email_list_users) { + + message("Skipping reminder for: ", user) + + } else { + + email_re_annotation(recipient = user, + list = todo[[project]][["naf"]][[user]], + type = "folder", + project = project, + test_user = test_user, + dcc = dcc_user, + dry_run = dry_run) + cat("\nEmail composed for:", user, "\n") + Sys.sleep(sleep_interval) + + } + } + } +} diff --git a/monitor_annotations/monitor_annotations.R b/monitor_annotations/monitor_annotations.R index 789193d..c0dca36 100644 --- a/monitor_annotations/monitor_annotations.R +++ b/monitor_annotations/monitor_annotations.R @@ -9,7 +9,7 @@ library(httr) # Check whether running as DEV, TEST or PROD, with DEV being default and catch-all. Behavior for: # PROD = Send emails to actual users # TEST = Send emails to nf-osi-service (3421893) -# DEV = Print emails to stout +# DEV = Print emails to stout and save in messages.log only PROFILE <- switch(Sys.getenv("PROFILE"), PROD = "PROD", TEST = "TEST", @@ -26,12 +26,14 @@ TEST_USER <- as.character(Sys.getenv("TEST_USER")) if(PROFILE == "TEST" && TEST_USER == "") error("For PROFILE=TEST you must set TEST_USER=xxx") -DRY_RUN <- if(PROFILE == "DEV") TRUE else FALSE -SLEEP_INTERVAL <- 6 # seconds +DRY_RUN <- if(PROFILE == "DEV") TRUE else FALSE -# Input/target tables +# Reference tables study_tab_id <- 'syn16787123' fileview_tab_id <- 'syn16858331' +no_email_list <- 'syn51907919' + +no_email_list_users <- as.character(unlist(table_query(no_email_list, columns = "user"), use.names = FALSE)) # Define job schedule <- if(Sys.getenv("SCHEDULE") != "") paste(Sys.getenv("SCHEDULE"), "-") else "" @@ -47,20 +49,13 @@ try({ withCallingHandlers( { todo <- make_active_study_reminder_list(study_tab_id, fileview_tab_id) - for(project in names(todo)) { - for(user in names(todo[[project]][["naf"]]) ) { - TEST_USER <- if(PROFILE == "TEST") TEST_USER else NULL - email_re_annotation(recipient = user, - list = todo[[project]][["naf"]][[user]], - type = "folder", - project = project, - test_user = TEST_USER, - dcc = DCC_USER, - dry_run = DRY_RUN) - cat("Email composed for:", user, "\n") - Sys.sleep(SLEEP_INTERVAL) - } - } + if(DRY_RUN) sink("messages.log", append = TRUE, split = TRUE) + send_message_list(todo, + dcc_user = DCC_USER, + test_user = TEST_USER, + no_email_list_users = no_email_list_users, + dry_run = DRY_RUN) + sink() }, warning = function(w) handleWarning(w, "main"), diff --git a/monitor_annotations/tests/unit_tests.R b/monitor_annotations/tests/unit_tests.R new file mode 100644 index 0000000..9ad780f --- /dev/null +++ b/monitor_annotations/tests/unit_tests.R @@ -0,0 +1,18 @@ +source("../helpers.R") + +mock_reminder_list <- list( + syn123 = list(`naf` = list(`3423450` = c("folderA", "folderB"), `3421893` = "folderC")), + syn456 = list(`naf` = list(`3434950` = c("folderA"))) +) + +mock_no_email_list <- c("3421893") + +testthat::test_that("Confirms that 3421893 is skipped", { + + testthat::expect_message(send_message_list(todo = mock_reminder_list, + no_email_list_users = mock_no_email_list, + dry_run = TRUE, + sleep_interval = 0), + "Skipping reminder for: 3421893") + } +)