diff --git a/DESCRIPTION b/DESCRIPTION index 910a09e7..0f0e746c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: DataQualityDashboard Type: Package Title: Execute and View Data Quality Checks on OMOP CDM Database -Version: 2.4.1 -Date: 2023-10-18 +Version: 2.5.0 +Date: 2023-11-04 Authors@R: c( person("Katy", "Sadowski", email = "sadowski@ohdsi.org", role = c("aut", "cre")), person("Clair", "Blacketer", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index 690105a7..8420f28a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(executeDqChecks) export(listDqChecks) export(reEvaluateThresholds) export(viewDqDashboard) +export(writeDBResultsToJson) export(writeJsonResultsToCsv) export(writeJsonResultsToTable) import(DatabaseConnector) diff --git a/R/executeDqChecks.R b/R/executeDqChecks.R index 5e9d1070..178e0ab4 100644 --- a/R/executeDqChecks.R +++ b/R/executeDqChecks.R @@ -94,6 +94,10 @@ executeDqChecks <- function(connectionDetails, stop("cdmVersion must contain a version of the form '5.X' where X is an integer between 2 and 4 inclusive.") } + if (sqlOnlyIncrementalInsert == TRUE && sqlOnly == FALSE) { + stop("Set `sqlOnly` to TRUE in order to use `sqlOnlyIncrementalInsert` mode.") + } + stopifnot(is.character(cdmDatabaseSchema), is.character(resultsDatabaseSchema), is.numeric(numThreads)) stopifnot(is.character(cdmSourceName), is.logical(sqlOnly), is.character(outputFolder), is.logical(verboseMode)) stopifnot(is.logical(writeToTable), is.character(checkLevels)) @@ -134,6 +138,10 @@ executeDqChecks <- function(connectionDetails, if (nrow(metadata) < 1) { stop("Please populate the cdm_source table before executing data quality checks.") } + if (nrow(metadata) > 1) { + metadata <- metadata[1, ] + warning("The cdm_source table has more than 1 row. A single row from this table has been selected to populate DQD metadata.") + } metadata$dqdVersion <- as.character(packageVersion("DataQualityDashboard")) DatabaseConnector::disconnect(connection) } else { diff --git a/R/runCheck.R b/R/runCheck.R index 85c31945..e4e2bd78 100644 --- a/R/runCheck.R +++ b/R/runCheck.R @@ -124,7 +124,7 @@ dfs <- do.call(rbind, dfs) - if (sqlOnlyIncrementalInsert) { + if (sqlOnly && sqlOnlyIncrementalInsert) { sqlToUnion <- dfs$query if (length(sqlToUnion) > 0) { .writeSqlOnlyQueries(sqlToUnion, sqlOnlyUnionCount, resultsDatabaseSchema, writeTableName, connectionDetails$dbms, outputFolder, checkDescription) diff --git a/R/writeDBResultsTo.R b/R/writeDBResultsTo.R new file mode 100644 index 00000000..2b4e446a --- /dev/null +++ b/R/writeDBResultsTo.R @@ -0,0 +1,77 @@ +# Copyright 2023 Observational Health Data Sciences and Informatics +# +# This file is part of DataQualityDashboard +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#' Write DQD results database table to json +#' +#' @param connection A connection object +#' @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 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 +#' + +writeDBResultsToJson <- function(connection, + resultsDatabaseSchema, + cdmDatabaseSchema, + writeTableName, + outputFolder, + outputFile) { + metadata <- DatabaseConnector::renderTranslateQuerySql( + connection, + sql = "select * from @cdmDatabaseSchema.cdm_source;", + snakeCaseToCamelCase = TRUE, + cdmDatabaseSchema = cdmDatabaseSchema + ) + + checkResults <- DatabaseConnector::renderTranslateQuerySql( + connection, + sql = "select * from @resultsDatabaseSchema.@writeTableName;", + snakeCaseToCamelCase = TRUE, + resultsDatabaseSchema = resultsDatabaseSchema, + writeTableName = writeTableName + ) + + # 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 secs" + checkResults["queryText"][checkResults["queryText"] == ""] <- "[Generated via SQL Only]" + + overview <- .summarizeResults( + checkResults = checkResults + ) + + # Quick patch for non-camel-case column name + names(checkResults)[names(checkResults) == "checkid"] <- "checkId" + + allResults <- list( + startTimestamp = Sys.time(), + endTimestamp = Sys.time(), + executionTime = "0 secs", + CheckResults = checkResults, + Metadata = metadata, + Overview = overview + ) + + .writeResultsToJson( + allResults, + outputFolder, + outputFile + ) +} diff --git a/R/writeJsonResultsTo.R b/R/writeJsonResultsTo.R index a5a70b20..869539b6 100644 --- a/R/writeJsonResultsTo.R +++ b/R/writeJsonResultsTo.R @@ -47,7 +47,7 @@ writeJsonResultsToTable <- function(connectionDetails, ParallelLogger::logInfo(sprintf("Writing results to table %s", tableName)) - if ("unitConceptId" %in% colnames(df)) { + if ("conceptId" %in% colnames(df)) { ddl <- SqlRender::loadRenderTranslateSql( sqlFilename = "result_table_ddl_concept.sql", packageName = "DataQualityDashboard", diff --git a/_pkgdown.yml b/_pkgdown.yml index 57941eb8..63139cb3 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -63,3 +63,7 @@ reference: desc: > Function to convert the case of a results JSON file between snakecase and camelcase contents: convertJsonResultsFileCase + - title: "Write database results to a JSON file" + desc: > + Function to write DQD results from a database table into a JSON file + contents: writeDBResultsToJson diff --git a/docs/404.html b/docs/404.html index ca0796ad..727f16f7 100644 --- a/docs/404.html +++ b/docs/404.html @@ -32,7 +32,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 31e872f3..10a745e8 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/articles/AddNewCheck.html b/docs/articles/AddNewCheck.html index 22f4f9a4..345db18c 100644 --- a/docs/articles/AddNewCheck.html +++ b/docs/articles/AddNewCheck.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -108,7 +108,7 @@

Add a New Data Quality Check

Don Torok

-

2023-10-18

+

2023-11-04

Source: vignettes/AddNewCheck.rmd diff --git a/docs/articles/CheckStatusDefinitions.html b/docs/articles/CheckStatusDefinitions.html index f75e735e..ac597143 100644 --- a/docs/articles/CheckStatusDefinitions.html +++ b/docs/articles/CheckStatusDefinitions.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

Check Status Descriptions

Dmitry Ilyn

-

2023-10-18

+

2023-11-04

Source: vignettes/CheckStatusDefinitions.rmd diff --git a/docs/articles/CheckTypeDescriptions.html b/docs/articles/CheckTypeDescriptions.html index 3e1ec5c5..ac9d2909 100644 --- a/docs/articles/CheckTypeDescriptions.html +++ b/docs/articles/CheckTypeDescriptions.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

Data Quality Check Type Definitions

Clair Blacketer

-

2023-10-18

+

2023-11-04

Source: vignettes/CheckTypeDescriptions.rmd diff --git a/docs/articles/DataQualityDashboard.html b/docs/articles/DataQualityDashboard.html index 023b49f0..fd3146d7 100644 --- a/docs/articles/DataQualityDashboard.html +++ b/docs/articles/DataQualityDashboard.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

Getting Started

Clair Blacketer

-

2023-10-18

+

2023-11-04

Source: vignettes/DataQualityDashboard.rmd diff --git a/docs/articles/DqdForCohorts.html b/docs/articles/DqdForCohorts.html index b0aa7263..4b7fae0e 100644 --- a/docs/articles/DqdForCohorts.html +++ b/docs/articles/DqdForCohorts.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

Running the DQD on a Cohort

Clair Blacketer

-

2023-10-18

+

2023-11-04

Source: vignettes/DqdForCohorts.rmd diff --git a/docs/articles/SqlOnly.html b/docs/articles/SqlOnly.html index 3ad568b6..47dec90d 100644 --- a/docs/articles/SqlOnly.html +++ b/docs/articles/SqlOnly.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

SqlOnly

Maxim Moinat

-

2023-10-18

+

2023-11-04

Source: vignettes/SqlOnly.rmd @@ -324,49 +324,18 @@

(OPTIONAL) Execute queries ) } -# 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) +# Extract results table to JSON file for viewing or secondary use -# 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 -) +DataQualityDashboard::writeDBResultsToJson( + c, + connectionDetails, + resultsDatabaseSchema, + cdmDatabaseSchema, + writeTableName, + jsonOutputFolder, + jsonOutputFile + ) -DataQualityDashboard:::.writeResultsToJson(result, jsonOutputFolder, jsonOutputFile) jsonFilePath <- R.utils::getAbsolutePath(file.path(jsonOutputFolder, jsonOutputFile)) DataQualityDashboard::viewDqDashboard(jsonFilePath) diff --git a/docs/articles/Thresholds.html b/docs/articles/Thresholds.html index dc6f190c..c2de633c 100644 --- a/docs/articles/Thresholds.html +++ b/docs/articles/Thresholds.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -109,7 +109,7 @@

Failure Thresholds and How to Change Them

Clair Blacketer

-

2023-10-18

+

2023-11-04

Source: vignettes/Thresholds.rmd diff --git a/docs/articles/index.html b/docs/articles/index.html index 78d2d687..fb48c266 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/authors.html b/docs/authors.html index 22af3d2d..30f2df9a 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/index.html b/docs/index.html index 9eff8be6..fe3503a6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,7 +33,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/news/index.html b/docs/news/index.html index 1431a0ed..967560e6 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index ec39574d..80cea51a 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -9,5 +9,5 @@ articles: DqdForCohorts: DqdForCohorts.html SqlOnly: SqlOnly.html Thresholds: Thresholds.html -last_built: 2023-10-19T03:18Z +last_built: 2023-11-04T22:21Z diff --git a/docs/pull_request_template.html b/docs/pull_request_template.html index a0d1e7e4..d1520df6 100644 --- a/docs/pull_request_template.html +++ b/docs/pull_request_template.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/convertJsonResultsFileCase.html b/docs/reference/convertJsonResultsFileCase.html index 9fea5133..a368edb5 100644 --- a/docs/reference/convertJsonResultsFileCase.html +++ b/docs/reference/convertJsonResultsFileCase.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-evaluateThresholds.html b/docs/reference/dot-evaluateThresholds.html index fe120aa0..36ea9d79 100644 --- a/docs/reference/dot-evaluateThresholds.html +++ b/docs/reference/dot-evaluateThresholds.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-getCheckId.html b/docs/reference/dot-getCheckId.html index 0ead8722..9af848d3 100644 --- a/docs/reference/dot-getCheckId.html +++ b/docs/reference/dot-getCheckId.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-processCheck.html b/docs/reference/dot-processCheck.html index 518d5bd0..7f6a37fb 100644 --- a/docs/reference/dot-processCheck.html +++ b/docs/reference/dot-processCheck.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-recordResult.html b/docs/reference/dot-recordResult.html index c8970af0..9bb44bc9 100644 --- a/docs/reference/dot-recordResult.html +++ b/docs/reference/dot-recordResult.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-runCheck.html b/docs/reference/dot-runCheck.html index 9ad867ae..74734e1c 100644 --- a/docs/reference/dot-runCheck.html +++ b/docs/reference/dot-runCheck.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-summarizeResults.html b/docs/reference/dot-summarizeResults.html index 9d0aadf6..02072c33 100644 --- a/docs/reference/dot-summarizeResults.html +++ b/docs/reference/dot-summarizeResults.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-writeResultsToCsv.html b/docs/reference/dot-writeResultsToCsv.html index fce5b97b..bff6ecef 100644 --- a/docs/reference/dot-writeResultsToCsv.html +++ b/docs/reference/dot-writeResultsToCsv.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-writeResultsToJson.html b/docs/reference/dot-writeResultsToJson.html index 2df4c8b7..023d75d4 100644 --- a/docs/reference/dot-writeResultsToJson.html +++ b/docs/reference/dot-writeResultsToJson.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/dot-writeResultsToTable.html b/docs/reference/dot-writeResultsToTable.html index 86861bc0..79c55e02 100644 --- a/docs/reference/dot-writeResultsToTable.html +++ b/docs/reference/dot-writeResultsToTable.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/executeDqChecks.html b/docs/reference/executeDqChecks.html index eb39a4ec..3641da35 100644 --- a/docs/reference/executeDqChecks.html +++ b/docs/reference/executeDqChecks.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/index.html b/docs/reference/index.html index fb1fd296..109e13a3 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 @@ -135,6 +135,14 @@

Convert results JSON file case convertJsonResultsFileCase()

Convert JSON results file case

+ +

Write database results to a JSON file

+

Function to write DQD results from a database table into a JSON file

+ + +

writeDBResultsToJson()

+ +

Write DQD results database table to json

diff --git a/docs/reference/reEvaluateThresholds.html b/docs/reference/reEvaluateThresholds.html index 0a7df4a7..04a13f52 100644 --- a/docs/reference/reEvaluateThresholds.html +++ b/docs/reference/reEvaluateThresholds.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/viewDqDashboard.html b/docs/reference/viewDqDashboard.html index 41557122..9ee5dba0 100644 --- a/docs/reference/viewDqDashboard.html +++ b/docs/reference/viewDqDashboard.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/writeDBResultsToJson.html b/docs/reference/writeDBResultsToJson.html new file mode 100644 index 00000000..a3423b33 --- /dev/null +++ b/docs/reference/writeDBResultsToJson.html @@ -0,0 +1,153 @@ + +Write DQD results database table to json — writeDBResultsToJson • DataQualityDashboard + + +
+
+ + + +
+
+ + +
+

Write DQD results database table to json

+
+ +
+
writeDBResultsToJson(
+  connection,
+  resultsDatabaseSchema,
+  cdmDatabaseSchema,
+  writeTableName,
+  outputFolder,
+  outputFile
+)
+
+ +
+

Arguments

+
connection
+

A connection object

+ + +
resultsDatabaseSchema
+

The fully qualified database name of the results schema

+ + +
cdmDatabaseSchema
+

The fully qualified database name of the CDM schema

+ + +
writeTableName
+

Name of DQD results table in the database to read from

+ + +
outputFolder
+

The folder to output the json results file to

+ + +
outputFile
+

The output filename of the json results file

+ +
+ +
+ +
+ + +
+ + + + + + + + diff --git a/docs/reference/writeJsonResultsToCsv.html b/docs/reference/writeJsonResultsToCsv.html index 3083ad75..3bbd7d1f 100644 --- a/docs/reference/writeJsonResultsToCsv.html +++ b/docs/reference/writeJsonResultsToCsv.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/reference/writeJsonResultsToTable.html b/docs/reference/writeJsonResultsToTable.html index 43b428f9..d23c3f39 100644 --- a/docs/reference/writeJsonResultsToTable.html +++ b/docs/reference/writeJsonResultsToTable.html @@ -17,7 +17,7 @@ DataQualityDashboard - 2.4.1 + 2.5.0 diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 99f8a396..5ae0b4bc 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -117,6 +117,9 @@ /reference/viewDqDashboard.html + + /reference/writeDBResultsToJson.html + /reference/writeJsonResultsToCsv.html diff --git a/extras/DataQualityDashboard.pdf b/extras/DataQualityDashboard.pdf index 24a693fa..eb9dfa03 100644 Binary files a/extras/DataQualityDashboard.pdf and b/extras/DataQualityDashboard.pdf differ diff --git a/inst/doc/AddNewCheck.pdf b/inst/doc/AddNewCheck.pdf index ff24ed57..fe53eb2d 100644 Binary files a/inst/doc/AddNewCheck.pdf and b/inst/doc/AddNewCheck.pdf differ diff --git a/inst/doc/CheckStatusDefinitions.pdf b/inst/doc/CheckStatusDefinitions.pdf index b158d7cb..762f2594 100644 Binary files a/inst/doc/CheckStatusDefinitions.pdf and b/inst/doc/CheckStatusDefinitions.pdf differ diff --git a/inst/doc/CheckTypeDescriptions.pdf b/inst/doc/CheckTypeDescriptions.pdf index 4fc209fa..ff299a02 100644 Binary files a/inst/doc/CheckTypeDescriptions.pdf and b/inst/doc/CheckTypeDescriptions.pdf differ diff --git a/inst/doc/DataQualityDashboard.pdf b/inst/doc/DataQualityDashboard.pdf index 9d1faceb..062c6e2b 100644 Binary files a/inst/doc/DataQualityDashboard.pdf and b/inst/doc/DataQualityDashboard.pdf differ diff --git a/inst/doc/DqdForCohorts.pdf b/inst/doc/DqdForCohorts.pdf index dc381e31..9025cf6f 100644 Binary files a/inst/doc/DqdForCohorts.pdf and b/inst/doc/DqdForCohorts.pdf differ diff --git a/inst/doc/SqlOnly.pdf b/inst/doc/SqlOnly.pdf index f9107177..85328c66 100644 Binary files a/inst/doc/SqlOnly.pdf and b/inst/doc/SqlOnly.pdf differ diff --git a/inst/doc/Thresholds.pdf b/inst/doc/Thresholds.pdf index bb044544..a26b1308 100644 Binary files a/inst/doc/Thresholds.pdf and b/inst/doc/Thresholds.pdf differ diff --git a/inst/sql/sql_server/field_fk_class.sql b/inst/sql/sql_server/field_fk_class.sql index e9d6871e..2505fefc 100755 --- a/inst/sql/sql_server/field_fk_class.sql +++ b/inst/sql/sql_server/field_fk_class.sql @@ -4,7 +4,7 @@ FK_CLASS Drug era standard concepts, ingredients only Parameters used in this template: -cdmDatabaseSchema = @cdmDatabaseSchema +schema = @schema vocabDatabaseSchema = @vocabDatabaseSchema cdmTableName = @cdmTableName cdmFieldName = @cdmFieldName @@ -32,7 +32,7 @@ FROM ( SELECT '@cdmTableName.@cdmFieldName' AS violating_field, cdmTable.* - FROM @cdmDatabaseSchema.@cdmTableName cdmTable + FROM @schema.@cdmTableName cdmTable LEFT JOIN @vocabDatabaseSchema.concept co ON cdmTable.@cdmFieldName = co.concept_id {@cohort & '@runForCohort' == 'Yes'}?{ @@ -48,7 +48,7 @@ FROM ( ( SELECT COUNT_BIG(*) AS num_rows - FROM @cdmDatabaseSchema.@cdmTableName cdmTable + FROM @schema.@cdmTableName cdmTable {@cohort & '@runForCohort' == 'Yes'}?{ JOIN @cohortDatabaseSchema.@cohortTableName c ON cdmTable.person_id = c.subject_id diff --git a/inst/sql/sql_server/field_fk_domain.sql b/inst/sql/sql_server/field_fk_domain.sql index ce596c47..a3e188f8 100755 --- a/inst/sql/sql_server/field_fk_domain.sql +++ b/inst/sql/sql_server/field_fk_domain.sql @@ -5,7 +5,7 @@ FIELD_FK_DOMAIN all standard concept ids are part of specified domain Parameters used in this template: -cdmDatabaseSchema = @cdmDatabaseSchema +schema = @schema vocabDatabaseSchema = @vocabDatabaseSchema cdmTableName = @cdmTableName cdmFieldName = @cdmFieldName @@ -32,7 +32,7 @@ FROM ( SELECT '@cdmTableName.@cdmFieldName' AS violating_field, cdmTable.* - FROM @cdmDatabaseSchema.@cdmTableName cdmTable + FROM @schema.@cdmTableName cdmTable LEFT JOIN @vocabDatabaseSchema.concept co ON cdmTable.@cdmFieldName = co.concept_id {@cohort & '@runForCohort' == 'Yes'}?{ @@ -48,7 +48,7 @@ FROM ( ( SELECT COUNT_BIG(*) AS num_rows - FROM @cdmDatabaseSchema.@cdmTableName cdmTable + FROM @schema.@cdmTableName cdmTable {@cohort & '@runForCohort' == 'Yes'}?{ JOIN @cohortDatabaseSchema.@cohortTableName c ON cdmTable.PERSON_ID = c.SUBJECT_ID diff --git a/inst/sql/sql_server/field_plausible_temporal_after.sql b/inst/sql/sql_server/field_plausible_temporal_after.sql index 8dd28eab..034ce0b8 100755 --- a/inst/sql/sql_server/field_plausible_temporal_after.sql +++ b/inst/sql/sql_server/field_plausible_temporal_after.sql @@ -4,7 +4,7 @@ PLAUSIBLE_TEMPORAL_AFTER get number of records and the proportion to total number of eligible records with datetimes that do not occur on or after their corresponding datetimes Parameters used in this template: -cdmDatabaseSchema = @cdmDatabaseSchema +schema = @schema cdmTableName = @cdmTableName cdmFieldName = @cdmFieldName plausibleTemporalAfterTableName = @plausibleTemporalAfterTableName @@ -33,9 +33,9 @@ FROM SELECT '@cdmTableName.@cdmFieldName' AS violating_field, cdmTable.* - FROM @cdmDatabaseSchema.@cdmTableName cdmTable - {@cdmDatabaseSchema.@cdmTableName != @cdmDatabaseSchema.@plausibleTemporalAfterTableName}?{ - JOIN @cdmDatabaseSchema.@plausibleTemporalAfterTableName plausibleTable ON cdmTable.person_id = plausibleTable.person_id} + FROM @schema.@cdmTableName cdmTable + {@schema.@cdmTableName != @schema.@plausibleTemporalAfterTableName}?{ + JOIN @schema.@plausibleTemporalAfterTableName plausibleTable ON cdmTable.person_id = plausibleTable.person_id} {@cohort & '@runForCohort' == 'Yes'}?{ JOIN @cohortDatabaseSchema.@cohortTableName c ON cdmTable.person_id = c.subject_id AND c.cohort_definition_id = @cohortDefinitionId @@ -55,7 +55,7 @@ FROM ( SELECT COUNT_BIG(*) AS num_rows - FROM @cdmDatabaseSchema.@cdmTableName cdmTable + FROM @schema.@cdmTableName cdmTable {@cohort & '@runForCohort' == 'Yes'}?{ JOIN @cohortDatabaseSchema.@cohortTableName c ON cdmTable.person_id = c.subject_id AND c.cohort_definition_id = @cohortDefinitionId diff --git a/man/writeDBResultsToJson.Rd b/man/writeDBResultsToJson.Rd new file mode 100644 index 00000000..3efc6b03 --- /dev/null +++ b/man/writeDBResultsToJson.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/writeDBResultsTo.R +\name{writeDBResultsToJson} +\alias{writeDBResultsToJson} +\title{Write DQD results database table to json} +\usage{ +writeDBResultsToJson( + connection, + resultsDatabaseSchema, + cdmDatabaseSchema, + writeTableName, + outputFolder, + outputFile +) +} +\arguments{ +\item{connection}{A connection object} + +\item{resultsDatabaseSchema}{The fully qualified database name of the results schema} + +\item{cdmDatabaseSchema}{The fully qualified database name of the CDM schema} + +\item{writeTableName}{Name of DQD results table in the database to read from} + +\item{outputFolder}{The folder to output the json results file to} + +\item{outputFile}{The output filename of the json results file} +} +\description{ +Write DQD results database table to json +} diff --git a/tests/testthat/test-executeDqChecks.R b/tests/testthat/test-executeDqChecks.R index 01cb11b3..1cafeff2 100644 --- a/tests/testthat/test-executeDqChecks.R +++ b/tests/testthat/test-executeDqChecks.R @@ -328,3 +328,30 @@ test_that("Incremental insert SQL is valid.", { DatabaseConnector::renderTranslateExecuteSql(connection, "DROP TABLE @database_schema.dqd_results;", database_schema = resultsDatabaseSchemaEunomia) }) + +test_that("Multiple cdm_source rows triggers warning.", { + outputFolder <- tempfile("dqd_") + on.exit(unlink(outputFolder, recursive = TRUE)) + + connectionDetailsEunomiaCS <- Eunomia::getEunomiaConnectionDetails() + connection <- DatabaseConnector::connect(connectionDetailsEunomiaCS) + on.exit(DatabaseConnector::disconnect(connection), add = TRUE) + DatabaseConnector::renderTranslateExecuteSql(connection, "INSERT INTO @database_schema.cdm_source VALUES ('foo',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);", database_schema = cdmDatabaseSchemaEunomia) + + w <- capture_warnings( + results <- executeDqChecks( + connectionDetails = connectionDetailsEunomiaCS, + cdmDatabaseSchema = cdmDatabaseSchemaEunomia, + resultsDatabaseSchema = resultsDatabaseSchemaEunomia, + cdmSourceName = "Eunomia", + checkNames = "measurePersonCompleteness", + outputFolder = outputFolder, + writeToTable = F + ) + ) + + expect_match(w, "Missing check names", all = FALSE) + expect_match(w, "The cdm_source table has", all = FALSE) + + expect_true(nrow(results$CheckResults) > 1) +}) diff --git a/tests/testthat/test-writeDBResultsTo.R b/tests/testthat/test-writeDBResultsTo.R new file mode 100644 index 00000000..8d10e79a --- /dev/null +++ b/tests/testthat/test-writeDBResultsTo.R @@ -0,0 +1,53 @@ +library(testthat) + +test_that("Write DB results to json", { + outputFolder <- tempfile("dqd_") + on.exit(unlink(outputFolder, recursive = TRUE)) + connectionDetailsEunomia <- Eunomia::getEunomiaConnectionDetails() + cdmDatabaseSchemaEunomia <- "main" + resultsDatabaseSchemaEunomia <- "main" + writeTableName <- "dqd_db_results" + + expect_warning( + results <- DataQualityDashboard::executeDqChecks( + connectionDetails = connectionDetailsEunomia, + cdmDatabaseSchema = cdmDatabaseSchemaEunomia, + resultsDatabaseSchema = resultsDatabaseSchemaEunomia, + cdmSourceName = "Eunomia", + checkNames = "measurePersonCompleteness", + outputFolder = outputFolder, + writeToTable = TRUE, + writeTableName = writeTableName + ), + regexp = "^Missing check names.*" + ) + + connection <- DatabaseConnector::connect(connectionDetailsEunomia) + + testExportFile <- "dq-result-test.json" + + DataQualityDashboard::writeDBResultsToJson( + connection, + resultsDatabaseSchemaEunomia, + cdmDatabaseSchemaEunomia, + writeTableName, + outputFolder, + testExportFile + ) + + on.exit(DatabaseConnector::disconnect(connection), add = TRUE) + + # Check that file was exported properly + expect_true(file.exists(file.path(outputFolder, testExportFile))) + + # 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, + snakeCaseToCamelCase = TRUE + ) + expect_true(length(results$CheckResults) == table_rows) +}) diff --git a/tests/testthat/test-writeJsonResultsTo.R b/tests/testthat/test-writeJsonResultsTo.R index 7aff258f..c9df7f0d 100644 --- a/tests/testthat/test-writeJsonResultsTo.R +++ b/tests/testthat/test-writeJsonResultsTo.R @@ -29,11 +29,11 @@ test_that("Write JSON results", { connectionDetails = connectionDetailsEunomia, resultsDatabaseSchema = resultsDatabaseSchemaEunomia, jsonFilePath = jsonPath, - writeTableName = "dqd_results" + writeTableName = "dqd_json_results" ) connection <- DatabaseConnector::connect(connectionDetailsEunomia) on.exit(DatabaseConnector::disconnect(connection), add = TRUE) tableNames <- DatabaseConnector::getTableNames(connection = connection, databaseSchema = resultsDatabaseSchemaEunomia) - expect_true("dqd_results" %in% tolower(tableNames)) - DatabaseConnector::renderTranslateExecuteSql(connection, "DROP TABLE @database_schema.dqd_results;", database_schema = resultsDatabaseSchemaEunomia) + expect_true("dqd_json_results" %in% tolower(tableNames)) + DatabaseConnector::renderTranslateExecuteSql(connection, "DROP TABLE @database_schema.dqd_json_results;", database_schema = resultsDatabaseSchemaEunomia) }) diff --git a/vignettes/SqlOnly.rmd b/vignettes/SqlOnly.rmd index 9fabcce0..dcaad7f1 100644 --- a/vignettes/SqlOnly.rmd +++ b/vignettes/SqlOnly.rmd @@ -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( + c, + connectionDetails, + resultsDatabaseSchema, + cdmDatabaseSchema, + writeTableName, + jsonOutputFolder, + jsonOutputFile + ) -DataQualityDashboard:::.writeResultsToJson(result, jsonOutputFolder, jsonOutputFile) jsonFilePath <- R.utils::getAbsolutePath(file.path(jsonOutputFolder, jsonOutputFile)) DataQualityDashboard::viewDqDashboard(jsonFilePath)