Skip to content

Commit

Permalink
Use no-email-list (#68)
Browse files Browse the repository at this point in the history
* Add log output sink for dev
* Add no-email check
* Refactor into wrapper fun for easier testing
* Add unit test
* Update README
* Ignore logs
  • Loading branch information
anngvu authored Jul 12, 2023
1 parent f146d8b commit de9db2a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.log
9 changes: 7 additions & 2 deletions monitor_annotations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
36 changes: 36 additions & 0 deletions monitor_annotations/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
}
}
31 changes: 13 additions & 18 deletions monitor_annotations/monitor_annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 ""
Expand All @@ -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"),
Expand Down
18 changes: 18 additions & 0 deletions monitor_annotations/tests/unit_tests.R
Original file line number Diff line number Diff line change
@@ -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")
}
)

0 comments on commit de9db2a

Please sign in to comment.