Skip to content
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

Feat/be/ttl, #764 #835

Merged
merged 3 commits into from
Sep 15, 2024
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
52 changes: 43 additions & 9 deletions Server/controllers/checkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const {
getTeamChecksQueryValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
} = require("../validation/joi");
const { successMessages } = require("../utils/messages");
const SERVICE_NAME = "check";
const SERVICE_NAME = "checkController";

const createCheck = async (req, res, next) => {
try {
Expand All @@ -31,7 +32,8 @@ const createCheck = async (req, res, next) => {
.status(200)
.json({ success: true, msg: successMessages.CHECK_CREATE, data: check });
} catch (error) {
error.service = SERVICE_NAME;
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "createCheck") : null;
next(error);
}
};
Expand All @@ -58,7 +60,8 @@ const getChecks = async (req, res, next) => {
data: { checksCount, checks },
});
} catch (error) {
error.service = SERVICE_NAME;
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getChecks") : null;
next(error);
}
};
Expand All @@ -83,7 +86,8 @@ const getTeamChecks = async (req, res, next) => {
data: checkData,
});
} catch (error) {
error.service = SERVICE_NAME;
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getTeamChecks") : null;
next(error);
}
};
Expand All @@ -108,7 +112,8 @@ const deleteChecks = async (req, res, next) => {
data: { deletedCount },
});
} catch (error) {
error.service = SERVICE_NAME;
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "deleteChecks") : null;
next(error);
}
};
Expand All @@ -117,10 +122,11 @@ const deleteChecksByTeamId = async (req, res, next) => {
try {
await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteChecksByTeam";
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "deleteChecksByTeam") : null;
error.status = 422;
next(error);
return;
}

try {
Expand All @@ -131,8 +137,35 @@ const deleteChecksByTeamId = async (req, res, next) => {
data: { deletedCount },
});
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteChecksByTeamId";
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "deleteChecksByTeamId") : null;
next(error);
}
};

const updateChecksTTL = async (req, res, next) => {
try {
await updateChecksTTLBodyValidation.validateAsync(req.body);
} catch (error) {
error.status = 422;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "updateChecksTTL") : null;
error.message =
error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}

try {
const ttl = req.body.ttl;
await req.db.updateChecksTTL(1, ttl);
return res.status(200).json({
success: true,
msg: successMessages.CHECK_UPDATE_TTL,
});
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "updateTTL") : null;
next(error);
}
};
Expand All @@ -143,4 +176,5 @@ module.exports = {
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
};
2 changes: 2 additions & 0 deletions Server/db/mongo/MongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const {
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
} = require("./modules/checkModule");

//****************************************
Expand Down Expand Up @@ -161,6 +162,7 @@ module.exports = {
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold up, dawg! We got a duplicate export in the house! 😵

Looks like updateChecksTTL is bein' exported twice, once at line 100 and again here. That ain't gonna fly, cuz it's just gonna cause mad confusion, ya feel me?

Let's keep it clean and remove this duplicate export, aight? Here's the diff to fix it:

-  updateChecksTTL,
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
updateChecksTTL,

createAlert,
getAlertsByUserId,
getAlertsByMonitorId,
Expand Down
15 changes: 15 additions & 0 deletions Server/db/mongo/modules/checkModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,26 @@ const deleteChecksByTeamId = async (teamId) => {
}
};

const updateChecksTTL = async (teamId, ttl) => {
try {
await Check.collection.dropIndex("expiry_1");
await Check.collection.createIndex(
{ expiry: 1 },
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
);
} catch (error) {
error.service = SERVICE_NAME;
error.method = "updateChecksTTL";
throw error;
}
};

module.exports = {
createCheck,
getChecksCount,
getChecks,
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
};
2 changes: 1 addition & 1 deletion Server/db/mongo/modules/monitorModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ const getMonitorStatsById = async (req) => {

return monitorStats;
} catch (error) {
error.methodName = "getMonitorStatsById";
error.method = "getMonitorStatsById";
throw error;
}
};
Expand Down
2 changes: 1 addition & 1 deletion Server/middleware/handleErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const handleErrors = (error, req, res, next) => {
const service = error.service || errorMessages.UNKNOWN_SERVICE;
logger.error(error.message, {
service: service,
methodName: error.methodName,
method: error.method,
});
res.status(status).json({ success: false, msg: message });
};
Expand Down
6 changes: 6 additions & 0 deletions Server/routes/checkRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@
checkController.deleteChecksByTeamId
);

router.put(
"/ttl",
isAllowed(["admin", "superadmin"]),

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
checkController.updateChecksTTL
);
Comment on lines +29 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yo, the new /ttl route is lookin' tight, but we gotta protect it from gettin' spammed, ya feel me?

The implementation of the /ttl route is on point with the PR objective. Mad props for using the isAllowed middleware to keep those sneaky non-admins outta there!

But hold up, the static analysis tool caught somethin' - we're missin' some rate limitin' on this bad boy. We gotta make sure it ain't gettin' hit too hard, or it might just puke all over the place, like mom's spaghetti on a sweater, ya know what I'm sayin'?

Yo, lemme help ya out with slappin' a rate limiter on there real quick, just say the word!

Tools
GitHub Check: CodeQL

[failure] 31-31: Missing rate limiting
This route handler performs authorization, but is not rate-limited.


module.exports = router;
1 change: 1 addition & 0 deletions Server/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const successMessages = {
CHECK_CREATE: "Check created successfully",
CHECK_GET: "Got checks successfully",
CHECK_DELETE: "Checks deleted successfully",
CHECK_UPDATE_TTL: "Checks TTL updated successfully",

//Monitor Controller
MONITOR_GET_ALL: "Got all monitors successfully",
Expand Down
5 changes: 5 additions & 0 deletions Server/validation/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ const deleteChecksByTeamIdParamValidation = joi.object({
teamId: joi.string().required(),
});

const updateChecksTTLBodyValidation = joi.object({
ttl: joi.number().required(),
});

//****************************************
// PageSpeedCheckValidation
//****************************************
Expand Down Expand Up @@ -423,6 +427,7 @@ module.exports = {
getTeamChecksQueryValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
deleteUserParamValidation,
getPageSpeedCheckParamValidation,
createPageSpeedCheckParamValidation,
Expand Down