-
Notifications
You must be signed in to change notification settings - Fork 97
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# 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 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 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, | ||
connectionDetails, | ||
resultsDatabaseSchema, | ||
cdmDatabaseSchema, | ||
writeTableName, | ||
outputFolder, | ||
outputFile) { | ||
startTime <- Sys.time() | ||
|
||
metadata <- DatabaseConnector::renderTranslateQuerySql( | ||
connection, | ||
sql = "select * from @cdmDatabaseSchema.cdm_source;", | ||
cdmDatabaseSchema = cdmDatabaseSchema, | ||
targetDialect = connectionDetails$dbms, | ||
snakeCaseToCamelCase = TRUE | ||
) | ||
|
||
checkResults <- DatabaseConnector::renderTranslateQuerySql( | ||
connection, | ||
sql = "select * from @resultsDatabaseSchema.@writeTableName;", | ||
resultsDatabaseSchema = resultsDatabaseSchema, | ||
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 secs' | ||
checkResults["queryText"][checkResults["queryText"] == ''] <- '[Generated via SQL Only]' | ||
|
||
overview <- .summarizeResults( | ||
checkResults = checkResults | ||
) | ||
|
||
endTime <- Sys.time() | ||
|
||
delta <- startTime - endTime | ||
|
||
# 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 | ||
) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 <- "dqdashboard_results" | ||
|
||
results <- DataQualityDashboard::executeDqChecks( | ||
connectionDetails = connectionDetailsEunomia, | ||
cdmDatabaseSchema = cdmDatabaseSchemaEunomia, | ||
resultsDatabaseSchema = resultsDatabaseSchemaEunomia, | ||
cdmSourceName = "Eunomia", | ||
checkNames = "measurePersonCompleteness", | ||
outputFolder = outputFolder, | ||
writeToTable = TRUE, | ||
writeTableName = writeTableName | ||
) | ||
|
||
|
||
connection <- DatabaseConnector::connect(connectionDetailsEunomia) | ||
|
||
testExportFile <- "dq-result-test.json" | ||
|
||
DataQualityDashboard::writeDBResultsToJson( | ||
connection, | ||
connectionDetailsEunomia, | ||
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))) | ||
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) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!