diff --git a/DESCRIPTION b/DESCRIPTION index 4bf3a6a..f739244 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,24 +1,24 @@ Package: wmf Type: Package Title: R Code for Wikimedia Foundation Internal Usage -Version: 0.2.5 -Date: 2017-01-18 +Version: 0.2.6 +Date: 2017-01-23 Authors@R: c( person("Oliver", "Keyes", role = "aut"), person("Mikhail", "Popov", email = "mpopov@wikimedia.org", role = c("aut", "cre"))) Description: More about what it does (maybe more than one line). License: Uhhhhh LazyData: TRUE Imports: RMySQL, urltools, pwr, ggplot2, readr, ggthemes URL: https://phabricator.wikimedia.org/diffusion/1821/ BugReports: https://phabricator.wikimedia.org/maniphest/task/create/? projects=Discovery-Analysis Suggests: testthat RoxygenNote: 5.0.1 diff --git a/NEWS.md b/NEWS.md index 4600f02..a414f5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,33 +1,38 @@ +wmf 0.2.6 +========= +* Adds support for more MySQL config filenames since those vary between the different machines +* Smarter about choosing a config file + wmf 0.2.5 ========= * Fixes Hive query execution to remove messages/warnings. wmf 0.2.4 ========= * Ungroups grouped data frames when rewriting. See [T146422](https://phabricator.wikimedia.org/T146422) for more details. wmf 0.2.3 ========= * Fixes ggplot2 theme margin bug [discovered & fixed](https://github.com/wikimedia/wikimedia-discovery-wmf/pull/1) by Oliver Keyes. wmf 0.2.2 ========= * Updates `query_hive()` to support [JAR path overriding](https://wikitech.wikimedia.org/wiki/Analytics/Cluster/Hive/QueryUsingUDF#Testing_changes_to_existing_udf) * Updates the MySQL config file path so the package can now also be used on stat1003 * Updates maintainer contact info in README wmf 0.2.1 ========= * Adds a Contributor Code of Conduct wmf 0.2.0 ========= * Adds compatibility with RMySQL 0.9.4+ wmf 0.1.1 ========= * Fix a bug in global_query wmf 0.1.0 ========= Initial release diff --git a/R/mysql.R b/R/mysql.R index 510e4d5..0d47e46 100644 --- a/R/mysql.R +++ b/R/mysql.R @@ -1,160 +1,182 @@ RMySQL_version <- function() { # Returns 93 if the installed version of RMySQL is 0.9.3 return(as.numeric(paste0(unlist(packageVersion("RMySQL")), collapse = ""))) } # Ensure that we recognise and error on 0 rows stop_on_empty <- function(data){ if(nrow(data) == 0){ stop("No rows were returned from the database") } return(invisible()) } #'@title Work with MySQL databases #'@description Read from, write to, and check data from the MySQL databases and #' tables in the Wikimedia cluster. Assumes the presence of a validly #' formatted configuration file. #' #'@param query A SQL query. #' #'@param database The name of the database to query. #' #'@param con A MySQL connection returned by \code{mysql_connect}. #' Optional -- if not provided, a temporary connection will be opened up. #' #'@param table The name of a table to check for the existence of or create, #' depending on the function. #' #'@param ... Further arguments to pass to dbWriteTable. See ?dbWriteTable for more details. #' #'@name mysql #'@rdname mysql #'@importMethodsFrom RMySQL dbConnect #' #'@seealso \code{\link{hive_query}} or \code{\link{global_query}} #' #'@export mysql_connect <- function(database, default_file = NULL) { if (is.null(default_file)) { - default_file = "/etc/mysql/conf.d/analytics-research-client.cnf" - # there's also "/etc/mysql/conf.d/stats-research-client.cnf" + possible_cnfs <- c( + "analytics-research-client.cnf", # on stat1002 + "stats-research-client.cnf", # on stat1003 + "research-client.cnf" # on notebook1001 + ) + for (cnf in file.path("/etc/mysql/conf.d", possible_cnfs)) { + if (file.exists(cnf)) { + default_file <- cnf + break + } + } + if (is.null(default_file)) { + if (dir.exists("/etc/mysql/conf.d")) { + cnfs <- dir("/etc/mysql/conf.d", pattern = "*.cnf") + if (length(cnfs) == 0) { + stop("no credentials found in mysql conf dir") + } else { + warning("didn't find any of the specified credentials (", paste0(possible_cnfs, collapse = ", "), "), but going to try this one: ", cnfs[1]) + default_file <- cnfs[1] + } + } else { + stop("no configuration directory for mysql credentials") + } + } } if (RMySQL_version() > 93) { con <- dbConnect(drv = RMySQL::MySQL(), host = "analytics-store.eqiad.wmnet", dbname = database, default.file = default_file) } else { # Using version RMySQL 0.9.3 or older: con <- dbConnect(drv = "MySQL", host = "analytics-store.eqiad.wmnet", dbname = database, default.file = default_file) } return(con) } #'@rdname mysql #'@importMethodsFrom RMySQL dbSendQuery dbDisconnect dbListResults dbClearResult fetch #'@export mysql_read <- function(query, database, con = NULL) { already_connected <- !is.null(con) if (!already_connected) { # Open a temporary connection to the db: con <- mysql_connect(database) } to_fetch <- dbSendQuery(con, query) data <- fetch(to_fetch, -1) message(sprintf("Fetched %.0f rows and %.0f columns.", nrow(data), ncol(data))) dbClearResult(dbListResults(con)[[1]]) if (!already_connected) { # Close temporary connection: mysql_close(con) } stop_on_empty(data) return(data) } #'@rdname mysql #'@importMethodsFrom RMySQL dbExistsTable dbDisconnect #'@export mysql_exists <- function(database, table_name, con = NULL) { already_connected <- !is.null(con) if (!already_connected) { # Open a temporary connection to the db: con <- mysql_connect(database) } # Grab the results and close off: table_exists <- dbExistsTable(conn = con, name = table_name) if (!already_connected) { # Close temporary connection: mysql_close(con) } #Return return(table_exists) } #'@rdname mysql #'@importMethodsFrom RMySQL dbWriteTable dbDisconnect #'@export mysql_write <- function(x, database, table_name, con = NULL, ...){ already_connected <- !is.null(con) if (!already_connected) { # Open a temporary connection to the db: con <- mysql_connect(database) } # Write: result <- dbWriteTable(conn = con, name = table_name, value = x, row.names = FALSE, ...) if (!already_connected) { # Close temporary connection: mysql_close(con) } # Return the success/failure: return(result) } #'@rdname mysql #'@importMethodsFrom RMySQL dbDisconnect #'@export mysql_close <- function(con) { dbDisconnect(con) return(invisible()) } #'@rdname mysql #'@export mysql_disconnect <- function(con) { mysql_close(con) } #'@title Builds a MySQL query aimed at the EventLogging-centric formats #'@description constructs a MySQL query with a conditional around date. #'This is aimed at eventlogging, where the date/time is always "timestamp". #' #'@param fields the SELECT statement. #' #'@param table the table to use. #' #'@param date the date to restrict to. If NULL, yesterday will be used. #' #'@param any other conditionals to include in the WHERE statement. #' #'@export build_query <- function(fields, table, date = NULL, conditionals = NULL){ # Ensure we have a date and deconstruct it into a MW-friendly format if (is.null(date)) { date <- Sys.Date() - 1 } date <- gsub(x = date, pattern = "-", replacement = "") # Build the query proper (this will work for EL schemas where the field is always 'timestamp') query <- paste(fields, "FROM", table, "WHERE LEFT(timestamp,8) =", date, ifelse(is.null(conditionals), "", "AND"), conditionals) results <- mysql_read(query, "log") stop_on_empty(results) return(results) }