add sqlite db for metadata build info
This commit is contained in:
parent
de2d7878e3
commit
fe99870a94
1 changed files with 307 additions and 40 deletions
155
R/build-metadata.R
Normal file
155
R/build-metadata.R
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
#' @importFrom DBI dbConnect dbDisconnect dbWriteTable dbGetQuery dbExecute
|
||||
#' @importFrom RSQLite SQLite
|
||||
store_build_metadata <- function(
|
||||
package_name, tag, platform,
|
||||
error_occurred, error) {
|
||||
# Create a new SQLite connection
|
||||
con <- dbConnect(RSQLite::SQLite(), dbname = "metadata.db")
|
||||
|
||||
dbExecute(con, "BEGIN TRANSACTION")
|
||||
|
||||
# Create the "metadata" table if it does not exist
|
||||
dbExecute(con, "CREATE TABLE IF NOT EXISTS metadata (
|
||||
package_name TEXT,
|
||||
tag TEXT,
|
||||
platform TEXT,
|
||||
error_occurred BOOLEAN,
|
||||
build_timestamp TEXT
|
||||
)")
|
||||
|
||||
# Create an index on the package_name and tag columns
|
||||
dbExecute(con, "CREATE INDEX IF NOT EXISTS idx_metadata_package_tag ON metadata(package_name, platform)")
|
||||
|
||||
# Check if an entry with the same package_name and tag already exists
|
||||
existing_entries <- dbGetQuery(con, paste0(
|
||||
"SELECT * FROM metadata WHERE package_name = '",
|
||||
package_name, "' AND tag = '", tag, "'"
|
||||
))
|
||||
|
||||
if (nrow(existing_entries) >= 1) {
|
||||
cli::cli_alert_info("{.fun store_build_metadata}: Build metadata for {.field {package_name}} {.field {tag}} already exists.")
|
||||
} else {
|
||||
cli::cli_alert_info("{.fun store_build_metadata}: Storing build metadata for {.field {package_name}} {.field {tag}}.")
|
||||
# Create a data frame with the metadata
|
||||
metadata <- data.frame(
|
||||
package_name = package_name,
|
||||
tag = tag,
|
||||
platform = platform,
|
||||
error_occurred = error_occurred,
|
||||
build_timestamp = format(Sys.time(), "%Y-%m-%d")
|
||||
)
|
||||
# Write the data frame to the SQLite database
|
||||
dbWriteTable(con, "metadata", metadata, append = TRUE)
|
||||
}
|
||||
|
||||
dbExecute(con, "COMMIT")
|
||||
|
||||
# Close the SQLite connection
|
||||
dbDisconnect(con)
|
||||
}
|
||||
|
||||
#' @importFrom DBI dbConnect dbGetQuery
|
||||
#' @importFrom RSQLite SQLite
|
||||
#' @importFrom dplyr filter_at vars all_vars
|
||||
query_build_metadata <- function(package_name = NULL, tag = NULL, platform = NULL,
|
||||
error_occurred = NULL, error = NULL, build_timestamp = NULL) {
|
||||
# Create a new SQLite connection
|
||||
con <- dbConnect(RSQLite::SQLite(), dbname = "metadata.db")
|
||||
|
||||
# Read the entire table
|
||||
metadata <- dbGetQuery(con, "SELECT * FROM metadata")
|
||||
|
||||
# Close the SQLite connection
|
||||
dbDisconnect(con)
|
||||
|
||||
# Filter the data frame based on the given parameters
|
||||
filter_conditions <- list(
|
||||
package_name = package_name, tag = tag, platform = platform,
|
||||
error_occurred = error_occurred, error = error,
|
||||
build_timestamp = build_timestamp
|
||||
)
|
||||
|
||||
# Remove NULL elements from the list
|
||||
filter_conditions <- filter_conditions[!sapply(filter_conditions, is.null)]
|
||||
|
||||
# Apply the filter conditions if any exist
|
||||
if (length(filter_conditions) > 0) {
|
||||
metadata <- dplyr::filter_at(
|
||||
metadata, dplyr::vars(names(filter_conditions)),
|
||||
dplyr::all_vars(. == filter_conditions[[.]])
|
||||
)
|
||||
}
|
||||
|
||||
return(metadata)
|
||||
}
|
||||
|
||||
|
||||
#' @importFrom DBI dbConnect dbDisconnect dbWriteTable dbGetQuery dbExecute dbListTables
|
||||
#' @importFrom RSQLite SQLite
|
||||
#' @importFrom dplyr group_by summarise filter n
|
||||
build_metadata_summary <- function(package_name, platform) {
|
||||
# Create a new SQLite connection
|
||||
con <- dbConnect(RSQLite::SQLite(), dbname = "metadata.db")
|
||||
|
||||
metadata <- dbGetQuery(con, paste0(
|
||||
"SELECT * FROM metadata WHERE package_name = '", package_name,
|
||||
"' AND platform = '", platform, "'"
|
||||
))
|
||||
|
||||
print(sprintf("Package: %s", package_name))
|
||||
print(sprintf("Platform: %s", 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)
|
||||
)
|
||||
|
||||
print("DEBUG")
|
||||
print(summary)
|
||||
|
||||
# Check if the metadata_summary table exists
|
||||
if ("metadata_summary" %in% dbListTables(con)) {
|
||||
# Update the row for the specified package
|
||||
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,
|
||||
" WHERE package_name = '", package_name,
|
||||
"' AND platform = '", platform, "'"
|
||||
))
|
||||
} else {
|
||||
# Write the summary to a new table in the SQLite database
|
||||
dbWriteTable(con, "metadata_summary", summary)
|
||||
}
|
||||
|
||||
# Close the SQLite connection
|
||||
dbDisconnect(con)
|
||||
}
|
||||
|
||||
#' @importFrom RSQLite SQLite
|
||||
query_metadata_summary <- function(package_name = NULL, platform = NULL) {
|
||||
# Create a new SQLite connection
|
||||
con <- dbConnect(RSQLite::SQLite(), dbname = "metadata.db")
|
||||
|
||||
# If both package_name and platform are NULL, return the full table
|
||||
if (is.null(package_name) && is.null(platform)) {
|
||||
summary <- dbGetQuery(con, "SELECT * FROM metadata_summary")
|
||||
} else {
|
||||
# Query the metadata_summary table for the specified package and platform
|
||||
summary <- dbGetQuery(con, paste0(
|
||||
"SELECT * FROM metadata_summary WHERE package_name = '",
|
||||
package_name, "' AND platform = '", platform, "'"
|
||||
))
|
||||
}
|
||||
|
||||
# Close the SQLite connection
|
||||
dbDisconnect(con)
|
||||
|
||||
return(summary)
|
||||
}
|
||||
Loading…
Reference in a new issue