-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAutoReportFuns.R
673 lines (588 loc) · 21.6 KB
/
AutoReportFuns.R
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
## Handle automated reports in config
#' Create and add report to config
#'
#' Adds an entry to the system configuration of reports to run at given
#' intervals. After generating the configuration from the new entry
#' the function load the current system configuration, adds the new
#' entry and saves the updated system configuration.
#'
#' @param synopsis String with description of the report and to be used in
#' subject field of email distributed reports
#' @param package String with package name also corresponding to registry
#' @param fun String providing name of function to be called for generating
#' report
#' @param paramNames String vector where each element corresponds to the input
#' parameter to be used in the above function
#' @param paramValues String vector with corresponding values to paramNames
#' @param owner String providing the owner of the report. Usually a user name
#' @param email String with email address to recipient of email containing the
#' report
#' @param organization String identifying the organization the owner belongs to
#' @param runDayOfYear Integer vector with day numbers of the year when the
#' report is to be run
#' @param terminateDate Date-class date after which report is no longer run.
#' Default value set to \code{NULL} in which case the function will provide an
#' expiry date adding 3 years to the current date if in a PRODUCTION context
#' and 1 month if not
#' @param interval String defining a time intervall as defined in
#' \code{\link[base:seq.POSIXt]{seq.POSIXt}}. Default value is an emty string
#' @param intervalName String providing a human uderstandable representation of
#' \code{interval}. Default value is an emty string
#' @param dryRun Logical defining if global auto report config actually is to
#' be updated. If set to TRUE the actual config (all of it) will be returned by
#' the function. FALSE by default
#'
#' @return Nothing unless dryRun is set TRUE in which case a list of all config
#' will be returned
#' @seealso \code{\link{deleteAutoReport}}
#' @export
createAutoReport <- function(synopsis, package, fun, paramNames, paramValues,
owner, email, organization, runDayOfYear,
terminateDate = NULL, interval = "",
intervalName = "", dryRun = FALSE) {
# When NULL, set expiry date based on context
if (is.null(terminateDate)) {
context <- Sys.getenv("R_RAP_INSTANCE")
terminateDate <- as.POSIXlt(Sys.Date())
if (context %in% c("PRODUCTION")) {
terminateDate$year <- terminateDate$year + 3
} else {
terminateDate$mon <- terminateDate$mon + 1
}
terminateDate <- as.Date(terminateDate)
}
# make unique id by (hashing) combination of owner and timestamp
ts <- as.character(as.integer(as.POSIXct(Sys.time())))
autoRepId <- digest::digest(paste0(owner, ts))
# make current entry, first named list of param names and values pairs
l <- list()
params <- paramValues
names(params) <- paramNames
paramsListVector <- list()
for (i in seq_len(length(params))) {
paramsListVector[[i]] <- as.list(params[i])
}
l$synopsis <- synopsis
l$package <- package
l$fun <- fun
l$params <- paramsListVector
l$owner <- owner
l$email <- email
l$organization <- organization
l$terminateDate <- as.character(terminateDate)
l$interval <- interval
l$intervalName <- intervalName
l$runDayOfYear <- runDayOfYear
rd <- readAutoReportData()
rd[[eval(autoRepId)]] <- l
if (dryRun) {
rd
} else {
writeAutoReportData(config = rd)
}
}
#' Delete existing report from config
#'
#' @param autoReportId String providing the auto report unique id
#'
#' @seealso \code{\link{createAutoReport}}
#' @export
deleteAutoReport <- function(autoReportId) {
rd <- readAutoReportData()
# just stop with an error if report does not exist
stopifnot(!is.null(rd[[autoReportId]]))
ind <- names(rd) == autoReportId
rd <- rd[!ind]
writeAutoReportData(config = rd)
}
#' Read automated report metadata
#'
#' @param fileName String defining name of the yaml configuration file. Default
#' 'autoReport.yml'
#' @param packageName String defining the package in which the above
#' configuration file resides. A configuration file within an R-package is
#' only used in case the environmental variable 'R_RAP_CONFIG_PATH' is not
#' defined (empty)
#'
#' @return a list of yaml data
#' @export
#'
#' @examples
#' readAutoReportData()
readAutoReportData <- function(fileName = "autoReport.yml",
packageName = "rapbase") {
path <- Sys.getenv("R_RAP_CONFIG_PATH")
if (path == "") {
stopifnot(file.exists(system.file(fileName, package = packageName)))
config_file <- system.file(fileName, package = packageName)
} else {
if (!file.exists(file.path(path, fileName))) {
warning(paste("No configuration file found in", path,
". A new file will be made from the package default"))
file.copy(system.file(fileName, package = packageName), path)
}
config_file <- file.path(path, fileName)
}
yaml::yaml.load_file(config_file)
}
#' Write automated report metadata
#'
#' @inheritParams readAutoReportData
#' @param config a list of yaml configuration
#'
#' @return NULL
#' @export
#'
#' @examples
#' \donttest{
#' # Example depend on environment variable R_RAP_CONFIG_PATH being set
#' config <- readAutoReportData()
#' writeAutoReportData(config = config)
#' }
writeAutoReportData <- function(fileName = "autoReport.yml", config,
packageName = "rapbase") {
path <- Sys.getenv("R_RAP_CONFIG_PATH")
if (path == "") {
# cannot proceed if there is nowhere to store config
stop(paste("There is nowhere to store config data.",
"The environment variable R_RAP_CONFIG_PATH must be defined",
"providing av path to a directory where configuration can",
"be written. Stopping"))
} else {
oriFile <- file.path(path, fileName)
# in case we screw-up, make a backup
tmpTag <- as.character(as.integer(as.POSIXct(Sys.time())))
nameParts <- strsplit(fileName, "[.]")[[1]]
bckFileName <- paste0(nameParts[1], tmpTag, ".", nameParts[-1])
bckFilePath <- file.path(path, "autoReportBackup")
if (!dir.exists(bckFilePath)) {
dir.create(bckFilePath)
}
file.copy(from = oriFile, to = file.path(bckFilePath, bckFileName),
overwrite = TRUE)
# to maintain some order, remove files older than 30 days
files <- file.info(list.files(bckFilePath, full.names = TRUE))
rmFiles <- rownames(files[difftime(Sys.time(), files[, "mtime"],
units = "days") > 30, ])
file.remove(rmFiles)
con <- file(oriFile, "w")
}
yaml::write_yaml(config, con)
close(con)
}
#' Select data on one registry from config (list)
#'
#' Pick all config corresponding to a given registry. Registry name is not
#' given as such, but rather as its corresponding R package name. Hence, a
#' registry must be given as the name of its R package
#'
#' @param config list of configuration for automated reports
#' @param reg string giving the exact name of the R package for the registry
#'
#' @return list with config for registry reg
#' @export
selectByReg <- function(config, reg) {
if (length(config) == 0) {
list()
} else {
ind <- integer()
for (i in seq_len(length(config))) {
if (config[[i]]$package == reg) {
ind <- c(ind, i)
}
}
c(config[ind])
}
}
#' Select data on one owner from config (list)
#'
#' Pick all config corresponding to a given owner (of the report)
#'
#' @param config list of configuration for automated reports
#' @param owner string giving the exact name owner
#'
#' @return list with config for reports belonging to owner
#' @export
selectByOwner <- function(config, owner) {
if (length(config) == 0) {
list()
} else {
ind <- integer()
for (i in seq_len(length(config))) {
if (config[[i]]$owner == owner) {
ind <- c(ind, i)
}
}
c(config[ind])
}
}
#' Select data on one organization from config (list)
#'
#' Pick all config corresponding to a given organization (of the report)
#'
#' @param config list of configuration for automated reports
#' @param organization string giving the exact organization
#'
#' @return list with config for reports belonging to organization
#' @export
selectByOrganization <- function(config, organization) {
if (length(config) == 0) {
list()
} else {
ind <- integer()
for (i in seq_len(length(config))) {
if (config[[i]]$organization == organization) {
ind <- c(ind, i)
}
}
c(config[ind])
}
}
#' Provide vector of registries (\emph{i.e.} their R packages) in config
#'
#' @param config list of configuration for automated reports
#'
#' @return character vector of registry (package) names
#' @export
getRegs <- function(config) {
regs <- vector(mode = "character")
for (i in seq_len(length(config))) {
reg <- config[[i]]$package
if (!(reg %in% regs)) {
regs <- c(regs, reg)
}
}
regs
}
## Run automated reports
#' Simple test of automated report
#'
#' Simple test of automated reporting from definitions provided in a yaml
#' config file
#'
#' @param aNum a number
#' @param aChar a character
#' @param anExp an expression
#'
#' @return A simple message listing the contents of the arguments
#' @export
#'
#' @examples
#' .testAutoReport()
.testAutoReport <- function(aNum = 1, aChar = "a", anExp = Sys.Date()) {
msg <- paste("This is a simple test of automated reports.",
"Arguments provided:\n",
"aNum:", as.character(aNum), ",\n",
"aChar:", aChar, ",\n",
"anExp:", as.character(anExp), "\n")
fileName <- paste0(tempfile(), ".txt")
con <- file(fileName, "w")
cat(msg, file = fileName)
close(con)
fileName
}
#' Provide explicit reference to function for do.call
#'
#' @param x string with explicit reference, i.e. 'package::function'
#'
#' @return value of the exported 'function' in 'package'
#' @export
.getFun <- function(x) {
if (length(grep("::", x)) > 0) {
parts <- strsplit(x, "::")[[1]]
getExportedValue(parts[1], parts[2])
} else { # nocov start
x
} # nocov end
}
#' Run reports as defined in yaml config and ship content by email
#'
#' Usually to be called by a scheduler, e.g. cron. If the provided day of
#' year matches those of the config the report is run as otherwise specified in
#' config. Functions called upon are expected to return a path to a file that
#' can be attached to an email. The email itself is defined and sent to
#' recipients defined in the config
#'
#' @param dayNumber Integer day of year where January 1st is 1. Defaults to
#' current day, i.e. as.POSIXlt(Sys.Date())$yday+1 (POSIXlt yday is base 0)
#' @param dryRun Logical defining if emails are to be sent. If TRUE a message
#' with reference to the payload file is given but no emails will actually be
#' sent. Default is FALSE
#'
#' @return Emails with corresponding file attachment. If dryRun == TRUE just a
#' message
#' @export
#'
#' @examples
#' \donttest{
#' # Example depend on environment variable R_RAP_CONFIG_PATH being set
#' runAutoReport()
#' }
runAutoReport <- function(dayNumber = as.POSIXlt(Sys.Date())$yday + 1,
dryRun = FALSE) {
# get report candidates
reps <- readAutoReportData()
# standard text for email body
stdTxt <- readr::read_file(system.file("autoReportStandardEmailText.txt",
package = "rapbase"))
# get sender from common config
conf <- rapbase::getConfig("rapbaseConfig.yml")
from <- conf$network$sender
# apply RFC 1342 on headers (i.e. subject)
charset <- "=?UTF-8?"
enc <- "B?"
headPost <- "?="
for (i in seq_len(length(reps))) {
tryCatch({
rep <- reps[[i]]
# get explicit referenced function
f <- .getFun(paste0(rep$package, "::", rep$fun))
if (dayNumber %in% rep$runDayOfYear &&
as.Date(rep$terminateDate) > Sys.Date()) {
attFile <- do.call(what = f, args = rep$params)
if (dryRun) {
message(paste("No emails sent. Attachment is", attFile))
} else { # nocov start
# escape spaces (e.g. when full name is added to <email>)
to <- gsub(" ", "\\ ", rep$email, fixed = TRUE)
# Subject is a header field, hence non-ascii must be handled this way
subject <- charToRaw(rep$synopsis)
subject <- base64enc::base64encode(subject, linewidth = 70,
newline = "\n")
subject <- paste0(charset, enc, subject, headPost)
body <- list(stdTxt, sendmailR::mime_part(attFile))
# ship the shite
sendmailR::sendmail(
from, to, subject, body,
control = list(smtpServer = conf$network$smtp$server,
smtpPortSMTP = conf$network$smtp$port))
} # nocov end
}
},
error = function(e) {
message(paste("Report could not be processed (moving on to the next):",
e))
})
}
}
## Miscellaneous functions
#' Make a sequence of day numbers from av given date and interval
#'
#' This function provides an even sequence of day numbers spanning 365/366
#' days from the start date and interval provided. Mainly to be used in
#' setting up automated reports at Rapporteket
#'
#' @param startDay Start date of sequence. May be provided as a string,
#' \emph{e.g.} \"2019-03-17\" or as class \"Date\". Defaults to today
#' @param interval String representing a valid seq.POSIXt interval such as
#' "DSTday", "week", "month", "quarter" or "year")
#'
#' @return Integer vector of day numbers
#' @export
#'
#' @examples
#' makeRunDayOfYearSequence(interval = "month")
#'
makeRunDayOfYearSequence <- function(startDay = Sys.Date(), interval) {
# set end to a year from start
start <- as.POSIXlt(startDay)
end <- start
end$year <- end$year + 1
s <- seq(from = start, to = end, by = interval)
# skip redundant end value
if (length(s) > 1) {
s <- s[seq_len(length(s)) - 1]
}
unique(as.integer(format(s, "%j")))
}
#' Find next run date for automated reports
#'
#' Find the next date that an automated report is supposed to be run. Likely,
#' this function will only be relevant for shiny apps when this date is to
#' be printed
#'
#' @param runDayOfYear Numeric vector providing year-day numbers
#' @param baseDayNum Numeric defining base year-day. Default is today
#' @param returnFormat String providing return format as described in
#' \code{\link[base]{strptime}} in the current locale. Defaults to
#' "\%A \%d. \%B \%Y" that will provide something like
#' 'Mandag 20. januar 2019' in a Norwegian locale
#' @return String date for printing
#' @examples
#' # Will return Jan 30 in the current year and locale with default formatting
#' findNextRunDate(c(10, 20, 30), 20)
#' @export
findNextRunDate <- function(runDayOfYear,
baseDayNum = as.POSIXlt(Sys.Date())$yday + 1,
returnFormat = "%A %e. %B %Y") {
year <- as.POSIXlt(Sys.Date())$year + 1900
if (baseDayNum >= max(runDayOfYear) | length(runDayOfYear) == 1 &
baseDayNum >= max(runDayOfYear)) {
# next run will be first run of next year
nextDayNum <- min(runDayOfYear)
year <- year + 1
} else {
# next run will be next run day this year
nextDayNum <- min(runDayOfYear[runDayOfYear > baseDayNum])
}
format(strptime(paste(year, nextDayNum), "%Y %j"), format = returnFormat)
}
#' Make table of subscriptions of reports
#'
#' Make a table to be rendered in a shiny app providing the active
#' subscriptions of a given user within a given registry which are
#' both collected from the shiny session object provided
#'
#' @param session A shiny session object
#'
#' @return Matrix providing a table to be rendered in a shiny app
#' @importFrom magrittr "%>%"
#' @export
makeUserSubscriptionTab <- function(session) {
. <- ""
l <- list()
autoRep <- readAutoReportData() %>%
selectByReg(., reg = getUserGroups(session)) %>%
selectByOwner(., owner = getUserName(session)) %>%
selectByOrganization(., organization = getUserReshId(session))
dateFormat <- "%A %e. %B %Y"
for (n in names(autoRep)) {
nextDate <- findNextRunDate(autoRep[[n]]$runDayOfYear,
returnFormat = dateFormat)
if (as.Date(nextDate, format = dateFormat) > autoRep[[n]]$terminateDate) {
nextDate <- "Utl\u00F8pt"
}
r <- list("Rapport" = autoRep[[n]]$synopsis,
"Enhet" = autoRep[[n]]$organization,
"Periode" = autoRep[[n]]$intervalName,
"Utl\u00F8p" = strftime(as.Date(autoRep[[n]]$terminateDate),
format = "%b %Y"),
"Neste" = nextDate,
"Slett" = as.character(
shiny::actionButton(inputId = paste0("del_", n),
label = "",
icon = shiny::icon("trash"),
onclick = 'Shiny.onInputChange(\"del_button\",
this.id)')))
l <- rbind(l, r)
}
l
}
#' Make table of subscriptions of reports
#'
#' Make a table to be rendered in a shiny app providing the active
#' subscriptions of a given user within a given registry which are
#' both collected from the shiny session object provided
#'
#' @param session A shiny session object
#' @param map_resh_name A data.table consisting og two columns,
#' Sykehusnavn and AvdRESH, providing a mapping between numeric
#' RESH and text label
#'
#' @return Matrix providing a table to be rendered in a shiny app
#' @importFrom magrittr "%>%"
#' @name makeUserSubscriptionTabV2
#' @aliases makeUserSubscriptionTab_v2 makeUserSubscriptionTabV2
NULL
#' @rdname makeUserSubscriptionTabV2
#' @export
makeUserSubscriptionTab_v2 <- function(session, map_resh_name = NULL) {
lifecycle::deprecate_warn("1.12.0", "rapbase::makeUserSubscriiptionTab_v2()",
"rapbase::makeUserSubscriiptionTabV2()")
. <- ""
l <- list()
autoRep <- readAutoReportData() %>%
selectByReg(., reg = getUserGroups(session)) %>%
selectByOwner(., owner = getUserName(session)) %>%
selectByOrganization(., organization = getUserReshId(session))
dateFormat <- "%A %e. %B %Y"
for (n in names(autoRep)) {
nextDate <- findNextRunDate(autoRep[[n]]$runDayOfYear,
returnFormat = dateFormat)
if (as.Date(nextDate, format = dateFormat) > autoRep[[n]]$terminateDate) {
nextDate <- "Utl\u00F8pt"
}
r <- list("Rapport" = autoRep[[n]]$synopsis,
"Periode" = autoRep[[n]]$intervalName,
"Utl\u00F8p" = strftime(as.Date(autoRep[[n]]$terminateDate),
format = "%b %Y"),
"Neste" = nextDate,
"Mottakere" = autoRep[[n]]$email,
"Avdeling" =
if ("reshID" %in% names(unlist(autoRep[[n]]$params))) {
unlist(autoRep[[n]]$params)[["reshID"]]
} else {
autoRep[[n]]$organization
}
,
"Slett" = as.character(
shiny::actionButton(inputId = paste0("del_", n),
label = "",
icon = shiny::icon("trash"),
onclick = 'Shiny.onInputChange(\"del_button\",
this.id)')))
l <- rbind(l, r)
}
if (!is.null(dim(l))) {
l <- as.data.frame(l, row.names = F)
l$Mottakere <- purrr::map_chr(l$Mottakere, function(x) {
paste0(x, collapse = "<br />")})
l$Avdeling <- purrr::map_chr(l$Avdeling, function(x) x)
if (!is.null(map_resh_name)) {
l$Avdeling <- map_resh_name$Sykehusnavn[match(as.numeric(l$Avdeling),
map_resh_name$AvdRESH)]
}
}
l
}
#' @rdname makeUserSubscriptionTabV2
#' @export
makeUserSubscriptionTabV2 <- function(session, map_resh_name = NULL) {
. <- ""
l <- list()
autoRep <- readAutoReportData() %>%
selectByReg(., reg = getUserGroups(session)) %>%
selectByOwner(., owner = getUserName(session)) %>%
selectByOrganization(., organization = getUserReshId(session))
dateFormat <- "%A %e. %B %Y"
for (n in names(autoRep)) {
nextDate <- findNextRunDate(autoRep[[n]]$runDayOfYear,
returnFormat = dateFormat)
if (as.Date(nextDate, format = dateFormat) > autoRep[[n]]$terminateDate) {
nextDate <- "Utl\u00F8pt"
}
r <- list("Rapport" = autoRep[[n]]$synopsis,
"Periode" = autoRep[[n]]$intervalName,
"Utl\u00F8p" = strftime(as.Date(autoRep[[n]]$terminateDate),
format = "%b %Y"),
"Neste" = nextDate,
"Mottakere" = autoRep[[n]]$email,
"Avdeling" =
if ("reshID" %in% names(unlist(autoRep[[n]]$params))) {
unlist(autoRep[[n]]$params)[["reshID"]]
} else {
autoRep[[n]]$organization
}
,
"Slett" = as.character(
shiny::actionButton(
inputId = paste0("del_", n),
label = "",
icon = shiny::icon("trash"),
onclick = 'Shiny.onInputChange(\"del_button\",
this.id)')))
l <- rbind(l, r)
}
if (!is.null(dim(l))) {
l <- as.data.frame(l, row.names = F)
l[["Mottakere"]] <- purrr::map_chr(l[["Mottakere"]], function(x) {
paste0(x, collapse = "<br />")})
l[["Avdeling"]] <- purrr::map_chr(l[["Avdeling"]], function(x) x)
if (!is.null(map_resh_name)) {
l[["Avdeling"]] <-
map_resh_name[["Sykehusnavn"]][match(as.numeric(l[["Avdeling"]]),
map_resh_name[["AvdRESH"]])]
}
}
l
}