105 lines
4.5 KiB
R
105 lines
4.5 KiB
R
#' Stores build metadata of single binary builds in a Postgres DB
|
|
#' @importFrom DBI dbConnect dbDisconnect dbWriteTable dbGetQuery dbExecute
|
|
store_build_metadata <- function(
|
|
package_name, tag, platform, error_occurred, arch,
|
|
force = FALSE, error = NA, build_duration = NA, size = NA) {
|
|
con <- DBI::dbConnect(RPostgres::Postgres(),
|
|
dbname = "build_metadata", host = "r-binaries.devxy.io",
|
|
port = 15432, user = "r_binaries", password = Sys.getenv("PGPASS"),
|
|
sslmode = "require"
|
|
)
|
|
|
|
# Check if an entry with the same package_name and tag already exists
|
|
existing_entries <- dbGetQuery(con, paste0(
|
|
"SELECT * FROM single_builds WHERE name = '",
|
|
package_name, "' AND tag = '", tag, "' AND platform = '", arch, "' AND arch = '", platform, "'"
|
|
))
|
|
|
|
if (nrow(existing_entries) >= 1 && !force) {
|
|
cli::cli_alert("{.fun store_build_metadata}: Build metadata for {.field {.pkg {package_name}}} {.field {tag}} already exists.")
|
|
} else if (nrow(existing_entries) >= 1 && force) {
|
|
cli::cli_alert_info("{.fun store_build_metadata}: Force overwriting build metadata for {.pkg {package_name}} {.field {tag}} ({platform}) because {.code force = TRUE} was set.")
|
|
|
|
DBI::dbExecute(con, "UPDATE single_builds SET timestamp = $1, error_occurred = $2, error_text = $3, duration = $4, size = $5 WHERE name = $6 and tag = $7 and platform = $8",
|
|
params = list(format(Sys.time(), "%Y-%m-%d %H:%M:%S"), error_occurred, error, build_duration, size, package_name, tag, platform)
|
|
)
|
|
} else if (nrow(existing_entries) == 0) {
|
|
cli::cli_alert("{.fun store_build_metadata}: Storing build metadata for {.pkg {package_name}} {.field {tag}}.")
|
|
# if no entry exists already, we can insert the info via dbWriteTable by passing a DF
|
|
metadata <- data.frame(
|
|
name = package_name,
|
|
tag = tag,
|
|
platform = platform,
|
|
arch = arch,
|
|
error_occurred = error_occurred,
|
|
error_text = error,
|
|
size = size,
|
|
timestamp = format(Sys.time(), "%Y-%m-%d %H:%M:%S"),
|
|
duration = build_duration
|
|
)
|
|
# Write the data frame to the SQLite database
|
|
dbWriteTable(con, "single_builds", metadata, append = TRUE)
|
|
}
|
|
dbDisconnect(con)
|
|
|
|
return(invisible(TRUE))
|
|
}
|
|
|
|
#' @importFrom DBI dbConnect dbDisconnect dbWriteTable dbGetQuery dbExecute dbListTables
|
|
#' @importFrom dplyr group_by summarise filter n
|
|
build_metadata_summary <- function(package_name, platform) {
|
|
con <- DBI::dbConnect(RPostgres::Postgres(),
|
|
dbname = "build_metadata", host = "r-binaries.devxy.io",
|
|
port = 15432, user = "r_binaries", password = Sys.getenv("PGPASS")
|
|
)
|
|
|
|
metadata <- dbGetQuery(con, sprintf(
|
|
"SELECT * FROM single_builds WHERE name = '%s' AND platform = '%s'", package_name, platform
|
|
))
|
|
|
|
cli::cli_alert("{.fun build_binary_package}: Building/updating metadata summary for {.pkg {package_name[[1]]}}.")
|
|
|
|
# add average_build_time_per_tag to metadata summary table
|
|
|
|
# If the entry does not exist or is not valid, entry will be NULL or NA
|
|
# entry_exists <- !is.null(entry$average_build_time_per_tag) && !is.na(entry$average_build_time_per_tag)
|
|
|
|
# If the entry does not exist, update the average_build_time_per_tag
|
|
# if (!entry_exists) {
|
|
# dbExecute(con, paste0(
|
|
# "UPDATE metadata_summary SET average_build_time_per_tag = ", average_build_time_per_tag,
|
|
# " WHERE package_name = '", package_name[[1]], "' AND platform = '", platform, "'"
|
|
# ))
|
|
# }
|
|
|
|
# Calculate the summary
|
|
summary <- metadata %>%
|
|
group_by(package_name, platform) %>%
|
|
summarise(
|
|
successful_builds = sum(!error_occurred),
|
|
unsuccessful_builds = sum(error_occurred),
|
|
total_builds = n(),
|
|
percentage_successful_builds = round(successful_builds / total_builds * 100, 2),
|
|
average_build_time_per_tag = round(sum(as.numeric(build_duration)) / total_builds, 2)
|
|
)
|
|
|
|
# Check if the metadata_summary table exists
|
|
if ("metadata_summary" %in% dbListTables(con)) {
|
|
dbExecute(con, paste0(
|
|
"UPDATE metadata_summary SET successful_builds = ", summary$successful_builds,
|
|
", unsuccessful_builds = ", summary$unsuccessful_builds,
|
|
", total_builds = ", summary$total_builds,
|
|
", percentage_successful_builds = ", summary$percentage_successful_builds,
|
|
", average_build_time_per_tag = '", summary$average_build_time_per_tag,
|
|
"', package_name = '", package_name,
|
|
"', platform = '", platform, "'",
|
|
"', arch = '", platform, "'",
|
|
|
|
))
|
|
} else {
|
|
dbWriteTable(con, "metadata_summary", summary)
|
|
}
|
|
|
|
# Close the SQLite connection
|
|
dbDisconnect(con)
|
|
}
|