Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Created January 30, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rich-iannone/6a192a41b6e195c17fc103db37c9e3ae to your computer and use it in GitHub Desktop.
Save rich-iannone/6a192a41b6e195c17fc103db37c9e3ae to your computer and use it in GitHub Desktop.
A demonstration script that uses pointblank, logging, email notification, and a gt table report
library(pointblank)
library(log4r)
library(blastula)
# Create a connection to the game analytics data
# This is the `intendo` database
con <-
DBI::dbConnect(
drv = RMariaDB::MariaDB(),
dbname = Sys.getenv("DBNAME"),
username = Sys.getenv("USERNAME"),
password = Sys.getenv("PASSWORD"),
host = Sys.getenv("HOST"),
port = Sys.getenv("PORT")
)
# Create a log4r `logger` object
logger <- logger("WARN", appenders = file_appender("log_file"))
# Create custom logging functions with `log4r` to react to any
# `warn` or `stop` states. The `.vars_list` list is available
# in the context where the function will be evaluated
log4r_warn <- function(vl) {
log4r::warn(logger, glue::glue(
"Step {vl$i} exceeded the `warn` threshold (f_failed = {vl$f_failed}) ['{vl$name}']"
))
}
log4r_stop <- function(vl) {
log4r::warn(logger, glue::glue(
"Step {vl$i} exceeded the `stop` threshold (f_failed = {vl$f_failed}) ['{vl$name}']"
))
}
# Create an email notifier using `blastula`
email_blast <- function(vl) {
compose_email(
body = glue::glue(
"Validation step {vl$i} indicates some serious problems \\
(f_failed = {vl$f_failed}) ['{vl$name}']. Problem rows extracted."
)
) %>%
smtp_send(
to = "person_1@example.com",
from = "person_2@example.com",
subject = "Validation of the table has failed.",
credentials = creds_key(id = "gmail")
)
}
# Set failure thresholds and functions that are
# actioned from exceeding certain error levels
al <-
action_levels(
warn_at = 0.02, stop_at = 0.05, notify_at = 0.10,
fns = list(
warn = ~ log4r_warn(.vars_list),
stop = ~ log4r_stop(.vars_list),
notify = ~ email_blast(.vars_list)
)
)
# Validate the Intendo `users` table
agent <-
tbl(con, "users") %>%
create_agent(name = "Intendo: 'users' table") %>%
rows_distinct(columns = vars(user_id)) %>%
col_vals_between(
vars(first_login), left = "2015-01-01", right = "2015-12-30",
actions = al
) %>%
col_vals_null(
vars(ad_revenue),
preconditions = ~ tbl %>% filter(is.na(ad_count)),
actions = al
) %>%
col_vals_equal(
vars(customer), value = 1,
preconditions = ~ tbl %>% filter(!is.na(iap_count)),
actions = al
) %>%
col_vals_in_set(
vars(country), set = c(
"China", "Egypt", "France", "Germany", "Hong Kong", "India",
"Japan", "Mexico", "Norway", "Philippines", "Portugal",
"Russia", "South Africa", "South Korea", "Spain", "Sweden",
"Switzerland", "United Kingdom", "United States"),
actions = al
) %>%
col_vals_in_set(
vars(platform), set = c("apple", "android"),
actions = al
) %>%
col_vals_in_set(
vars(acquired), set = c("apple", "facebook", "organic"),
actions = al
) %>%
interrogate()
# This is the part where gt comes in
agent %>% get_agent_report()
# Read from the generated `log_file`
readLines("log_file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment