-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
468 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ docs/* | |
local.env.txt | ||
^hosts/.* | ||
^data-raw$ | ||
^credentials$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: redcapcustodian | ||
Type: Package | ||
Title: System data cleaning for REDCap | ||
Version: 1.4.1 | ||
Version: 1.5.0 | ||
Authors@R: c( | ||
person("Philip", "Chase", | ||
email = "[email protected]", | ||
|
@@ -58,7 +58,8 @@ Suggests: | |
digest, | ||
RSQLite, | ||
knitr (>= 1.18), | ||
rmarkdown (>= 2.0) | ||
rmarkdown (>= 2.0), | ||
fs | ||
VignetteBuilder: knitr | ||
Config/testthat/edition: 3 | ||
RoxygenNote: 7.2.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' Format and write summary metrics to the redcap_summary_metrics table in your LOG_DB | ||
#' | ||
#' @param reporting_period_start a datetime object, e.g. ymd_hms("2022-11-01 00:00:00") | ||
#' @param reporting_period_end a datetime object, e.g. ymd_hms("2022-12-01 00:00:00") | ||
#' @param metric_type a character string representing the metric type, e.g. "flux", "state" | ||
#' @param metric_dataframe A wide data frame of key-value pairs with a single row of data | ||
#' | ||
#' @return nothing | ||
#' | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' write_summary_metrics( | ||
#' reporting_period_start = ymd_hms("2022-01-01 00:00:00", tz=Sys.getenv("TIME_ZONE")), | ||
#' reporting_period_end = ceiling_date(reporting_period_start, "month", change_on_boundary = T) | ||
#' metric_type = "state", | ||
#' metric_dataframe = my_cool_df | ||
#' ) | ||
#' } | ||
write_summary_metrics <- function(reporting_period_start, | ||
reporting_period_end, | ||
metric_type, | ||
metric_dataframe) { | ||
|
||
tall_df <- metric_dataframe %>% | ||
tidyr::pivot_longer( | ||
cols = dplyr::everything(), | ||
names_to = "key", | ||
values_to = "value" | ||
) %>% | ||
cbind( | ||
reporting_period_start, | ||
reporting_period_end, | ||
metric_type, | ||
script_name = get_script_name(), | ||
script_run_time = get_script_run_time() | ||
) %>% | ||
dplyr::select( | ||
reporting_period_start, | ||
reporting_period_end, | ||
.data$key, | ||
.data$value, | ||
.data$metric_type, | ||
.data$script_name, | ||
.data$script_run_time | ||
) | ||
|
||
log_conn <- get_package_scope_var("log_con") | ||
|
||
# log data in redcap_summary_metrics | ||
DBI::dbAppendTable(log_conn, "redcap_summary_metrics", tall_df) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.1 | ||
1.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE `redcap_summary_metrics` ( | ||
`id` INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
`script_run_time` datetime NOT NULL, | ||
`script_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`reporting_period_start` datetime NOT NULL, | ||
`reporting_period_end` datetime NOT NULL, | ||
`key` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`value` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`metric_type` enum('flux', 'state') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, | ||
KEY `script_name` (`script_name`), | ||
KEY `script_run_time` (`script_run_time`), | ||
KEY `metric_type` (`metric_type`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/perl | ||
while (<>){ | ||
if (m/^SET/) { next }; | ||
s/CHARACTER SET \S+ //; # remove CHARACTER SET mumble | ||
s/ENGINE=\S+ *//; # remove ENGINE | ||
s/DEFAULT CHARSET=\S+ *//; # remove DEFAULT CHARSET | ||
s/COLLATE [^, ]+//; # remove COLLATE on column | ||
s/ UNSIGNED//i; # remove unsigned on column | ||
s/COLLATE=\S+ *//; # remove COLLATE on table | ||
s/COMMENT '.+'//; # remove COMMENT on column | ||
s/COMMENT='.+'//; # remove COMMENT on table | ||
s/enum\(.*\)/varchar(255)/; # replace enum | ||
if (m/^ALTER TABLE/) { next }; # remove ALTER TABLE | ||
if (m/^\s*ADD /) { next }; # Remove indented ADD. Note: this is very crude | ||
if (m/^\s*MODIFY /) { next }; # Remove indented MODIFY. Note: this is very crude | ||
s/int\(\d+\)/integer/g; # Replace int(NN) with integer | ||
s/\\'/''/g; # Use '' instead of \' | ||
s/\\"/"/g; # Use " instead of \" | ||
s/\\r\\n/\r\n/g; # Convert escaped \r\n to literal | ||
s/\\\\/\\/g; # Convert escaped \ to literal | ||
s/ auto_increment=?\d*//gi; # Remove auto_increment | ||
s/^[UN]*?LOCK TABLES.*//g; # Remove locking statements | ||
if (m/^\s*KEY /) { next }; # Remove indented KEY | ||
if (m/^\s*UNIQUE KEY /) { next }; # Remove indented KEY | ||
if (m/^\s*PRIMARY KEY /) { next }; # Remove indented KEY | ||
$lines .= $_; | ||
} | ||
|
||
# remove the comma from the last param before the close paren | ||
local $/ = undef; | ||
$lines =~ s/,\n\)/\n\)/; | ||
|
||
print $lines; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
library(tidyverse) | ||
library(dotenv) | ||
library(REDCapR) | ||
library(lubridate) | ||
library(rmarkdown) | ||
library(sendmailR) | ||
library(redcapcustodian) | ||
|
||
init_etl("render_report") | ||
|
||
if (!dir.exists("output")){ | ||
dir.create("output") | ||
} | ||
|
||
if (!interactive()) { | ||
args <- commandArgs(trailingOnly = T) | ||
script_name <- word(args, 2, sep = "=") | ||
} else { | ||
script_name <- "sample_report.Rmd" | ||
} | ||
|
||
report_name <- word(script_name, 1, sep = "\\.") | ||
|
||
script_run_time <- set_script_run_time() | ||
|
||
output_file <- here::here( | ||
"output", | ||
paste0(report_name, | ||
"_", | ||
format(script_run_time, "%Y%m%d%H%M%S")) | ||
) | ||
|
||
full_path_to_output_file <- render( | ||
here::here("report", script_name), | ||
output_file = output_file | ||
) | ||
|
||
output_file_extension <- word(full_path_to_output_file, 2 , sep = "\\.") | ||
attachment_object <- mime_part(full_path_to_output_file, basename(full_path_to_output_file)) | ||
|
||
email_subject <- paste(report_name, "|", script_run_time) | ||
body <- "Please see the attached report." | ||
|
||
email_body <- list(body, attachment_object) | ||
|
||
# send the email with the attached output file | ||
send_email(email_body, email_subject) | ||
|
||
log_job_success(jsonlite::toJSON(script_name)) |
Oops, something went wrong.