Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add notification helpers #136

Merged
merged 27 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: gDRutils
Type: Package
Title: A package with helper functions for processing drug response data
Version: 1.3.16
Date: 2024-10-11
Version: 1.3.17
Date: 2024-10-18
Authors@R: c(person("Bartosz", "Czech", role=c("aut"),
comment = c(ORCID = "0000-0002-9908-3007")),
person("Arkadiusz", "Gladki", role=c("cre", "aut"), email="[email protected]",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export(get_default_identifiers)
export(get_duplicated_rows)
export(get_env_assay_names)
export(get_env_identifiers)
export(get_env_var)
export(get_expect_one_identifiers)
export(get_experiment_groups)
export(get_header)
Expand Down Expand Up @@ -98,6 +99,7 @@ export(rename_DFrame)
export(rename_bumpy)
export(reset_env_identifiers)
export(round_concentration)
export(send_email)
export(set_SE_experiment_metadata)
export(set_SE_experiment_raw_data)
export(set_SE_fit_parameters)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## gDRutils 1.3.17 - 2024-10-18
* add notification helpers

## gDRutils 1.3.16 - 2024-10-11
* make duplicates' helpers supporting combo assays as well

Expand Down
58 changes: 58 additions & 0 deletions R/duplicates.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,61 @@ throw_msg_if_duplicates <- function(dt, assay_name = "unknown", msg_f = stop, pr
msg_f(paste0(msg, msg2, msg3))
}
}

#' send email if assay data.table contains duplicated rows
#'
#' An auxiliary function to send email if duplicated rows in assay data.table are found
#'
#' @param dt data.table with assay data
#' @param by charvec with notification methods (currently 'email' supported)
#' @param assay_name string with the name of the assay
#' @param preview_max_numb number of rows to preview if duplicates found
#' @param metadata list with the additional metadata to send via email
#' @examples
#' sdata <- get_synthetic_data("finalMAE_small")
#' smetrics_data <- convert_se_assay_to_dt(sdata[[1]], "Metrics")
#' throw_msg_if_duplicates(smetrics_data, assay_name = "Metrics", msg_f = futile.logger::flog.info)
#' @return NULL
#' @keywords duplicates
#'
#' @export
#'
notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", preview_max_numb = 4, metadata = NULL) {
j-smola marked this conversation as resolved.
Show resolved Hide resolved

checkmate::assert_data_table(dt)
checkmate::assert_subset(by, c("email", "slack"))
checkmate::assert_string(assay_name)
checkmate::assert_number(preview_max_numb)
checkmate::assert_list(metadata, null.ok = TRUE)

dup_dt <- get_assay_dt_duplicated_rows(dt, output = "data")

msg <- sprintf(
"The %i ouf of %i rows are duplicated in the assay '%s'",
gladkia marked this conversation as resolved.
Show resolved Hide resolved
NROW(dup_dt),
NROW(dt),
assay_name)
msg2 <- sprintf(" when checking uniquness with the following set of columns: '%s'. ",
gladkia marked this conversation as resolved.
Show resolved Hide resolved
toString(get_assay_req_uniq_cols(dt)))
msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'",
preview_numb,
gladkia marked this conversation as resolved.
Show resolved Hide resolved
jsonlite::toJSON(dup_dt[seq(preview_numb), ]))
gladkia marked this conversation as resolved.
Show resolved Hide resolved
msg <- paste0(msg, msg2, msg3)

if ("mail" %in% by) {
gladkia marked this conversation as resolved.
Show resolved Hide resolved
att_l <- list(c(dup_dt = dupt_dt, metadata))
gladkia marked this conversation as resolved.
Show resolved Hide resolved
att_f <- tempfile()
qs::qsave(att_l, att_f)
m_to <- get_env_var("EMAIL_RECIPIENT")
stopifnot(nchar(m_to) > 0)
send_email(body = msg, to = m_to, from = from, attached_files = att_f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg from is not definied

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 1c494b0.

}
j-smola marked this conversation as resolved.
Show resolved Hide resolved

if ("slack" %in% by) {
s_to <- get_env_var("EMAIL_SLACK_NOTIFICATION")
stopifnot(nchar(s_to) > 0)
send_email(body = msg, to = s_to, from = from)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg from is not definied

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 1c494b0.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a.a.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to GC



}
64 changes: 64 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,67 @@ calc_sd <- function(x) {
}
}


#' send email by wrapping `mailR::send.mail`
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#'
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#' send email by wrapping `mailR::send.mail`
#'
#' @param body a body content of the email
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#' @param to a string with the recipient
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#' @param subject a string with the subject
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#' @param from a string with the sender
gladkia marked this conversation as resolved.
Show resolved Hide resolved
#' @param html a logical flag indicating whether the body of the email should be parsed as HTML
#' @param inline a logical flag indicating whether images in the HTML file should be embedded inline
#' @param host_name a string with the hostname
#' @param attached_files a character with file paths to be attached to the email
#'
#' @keywords utils
#' @export
send_email <-
function(body,
subject,
to = get_env_var("EMAIL_RECIPIENT"),
from = get_env_var("EMAIL_SENDER"),
html = TRUE,
inline = FALSE,
host_name = get_env_var("EMAIL_HOSTNAME"),
attached_files = NULL) {

checkmate::assert_character(body)
checkmate::assert_string(subject, min.chars = 1)
checkmate::assert_string(to, min.chars = 1)
checkmate::assert_from(from, min.chars = 1)
gladkia marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_flag(html)
checkmate::assert_flag(inline)
checkmate::assert_from(host_name, min.chars = 1)
gladkia marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_character(attached_files)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines should be wrapped into if, to allow for using def arg with NULL, i.e.

    if (!is.null(attached_files)) {
        checkmate::assert_character(attached_files)
        checkmate::assert_file_exists(attached_files)
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 708cae4.

checkmate::assert_file_exists(attached_files)

mailR::send.mail(
from = from,
to = to,
subject = subject,
body = html_data,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
body = html_data,
body = body,

wrong arg

smtp = list(host.name = host_name),
html = html,
inline = inline,
attach.files = attached_files
)
}


#' safe wrapper of Sys.getenv()
#'
#' So far the helper is needed to handle env vars containing `:`
#' for which the backslash is automatically added in some contexts
#' and R could not get the original value for these env vars.
#'
#' @param x string with the name of the environemntal variable
#' @param ... additional params for Sys.getenev
#' @keywords utils
j-smola marked this conversation as resolved.
Show resolved Hide resolved
#
#' @export
#' @return sanitized value of the env variable
get_env_var <- function(x, ...) {
j-smola marked this conversation as resolved.
Show resolved Hide resolved
gsub("\\\\", "", Sys.getenv(x, ...))
}
22 changes: 22 additions & 0 deletions man/get_env_var.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/send_email.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading