forked from dimagi/dimagi-data-platform-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.R
More file actions
58 lines (49 loc) · 2.37 KB
/
reports.R
File metadata and controls
58 lines (49 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
library(plyr)
library(dplyr)
source(file.path("function_libraries","config_file_funcs.R", fsep = .Platform$file.sep))
source(file.path("config_setup.R", fsep = .Platform$file.sep)) # sets the path to the run config to use
run_conf <-get_run_config(config_run_path)
system_conf <- get_system_config(file.path("config_system.json"))
# in debug mode, csv files from the dir r_test_data_dir are used instead of db queries
debug_mode <- run_conf$debug
if (debug_mode == T) {
source(file.path("function_libraries","csv_sources.R", fsep = .Platform$file.sep))
test_data_dir <- system_conf$directories$r_test_data_dir
domain_table <- get_domain_table_from_csv (test_data_dir)
} else {
source(file.path("function_libraries","db_queries.R", fsep = .Platform$file.sep))
con <- get_con(dbname=system_conf$database$dbname,
user=system_conf$database$user,
pass=system_conf$database$pass,
host=system_conf$database$host,
port=system_conf$database$port)
domain_table <- get_domain_table(con)
}
domains_for_run <- get_domains_for_run(domain_table,run_conf)
if (length(domains_for_run) == 0) {
warning("No domains matched config file domain section, nothing to run on.")
} else {
reports <- get_report_module_names(run_conf)
aggregate_tables_dir <- file.path(system_conf$directories$output,"aggregate_tables")
tmp_report_pdf_dir <- file.path(system_conf$directories$tmp,"report_pdfs")
dir.create(tmp_report_pdf_dir, showWarnings = FALSE)
report_pdfs <- vector()
# run the report modules
for (report in reports) {
print(sprintf("Running report module: %s", report))
report_file <- sprintf("%s.R", report)
report_options <- get_report_options(run_conf,report)
source(file.path("report_modules",report_file, fsep = .Platform$file.sep))
if (debug_mode == F) {
module_pdfs <- render(con,domains_for_run,report_options,aggregate_tables_dir,tmp_report_pdf_dir)
} else {
module_pdfs <- render_debug(test_data_dir,domains_for_run,report_options,aggregate_tables_dir,tmp_report_pdf_dir)
}
report_pdfs <- c(report_pdfs,module_pdfs)
}
report_file_name <- file.path(system_conf$directories$output,paste(run_conf$reports$report_file_name,"pdf",sep="."))
system2(command = "pdftk",args = c(shQuote(report_pdfs), "cat output", shQuote(report_file_name)))
}
if (debug_mode == F) {
close_con(con)
}