Skip to content

Commit

Permalink
update: automation script modification to execute combind view and la…
Browse files Browse the repository at this point in the history
…test metrics #29
  • Loading branch information
MeetAnithaVarghese committed Oct 8, 2024
1 parent 8573238 commit f19226b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DB } from "https://deno.land/x/sqlite/mod.ts";
export function createCombinedCGMView(dbFilePath: string): void {
// Open the existing database
const db = new DB(dbFilePath);
console.log(`Opened database: ${dbFilePath}`);
//console.log(`Opened database: ${dbFilePath}`);

// Create error log table if it doesn't exist
db.execute(`
Expand All @@ -14,7 +14,7 @@ export function createCombinedCGMView(dbFilePath: string): void {
error_message TEXT
);
`);
console.log("Error log table created or already exists.");
//console.log("Error log table created or already exists.");

// Drop the view if it exists and create it
try {
Expand All @@ -29,7 +29,7 @@ export function createCombinedCGMView(dbFilePath: string): void {
GROUP BY
patient_id;
`);
console.log("View 'drh_participant_file_names' created successfully.");
//console.log("View 'drh_participant_file_names' created successfully.");
} catch (error) {
console.error("Error creating view 'drh_participant_file_names':", error);
const sqlQuery = "INSERT INTO error_log (error_message) VALUES (?);";
Expand Down Expand Up @@ -80,11 +80,11 @@ export function createCombinedCGMView(dbFilePath: string): void {
}

db.close();
console.log(`Closed database: ${dbFilePath}`);
//console.log(`Closed database: ${dbFilePath}`);
}

// If the script is being run directly, execute the function
if (import.meta.main) {
const dbFilePath = "resource-surveillance.sqlite.db"; // You can modify this path as needed
const dbFilePath = "resource-surveillance.sqlite.db";
createCombinedCGMView(dbFilePath);
}
74 changes: 47 additions & 27 deletions lib/service/diabetes-research-hub/drhctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as colors from "https://deno.land/std@0.224.0/fmt/colors.ts";
import { DB } from "https://deno.land/x/sqlite@v3.6.0/mod.ts";
import * as drhux from "./package.sql.ts";
import { createCombinedCGMView } from "./combined-cgm-tracing-generator.ts";
import {
FlexibleTextSupplierSync,
spawnedResult,
Expand All @@ -13,10 +14,13 @@ import {
// Detect platform-specific command format
const isWindows = Deno.build.os === "windows";
const toolCmd = isWindows ? ".\\surveilr" : "surveilr";
const dbFilePath = "resource-surveillance.sqlite.db"; // Path to your SQLite DB

const RSC_BASE_URL =
"https://raw.githubusercontent.com/surveilr/www.surveilr.com/main/lib/service/diabetes-research-hub";

const UX_URL ="https://www.surveilr.com/lib/service/diabetes-research-hub"

// Helper function to fetch SQL content
async function fetchSqlContent(url: string): Promise<string> {
try {
Expand Down Expand Up @@ -87,30 +91,28 @@ async function checkAndDeleteFile(filePath: string) {
}
}

// Function to fetch UX SQL content
async function fetchUxSqlContent(): Promise<string> {
try {
const uxSQLContent = await drhux.drhSQL();
return uxSQLContent.join("\n");
} catch (error) {
console.error(
colors.red("Error fetching UX SQL content:"),
error.message,
);
Deno.exit(1);
}
}
// Function to check for table existence and create combined view
async function checkAndCreateCombinedView() {
const db = new DB(dbFilePath);

// Function to execute SQL commands directly on SQLite database
function executeSqlCommands(sqlCommands: string) {
try {
const db = new DB(dbFilePath);
db.execute(sqlCommands); // Execute the SQL commands
db.close();
console.log(colors.green("UX SQL executed successfully."));
// Check if the specific table exists
const tableName = 'uniform_resource_cgm_file_metadata'; // Change to your required table name
const tableExists = db.query(`SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}';`).length > 0;


if (tableExists) {
console.log(colors.green("Required table exists. Proceeding to create the combined view."));
await createCombinedCGMView(dbFilePath); // Call the function to create the combined view

} else {
console.error(colors.red("The required table does not exist. Cannot create the combined view."));
}
} catch (error) {
console.error(colors.red("Error executing SQL commands:"), error.message);
Deno.exit(1);
console.error(colors.red("Error in checkAndCreateCombinedView:"), error.message);
} finally {
// Close the database connection
db.close();
}
}

Expand All @@ -125,28 +127,33 @@ if (Deno.args.length === 0) {
// Store the folder name in a variable
const folderName = Deno.args[0];

// Path to the SQLite database file
const dbFilePath = "./resource-surveillance.sqlite.db";

// Path to the SQLite database journal
const dbjournalfile = "./resource-surveillance.sqlite.db-journal";

// Define synchronous suppliers
const deidentificationSQLSupplier: FlexibleTextSupplierSync = () =>
deidentificationSQL;
const vvSQLSupplier: FlexibleTextSupplierSync = () => vvSQL;
//const uxSQLSupplier: FlexibleTextSupplierSync = () => uxSQL;
const uxSQLSupplier: FlexibleTextSupplierSync = () => uxSQL;

let deidentificationSQL: string;
let vvSQL: string;
let uxSQL: string;



try {
// Fetch SQL content for DeIdentification, Verification & Validation, and UX orchestration
deidentificationSQL = await fetchSqlContent(
`${RSC_BASE_URL}/de-identification/drh-deidentification.sql`,
);
vvSQL = await fetchSqlContent(
`${RSC_BASE_URL}/verfication-validation/orchestrate-drh-vv.sql`,
);
uxSQL = await fetchSqlContent(
`${UX_URL}/package.sql`,
);
uxSQL = await fetchUxSqlContent(); // Fetch UX SQL content
} catch (error) {
console.error(
colors.red(
Expand All @@ -171,6 +178,7 @@ try {
Deno.exit(1);
}


try {
await executeCommand([toolCmd, "orchestrate", "transform-csv"]);
console.log(
Expand All @@ -181,6 +189,18 @@ try {
Deno.exit(1);
}

// Check and delete the file if it exists
await checkAndDeleteFile(dbjournalfile);

try {
console.log(colors.dim(`Generate combined views: ${folderName}...`));
checkAndCreateCombinedView();
console.log(colors.green("View generation completed successfully."));
} catch (error) {
console.error(colors.red("Error during View generation:"), error.message);
Deno.exit(1);
}


try {
console.log(colors.dim(`Performing DeIdentification: ${folderName}...`));
Expand Down Expand Up @@ -213,8 +233,8 @@ try {
}

try {
console.log(colors.dim(`Performing UX orchestration: ${folderName}...`));
executeSqlCommands(uxSQL); // Execute UX SQL commands
console.log(colors.dim(`Performing UX orchestration: ${folderName}...`));
await executeCommand([toolCmd, "shell"], uxSQLSupplier);
console.log(colors.green("UX orchestration completed successfully."));
} catch (error) {
console.error(colors.red("Error during UX orchestration:"), error.message);
Expand Down

0 comments on commit f19226b

Please sign in to comment.