Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create function for exporting dqdashboard_results table to a json file #497

Merged
merged 3 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions R/writeDBResultsTo.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#' @param connectionDetails A connectionDetails object for connecting to the CDM database
#' @param resultsDatabaseSchema The fully qualified database name of the results schema
#' @param cdmDatabaseSchema The fully qualified database name of the CDM schema
#' @param writeTableName Name of table in the database to write results to
#' @param outputFolder The output folder
#' @param outputFile The output filename
#' @param writeTableName Name of DQD results table in the database to read from
#' @param outputFolder The folder to output the json results file to
#' @param outputFile The output filename of the json results file
#'
#' @export
#'
Expand All @@ -36,43 +36,27 @@ writeDBResultsToJson <- function(connection,
outputFile) {
startTime <- Sys.time()
Copy link
Collaborator

@katy-sadowski katy-sadowski Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove start and end time, and the delta variable now we're not using it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


sql <- SqlRender::render(
metadata <- DatabaseConnector::renderTranslateQuerySql(
connection,
sql = "select * from @cdmDatabaseSchema.cdm_source;",
cdmDatabaseSchema = cdmDatabaseSchema
cdmDatabaseSchema = cdmDatabaseSchema,
targetDialect = connectionDetails$dbms,
snakeCaseToCamelCase = TRUE
)

sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms
)

metadata <- DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = TRUE
)

sql <- SqlRender::render(
checkResults <- DatabaseConnector::renderTranslateQuerySql(
connection,
sql = "select * from @resultsDatabaseSchema.@writeTableName;",
resultsDatabaseSchema = resultsDatabaseSchema,
writeTableName = writeTableName
)

sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms
)

checkResults <- DatabaseConnector::querySql(
connection,
sql,
snakeCaseToCamelCase = TRUE
writeTableName = writeTableName,
targetDialect = connectionDetails$dbms,
snakeCaseToCamelCase = TRUE
)

# Quick patch for missing value issues related to SQL Only Implementation
checkResults["error"][checkResults["error"] == ''] <- NA
checkResults["warning"][checkResults["warning"] == ''] <- NA
checkResults["executionTime"][checkResults["executionTime"] == ''] <- '0.1 secs'
checkResults["executionTime"][checkResults["executionTime"] == ''] <- '0 secs'
checkResults["queryText"][checkResults["queryText"] == ''] <- '[Generated via SQL Only]'

overview <- .summarizeResults(
Expand All @@ -89,7 +73,7 @@ writeDBResultsToJson <- function(connection,
allResults <- list(
startTimestamp = Sys.time(),
endTimestamp = Sys.time(),
executionTime = sprintf("%.0f %s", delta, attr(delta, "units")),
executionTime = '0 secs',
CheckResults = checkResults,
Metadata = metadata,
Overview = overview
Expand Down
36 changes: 24 additions & 12 deletions tests/testthat/test-writeDBResultsTo.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ test_that("Write DB results to json", {
connectionDetailsEunomia <- Eunomia::getEunomiaConnectionDetails()
cdmDatabaseSchemaEunomia <- "main"
resultsDatabaseSchemaEunomia <- "main"
writeTableName <- "dqdashboard_results"

results <- DataQualityDashboard::executeDqChecks(
connectionDetails = connectionDetailsEunomia,
cdmDatabaseSchema = cdmDatabaseSchemaEunomia,
resultsDatabaseSchema = resultsDatabaseSchemaEunomia,
cdmSourceName = "Eunomia",
checkNames = "measurePersonCompleteness",
outputFolder = outputFolder,
writeToTable = TRUE,
writeTableName = "dqdashboard_results"
connectionDetails = connectionDetailsEunomia,
cdmDatabaseSchema = cdmDatabaseSchemaEunomia,
resultsDatabaseSchema = resultsDatabaseSchemaEunomia,
cdmSourceName = "Eunomia",
checkNames = "measurePersonCompleteness",
outputFolder = outputFolder,
writeToTable = TRUE,
writeTableName = writeTableName
)


connection <- DatabaseConnector::connect(connectionDetailsEunomia)
tableNames <- DatabaseConnector::getTableNames(connection = connection, databaseSchema = resultsDatabaseSchemaEunomia)
expect_true("dqdashboard_results" %in% tolower(tableNames))

testExportFile <- "dq-result-test.json"

Expand All @@ -30,13 +29,26 @@ test_that("Write DB results to json", {
connectionDetailsEunomia,
resultsDatabaseSchemaEunomia,
cdmDatabaseSchemaEunomia,
"dqdashboard_results",
writeTableName,
outputFolder,
testExportFile
)
)

on.exit(DatabaseConnector::disconnect(connection), add = TRUE)

# Check that file was exported properly
expect_true(file.exists(file.path(outputFolder,testExportFile)))
katy-sadowski marked this conversation as resolved.
Show resolved Hide resolved

# Check that export length matches length of db table
results <- jsonlite::fromJSON(file.path(outputFolder,testExportFile))
table_rows <- DatabaseConnector::renderTranslateQuerySql(
connection,
sql = "select count(*) from @resultsDatabaseSchema.@writeTableName;",
resultsDatabaseSchema = resultsDatabaseSchemaEunomia,
writeTableName = writeTableName,
targetDialect = connectionDetailsEunomia$dbms,
snakeCaseToCamelCase = TRUE
)
expect_true(length(results$CheckResults) == table_rows)

})
53 changes: 11 additions & 42 deletions vignettes/SqlOnly.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -171,49 +171,18 @@ for (dqdSqlFile in dqdSqlFiles) {
)
}

# Get results
checkResults <- DatabaseConnector::querySql(
c,
SqlRender::render(
"SELECT * FROM @resultsDatabaseSchema.@writeTableName",
resultsDatabaseSchema = resultsDatabaseSchema,
writeTableName = writeTableName
),
snakeCaseToCamelCase = TRUE
)
DatabaseConnector::disconnect(c)

# convert check ID column name to correct format
colnames(checkResults)[colnames(checkResults) == "checkid"] ="checkId"

# Get overview of DQD results
library(DataQualityDashboard)
overview <- DataQualityDashboard:::.summarizeResults(checkResults = checkResults)

# Create results object, adding fake metadata
result <- list(
startTimestamp = Sys.time(),
endTimestamp = Sys.time(),
executionTime = "",
Metadata = data.frame(
cdmSourceName = cdmSourceName,
cdmSourceAbbreviation = cdmSourceName,
cdmHolder = "",
sourceDescription = "",
sourceDocumentationReference = "",
cdmEtlReference = "",
sourceReleaseDate = "",
cdmReleaseDate = "",
cdmVersion = cdmVersion,
cdmVersionConceptId = 0,
vocabularyVersion = "",
dqdVersion = as.character(packageVersion("DataQualityDashboard"))
),
Overview = overview,
CheckResults = checkResults
)
# Extract results table to JSON file for viewing or secondary use

DataQualityDashboard::writeDBResultsToJson(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩 love to see those lines of code slashed!

c,
connectionDetails,
resultsDatabaseSchema,
cdmDatabaseSchema,
writeTableName,
jsonOutputFolder,
jsonOutputFile
)

DataQualityDashboard:::.writeResultsToJson(result, jsonOutputFolder, jsonOutputFile)

jsonFilePath <- R.utils::getAbsolutePath(file.path(jsonOutputFolder, jsonOutputFile))
DataQualityDashboard::viewDqDashboard(jsonFilePath)
Expand Down