diff --git a/Client/src/Components/BasicTable/index.jsx b/Client/src/Components/BasicTable/index.jsx
index 3f89de700..76f1c61c7 100644
--- a/Client/src/Components/BasicTable/index.jsx
+++ b/Client/src/Components/BasicTable/index.jsx
@@ -9,7 +9,7 @@ import {
Pagination,
PaginationItem,
} from "@mui/material";
-import React, { useState } from "react";
+import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import "./index.css";
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
@@ -73,6 +73,10 @@ const BasicTable = ({ data, paginated, reversed, rowsPerPage = 5 }) => {
const [page, setPage] = useState(0);
+ useEffect(() => {
+ setPage(0);
+ }, [data]);
+
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
diff --git a/Client/src/Components/Charts/MonitorDetailsAreaChart/index.jsx b/Client/src/Components/Charts/MonitorDetailsAreaChart/index.jsx
index 39aa8dcbf..b25c3b57b 100644
--- a/Client/src/Components/Charts/MonitorDetailsAreaChart/index.jsx
+++ b/Client/src/Components/Charts/MonitorDetailsAreaChart/index.jsx
@@ -47,37 +47,9 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
});
};
- const filterChecks = (checks, filter, numToDisplay) => {
- const limits = {
- day: 24 * 60 * 60 * 1000,
- week: 24 * 60 * 60 * 1000 * 7,
- month: 24 * 60 * 60 * 1000 * 30, //TODO better monthly calculations
- };
-
- const now = new Date().getTime();
- let result = [];
- for (let i = 0; i < checks.length; i++) {
- const checkTime = new Date(checks[i].createdAt).getTime();
- if (now - checkTime < limits[filter]) {
- result.push(checks[i]);
- }
- }
-
- // If more than numToDisplay checks, pick every nth check
- if (result.length > numToDisplay) {
- const n = Math.ceil(result.length / numToDisplay);
- result = result.filter((_, idx) => {
- return idx % n === 0;
- });
- }
-
- return result;
- };
-
let normalizedChecks = [];
if (checks && checks.length > 0) {
- const filteredChecks = filterChecks(checks, filter, 75);
- normalizedChecks = NormalizeData(filteredChecks, 10, 100);
+ normalizedChecks = NormalizeData(checks, 10, 100);
}
return (
diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx
index b17eb831b..6033ab740 100644
--- a/Client/src/Pages/Monitors/Details/index.jsx
+++ b/Client/src/Pages/Monitors/Details/index.jsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useState, useCallback } from "react";
import PropTypes from "prop-types";
import { Box, Skeleton, Stack, Typography, useTheme } from "@mui/material";
import { useSelector } from "react-redux";
@@ -115,62 +115,84 @@ const DetailsPage = () => {
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
const [filter, setFilter] = useState("day");
+ const navigate = useNavigate();
+
+ const fetchDataForTable = useCallback(async () => {
+ try {
+ const limit = 0;
+
+ const res = await axiosInstance.get(
+ `/monitors/${monitorId}?sortOrder=asc&limit=${limit}`,
+ {
+ headers: {
+ Authorization: `Bearer ${authToken}`,
+ },
+ }
+ );
+ const data = {
+ cols: [
+ { id: 1, name: "Status" },
+ { id: 2, name: "Date & Time" },
+ { id: 3, name: "Message" },
+ ],
+ rows: res.data.data.checks.map((check, idx) => {
+ const status = check.status === true ? "up" : "down";
+
+ return {
+ id: check._id,
+ data: [
+ {
+ id: idx,
+ data: (
+
+ ),
+ },
+ {
+ id: idx + 1,
+ data: new Date(check.createdAt).toLocaleString(),
+ },
+ { id: idx + 2, data: check.statusCode },
+ ],
+ };
+ }),
+ };
+
+ setData(data);
+ } catch (error) {
+ console.log(error);
+ }
+ }, [authToken, monitorId]);
+
+ const fetchMonitor = useCallback(async () => {
+ try {
+ const res = await axiosInstance.get(
+ `/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50`,
+ {
+ headers: {
+ Authorization: `Bearer ${authToken}`,
+ },
+ }
+ );
+ setMonitor(res.data.data);
+ } catch (error) {
+ console.error("Error fetching monitor of id: " + monitorId);
+ navigate("/not-found");
+ }
+ }, [authToken, monitorId, navigate, filter]);
useEffect(() => {
- const fetchMonitor = async () => {
- try {
- const res = await axiosInstance.get(
- `/monitors/${monitorId}?sortOrder=asc`,
- {
- headers: {
- Authorization: `Bearer ${authToken}`,
- },
- }
- );
- setMonitor(res.data.data);
- const data = {
- cols: [
- { id: 1, name: "Status" },
- { id: 2, name: "Date & Time" },
- { id: 3, name: "Message" },
- ],
- rows: res.data.data.checks.map((check, idx) => {
- const status = check.status === true ? "up" : "down";
-
- return {
- id: check._id,
- data: [
- {
- id: idx,
- data: (
-
- ),
- },
- {
- id: idx + 1,
- data: new Date(check.createdAt).toLocaleString(),
- },
- { id: idx + 2, data: check.statusCode },
- ],
- };
- }),
- };
-
- setData(data);
- } catch (error) {
- console.error("Error fetching monitor of id: " + monitorId);
- navigate("/not-found");
- }
- };
fetchMonitor();
- }, [monitorId, authToken]);
+ }, [fetchMonitor]);
+
+ useEffect(() => {
+ fetchDataForTable();
+ }, [fetchDataForTable]);
const theme = useTheme();
- const navigate = useNavigate();
/**
* Function to calculate uptime duration based on the most recent check.
diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js
index 9bca432c2..df9790003 100644
--- a/Server/db/mongo/modules/monitorModule.js
+++ b/Server/db/mongo/modules/monitorModule.js
@@ -32,7 +32,13 @@ const getAllMonitors = async (req, res) => {
const getMonitorById = async (req, res) => {
try {
const { monitorId } = req.params;
- let { status, limit, sortOrder } = req.query;
+ let { status, limit, sortOrder, filter, numToDisplay } = req.query;
+
+ 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)),
+ };
// This effectively removes limit, returning all checks
if (limit === undefined) limit = 0;
@@ -52,18 +58,30 @@ const getMonitorById = async (req, res) => {
checksQuery.status = status;
}
+ // Filter checks by "day", "week", or "month"
+ if (filter !== undefined) {
+ checksQuery.createdAt = { $gte: filterLookup[filter] };
+ }
+
// Determine model type
let model =
monitor.type === "http" || monitor.type === "ping"
? Check
: PageSpeedCheck;
- const checks = await model
+ let checks = await model
.find(checksQuery)
.sort({
createdAt: sortOrder,
})
.limit(limit);
+
+ // If more than numToDisplay checks, pick every nth check
+ if (numToDisplay !== undefined && checks && checks.length > numToDisplay) {
+ const n = Math.ceil(checks.length / numToDisplay);
+ checks = checks.filter((_, index) => index % n === 0);
+ }
+
const notifications = await Notification.find({ monitorId: monitor._id });
const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
return monitorWithChecks;
diff --git a/Server/validation/joi.js b/Server/validation/joi.js
index ccf16f257..e1032cb6d 100644
--- a/Server/validation/joi.js
+++ b/Server/validation/joi.js
@@ -126,6 +126,8 @@ const getMonitorByIdQueryValidation = joi.object({
status: joi.boolean(),
sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(),
+ filter: joi.string().valid("day", "week", "month"),
+ numToDisplay: joi.number(),
});
const getMonitorsByUserIdValidation = joi.object({