Skip to content

Commit

Permalink
Gjør runAutoReport uavhengig av runDayOfYear (#201)
Browse files Browse the repository at this point in the history
Fjerner avhengighet til pakkene "tibble" og "magrittr", legger til
"timeplyr". Gjør runAutoReport uavhengig av runDayOfYear.

---------

Co-authored-by: arnfinn <arnfinn.steindal@gmail.com>
  • Loading branch information
kevinthon and arnfinn authored Dec 20, 2024
1 parent 620308c commit 804cc84
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 57 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Imports:
kableExtra,
knitr,
logger,
magrittr,
readr,
rlang,
RMariaDB,
Expand All @@ -49,7 +48,7 @@ Imports:
shiny,
shinyalert,
sship (>= 0.9.0),
tibble,
timeplyr,
utils,
yaml
RoxygenNote: 7.3.2
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export(upgradeAutoReportData)
export(userAttribute)
export(userInfo)
export(writeAutoReportData)
importFrom(magrittr,"%>%")
importFrom(dplyr,"%>%")
importFrom(rlang,.data)
importFrom(utils,Sweave)
importFrom(utils,write.table)
32 changes: 18 additions & 14 deletions R/autoReport.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ writeAutoReportData <- function(fileName = "autoReport.yml", config,
dplyr::mutate(dplyr::across(dplyr::everything(), as.character))
for (element in config) {
for (email in element$email) {
dataframe <- dataframe |> tibble::add_row(
dataframe <- dataframe |> dplyr::add_row(
id = digest::digest(
paste0(
email,
Expand Down Expand Up @@ -512,6 +512,8 @@ getRegs <- function(config) {
#' @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
#' @param dato Date-class date when report will be run first time. Default value
#' is set to \code{Sys.Date()}
#'
#' @return Emails with corresponding file attachment. If dryRun == TRUE just a
#' message
Expand All @@ -525,6 +527,7 @@ getRegs <- function(config) {
#'

runAutoReport <- function(dayNumber = as.POSIXlt(Sys.Date())$yday + 1,
dato = Sys.Date(),
type = c("subscription", "dispatchment"),
target = "file", dryRun = FALSE) {

Expand All @@ -547,25 +550,26 @@ runAutoReport <- function(dayNumber = as.POSIXlt(Sys.Date())$yday + 1,
{
if (target == "db") {
rep <- reps[i, ]
runDayOfYear <- as.vector(
as.integer(
strsplit(
rep$runDayOfYear,
","
)[[1]]
)
)
params <- jsonlite::fromJSON(rep$params)
} else {
rep <- reps[[i]]
runDayOfYear <- rep$runDayOfYear
params <- rep$params
}
if (
dayNumber %in% runDayOfYear
if ((
target == "file"
&& dayNumber %in% rep$runDayOfYear
&& as.Date(rep$terminateDate) > Sys.Date()
&& as.Date(rep$startDate) <= Sys.Date()
) {
) || (
target == "db"
&& as.Date(rep$startDate) <= dato
&& as.Date(rep$terminateDate) > dato
&& dato %in% timeplyr::time_seq(
as.Date(rep$startDate),
dato,
time_by = rep$interval
) # 'days', 'weeks', 'months', 'years',
)) {
# get explicit referenced function and call it
f <- .getFun(paste0(rep$package, "::", rep$fun))
content <- do.call(what = f, args = params)
Expand Down Expand Up @@ -790,7 +794,7 @@ findNextRunDate <- function(runDayOfYear,
#' @param target autoreport-list in file or database
#'
#' @return Matrix providing a table to be rendered in a shiny app
#' @importFrom magrittr "%>%"
#' @importFrom dplyr "%>%"
#' @export
# nolint end

Expand Down
22 changes: 4 additions & 18 deletions R/moduleAutoReport.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,8 @@ autoReportServer <- function(id, registryName, type, org = NULL,
as.character(shiny::icon("calendar")), "F\u00F8rste utsending:"
)
),
# if freq is year make first issue tomorrow, otherwise postpone by freq
value = if (strsplit(input$freq, "-")[[1]][2] == "year") {
Sys.Date() + 1
} else {
seq.Date(Sys.Date(),
by = strsplit(input$freq, "-")[[1]][2],
length.out = 2
)[2]
},
# set the following day as default
value = Sys.Date() + 1,
min = Sys.Date() + 1,
max = seq.Date(Sys.Date(), length.out = 2, by = "1 years")[2] - 1
)
Expand Down Expand Up @@ -799,15 +792,8 @@ autoReportServer2 <- function(
as.character(shiny::icon("calendar")), "F\u00F8rste utsending:"
)
),
# if freq is year make first issue tomorrow, otherwise postpone by freq
value = if (strsplit(input$freq, "-")[[1]][2] == "year") {
Sys.Date() + 1
} else {
seq.Date(Sys.Date(),
by = strsplit(input$freq, "-")[[1]][2],
length.out = 2
)[2]
},
# set default to following day
value = Sys.Date() + 1,
min = Sys.Date() + 1,
max = seq.Date(Sys.Date(), length.out = 2, by = "1 years")[2] - 1
)
Expand Down
6 changes: 1 addition & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @importFrom dplyr %>%
#' @usage lhs \%>\% rhs
NULL

#' Data pronouns
#'
#' See \code{rlang::\link[rlang:tidyeval-data]{.data}} for details
#'
#' @name .data
#' @rdname pronoun
#' @keywords internal
Expand Down
18 changes: 9 additions & 9 deletions inst/autoReport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ testAutoReportFirst:
- Jesus <jesus@sky.com>
organization: '999999'
startDate: '1900-01-01'
terminateDate: '9999-12-31'
interval:
terminateDate: '2099-12-31'
interval: months
intervalName:
runDayOfYear:
- 30
Expand All @@ -34,8 +34,8 @@ testAutoReportSecond:
email: <someone@nowhere.com>
organization: '999999'
startDate: '1900-01-01'
terminateDate: '9999-12-31'
interval:
terminateDate: '2099-12-31'
interval: weeks
intervalName:
runDayOfYear:
- 30
Expand All @@ -58,8 +58,8 @@ testAutoReportThird:
- jesus@sky.com
organization: '999999'
startDate: '1900-01-01'
terminateDate: '9999-12-31'
interval:
terminateDate: '2099-12-31'
interval: months
intervalName:
runDayOfYear:
- 30
Expand All @@ -78,8 +78,8 @@ testAutoReportThird:
email: tester@skde.no
organization: '999999'
startDate: '1900-01-01'
terminateDate: '9999-12-31'
interval:
terminateDate: '2099-12-31'
interval: years
intervalName:
runDayOfYear: 170.0
cd467ac14dd848b8798a384a3e51512c:
Expand All @@ -96,6 +96,6 @@ cd467ac14dd848b8798a384a3e51512c:
organization: '999999'
startDate: '1900-01-01'
terminateDate: '0000-01-01'
interval:
interval: years
intervalName:
runDayOfYear: 171.0
2 changes: 1 addition & 1 deletion man/pipe.Rd

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

2 changes: 1 addition & 1 deletion man/pronoun.Rd

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

4 changes: 4 additions & 0 deletions man/runAutoReport.Rd

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

65 changes: 59 additions & 6 deletions tests/testthat/test-auto-report-db.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,79 @@ test_that("sample auto report data can be read from db", {
expect_equal(nrow(readAutoReportData(target = "db")), 14)
})

# For a valid test make sure there is ONE standard dummy report scheduled for
# day 90
test_that("Auto reports can be processed (shipment by email not tested)", {
# For a valid test make sure there is ONE standard dummy report scheduled
# monthly and with start date first of some month.
# 1 January 1900 is a Monday
# 1 December 2024 is a Sunday, but given number of months from start date
test_that("Auto reports can be processed monthly", {
check_db()
expect_message(runAutoReport(dayNumber = 90, dryRun = TRUE, target = "db"),
expect_message(runAutoReport(
dato = "2024-12-01",
dryRun = TRUE,
target = "db"
),
"No emails sent. Content is:",
all = FALSE
)
})

# 1 January 1900 is a Monday
# 2 December 2024 is a Monday
test_that("Auto reports can be processed weekly", {
check_db()
expect_message(runAutoReport(
dato = "2024-12-02",
dryRun = TRUE,
target = "db"
),
"No emails sent. Content is:",
all = FALSE
)
})

# Do the same for a bulletin, above conditions also apply!
test_that("Auto reports can be processed (shipment by email not tested)", {
test_that("Bulletin reports can be processed (monthly)", {
check_db()
expect_message(
runAutoReport(dayNumber = 90, type = c("bulletin"), dryRun = TRUE, target = "db"),
runAutoReport(
dato = "2024-12-01",
type = c("bulletin"),
dryRun = TRUE,
target = "db"
),
"No emails sent. Content is: This is a simple",
all = FALSE
)
})

test_that("Auto reports not sent because of no reports this date", {
check_db()
expect_silent(runAutoReport(
dato = "2024-12-03",
dryRun = TRUE,
target = "db"
))
})

test_that("Auto reports not sent if before start date", {
check_db()
expect_silent(runAutoReport(
dato = "1800-01-01",
dryRun = TRUE,
target = "db"
))
})

test_that("Auto reports not sent if after start date", {
check_db()
expect_silent(runAutoReport(
dato = "3000-01-01",
dryRun = TRUE,
target = "db"
))
})


# Test autoReportServer2 with db.
# Tests are copied from test-moduleAutoReport.R since the current file
# is ready for db.
Expand Down

0 comments on commit 804cc84

Please sign in to comment.