Skip to content

Commit 3eb814f

Browse files
authored
Merge pull request #160 from ljwoodley/add_project_and_instance_fields_to_rcc_job_log
Add project and instance fields to rcc job log
2 parents 7ecd1ca + a624786 commit 3eb814f

12 files changed

+167
-3
lines changed

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export(get_current_time)
2323
export(get_institutional_person_data)
2424
export(get_job_duration)
2525
export(get_package_scope_var)
26+
export(get_project_instance)
2627
export(get_project_life_cycle)
28+
export(get_project_name)
2729
export(get_redcap_credentials)
2830
export(get_redcap_db_connection)
2931
export(get_redcap_email_revisions)
@@ -45,6 +47,8 @@ export(scrape_user_api_tokens)
4547
export(send_email)
4648
export(set_package_scope_var)
4749
export(set_project_api_token)
50+
export(set_project_instance)
51+
export(set_project_name)
4852
export(set_script_name)
4953
export(set_script_run_time)
5054
export(set_super_api_token)

R/logging.R

+66
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ build_etl_job_log_df <- function(job_duration, job_summary, level) {
2929
) %>%
3030
dplyr::mutate(
3131
log_date = get_current_time(),
32+
project = get_project_name(),
33+
instance = get_project_instance(),
3234
script_name = get_script_name(),
3335
script_run_time = get_script_run_time(),
3436
job_duration = .data$job_duration,
@@ -65,6 +67,8 @@ build_formatted_df_from_result <- function(result, database_written, table_writt
6567
dplyr::mutate(record_level_data = purrr::pmap(.data, ~ rjson::toJSON(c(...)))) %>%
6668
dplyr::select(primary_key = pk_col, .data$record_level_data) %>%
6769
dplyr::mutate(
70+
project = get_project_name(),
71+
instance = get_project_instance(),
6872
script_name = get_script_name(),
6973
script_run_time = get_script_run_time(),
7074
log_date = get_current_time(),
@@ -130,6 +134,19 @@ get_script_run_time <- function() {
130134
return(redcapcustodian.env$script_run_time)
131135
}
132136

137+
#' Fetches the package-scoped value of project_name
138+
#' @export
139+
get_project_name <- function() {
140+
return(redcapcustodian.env$project_name)
141+
}
142+
143+
#' Fetches the package-scoped value of project_instance
144+
#' @export
145+
get_project_instance <- function() {
146+
return(redcapcustodian.env$project_instance)
147+
}
148+
149+
133150
#' Initialize the connection to the log db and set redcapcustodian.env$log_con
134151
#'
135152
#' @param drv, an object that inherits from DBIDriver (e.g. RMariaDB::MariaDB()), or an existing DBIConnection object (in order to clone an existing connection).
@@ -205,6 +222,55 @@ set_script_run_time <- function(fake_runtime = lubridate::NA_POSIXct_) {
205222
return(redcapcustodian.env$script_run_time)
206223
}
207224

225+
#' Sets the package-scoped value of project_name
226+
#'
227+
#' @param project_name Defaults to NULL. If provided and not NULL, this value is used.
228+
#' If NULL, the function attempts to fetch the value from the environment variable.
229+
#' @return the package-scoped value of project_name
230+
231+
#' @examples
232+
#' \dontrun{
233+
#' project_name <- set_project_name()
234+
#' project_name <- set_project_name("project_name")
235+
#' }
236+
#'
237+
#' @export
238+
set_project_name <- function(project_name = "") {
239+
if (project_name == "") {
240+
project_name <- Sys.getenv("PROJECT")
241+
}
242+
243+
assign("project_name",
244+
project_name,
245+
envir = redcapcustodian.env)
246+
247+
return(redcapcustodian.env$project_name)
248+
}
249+
250+
#' Sets the package-scoped value of project_instance
251+
#' @param project_instance Defaults to NULL. If provided and not NULL, this value is used.
252+
#' If NULL, the function attempts to fetch the value from the environment variable.
253+
#'
254+
#' @return the package-scoped value of project_instance
255+
#' @examples
256+
#' \dontrun{
257+
#' project_instance <- set_project_instance()
258+
#' project_instance <- set_project_instance("project_instance")
259+
#' }
260+
#'
261+
#' @export
262+
set_project_instance <- function(project_instance = "") {
263+
if (project_instance == "") {
264+
project_instance <- Sys.getenv("INSTANCE")
265+
}
266+
267+
assign("project_instance",
268+
project_instance,
269+
envir = redcapcustodian.env)
270+
271+
return(redcapcustodian.env$project_instance)
272+
}
273+
208274
#' Attempts to connect to the DB using all LOG_DB_* environment variables. Returns an empty list if a connection is established, returns an `error_list` entry otherwise.
209275
#'
210276
#' @param drv, an object that inherits from DBIDriver (e.g. RMariaDB::MariaDB()), or an existing DBIConnection object (in order to clone an existing connection).

R/utils.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ get_package_scope_var <- function(key) {
4747

4848
#' Initialize all etl dependencies
4949
#'
50+
#' @param project_name name passed to \code{\link{set_project_name}}
51+
#' @param project_instance name passed to \code{\link{set_project_instance}}
5052
#' @param script_name name passed to \code{\link{set_script_name}}
5153
#' @param fake_runtime An optional asserted script run time passed to \code{\link{set_script_run_time}}, defaults to the time this function is called
5254
#' @param log_db_drv, an object that inherits from DBIDriver (e.g. RMariaDB::MariaDB()), or an existing DBIConnection object (in order to clone an existing connection).
@@ -57,7 +59,9 @@ get_package_scope_var <- function(key) {
5759
#' init_etl("name_of_file")
5860
#' }
5961
#'
60-
init_etl <- function(script_name = "", fake_runtime = NULL, log_db_drv = RMariaDB::MariaDB()) {
62+
init_etl <- function(script_name = "", project_name = "", project_instance = "", fake_runtime = NULL, log_db_drv = RMariaDB::MariaDB()) {
63+
set_project_name(project_name)
64+
set_project_instance(project_instance)
6165
set_script_name(script_name)
6266
if (!is.null(fake_runtime)) {
6367
set_script_run_time(fake_runtime)

inst/schema/rcc_job_log.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
CREATE TABLE `rcc_job_log` (
22
`id` bigint NOT NULL AUTO_INCREMENT,
33
`log_date` datetime NOT NULL,
4+
`project` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
5+
`instance` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
46
`script_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
57
`script_run_time` datetime NOT NULL,
68
`job_summary_data` mediumtext DEFAULT NULL,
79
`job_duration` double not null,
810
`level` enum('SUCCESS','DEBUG','ERROR') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
911
PRIMARY KEY (`id`),
1012
KEY `log_date` (`log_date`),
13+
KEY `project` (`project`),
14+
KEY `instance` (`instance`),
1115
KEY `script_name` (`script_name`),
1216
KEY `script_run_time` (`script_run_time`),
1317
KEY `level` (`level`)

man/get_project_instance.Rd

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_project_name.Rd

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/init_etl.Rd

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_project_instance.Rd

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_project_name.Rd

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rcc.log.db/schema/rcc_job_log.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
CREATE TABLE `rcc_job_log` (
22
`id` bigint NOT NULL AUTO_INCREMENT,
33
`log_date` datetime NOT NULL,
4+
`project` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
5+
`instance` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
46
`script_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
57
`script_run_time` datetime NOT NULL,
68
`job_summary_data` mediumtext DEFAULT NULL,
79
`job_duration` double not null,
810
`level` enum('SUCCESS','DEBUG','ERROR') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
911
PRIMARY KEY (`id`),
1012
KEY `log_date` (`log_date`),
13+
KEY `project` (`project`),
14+
KEY `instance` (`instance`),
1115
KEY `script_name` (`script_name`),
1216
KEY `script_run_time` (`script_run_time`),
1317
KEY `level` (`level`)

study_template/example.env

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
PROJECT=study_template
12
INSTANCE=Development
23
TIME_ZONE=America/New_York
34

vignettes/custom_rscript.Rmd

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ If this is not your first REDCap Custodian rodeo, you might remember what you ne
5353
You'll need to talk to at least a REDCap or a MySQL database. You will probably want both. Rename the `example.env` to `testing.env` and configure it for development on your computer. That file is composed of five sections.
5454

5555
```sh
56+
PROJECT=study_template
5657
INSTANCE=Development
5758
TIME_ZONE=America/New_York
5859
```
5960

60-
`INSTANCE` names the REDCap system or _instance_ you'll be talking to. This file assumes you are talking to only one REDCap in your script. There are other tools for multiple-instances.
61+
`PROJECT` names the project, study, or git repository that owns the scripts. This is useful when reading log data from a shared log database. A unique value in the PROJECT field allows you to identify log entries specific to a project.
6162

62-
`TIME_ZONE` should be the local timezone of your REDCap instance. Note that REDCap time facts in local time. The MariaDB driver and the Lubridate library default to UTC. That can get complicated. Be careful with time. For more details see [`stupid_date_tricks.R`](https://gist.github.com/pbchase/ed55ab5dacbcc5d8a702a9cb935cccb5)
63+
`INSTANCE` names the _instance_ or a project, study, or REDCap system. If scripts are deployed in development, testing, and production, a unique value in this field allows you to identify log entries from different deployments.
64+
65+
`TIME_ZONE` should be the local timezone of your REDCap instance. Note that REDCap records time facts in local time. The MariaDB driver and the Lubridate library default to UTC. That can get complicated. Be careful with time. For more details see [`stupid_date_tricks.R`](https://gist.github.com/pbchase/ed55ab5dacbcc5d8a702a9cb935cccb5)
6366

6467
```sh
6568
# Email config

0 commit comments

Comments
 (0)