Skip to content

Commit 4e1e128

Browse files
committed
Update safe outputs to extract timestamps and action type
1 parent bdcf072 commit 4e1e128

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

R/safe-outputs.R

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ safe_output.opal <- function(
6363
logs_from = logs_to - 24 * 60^2
6464
) {
6565
# local bindings
66-
`@timestamp` <- logger_name <- safe_people_id <- NULL
67-
ds_eval <- ds_symbol <- ds_table <- username <- NULL
66+
`@timestamp` <- logger_name <- safe_people_id <- username <- NULL
67+
ds_action <- ds_eval <- ds_function <- ds_symbol <- ds_table <- NULL
6868

6969
# create formatted versions of input dates
7070
logs_from_formatted <- ifelse(
@@ -196,15 +196,20 @@ safe_output.opal <- function(
196196
)
197197

198198
# extract list of functions executed
199-
## tables/symbols mapped - only records with ds_table
200-
userlogs_tbl_mappings <- userlogs_tbl |>
201-
dplyr::filter(!is.na(ds_table)) |>
202-
dplyr::distinct(ds_table, ds_symbol)
203-
## evaluated functions - only records with ds_eval
204-
userlogs_tbl_evaluations <- userlogs_tbl |>
205-
dplyr::filter(!is.na(ds_eval)) |>
206-
dplyr::distinct(username, ds_eval) |>
199+
## evaluated functions and tables/symbols mapped
200+
userlogs_tbl_maps_evals <- userlogs_tbl |>
201+
dplyr::filter(ds_action %in% c("ASSIGN", "AGGREGATE")) |>
202+
# create derived `ds_eval` when `ds_action` = 'ASSIGN'
207203
dplyr::mutate(
204+
ds_eval = dplyr::coalesce(
205+
ds_eval,
206+
paste0(ds_symbol, " <- opal[", ds_table, "]")
207+
)
208+
) |>
209+
dplyr::distinct(username, ds_action, ds_eval, ds_table, `@timestamp`) |>
210+
dplyr::mutate(
211+
# format timestamp
212+
`@timestamp` = format(`@timestamp`, '%Y-%m-%dT%H:%M:%S'),
208213
# extract function name from ds_eval
209214
ds_function = ds_eval |>
210215
gsub(pattern = "(?=\\().*$", replacement = "", perl = TRUE),
@@ -214,13 +219,19 @@ safe_output.opal <- function(
214219
gsub(pattern = "(?=\\)).*$", replacement = "", perl = TRUE) |>
215220
gsub(pattern = '"|\'', replacement = "", perl = TRUE) |>
216221
gsub(pattern = "(?=\\$).*", replacement = "", perl = TRUE),
222+
ds_symbol = ifelse(ds_symbol == ds_eval, NA, ds_symbol),
223+
ds_function = ifelse(ds_symbol == ds_eval, NA, ds_function),
217224
.before = 1
218225
) |>
219-
## verify that `ds_symbol` is a mapped object (`userlogs_tbl_mappings`)
220-
dplyr::filter(ds_symbol %in% userlogs_tbl_mappings$ds_symbol)
221-
## combine mappings and evaluated functions
222-
userlogs_tbl_maps_evals <- userlogs_tbl_mappings |>
223-
dplyr::left_join(userlogs_tbl_evaluations, by = "ds_symbol")
226+
dplyr::select(
227+
timestamp = `@timestamp`,
228+
ds_action,
229+
username,
230+
ds_eval,
231+
ds_function,
232+
ds_symbol,
233+
ds_table
234+
)
224235

225236
log_maps_filename <- paste0(Sys.Date(), "-dslogs-", user, "_mappings.csv")
226237

0 commit comments

Comments
 (0)