|
| 1 | +# ============================================================================= |
| 2 | +# EXPORTED FUNCTIONS |
| 3 | + |
1 | 4 | # ----------------------------------------------------------------------------- |
2 | 5 | #' Generate a data frame containing alert results |
3 | 6 | #' |
@@ -125,14 +128,6 @@ alertspec <- function(alert_rules, |
125 | 128 | class = "mantis_alertspec") |
126 | 129 | } |
127 | 130 |
|
128 | | -# ----------------------------------------------------------------------------- |
129 | | -#' Test if object is an alertspec object |
130 | | -#' |
131 | | -#' @param x object to test |
132 | | -#' @return Logical |
133 | | -#' @noRd |
134 | | -is_alertspec <- function(x) inherits(x, "mantis_alertspec") |
135 | | - |
136 | 131 |
|
137 | 132 | # ----------------------------------------------------------------------------- |
138 | 133 | #' Create set of alert rules |
@@ -201,63 +196,6 @@ alert_rules <- function(...) { |
201 | 196 | structure(ars, class = "mantis_alert_rules") |
202 | 197 | } |
203 | 198 |
|
204 | | -# ----------------------------------------------------------------------------- |
205 | | -#' Test if object is an alert_rules object |
206 | | -#' |
207 | | -#' @param x object to test |
208 | | -#' @return Logical |
209 | | -#' @noRd |
210 | | -is_alert_rules <- function(x) inherits(x, "mantis_alert_rules") |
211 | | - |
212 | | - |
213 | | -# ----------------------------------------------------------------------------- |
214 | | -#' Constructor for an alert_rule |
215 | | -#' |
216 | | -#' @param type type of rule |
217 | | -#' @param function_call expression to pass to `eval()`, that returns either `TRUE` or `FALSE`. |
218 | | -#' Return value of `TRUE` means alert result is "FAIL" |
219 | | -#' @param short_name a short computer-friendly name to uniquely identify the rule |
220 | | -#' @param description brief but user-friendly explanation of why the rule result is "FAIL" |
221 | | -#' @param items Named list with names corresponding to members of `item_cols`. List members are |
222 | | -#' character vectors of values contained in the named column that the rule should be applied to. |
223 | | -#' If `items = NULL` the rule will be applied to all items. See Details. |
224 | | -#' |
225 | | -#' @section Details: Use `items` to restrict the rule to be applied only to specified items. |
226 | | -#' `items` can either be NULL or a named list of character vectors. If `NULL`, the rule will be |
227 | | -#' applied to all items. If a named list, the names must match members of the `item_cols` |
228 | | -#' parameter in the `inputspec`, (as well as column names in the `df`), though can be a subset. |
229 | | -#' If an `item_col` is not named in the list, the rule will apply to all its members. If an |
230 | | -#' `item_col` is named in the list, the rule will only be applied when the `item_col`'s value is |
231 | | -#' contained in the corresponding character vector. When multiple `item_col`s are |
232 | | -#' specified, the rule will be applied only to items that satisfy all the conditions. |
233 | | -#' |
234 | | -#' @noRd |
235 | | -alert_rule <- function(type, |
236 | | - function_call, |
237 | | - short_name, |
238 | | - description, |
239 | | - items = NULL) { |
240 | | - |
241 | | - structure( |
242 | | - list( |
243 | | - type = type, |
244 | | - function_call = function_call, |
245 | | - short_name = short_name, |
246 | | - description = description, |
247 | | - items = items |
248 | | - ), |
249 | | - class = c(paste0("mantis_alert_rule_", type), "mantis_alert_rule") |
250 | | - ) |
251 | | -} |
252 | | - |
253 | | -# ----------------------------------------------------------------------------- |
254 | | -#' Test if object is an alert_rule object |
255 | | -#' |
256 | | -#' @param x object to test |
257 | | -#' @return Logical |
258 | | -#' @noRd |
259 | | -is_alert_rule <- function(x) inherits(x, "mantis_alert_rule") |
260 | | - |
261 | 199 |
|
262 | 200 | # ----------------------------------------------------------------------------- |
263 | 201 | #' Built-in alert rules |
@@ -285,6 +223,7 @@ is_alert_rule <- function(x) inherits(x, "mantis_alert_rule") |
285 | 223 | #' |
286 | 224 | NULL |
287 | 225 |
|
| 226 | + |
288 | 227 | # ----------------------------------------------------------------------------- |
289 | 228 | #' Test for missing values |
290 | 229 | #' |
@@ -351,6 +290,7 @@ alert_missing <- function(extent_type = "all", |
351 | 290 | ) |
352 | 291 | } |
353 | 292 |
|
| 293 | + |
354 | 294 | # ----------------------------------------------------------------------------- |
355 | 295 | #' Test for specific values |
356 | 296 | #' |
@@ -412,6 +352,7 @@ alert_equals <- function(extent_type = "all", |
412 | 352 | ) |
413 | 353 | } |
414 | 354 |
|
| 355 | + |
415 | 356 | # ----------------------------------------------------------------------------- |
416 | 357 | #' Test for values greater than a specific value |
417 | 358 | #' |
@@ -473,6 +414,7 @@ alert_above <- function(extent_type = "all", |
473 | 414 | ) |
474 | 415 | } |
475 | 416 |
|
| 417 | + |
476 | 418 | # ----------------------------------------------------------------------------- |
477 | 419 | #' Test for values less than a specific value |
478 | 420 | #' |
@@ -533,6 +475,7 @@ alert_below <- function(extent_type = "all", |
533 | 475 | ) |
534 | 476 | } |
535 | 477 |
|
| 478 | + |
536 | 479 | # ----------------------------------------------------------------------------- |
537 | 480 | #' Test for when there is a percentage increase in latest values |
538 | 481 | #' |
@@ -596,6 +539,7 @@ alert_difference_above_perc <- function(current_period, |
596 | 539 | ) |
597 | 540 | } |
598 | 541 |
|
| 542 | + |
599 | 543 | # ----------------------------------------------------------------------------- |
600 | 544 | #' Test for when there is a percentage drop in latest values |
601 | 545 | #' |
@@ -659,6 +603,7 @@ alert_difference_below_perc <- function(current_period, |
659 | 603 | ) |
660 | 604 | } |
661 | 605 |
|
| 606 | + |
662 | 607 | # ----------------------------------------------------------------------------- |
663 | 608 | #' Create a custom alert rule |
664 | 609 | #' |
@@ -722,6 +667,76 @@ alert_custom <- function(short_name, |
722 | 667 | } |
723 | 668 |
|
724 | 669 |
|
| 670 | +# ============================================================================= |
| 671 | +# INTERNAL FUNCTIONS |
| 672 | + |
| 673 | +# ----------------------------------------------------------------------------- |
| 674 | +#' Test if object is an alertspec object |
| 675 | +#' |
| 676 | +#' @param x object to test |
| 677 | +#' @return Logical |
| 678 | +#' @noRd |
| 679 | +is_alertspec <- function(x) inherits(x, "mantis_alertspec") |
| 680 | + |
| 681 | + |
| 682 | +# ----------------------------------------------------------------------------- |
| 683 | +#' Test if object is an alert_rules object |
| 684 | +#' |
| 685 | +#' @param x object to test |
| 686 | +#' @return Logical |
| 687 | +#' @noRd |
| 688 | +is_alert_rules <- function(x) inherits(x, "mantis_alert_rules") |
| 689 | + |
| 690 | + |
| 691 | +# ----------------------------------------------------------------------------- |
| 692 | +#' Constructor for an alert_rule |
| 693 | +#' |
| 694 | +#' @param type type of rule |
| 695 | +#' @param function_call expression to pass to `eval()`, that returns either `TRUE` or `FALSE`. |
| 696 | +#' Return value of `TRUE` means alert result is "FAIL" |
| 697 | +#' @param short_name a short computer-friendly name to uniquely identify the rule |
| 698 | +#' @param description brief but user-friendly explanation of why the rule result is "FAIL" |
| 699 | +#' @param items Named list with names corresponding to members of `item_cols`. List members are |
| 700 | +#' character vectors of values contained in the named column that the rule should be applied to. |
| 701 | +#' If `items = NULL` the rule will be applied to all items. See Details. |
| 702 | +#' |
| 703 | +#' @section Details: Use `items` to restrict the rule to be applied only to specified items. |
| 704 | +#' `items` can either be NULL or a named list of character vectors. If `NULL`, the rule will be |
| 705 | +#' applied to all items. If a named list, the names must match members of the `item_cols` |
| 706 | +#' parameter in the `inputspec`, (as well as column names in the `df`), though can be a subset. |
| 707 | +#' If an `item_col` is not named in the list, the rule will apply to all its members. If an |
| 708 | +#' `item_col` is named in the list, the rule will only be applied when the `item_col`'s value is |
| 709 | +#' contained in the corresponding character vector. When multiple `item_col`s are |
| 710 | +#' specified, the rule will be applied only to items that satisfy all the conditions. |
| 711 | +#' |
| 712 | +#' @noRd |
| 713 | +alert_rule <- function(type, |
| 714 | + function_call, |
| 715 | + short_name, |
| 716 | + description, |
| 717 | + items = NULL) { |
| 718 | + |
| 719 | + structure( |
| 720 | + list( |
| 721 | + type = type, |
| 722 | + function_call = function_call, |
| 723 | + short_name = short_name, |
| 724 | + description = description, |
| 725 | + items = items |
| 726 | + ), |
| 727 | + class = c(paste0("mantis_alert_rule_", type), "mantis_alert_rule") |
| 728 | + ) |
| 729 | +} |
| 730 | + |
| 731 | +# ----------------------------------------------------------------------------- |
| 732 | +#' Test if object is an alert_rule object |
| 733 | +#' |
| 734 | +#' @param x object to test |
| 735 | +#' @return Logical |
| 736 | +#' @noRd |
| 737 | +is_alert_rule <- function(x) inherits(x, "mantis_alert_rule") |
| 738 | + |
| 739 | + |
725 | 740 | # ----------------------------------------------------------------------------- |
726 | 741 | #' Run all alert rules and return results |
727 | 742 | #' |
@@ -761,6 +776,7 @@ run_alerts <- function(prepared_df, |
761 | 776 | dplyr::filter(alert_result %in% filter_results) |
762 | 777 | } |
763 | 778 |
|
| 779 | + |
764 | 780 | # ----------------------------------------------------------------------------- |
765 | 781 | #' Run single alert rule and return results |
766 | 782 | #' |
|
0 commit comments