Skip to content

Commit

Permalink
Merge pull request #40 from oslabs-beta/PostgreSQL
Browse files Browse the repository at this point in the history
Postgres sql
  • Loading branch information
bryentsariwati authored Apr 28, 2023
2 parents 1b1613a + f306aac commit 5cd4b14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions server/aws_sdk/traceDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,31 @@ const getTraceMiddleware = {
}
// inserting new traces into traces table
try {
const insertTraceQuery =
'INSERT INTO traces (_id, user_id, root_node) VALUES ($1,$2,$3) ON CONFLICT (_id) DO NOTHING RETURNING * ;';
const insertTraceQuery = `
INSERT INTO traces (_id, root_node, role_arn)
VALUES ($1, $2, (SELECT role_arn FROM users WHERE _id = $3))
ON CONFLICT (_id) DO NOTHING RETURNING *;
`;
for (let i = 0; i < allNodes.length; i++) {
const rootNode = allNodes[i];
const traceId = rootNode.id;
console.log(rootNode.fullData.Document.start_time);
const result = await query(insertTraceQuery, [
const resultTraces = await query(insertTraceQuery, [
traceId,
userId,
JSON.stringify(rootNode),
userId,
]);
if (result.rowCount > 0) {
console.log('Inserted trace in DB', result.rows[0]);
if (resultTraces.rowCount > 0) {
console.log('Inserted trace in DB', resultTraces.rows[0]);
} else {
console.log(`Trace with id ${traceId} already exists in DB`);
}
}
// Deleting traces older than 7 days long
const deleteOldTracesQuery = `DELETE FROM traces WHERE user_id = $1 AND ((root_node -> 'fullData' ->
'Document' ->> 'start_time')::double precision < EXTRACT(EPOCH FROM (NOW() - INTERVAL '7 days')));`;
const deleteOldTracesQuery = `
DELETE FROM traces
WHERE role_arn = (SELECT role_arn FROM users WHERE _id = $1)
AND ((root_node -> 'fullData' -> 'Document' ->> 'start_time')::
double precision < EXTRACT(EPOCH FROM (NOW() - INTERVAL '7 days')));`;
await query(deleteOldTracesQuery, [userId]);
} catch (err) {
console.log('error', err);
Expand All @@ -266,8 +271,13 @@ const getTraceMiddleware = {

// Selecting traces, sorting them in descending order and passing on to res.locals
try {
const selectTracesQuery = `SELECT root_node FROM traces WHERE user_id = $1 ORDER BY (root_node -> 'fullData' ->
'Document' ->> 'start_time')::double precision DESC;`;
const selectTracesQuery = `
SELECT t.root_node
FROM traces t
JOIN users u ON t.role_arn = u.role_arn
WHERE u._id = $1
ORDER BY (t.root_node -> 'fullData' -> 'Document' ->> 'start_time')::double precision DESC;
`;
const tracesResult = await query(selectTracesQuery, [userId]);

const userTraces = tracesResult.rows.map((row) => row.root_node);
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/awsCredentialsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ awsCredentialsController.getCredentials = async (req, res, next) => {
//SELECT user.role_ARN where user.id = res.locals.userID
const roleResult = await query('SELECT role_arn FROM users WHERE _id = $1 ;', [res.locals.userId])
console.log('this is roleResult in AWS credentials', roleResult)
const getRole = roleResult.rows.map(row => row.role_arn)[0]
const getRole = roleResult.rows[0].role_arn
console.log('this is getRole in AWS credentials', getRole)

const userRoleArn = getRole;
Expand Down

0 comments on commit 5cd4b14

Please sign in to comment.