-
Notifications
You must be signed in to change notification settings - Fork 51
/
get_agent_x_list.R
360 lines (333 loc) · 11.9 KB
/
get_agent_x_list.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
#------------------------------------------------------------------------------#
#
# _ _ _ _ _
# (_) | | | | | | | |
# _ __ ___ _ _ __ | |_ | |__ | | __ _ _ __ | | __
# | '_ \ / _ \ | || '_ \ | __|| '_ \ | | / _` || '_ \ | |/ /
# | |_) || (_) || || | | || |_ | |_) || || (_| || | | || <
# | .__/ \___/ |_||_| |_| \__||_.__/ |_| \__,_||_| |_||_|\_\
# | |
# |_|
#
# This file is part of the 'rstudio/pointblank' project.
#
# Copyright (c) 2017-2024 pointblank authors
#
# For full copyright and license information, please look at
# https://rstudio.github.io/pointblank/LICENSE.html
#
#------------------------------------------------------------------------------#
#' Get the agent's **x-list**
#'
#' @description
#'
#' The agent's **x-list** is a record of information that the agent possesses at
#' any given time. The **x-list** will contain the most complete information
#' after an interrogation has taken place (before then, the data largely
#' reflects the validation plan). The **x-list** can be constrained to a
#' particular validation step (by supplying the step number to the `i`
#' argument), or, we can get the information for all validation steps by leaving
#' `i` unspecified. The **x-list** is indeed an R `list` object that contains a
#' veritable cornucopia of information.
#'
#' For an **x-list** obtained with `i` specified for a validation step, the
#' following components are available:
#' \itemize{
#' \item `time_start`: the time at which the interrogation began
#' (`POSIXct [0 or 1]`)
#' \item `time_end`: the time at which the interrogation ended
#' (`POSIXct [0 or 1]`)
#' \item `label`: the optional label given to the agent (`chr [1]`)
#' \item `tbl_name`: the name of the table object, if available (`chr [1]`)
#' \item `tbl_src`: the type of table used in the validation (`chr [1]`)
#' \item `tbl_src_details`: if the table is a database table, this provides
#' further details for the DB table (`chr [1]`)
#' \item `tbl`: the table object itself
#' \item `col_names`: the table's column names (`chr [ncol(tbl)]`)
#' \item `col_types`: the table's column types (`chr [ncol(tbl)]`)
#' \item `i`: the validation step index (`int [1]`)
#' \item `type`: the type of validation, value is validation function name
#' (`chr [1]`)
#' \item `columns`: the columns specified for the validation function
#' (`chr [variable length]`)
#' \item `values`: the values specified for the validation function
#' (`mixed types [variable length]`)
#' \item `briefs`: the brief for the validation step in the specified `lang`
#' (`chr [1]`)
#' \item `eval_error`, `eval_warning`: indicates whether the evaluation of the
#' step function, during interrogation, resulted in an error or a warning
#' (`lgl [1]`)
#' \item `capture_stack`: a list of captured errors or warnings during
#' step-function evaluation at interrogation time (`list [1]`)
#' \item `n`: the number of test units for the validation step (`num [1]`)
#' \item `n_passed`, `n_failed`: the number of passing and failing test units
#' for the validation step (`num [1]`)
#' \item `f_passed`: the fraction of passing test units for the validation step,
#' `n_passed` / `n` (`num [1]`)
#' \item `f_failed`: the fraction of failing test units for the validation step,
#' `n_failed` / `n` (`num [1]`)
#' \item `warn`, `stop`, `notify`: a logical value indicating whether the level
#' of failing test units caused the corresponding conditions to be entered
#' (`lgl [1]`)
#' \item `lang`: the two-letter language code that indicates which
#' language should be used for all briefs, the agent report, and the reporting
#' generated by the [scan_data()] function (`chr [1]`)
#' }
#'
#' If `i` is unspecified (i.e., not constrained to a specific validation step)
#' then certain length-one components in the **x-list** will be expanded to the
#' total number of validation steps (these are: `i`, `type`, `columns`,
#' `values`, `briefs`, `eval_error`, `eval_warning`, `capture_stack`, `n`,
#' `n_passed`, `n_failed`, `f_passed`, `f_failed`, `warn`, `stop`, and
#' `notify`). The **x-list** will also have additional components when `i` is
#' `NULL`, which are:
#' \itemize{
#' \item `report_object`: a **gt** table object, which is also presented as the
#' default print method for a `ptblank_agent`
#' \item `email_object`: a **blastula** `email_message` object with a default
#' set of components
#' \item `report_html`: the HTML source for the `report_object`, provided as
#' a length-one character vector
#' \item `report_html_small`: the HTML source for a narrower, more condensed
#' version of `report_object`, provided as a length-one character vector; The
#' HTML has inlined styles, making it more suitable for email message bodies
#' }
#'
#' @param agent *The pointblank agent object*
#'
#' `obj:<ptblank_agent>` // **required**
#'
#' A **pointblank** *agent* object that is commonly created through the use of
#' the [create_agent()] function.
#'
#' @param i *A validation step number*
#'
#' `scalar<integer>` // *default:* `NULL` (`optional`)
#'
#' The validation step number, which is assigned to each validation step in
#' the order of invocation. If `NULL` (the default), the **x-list** will
#' provide information for all validation steps. If a valid step number is
#' provided then **x-list** will have information pertaining only to that
#' step.
#'
#' @return An `x_list` object.
#'
#' @section Examples:
#'
#' Create a simple data frame with a column of numerical values.
#'
#' ```{r}
#' tbl <- dplyr::tibble(a = c(5, 7, 8, 5))
#'
#' tbl
#' ```
#'
#' Create an `action_levels()` list with fractional values for the `warn`,
#' `stop`, and `notify` states.
#'
#' ```r
#' al <-
#' action_levels(
#' warn_at = 0.2,
#' stop_at = 0.8,
#' notify_at = 0.345
#' )
#' ```
#'
#' Create an agent (giving it the `tbl` and the `al` objects), supply two
#' validation step functions, then interrogate.
#'
#' ```r
#' agent <-
#' create_agent(
#' tbl = tbl,
#' actions = al
#' ) %>%
#' col_vals_gt(columns = a, value = 7) %>%
#' col_is_numeric(columns = a) %>%
#' interrogate()
#' ```
#'
#' Get the `f_passed` component of the agent x-list.
#'
#' ```r
#' x <- get_agent_x_list(agent)
#'
#' x$f_passed
#' ```
#'
#' ```
#' #> [1] 0.25 1.00
#' ```
#'
#' @family Post-interrogation
#' @section Function ID:
#' 8-1
#'
#' @export
get_agent_x_list <- function(
agent,
i = NULL
) {
if (!is.null(i)) {
.warn <- agent$validation_set[[i, "warn"]]
.notify <- agent$validation_set[[i, "notify"]]
.stop <- agent$validation_set[[i, "stop"]]
.agent_label <- agent$label
.time_start <- agent$time_start
.time_end <- agent$time_end
.lang <- agent$lang
.tbl <- agent$tbl
.tbl_name <- agent$tbl_name
.tbl_src <- agent$tbl_src
.tbl_src_details <- agent$tbl_src_details
.col_names <- agent$col_names
.col_types <- agent$col_types
.i <- i
.type <- agent$validation_set[[i, "assertion_type"]]
.columns <- agent$validation_set[[i, "column"]] %>% unlist()
.values <- agent$validation_set[[i, "values"]] %>% unlist()
.label <- agent$validation_set[[i, "label"]]
.briefs <- agent$validation_set[[i, "brief"]]
.eval_error <- agent$validation_set[[i, "eval_error"]]
.eval_warning <- agent$validation_set[[i, "eval_warning"]]
.capture_stack <- agent$validation_set[[i, "capture_stack"]]
.n <- agent$validation_set[[i, "n"]]
.n_passed <- agent$validation_set[[i, "n_passed"]]
.n_failed <- agent$validation_set[[i, "n_failed"]]
.f_passed <- agent$validation_set[[i, "f_passed"]]
.f_failed <- agent$validation_set[[i, "f_failed"]]
x <-
list(
time_start = .time_start,
time_end = .time_end,
label = .agent_label,
tbl_name = .tbl_name,
tbl_src = .tbl_src,
tbl_src_details = .tbl_src_details,
tbl = .tbl,
col_names = .col_names,
col_types = .col_types,
i = .i,
type = .type,
columns = .columns,
values = .values,
label = .label,
briefs = .briefs,
eval_error = .eval_error,
eval_warning = .eval_warning,
capture_stack = .capture_stack,
n = .n,
n_passed = .n_passed,
n_failed = .n_failed,
f_passed = .f_passed,
f_failed = .f_failed,
warn = .warn,
stop = .stop,
notify = .notify,
lang = .lang
)
class(x) <- c("x_list_i", "x_list")
}
if (is.null(i)) {
.warn <- agent$validation_set$warn
.notify <- agent$validation_set$notify
.stop <- agent$validation_set$stop
.agent_label <- agent$label
.time_start <- agent$time_start
.time_end <- agent$time_end
.lang <- agent$lang
.tbl <- agent$tbl
.tbl_name <- agent$tbl_name
.tbl_src <- agent$tbl_src
.tbl_src_details <- agent$tbl_src_details
.col_names <- agent$col_names
.col_types <- agent$col_types
.i <- agent$validation_set$i
.type <- agent$validation_set$assertion_type
.columns <- agent$validation_set$column
.values <- agent$validation_set$values
.label <- agent$validation_set$label
.briefs <- agent$validation_set$brief
.eval_error <- agent$validation_set$eval_error
.eval_warning <- agent$validation_set$eval_warning
.capture_stack <- agent$validation_set$capture_stack
.n <- agent$validation_set$n
.n_passed <- agent$validation_set$n_passed
.n_failed <- agent$validation_set$n_failed
.f_passed <- agent$validation_set$f_passed
.f_failed <- agent$validation_set$f_failed
.validation_set <- agent$validation_set
.report_object <- agent %>% get_agent_report()
.report_object_small <- agent %>% get_agent_report(size = "small")
if (!is.null(.report_object)) {
.report_html <- gt::as_raw_html(.report_object, inline_css = FALSE)
} else {
.report_html <- NULL
}
if (!is.null(.report_object_small)) {
.report_html_small <-
gt::as_raw_html(.report_object_small, inline_css = TRUE)
} else {
.report_html_small <- NULL
}
if (length(.time_start) != 0) {
# Create a 'temporary' x-list just for the `stock_msg_body()`
# function used in the `blastula::compose_email()` call
x <-
list(
time_start = .time_start,
report_html_small = .report_html_small,
lang = .lang,
i = .i,
n = .n,
validation_set = .validation_set
)
.email_object <-
blastula::compose_email(
header = NULL,
body = glue::glue(stock_msg_body()) %>% gt::html(),
footer = glue::glue(stock_msg_footer()) %>% gt::html(),
)
} else {
.email_object <- NULL
}
x <-
list(
time_start = .time_start,
time_end = .time_end,
label = .agent_label,
tbl_name = .tbl_name,
tbl_src = .tbl_src,
tbl_src_details = .tbl_src_details,
tbl = .tbl,
col_names = .col_names,
col_types = .col_types,
i = .i,
type = .type,
columns = .columns,
values = .values,
label = .label,
briefs = .briefs,
eval_error = .eval_error,
eval_warning = .eval_warning,
capture_stack = .capture_stack,
n = .n,
n_passed = .n_passed,
n_failed = .n_failed,
f_passed = .f_passed,
f_failed = .f_failed,
warn = .warn,
stop = .stop,
notify = .notify,
lang = .lang,
validation_set = .validation_set,
report_object = .report_object,
email_object = .email_object,
report_html = .report_html,
report_html_small = .report_html_small
)
class(x) <- c("x_list_n", "x_list")
}
x
}