Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions server/controllers/helperFunctions/universal.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface NewColumn {
}

//---------------CONNECT TO THE DATABASE-----------------------------------------------------------------------------------------
let dbDataSource: DataSource | null = null;

export const dbConnect = async (req: Request) => {
const { db_type, hostname, password, port, username, database_name, service_name } =
Expand Down
50 changes: 50 additions & 0 deletions server/controllers/postgresData.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const postgresController = {
//----------Function to collect all schema and data from database-----------------------------------------------------------------
postgresQuery: async (req: Request, res: Response, next: NextFunction) => {
const PostgresDataSource = await dbConnect(req);
console.log('postgresQuery REQ: ', req);
console.log('MADE IT TO postgresQuery MIDDLEWARE');

/*
* Used for storing Primary Key table and column names that are
* part of Foreign Keys to adjust IsDestination to be true.
Expand Down Expand Up @@ -146,6 +148,8 @@ const postgresController = {
// Storage of queried results into res.locals
res.locals.schema = schema;
res.locals.data = tableData;
// res.locals.database_link = req.query.database_link;
// res.locals.db_connection = PostgresDataSource;

// Disconnecting after data has been received
PostgresDataSource.destroy();
Expand All @@ -159,6 +163,52 @@ const postgresController = {
}
},

//----------Function to gather query metrics from database-----------------------------------------------------------------
postgresGetMetrics: async (req: Request, res: Response, next: NextFunction) => {
// if we pass database_link from FE then we might not need to initialize dbConnect again
// database_link and query can be passed in from FE
// console.log('FULL REQUEST: ', req);
const PostgresGetMetrics = await dbConnect(req);
// console.log('dblink check: ', res.locals.database_link);
console.log('REACHED postgresGetMetrics MIDDLEWARE');
console.log('REQ QUERY: ', req.query);
const { queryString } = req.query;
console.log('❓ QUERY FROM FE IS: ', queryString);

// Query string (EXPLAIN) to access performance data
const testQuery = `EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, BUFFERS) ${queryString};`;
// view result of Explain query
const result = await PostgresGetMetrics.query(testQuery);
// console.log('🌟 result of testing: ', result);
console.log('⭐️QUERY PLAN RESULT: ', result[0]['QUERY PLAN']);

// Pull Execution time only
const executionTime = `Execution Time: ${result[0]['QUERY PLAN'][0]['Execution Time']}`;
// console.log('⏰ EXECUTION TIME METRIC', executionTime);

// Create date metric to add to response
const now = new Date();
const options: any = {
year: 'numeric',
month: 'long',
day: 'numeric',
// hour: 'numeric',
// minute: 'numeric',
// second: 'numeric',
// hour12: true, // Use 12-hour time format
timeZone: 'America/New_York', // Set to US Eastern Time
};
const formattedDate = `Date Run: ${now.toLocaleString('en-US', options)}`;
// console.log(`🗓️ TODAY'S DATE`, formattedDate);

// console.log('done w getMetrics controller');

// Send date and execution time on response
res.locals.metrics = [formattedDate, executionTime] ;

return next();
},

//-------------------------------------DATA TABLE ROWS-------------------------------------------------
//-------------------ADD NEW ROW-----------------------------------------------------------
postgresAddNewRow: async (req: Request, res: Response, next: NextFunction) => {
Expand Down
8 changes: 8 additions & 0 deletions server/routes/postgres.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ postgresRouter.get(
}
);

postgresRouter.get(
'/run-query',
postgresController.postgresGetMetrics,
(_req: Request, res: Response) => {
return res.status(200).json(res.locals.metrics);
}
);

//-------------------------------------DATA TABLE ROWS----------------------------------------------------------------------------------------
//-------------------ADD NEW ROW-----------------------------------------------------------------------------------------
postgresRouter.post(
Expand Down
1 change: 1 addition & 0 deletions src/components/DBDisplay/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const Sidebar = (props: any) => {
values.password = internalLinkArray_Postgres[0];
values.port = postgresPort ? postgresPort : '5432';
values.database_name = postgresName;
// values.database_link = fullLink;
values.db_type = 'postgres';
break;
}
Expand Down
Loading