Skip to content

Commit 7d500c5

Browse files
author
hagan
committed
removed non-standard calendar support
1 parent de89bbe commit 7d500c5

17 files changed

+1370
-1983
lines changed

R/classes.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ datum <- S7::new_class(
100100
class=S7::class_character
101101
,default = "standard"
102102
,validator =function(value){
103-
104-
valid_names <- c("standard","544","445","454",NA_character_)
105-
106-
if(!any(value %in% valid_names)) return(cli::format_error("Please return {.or {.val {valid_names}}}"))
103+
valid_names <- c("standard", NA_character_)
104+
if (!all(value %in% valid_names)) {
105+
return(paste0("Must be one of: ", paste(valid_names, collapse = ", ")))
106+
}
107+
NULL
107108

108109
}
109110
,setter=function(self,value){

R/methods.R

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ create_calendar <- S7::new_generic("create_calendar","x")
77

88
calculate <- S7::new_generic("calculate","x")
99

10-
11-
12-
1310
#' Create Calendar Table
1411
#' @name create_calendar
1512
#' @param x ti object
@@ -28,69 +25,35 @@ calculate <- S7::new_generic("calculate","x")
2825
#' @keywords internal
2926
S7::method(create_calendar,ti) <- function(x){
3027

31-
# 1. Determine the Anchor Start Date --------------------------------------
32-
# For 5-5-4 calendars, we must anchor to the fiscal start (Sunday closest to Feb 1).
33-
# For standard calendars, we use the natural data minimum.
34-
35-
# 2. Summarize Raw Data ---------------------------------------------------
36-
# Aggregate the source data to the target time unit (day, month, etc.)
37-
# before building the scaffold.
38-
3928

4029
summary_dbi <- x@datum@data |>
4130
dplyr::ungroup() |>
4231
make_db_tbl() |>
4332
dplyr::mutate(
44-
date = lubridate::floor_date(!!x@datum@date_quo, unit = !!x@time_unit@value,week_start = 1),
33+
date = lubridate::floor_date(!!x@datum@date_quo, unit = !!x@time_unit@value,week_start = 7),
4534
time_unit = !!x@time_unit@value
4635
) |>
4736
dplyr::summarise(
4837
!!x@value@value_vec := sum(!!x@value@value_quo, na.rm = TRUE),
4938
.by = c(date, !!!x@datum@group_quo)
5039
)
5140

52-
53-
if (x@datum@calendar_type != "standard") {
54-
min_year <- lubridate::year(x@datum@min_date)
55-
start_date <- closest_sunday_feb1(min_year)
56-
57-
# Ensure the anchor doesn't start after our actual data
58-
if (min_year < lubridate::year(start_date)) {
59-
start_date <- closest_sunday_feb1(min_year - 1)
60-
}
61-
} else {
62-
start_date <- x@datum@min_date
63-
}
64-
65-
66-
67-
# 3. Define "Active Life" Bounds ------------------------------------------
68-
# Optimization: Calculate the first and last activity per group.
69-
# This prevents generating thousands of 'zero' rows for products that
70-
# didn't exist yet or were discontinued.
71-
7241
active_bounds <- summary_dbi |>
7342
dplyr::summarise(
7443
min_g = min(date, na.rm = TRUE),
7544
max_g = max(date, na.rm = TRUE),
7645
.by = c(!!!x@datum@group_quo)
7746
)
7847

79-
# 4. Generate Master Date Sequence ---------------------------------------
80-
# Create a single-column table of all possible dates in the range.
8148

8249
master_dates <- seq_date_sql(
83-
start_date = start_date,
50+
start_date = x@datum@min_date,
8451
end_date = x@datum@max_date,
8552
calendar_type = x@datum@calendar_type,
8653
time_unit = x@time_unit@value,
8754
.con = dbplyr::remote_con(x@datum@data)
8855
)
8956

90-
# 5. Build the Scaffolding ------------------------------------------------
91-
# If groups exist, expand the calendar. We use an inner join to the bounds
92-
# to constrain the cross-join to only the "Active Life" of each group.
93-
9457
if (x@datum@group_indicator) {
9558

9659
calendar_dbi <- master_dates |>
@@ -107,11 +70,6 @@ S7::method(create_calendar,ti) <- function(x){
10770
calendar_dbi <- master_dates
10871
}
10972

110-
# 6. Final Join & Gap Filling ---------------------------------------------
111-
# Combine the scaffold with actual data. Dates with no records are
112-
# flagged and filled with 0 to ensure continuous time intelligence.
113-
114-
11573

11674
full_dbi <-
11775
calendar_dbi |>
@@ -124,6 +82,7 @@ S7::method(create_calendar,ti) <- function(x){
12482
!!x@value@value_vec := dplyr::coalesce(!!x@value@value_quo, 0)
12583
)
12684

85+
12786
return(full_dbi)
12887

12988

@@ -327,7 +286,6 @@ S7::method(print,segment_abc) <- function(x,...){
327286
)
328287
)
329288

330-
331289
cli::cat_line("")
332290

333291
print_actions_steps(x)

R/time_intelligence_fn.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ ytdopy_fn <- function(x){
242242
# join together
243243

244244
out_dbi <- ytd_dbi |>
245-
select(
245+
dplyr::select(
246246
-c(!!x@value@value_quo)
247247
) |>
248248
dplyr::left_join(
@@ -681,7 +681,7 @@ mom_fn <- function(x){
681681

682682
# create lag
683683
lag_dbi <- full_dbi |>
684-
dplyr::select(-c(year,month)) |>
684+
dplyr::select(-c(year,quarter,month)) |>
685685
dbplyr::window_order(date,!!!x@datum@group_quo) |>
686686
dplyr::mutate(
687687
date_lag=dplyr::lead(date,n = !!x@fn@lag_n)
@@ -728,7 +728,7 @@ mtdopm_fn <- function(x){
728728
dplyr::group_by(!!!x@datum@group_quo) |>
729729
mtd(.data = _,.date = !!x@datum@date_quo,.value = !!x@value@value_quo,calendar_type = x@datum@calendar_type) |>
730730
calculate() |>
731-
select(
731+
dplyr::select(
732732
-c(missing_date_indicator,!!x@value@value_quo)
733733
)
734734

@@ -944,7 +944,7 @@ wow_fn <- function(x){
944944
full_dbi <- create_full_dbi(x)
945945

946946
lag_dbi <- full_dbi|>
947-
dplyr::select(-c(year,month,week)) |>
947+
dplyr::select(-c(year,quarter,month,week)) |>
948948
dbplyr::window_order(date) |>
949949
dplyr::mutate(
950950
date_lag=dplyr::lead(date,n = !!x@fn@lag_n)
@@ -1066,6 +1066,7 @@ dod_fn <- function(x){
10661066
full_dbi <- create_full_dbi(x)
10671067

10681068
lag_dbi <- full_dbi |>
1069+
dplyr::select(-c(year,quarter,month,week,day)) |>
10691070
dbplyr::window_order(date) |>
10701071
dplyr::mutate(
10711072
date_lag=dplyr::lead(date,n=!!x@fn@lag_n)

0 commit comments

Comments
 (0)