This commit is contained in:
Alex Holliday committed 2024-08-08 12:37:02 -07:00
1 parent 284253638b
commit b3fab31a61
5 files changed
+43 -9

No files matched your search

+3 -5
View File
@@ -119,10 +119,8 @@ const DetailsPage = () => {
const fetchDataForTable = useCallback(async () => { const fetchDataForTable = useCallback(async () => {
try { try {
const limit = 0;
const res = await axiosInstance.get( const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc&limit=${limit}`, `/checks/${monitorId}?sortOrder=desc&filter=${filter}`,
{ {
headers: { headers: {
Authorization: `Bearer ${authToken}`, Authorization: `Bearer ${authToken}`,
@@ -135,7 +133,7 @@ const DetailsPage = () => {
{ id: 2, name: "Date & Time" }, { id: 2, name: "Date & Time" },
{ id: 3, name: "Message" }, { id: 3, name: "Message" },
], ],
rows: res.data.data.checks.map((check, idx) => { rows: res.data.data.map((check, idx) => {
const status = check.status === true ? "up" : "down"; const status = check.status === true ? "up" : "down";
return { return {
@@ -165,7 +163,7 @@ const DetailsPage = () => {
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
}, [authToken, monitorId]); }, [authToken, monitorId, filter]);
const fetchMonitor = useCallback(async () => { const fetchMonitor = useCallback(async () => {
try { try {
+3 -1
View File
@@ -2,6 +2,7 @@ const {
createCheckParamValidation, createCheckParamValidation,
createCheckBodyValidation, createCheckBodyValidation,
getChecksParamValidation, getChecksParamValidation,
getChecksQueryValidation,
deleteChecksParamValidation, deleteChecksParamValidation,
} = require("../validation/joi"); } = require("../validation/joi");
const { successMessages } = require("../utils/messages"); const { successMessages } = require("../utils/messages");
@@ -35,6 +36,7 @@ const createCheck = async (req, res, next) => {
const getChecks = async (req, res, next) => { const getChecks = async (req, res, next) => {
try { try {
await getChecksParamValidation.validateAsync(req.params); await getChecksParamValidation.validateAsync(req.params);
await getChecksQueryValidation.validateAsync(req.query);
} catch (error) { } catch (error) {
error.status = 422; error.status = 422;
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
@@ -45,7 +47,7 @@ const getChecks = async (req, res, next) => {
} }
try { try {
const checks = await req.db.getChecks(req.params.monitorId); const checks = await req.db.getChecks(req);
return res return res
.status(200) .status(200)
.json({ success: true, msg: successMessages.CHECK_GET, data: checks }); .json({ success: true, msg: successMessages.CHECK_GET, data: checks });
+30 -2
View File
@@ -30,9 +30,37 @@ const createCheck = async (checkData) => {
* @throws {Error} * @throws {Error}
*/ */
const getChecks = async (monitorId) => { const getChecks = async (req) => {
const filterLookup = {
day: new Date(new Date().setDate(new Date().getDate() - 1)),
week: new Date(new Date().setDate(new Date().getDate() - 7)),
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
};
try { try {
const checks = await Check.find({ monitorId }); const { monitorId } = req.params;
let { sortOrder, limit, filter } = req.query;
// Default limit to 0 if not provided
if (limit === undefined) limit = 0;
// Default sort order is newest -> oldest
if (sortOrder === "asc") {
sortOrder = 1;
} else if (sortOrder === "desc") {
sortOrder = -1;
} else sortOrder = -1;
// Build query
const checksQuery = { monitorId: monitorId };
// Filter checks by "day", "week", or "month"
if (filter !== undefined) {
checksQuery.createdAt = { $gte: filterLookup[filter] };
}
const checks = await Check.find(checksQuery)
.limit(limit)
.sort({ createdAt: sortOrder });
return checks; return checks;
} catch (error) { } catch (error) {
throw error; throw error;
-1
View File
@@ -19,7 +19,6 @@ const NormalizeData = (checks, rangeMin, rangeMax) => {
const normalizedChecks = checks.map((check) => { const normalizedChecks = checks.map((check) => {
const originalResponseTime = check.responseTime; const originalResponseTime = check.responseTime;
console.log(originalResponseTime);
// Normalize the response time between 1 and 100 // Normalize the response time between 1 and 100
let normalizedResponseTime = let normalizedResponseTime =
rangeMin + rangeMin +
+7
View File
@@ -232,6 +232,12 @@ const getChecksParamValidation = joi.object({
monitorId: joi.string().required(), monitorId: joi.string().required(),
}); });
const getChecksQueryValidation = joi.object({
sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(),
filter: joi.string().valid("day", "week", "month"),
});
const deleteChecksParamValidation = joi.object({ const deleteChecksParamValidation = joi.object({
monitorId: joi.string().required(), monitorId: joi.string().required(),
}); });
@@ -311,6 +317,7 @@ module.exports = {
createCheckParamValidation, createCheckParamValidation,
createCheckBodyValidation, createCheckBodyValidation,
getChecksParamValidation, getChecksParamValidation,
getChecksQueryValidation,
deleteChecksParamValidation, deleteChecksParamValidation,
deleteUserParamValidation, deleteUserParamValidation,
getPageSpeedCheckParamValidation, getPageSpeedCheckParamValidation,