-
Notifications
You must be signed in to change notification settings - Fork 51
/
get_agent_report.R
2453 lines (2184 loc) · 72 KB
/
get_agent_report.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------#
#
# _ _ _ _ _
# (_) | | | | | | | |
# _ __ ___ _ _ __ | |_ | |__ | | __ _ _ __ | | __
# | '_ \ / _ \ | || '_ \ | __|| '_ \ | | / _` || '_ \ | |/ /
# | |_) || (_) || || | | || |_ | |_) || || (_| || | | || <
# | .__/ \___/ |_||_| |_| \__||_.__/ |_| \__,_||_| |_||_|\_\
# | |
# |_|
#
# 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 a summary report from an agent
#'
#' @description
#'
#' We can get an informative summary table from an agent by using the
#' `get_agent_report()` function. The table can be provided in two substantially
#' different forms: as a **gt** based display table (the default), or, as a
#' tibble. The amount of fields with intel is different depending on whether or
#' not the agent performed an interrogation (with the [interrogate()] function).
#' Basically, before [interrogate()] is called, the agent will contain just the
#' validation plan (however many rows it has depends on how many validation
#' functions were supplied a part of that plan). Post-interrogation, information
#' on the passing and failing test units is provided, along with indicators on
#' whether certain failure states were entered (provided they were set through
#' `actions`). The display table variant of the agent report, the default form,
#' will have the following columns:
#'
#' \itemize{
#' \item i (unlabeled): the validation step number.
#' \item STEP: the name of the validation function used for the validation step,
#' \item COLUMNS: the names of the target columns used in the validation step
#' (if applicable).
#' \item VALUES: the values used in the validation step, where applicable; this
#' could be as literal values, as column names, an expression, etc.
#' \item TBL: indicates whether any there were any changes to the target table
#' just prior to interrogation. A rightward arrow from a small circle indicates
#' that there was no mutation of the table. An arrow from a circle to a purple
#' square indicates that preconditions were used to modify the target table. An
#' arrow from a circle to a half-filled circle indicates that the target table
#' has been segmented.
#' \item EVAL: a symbol that denotes the success of interrogation evaluation
#' for each step. A checkmark indicates no issues with evaluation. A warning
#' sign indicates that a warning occurred during evaluation. An explosion symbol
#' indicates that evaluation failed due to an error. Hover over the symbol for
#' details on each condition.
#' \item UNITS: the total number of test units for the validation step
#' \item PASS: on top is the absolute number of *passing* test units and below
#' that is the fraction of *passing* test units over the total number of test
#' units.
#' \item FAIL: on top is the absolute number of *failing* test units and below
#' that is the fraction of *failing* test units over the total number of test
#' units.
#' \item W, S, N: indicators that show whether the `warn`, `stop`, or `notify`
#' states were entered; unset states appear as dashes, states that are set with
#' thresholds appear as unfilled circles when not entered and filled when
#' thresholds are exceeded (colors for W, S, and N are amber, red, and blue)
#' \item EXT: a column that provides buttons to download data extracts as CSV
#' files for row-based validation steps having **failing** test units. Buttons
#' only appear when there is data to collect.
#' }
#'
#' The small version of the display table (obtained using `size = "small"`)
#' omits the `COLUMNS`, `TBL`, and `EXT` columns. The width of the small table
#' is 575px; the standard table is 875px wide.
#'
#' The `ptblank_agent_report` can be exported to a standalone HTML document
#' with the [export_report()] function.
#'
#' If choosing to get a tibble (with `display_table = FALSE`), it will have the
#' following columns:
#'
#' \itemize{
#' \item i: the validation step number.
#' \item type: the name of the validation function used for the validation step.
#' \item columns: the names of the target columns used in the validation step
#' (if applicable).
#' \item values: the values used in the validation step, where applicable; for
#' a [conjointly()] validation step, this is a listing of all sub-validations.
#' \item precon: indicates whether any there are any preconditions to apply
#' before interrogation and, if so, the number of statements used.
#' \item active: a logical value that indicates whether a validation step is
#' set to `"active"` during an interrogation.
#' \item eval: a character value that denotes the success of interrogation
#' evaluation for each step. A value of `"OK"` indicates no issues with
#' evaluation. The `"WARNING"` value indicates a warning occurred during
#' evaluation. The `"ERROR"` VALUES indicates that evaluation failed due to an
#' error. With `"W+E"` both warnings and an error occurred during evaluation.
#' \item units: the total number of test units for the validation step.
#' \item n_pass: the number of *passing* test units.
#' \item f_pass: the fraction of *passing* test units.
#' \item W, S, N: logical value stating whether the `warn`, `stop`, or `notify`
#' states were entered. Will be `NA` for states that are unset.
#' \item extract: an integer value that indicates the number of rows available
#' in a data extract. Will be `NA` if no extract is available.
#' }
#'
#' @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 arrange_by *Method of arranging the report's table rows*
#'
#' `singl-kw:[i|severity]` // *default:* `"i"`
#'
#' A choice to arrange the report table rows by the validation step number
#' (`"i"`, the default), or, to arrange in descending order by severity of the
#' failure state (with `"severity"`).
#'
#' @param keep *Which table rows should be kept?*
#'
#' `singl-kw:[all|fail_states]` // *default:* `"all"`
#'
#' An option to keep `"all"` of the report's table rows (the
#' default), or, keep only those rows that reflect one or more
#' `"fail_states"`.
#'
#' @param display_table *Return a display-table report via gt*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' Should a display table be generated? If `TRUE`, and if the **gt** package
#' is installed, a display table for the report will be shown in the Viewer.
#' If `FALSE`, or if **gt** is not available, then a tibble will be returned.
#'
#' @param size *Size option for display-table report*
#'
#' `scalar<character>` // *default:* `"standard"`
#'
#' The size of the display table, which can be either `"standard"` (the
#' default) or `"small"`. This only applies to a display table (where
#' `display_table = TRUE`).
#'
#' @param title *Title customization options*
#'
#' `scalar<character>` // *default:* `":default:"`
#'
#' Options for customizing the title of the report. The default is the keyword
#' `":default:"` which produces generic title text that refers to the
#' **pointblank** package in the language governed by the `lang` option.
#' Another keyword option is `":tbl_name:"`, and that presents the name of the
#' table as the title for the report. If no title is wanted, then the
#' `":none:"` keyword option can be used. Aside from keyword options, text can
#' be provided for the title and `glue::glue()` calls can be used to construct
#' the text string. If providing text, it will be interpreted as Markdown text
#' and transformed internally to HTML. To circumvent such a transformation,
#' use text in [I()] to explicitly state that the supplied text should not be
#' transformed.
#'
#' @param lang *Reporting language*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' The language to use for automatic creation of briefs (short descriptions
#' for each validation step) and for the *agent report* (a summary table that
#' provides the validation plan and the results from the interrogation. By
#' default, `NULL` will create English (`"en"`) text. Other options include
#' French (`"fr"`), German (`"de"`), Italian (`"it"`), Spanish (`"es"`),
#' Portuguese (`"pt"`), Turkish (`"tr"`), Chinese (`"zh"`), Russian (`"ru"`),
#' Polish (`"pl"`), Danish (`"da"`), Swedish (`"sv"`), and Dutch (`"nl"`).
#' This `lang` option will override any previously set language setting (e.g.,
#' by the [create_agent()] call).
#'
#' @param locale *Locale for value formatting*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' An optional locale ID to use for formatting values in the
#' *agent report* summary table according the locale's rules. Examples include
#' `"en_US"` for English (United States) and `"fr_FR"` for French (France);
#' more simply, this can be a language identifier without a country
#' designation, like `"es"` for Spanish (Spain, same as `"es_ES"`). This
#' `locale` option will override any previously set locale value (e.g., by the
#' [create_agent()] call).
#'
#' @return A `ptblank_agent_report` object if `display_table = TRUE` or a tibble
#' if `display_table = FALSE`.
#'
#' @section Examples:
#'
#' For the example here, we'll use a simple table with a single numerical column
#' `a`.
#'
#' ```{r}
#' tbl <- dplyr::tibble(a = c(5, 7, 8, 5))
#'
#' tbl
#' ```
#'
#' Let's create an *agent* and validate that values in column `a` are always
#' greater than `4`.
#'
#' ```r
#' agent <-
#' create_agent(
#' tbl = tbl,
#' tbl_name = "small_table",
#' label = "An example."
#' ) %>%
#' col_vals_gt(columns = a, value = 4) %>%
#' interrogate()
#' ```
#'
#' We can get a tibble-based report from the agent by using `get_agent_report()`
#' with `display_table = FALSE`.
#'
#' ```r
#' agent %>% get_agent_report(display_table = FALSE)
#' ```
#'
#' \preformatted{## # A tibble: 1 × 14
#' ## i type columns values precon active eval units n_pass
#' ## <int> <chr> <chr> <chr> <chr> <lgl> <chr> <dbl> <dbl>
#' ## 1 1 col_va… a 4 NA TRUE OK 4 4
#' ## # … with 5 more variables: f_pass <dbl>, W <lgl>, S <lgl>,
#' ## # N <lgl>, extract <int>}
#'
#'
#'
#' The full-featured display-table-based report can be viewed by printing the
#' `agent` object, but, we can get a `"ptblank_agent_report"` object returned to
#' us when using `display_table = TRUE` (the default for `get_agent_report`).
#'
#' ```r
#' report <- get_agent_report(agent)
#'
#' report
#' ```
#'
#' \if{html}{
#' \out{
#' `r pb_get_image_tag(file = "man_get_agent_report_1.png")`
#' }
#' }
#'
#' What can you do with the `report` object? Print it at will wherever, and, it
#' can serve as an input to the [export_report()] function.
#'
#' However, the better reason to use `get_agent_report()` over just printing the
#' agent for display-table purposes is to make use of the different display
#' options.
#'
#' The agent report as a **gt** display table comes in two sizes: `"standard"`
#' (the default, 875px wide) and `"small"` (575px wide). Let's take a look at
#' the smaller-sized version of the report.
#'
#' ```r
#' small_report <-
#' get_agent_report(
#' agent = agent,
#' size = "small"
#' )
#'
#' small_report
#' ```
#'
#' \if{html}{
#' \out{
#' `r pb_get_image_tag(file = "man_get_agent_report_2.png")`
#' }
#' }
#'
#' We can use our own title by supplying it to the `title` argument, or, use
#' a special keyword like `":tbl_name:"` to get the table name (set in the
#' [create_agent()] call) as the title.
#'
#' ```r
#' report_title <- get_agent_report(agent, title = ":tbl_name:")
#'
#' report_title
#' ```
#'
#' \if{html}{
#' \out{
#' `r pb_get_image_tag(file = "man_get_agent_report_3.png")`
#' }
#' }
#'
#' There are more options! You can change the language of the display table with
#' the `lang` argument (this overrides the language set in [create_agent()]),
#' validation steps can be rearranged using the `arrange_by` argument, and we
#' can also apply some filtering with the `keep` argument in
#' `get_agent_report()`.
#'
#' @family Interrogate and Report
#' @section Function ID:
#' 6-2
#'
#' @export
get_agent_report <- function(
agent,
arrange_by = c("i", "severity"),
keep = c("all", "fail_states"),
display_table = TRUE,
size = "standard",
title = ":default:",
lang = NULL,
locale = NULL
) {
arrange_by <- match.arg(arrange_by)
keep <- match.arg(keep)
validation_set <- agent$validation_set
eval <-
validation_set %>%
dplyr::select(eval_error, eval_warning) %>%
dplyr::mutate(condition = dplyr::case_when(
!eval_error & !eval_warning ~ "OK",
eval_error & eval_warning ~ "W + E",
eval_error ~ "ERROR",
eval_warning ~ "WARNING"
)) %>%
dplyr::pull(condition)
columns <-
validation_set$column %>%
vapply(
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
if (is.null(x)) {
NA_character_
} else {
toString(unique(x))
}
}
)
values <-
validation_set$values %>%
vapply(
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
if (is.function(x)) {
x <- capture_function(x)
}
ifelse(
is.null(x),
NA_character_,
paste(gsub("~", "", x), collapse = ", ")
)
}
)
precon_count <-
validation_set$preconditions %>%
vapply(
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
ifelse(
is.null(x),
NA_character_,
as.character(length(rlang::as_function(x)))
)
}
)
if (!has_agent_intel(agent)) {
extract_count <- rep(NA, nrow(validation_set))
} else {
extract_count <-
as.character(validation_set[["i"]]) %in% names(agent$extracts)
extract_count[extract_count == FALSE] <- NA_integer_
suppressWarnings(
extract_count[!is.na(extract_count)] <-
vapply(
agent$extracts,
FUN.VALUE = integer(1),
USE.NAMES = FALSE,
FUN = nrow
)
)
}
report_tbl <-
dplyr::tibble(
i = validation_set$i,
type = validation_set$assertion_type,
columns = columns,
values = values,
precon = precon_count,
active = validation_set$eval_active,
eval = eval,
units = validation_set$n,
n_pass = validation_set$n_passed,
f_pass = validation_set$f_passed,
W = validation_set$warn,
S = validation_set$stop,
N = validation_set$notify,
extract = extract_count
)
report_tbl <-
report_tbl %>%
dplyr::mutate(
eval_pts = ifelse(eval != "OK", 10, 0),
N_pts = ifelse(!is.na(N) & N, 3, 0),
S_pts = ifelse(!is.na(S) & S, 2, 0),
W_pts = ifelse(!is.na(W) & W, 1, 0),
total_pts = eval_pts + N_pts + S_pts + W_pts
)
if (arrange_by == "severity") {
report_tbl <-
report_tbl %>%
dplyr::arrange(dplyr::desc(total_pts))
}
if (keep == "fail_states") {
report_tbl <- report_tbl %>% dplyr::filter(total_pts > 0)
}
report_tbl <-
report_tbl %>%
dplyr::select(-dplyr::ends_with("pts"))
if (!display_table) {
return(report_tbl)
}
#
# Generate a gt table if `display_table == TRUE`
#
# nocov start
validation_set <- validation_set[report_tbl$i, ]
eval <- eval[report_tbl$i]
extracts <-
agent$extracts[
as.character(
base::intersect(as.numeric(names(agent$extracts)), report_tbl$i)
)
]
step_indices <- seq_len(nrow(validation_set))
if (is.null(lang)) {
lang <- agent$lang
if (is.null(locale)) locale <- agent$locale
} else {
normalize_reporting_language(lang = lang)
# Set the `locale` to the `lang` value if `locale` isn't set
if (is.null(locale)) locale <- lang
}
assertion_type <- validation_set$assertion_type
briefs <- validation_set$brief
label <- validation_set$label
tbl_src <- agent$tbl_src
tbl_name <- agent$tbl_name
# Initialize a table for gt footnotes
footnotes_tbl <- initialize_footnotes_tbl()
# Generate the report title with the `title` option
title_text <-
process_title_text(
title = title,
tbl_name = tbl_name,
report_type = "agent",
lang = lang
)
# Generate agent label HTML
agent_label_styled <- create_agent_label_html(agent = agent)
# Generate table type HTML
table_type <-
create_table_type_html(
tbl_src = tbl_src,
tbl_name = tbl_name
)
# Generate action levels HTML
action_levels <- make_action_levels_html(agent = agent, locale = locale)
# Combine label, table type, and action levels into
# a table subtitle <div>
combined_subtitle <-
as.character(
htmltools::tagList(
htmltools::HTML(agent_label_styled),
htmltools::tags$div(
style = htmltools::css(
height = "25px",
`padding-top` = "10px"
),
htmltools::HTML(paste0(table_type, action_levels))
)
)
)
# Generate table execution start/end time (and duration)
# as a table source note
table_time <-
create_table_time_html(
time_start = agent$time_start,
time_end = agent$time_end,
size = size,
locale = locale
)
#
# Reformat the `type` column
#
type_upd <-
vapply(
step_indices,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
# Get the `assertion_type` as a string
assertion_str <- icon_name <- assertion_type[x]
# Append `()` to the `assertion_str`
assertion_str <- paste0(assertion_str, "()")
# Get the `label` as a string
label_str <- label[x]
# Get the `brief` as a string
brief_str <- briefs[x]
if (
assertion_type[x] == "serially" &&
!is.na(agent$validation_set[x, ]$eval_active) &&
agent$validation_set[x, ]$eval_active
) {
interrogation_notes <-
agent$validation_set[x, ]$interrogation_notes[[1]]
failed_testing <- interrogation_notes$failed_testing
final_validation_str <-
if (interrogation_notes$has_final_validation) "+V" else ""
assertion_str <-
as.character(
paste0(
htmltools::HTML(paste0(assertion_str, ": ")),
htmltools::tags$span(
style = htmltools::css(
`text-decoration-style` =
if (failed_testing) "solid" else NULL,
`text-decoration-line` =
if (failed_testing) "underline" else NULL,
`text-decoration-color` =
if (failed_testing) "red" else NULL,
`text-underline-position` =
if (failed_testing) "under" else NULL
),
htmltools::HTML(
paste0(interrogation_notes$total_test_steps, "T")
)
),
final_validation_str
)
)
}
# Obtain the number of characters contained in the assertion
# string; this is important for sizing components appropriately
assertion_type_nchar <- nchar(assertion_str)
# Declare the text size based on the length of `assertion_str`
# and also whether the report is of the standard size or `"small"`
text_size <- ifelse(assertion_type_nchar + 2 >= 20, 10, 11)
text_size <- ifelse(size == "small", 11, text_size)
if (size == "small") {
as.character(
htmltools::tags$code(
style = htmltools::css(
`font-size` = paste0(text_size, "px")
),
htmltools::HTML(paste0(" ", assertion_str))
)
)
} else {
if (!is.na(label_str)) {
as.character(
htmltools::tags$div(
`aria-label` = brief_str,
`data-balloon-pos` = "right",
style = htmltools::css(width = "fit-content"),
htmltools::tags$div(
style = htmltools::css(
float = "left",
width = "30px"
),
htmltools::HTML(add_icon_svg(icon = icon_name))
),
htmltools::tags$div(
style = htmltools::css(
`line-height` = "0.7em",
`margin-left` = "32px",
`padding-left` = "3px"
),
htmltools::tags$p(
style = htmltools::css(
margin = "0",
`padding-top` = "4px",
`font-size` = "9px"
),
htmltools::HTML(label_str)
),
htmltools::tags$p(
style = htmltools::css(margin = "0"),
htmltools::tags$code(
style = htmltools::css(`font-size` = "11px"),
htmltools::HTML(assertion_str)
)
)
)
)
)
} else {
as.character(
htmltools::tags$div(
`aria-label` = brief_str,
`data-balloon-pos` = "right",
style = htmltools::css(width = "fit-content"),
htmltools::HTML(add_icon_svg(icon = icon_name)),
htmltools::tags$code(
style = htmltools::css(`font-size` = paste0(text_size, "px")),
htmltools::HTML(paste0(" ", assertion_str))
)
)
)
}
}
}
)
#
# Reformat the `columns` column
#
columns_upd <-
vapply(
step_indices,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
# TODO: Display all columns used in conjointly case
# Get the `column` value
column_i <- validation_set$column[[x]]
# Get the `assertion_type` as a string
assertion_str <- assertion_type[x]
if (
assertion_str == "serially" &&
has_agent_intel(agent)
) {
if (
!is.na(agent$validation_set[x, ]$eval_active) &&
!agent$validation_set[x, ]$eval_active
) {
return(NA_character_)
}
interrogation_notes <-
agent$validation_set[x, ]$interrogation_notes[[1]]
if (
!interrogation_notes$has_final_validation &&
!interrogation_notes$failed_testing
) {
return(NA_character_)
} else if (
!interrogation_notes$has_final_validation &&
interrogation_notes$failed_testing
) {
# Case where `serially()` does not have a final validation
# and testing failed
# T -> T ->|
# Replace `column_i` with the value at the failing step
column_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$column[[1]]
} else if (
interrogation_notes$has_final_validation &&
interrogation_notes$failed_testing
) {
# Case where tests where unsuccessful and the final
# validation step was not reached
# T -> T ->| (V)
# Replace `column_i` with the value at the failing step
column_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$column[[1]]
} else if (
interrogation_notes$has_final_validation &&
!interrogation_notes$failed_testing
) {
# Case where all tests passed and the final
# validation step was reached
# Replace `column_i` with the value at the final step (validation)
column_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$column[[1]]
}
}
# If column missing
if (is.null(column_i) || identical(unlist(column_i), NA_character_)) {
columns_expr <- validation_set$columns_expr[[x]]
not_interrogated <- is.na(validation_set$eval_error[[x]])
eval_error <- isTRUE(validation_set$eval_error[[x]])
# If column selection attempted AND:
# - in validation planning, OR
# - the evaluation errors, OR
# - is a col_exists() step
columns_expr_exists <- !is.na(columns_expr) && columns_expr != "NULL"
show_column_expr <- columns_expr_exists &&
(not_interrogated || eval_error || assertion_str == "col_exists")
# Then display the original column selection expression for debugging
if (show_column_expr) {
as.character(
htmltools::tags$p(
title = columns_expr,
style = htmltools::css(
`margin-top` = "0",
`margin-bottom` = "0",
`font-family` = "monospace",
`font-size` = "10px",
`white-space` = "nowrap",
`text-overflow` = "ellipsis",
overflow = "hidden",
color = if (eval_error) "firebrick",
`font-face` = "maroon"
),
columns_expr
)
)
} else {
NA_character_
}
} else {
text <-
column_i %>%
unlist() %>%
strsplit(", ") %>%
unlist()
title <- text
text_fragments <-
paste0(
htmltools::tags$span(
style = htmltools::css(color = "purple"),
htmltools::HTML("▮")
),
text,
collapse = ", "
)
if (size == "small") {
as.character(
htmltools::tags$div(
htmltools::tags$p(
title = paste(title, collapse = ", "),
style = htmltools::css(
`margin-top` = "0",
`margin-bottom` = "0",
`font-family` = "monospace",
`white-space` = "nowrap",
`text-overflow` = "ellipsis",
overflow = "hidden"
),
htmltools::HTML(text_fragments)
)
)
)
} else {
as.character(
htmltools::tags$div(
`aria-label` = paste(title, collapse = ", "),
`data-balloon-pos` = "left",
htmltools::tags$p(
style = htmltools::css(
`margin-top` = "0",
`margin-bottom` = "0",
`font-size` = "11px",
`white-space` = "nowrap",
`text-overflow` = "ellipsis",
overflow = "hidden",
`line-height` = "2em"
),
htmltools::tags$code(htmltools::HTML(text_fragments))
)
)
)
}
}
}
)
#
# Reformat the `values` column
#
values_upd <-
vapply(
step_indices,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
# Get the `values` value
values_i <- validation_set$values[[x]]
# Get the `assertion_type` as a string
assertion_str <- assertion_type[x]
if (assertion_str == "specially") {
values_i <- capture_function(values_i)
}
# In the `serially()` step, there are two possibilities for what
# should be displayed in the values column
# [1] has final validation: show the values for validation step
# [2] has no final validation and all tests passed: display the
# number of tests performed
# [3] has no final validation and a test failed: show the values
# for the failing test step
if (assertion_str == "serially") {
if (
!has_agent_intel(agent) ||
!agent$validation_set[x, ]$eval_active
) {
# TODO: Get the exact number of test steps rather than
# getting the number of expressions (each expr could
# expand to multiple steps)
step_text <-
paste0(
length(values_i), " ",
get_lsv(
paste0(
"agent_report/report_col_step",
ifelse(length(values_i) > 1, "s", "")
)
)[[lang]]
)
return(
paste0(
"<div><p style=\"margin-top: 0px; margin-bottom: 0px; ",
"font-size: 0.75rem;\">", step_text, "</p></div>"
)
)
}
interrogation_notes <-
agent$validation_set[x, ]$interrogation_notes[[1]]
if (
!interrogation_notes$has_final_validation &&
!interrogation_notes$failed_testing
) {
# Case where `serially()` does not have a final validation
# but all tests passed
total_test_steps <- interrogation_notes$total_test_steps
# TODO: change localized string to be: `x TESTS`
step_text <-
paste0(
total_test_steps, " ",
get_lsv(
paste0(
"agent_report/report_col_step",
ifelse(total_test_steps > 1, "s", "")
)
)[[lang]]
)
return(
paste0(
"<div><p style=\"margin-top: 0px; margin-bottom: 0px; ",
"font-size: 0.75rem;\">", step_text, "</p></div>"
)
)
} else if (
!interrogation_notes$has_final_validation &&
interrogation_notes$failed_testing
) {
# Case where `serially()` does not have a final validation
# and testing failed
# T -> T ->|
# Replace `values_i` with the value at the failing step
values_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$values[[1]]
} else if (
interrogation_notes$has_final_validation &&
interrogation_notes$failed_testing
) {
# Case where tests where unsuccessful and the final
# validation step was not reached
# T -> T ->| (V)
# Replace `values_i` with the value at the failing step
values_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$values[[1]]
} else if (
interrogation_notes$has_final_validation &&
!interrogation_notes$failed_testing
) {
# Case where all tests passed and the final
# validation step was reached
# Replace `values_i` with the value at the final step (validation)
values_i <-
interrogation_notes$testing_validation_set[
nrow(interrogation_notes$testing_validation_set), ]$values[[1]]
}
}
if (assertion_str %in% c("row_count_match", "col_count_match")) {
if (!is.numeric(values_i)) {
return(
paste0(
"<div><p style=\"margin-top: 0px; margin-bottom: 0px; ",
"font-size: 0.75rem;\">EXTERNAL TABLE</p></div>"
)
)
}
}