Use new query in Uptime page
This commit is contained in:
1 parent
7862f8d967
commit
de3a8be76c
3 files changed
+93
-32
No files matched your search
@@ -12,13 +12,42 @@ import ActionsMenu from "../actionsMenu";
|
|||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import useDebounce from "../../../../Utils/debounce";
|
|
||||||
import { useState } from "react";
|
|
||||||
import useUtils from "../../utils";
|
import useUtils from "../../utils";
|
||||||
import { memo } from "react";
|
import { memo } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import "../index.css";
|
import "../index.css";
|
||||||
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UptimeDataTable displays a table of uptime monitors with sorting, searching, and action capabilities
|
||||||
|
* @param {Object} props - Component props
|
||||||
|
* @param {boolean} props.isAdmin - Whether the current user has admin privileges
|
||||||
|
* @param {boolean} props.isLoading - Loading state of the table
|
||||||
|
* @param {Array<{
|
||||||
|
* _id: string,
|
||||||
|
* url: string,
|
||||||
|
* title: string,
|
||||||
|
* percentage: number,
|
||||||
|
* percentageColor: string,
|
||||||
|
* monitor: {
|
||||||
|
* _id: string,
|
||||||
|
* type: string,
|
||||||
|
* checks: Array
|
||||||
|
* }
|
||||||
|
* }>} props.monitors - Array of monitor objects to display
|
||||||
|
* @param {number} props.monitorCount - Total count of monitors
|
||||||
|
* @param {Object} props.sort - Current sort configuration
|
||||||
|
* @param {string} props.sort.field - Field to sort by
|
||||||
|
* @param {'asc'|'desc'} props.sort.order - Sort direction
|
||||||
|
* @param {Function} props.setSort - Callback to update sort configuration
|
||||||
|
* @param {string} props.search - Current search query
|
||||||
|
* @param {Function} props.setSearch - Callback to update search query
|
||||||
|
* @param {boolean} props.isSearching - Whether a search is in progress
|
||||||
|
* @param {Function} props.setIsSearching - Callback to update search state
|
||||||
|
* @param {Function} props.setIsLoading - Callback to update loading state
|
||||||
|
* @param {Function} props.triggerUpdate - Callback to trigger a data refresh
|
||||||
|
* @returns {JSX.Element} Rendered component
|
||||||
|
*/
|
||||||
const UptimeDataTable = ({
|
const UptimeDataTable = ({
|
||||||
isAdmin,
|
isAdmin,
|
||||||
isLoading,
|
isLoading,
|
||||||
@@ -30,6 +59,7 @@ const UptimeDataTable = ({
|
|||||||
setSearch,
|
setSearch,
|
||||||
isSearching,
|
isSearching,
|
||||||
setIsSearching,
|
setIsSearching,
|
||||||
|
setIsLoading,
|
||||||
triggerUpdate,
|
triggerUpdate,
|
||||||
}) => {
|
}) => {
|
||||||
const { determineState } = useUtils();
|
const { determineState } = useUtils();
|
||||||
@@ -37,8 +67,6 @@ const UptimeDataTable = ({
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
//Utils
|
|
||||||
const debouncedFilter = useDebounce(search, 500);
|
|
||||||
const handleSearch = (value) => {
|
const handleSearch = (value) => {
|
||||||
setIsSearching(true);
|
setIsSearching(true);
|
||||||
setSearch(value);
|
setSearch(value);
|
||||||
@@ -146,6 +174,7 @@ const UptimeDataTable = ({
|
|||||||
monitor={row.monitor}
|
monitor={row.monitor}
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
updateRowCallback={triggerUpdate}
|
updateRowCallback={triggerUpdate}
|
||||||
|
setIsLoading={setIsLoading}
|
||||||
pauseCallback={triggerUpdate}
|
pauseCallback={triggerUpdate}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
@@ -187,7 +216,7 @@ const UptimeDataTable = ({
|
|||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box position="relative">
|
<Box position="relative">
|
||||||
{isSearching && (
|
{(isSearching || isLoading) && (
|
||||||
<>
|
<>
|
||||||
<Box
|
<Box
|
||||||
width="100%"
|
width="100%"
|
||||||
@@ -240,3 +269,21 @@ const UptimeDataTable = ({
|
|||||||
|
|
||||||
const MemoizedUptimeDataTable = memo(UptimeDataTable);
|
const MemoizedUptimeDataTable = memo(UptimeDataTable);
|
||||||
export default MemoizedUptimeDataTable;
|
export default MemoizedUptimeDataTable;
|
||||||
|
|
||||||
|
UptimeDataTable.propTypes = {
|
||||||
|
isAdmin: PropTypes.bool,
|
||||||
|
isLoading: PropTypes.bool,
|
||||||
|
monitors: PropTypes.array,
|
||||||
|
monitorCount: PropTypes.number,
|
||||||
|
sort: PropTypes.shape({
|
||||||
|
field: PropTypes.string,
|
||||||
|
order: PropTypes.oneOf(["asc", "desc"]),
|
||||||
|
}),
|
||||||
|
setSort: PropTypes.func,
|
||||||
|
search: PropTypes.string,
|
||||||
|
setSearch: PropTypes.func,
|
||||||
|
isSearching: PropTypes.bool,
|
||||||
|
setIsSearching: PropTypes.func,
|
||||||
|
setIsLoading: PropTypes.func,
|
||||||
|
triggerUpdate: PropTypes.func,
|
||||||
|
};
|
||||||
@@ -8,13 +8,18 @@ import { IconButton, Menu, MenuItem } from "@mui/material";
|
|||||||
import {
|
import {
|
||||||
deleteUptimeMonitor,
|
deleteUptimeMonitor,
|
||||||
pauseUptimeMonitor,
|
pauseUptimeMonitor,
|
||||||
getUptimeMonitorsByTeamId,
|
|
||||||
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import Settings from "../../../assets/icons/settings-bold.svg?react";
|
import Settings from "../../../assets/icons/settings-bold.svg?react";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import Dialog from "../../../Components/Dialog";
|
import Dialog from "../../../Components/Dialog";
|
||||||
|
|
||||||
const ActionsMenu = ({ monitor, isAdmin, updateRowCallback, pauseCallback }) => {
|
const ActionsMenu = ({
|
||||||
|
monitor,
|
||||||
|
isAdmin,
|
||||||
|
updateRowCallback,
|
||||||
|
pauseCallback,
|
||||||
|
setIsLoading,
|
||||||
|
}) => {
|
||||||
const [anchorEl, setAnchorEl] = useState(null);
|
const [anchorEl, setAnchorEl] = useState(null);
|
||||||
const [actions, setActions] = useState({});
|
const [actions, setActions] = useState({});
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
@@ -33,7 +38,6 @@ const ActionsMenu = ({ monitor, isAdmin, updateRowCallback, pauseCallback }) =>
|
|||||||
);
|
);
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
setIsOpen(false); // close modal
|
setIsOpen(false); // close modal
|
||||||
dispatch(getUptimeMonitorsByTeamId(authState.authToken));
|
|
||||||
updateRowCallback();
|
updateRowCallback();
|
||||||
createToast({ body: "Monitor deleted successfully." });
|
createToast({ body: "Monitor deleted successfully." });
|
||||||
} else {
|
} else {
|
||||||
@@ -43,6 +47,7 @@ const ActionsMenu = ({ monitor, isAdmin, updateRowCallback, pauseCallback }) =>
|
|||||||
|
|
||||||
const handlePause = async () => {
|
const handlePause = async () => {
|
||||||
try {
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
const action = await dispatch(
|
const action = await dispatch(
|
||||||
pauseUptimeMonitor({ authToken, monitorId: monitor._id })
|
pauseUptimeMonitor({ authToken, monitorId: monitor._id })
|
||||||
);
|
);
|
||||||
@@ -223,6 +228,7 @@ ActionsMenu.propTypes = {
|
|||||||
isAdmin: PropTypes.bool,
|
isAdmin: PropTypes.bool,
|
||||||
updateRowCallback: PropTypes.func,
|
updateRowCallback: PropTypes.func,
|
||||||
pauseCallback: PropTypes.func,
|
pauseCallback: PropTypes.func,
|
||||||
|
setIsLoading: PropTypes.func,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ActionsMenu;
|
export default ActionsMenu;
|
||||||
@@ -9,11 +9,7 @@ import { Pagination } from "../../../Components/Table/TablePagination";
|
|||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useEffect, useState, useCallback, useMemo, useRef } from "react";
|
import { useEffect, useState, useCallback, useMemo } from "react";
|
||||||
import {
|
|
||||||
getUptimeSummaryByTeamId,
|
|
||||||
getUptimeMonitorsByTeamId,
|
|
||||||
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
|
||||||
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
|
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
|
||||||
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
@@ -21,6 +17,7 @@ import { useNavigate } from "react-router-dom";
|
|||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
||||||
import useDebounce from "../../../Utils/debounce";
|
import useDebounce from "../../../Utils/debounce";
|
||||||
|
import { networkService } from "../../../main";
|
||||||
|
|
||||||
const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
|
const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
|
||||||
|
|
||||||
@@ -33,7 +30,9 @@ const UptimeMonitors = () => {
|
|||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const [page, setPage] = useState(0);
|
const [page, setPage] = useState(0);
|
||||||
const [isSearching, setIsSearching] = useState(false);
|
const [isSearching, setIsSearching] = useState(false);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
|
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
|
||||||
|
const [monitorsSummary, setMonitorsSummary] = useState({});
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const debouncedFilter = useDebounce(search, 500);
|
const debouncedFilter = useDebounce(search, 500);
|
||||||
@@ -41,7 +40,6 @@ const UptimeMonitors = () => {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const isAdmin = useIsAdmin();
|
const isAdmin = useIsAdmin();
|
||||||
const { isLoading, monitorsSummary } = useSelector((state) => state.uptimeMonitors);
|
|
||||||
const authState = useSelector((state) => state.auth);
|
const authState = useSelector((state) => state.auth);
|
||||||
|
|
||||||
const fetchParams = useMemo(
|
const fetchParams = useMemo(
|
||||||
@@ -89,30 +87,38 @@ const UptimeMonitors = () => {
|
|||||||
|
|
||||||
const fetchMonitors = useCallback(async () => {
|
const fetchMonitors = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const action = await dispatch(getUptimeMonitorsByTeamId(fetchParams));
|
setIsLoading(true);
|
||||||
if (action.payload.success) {
|
const config = fetchParams;
|
||||||
const { monitors } = action.payload.data;
|
const res = await networkService.getMonitorsByTeamId({
|
||||||
const mappedMonitors = monitors.map((monitor) =>
|
authToken: config.authToken,
|
||||||
getMonitorWithPercentage(monitor, theme)
|
teamId: config.teamId,
|
||||||
);
|
limit: 25,
|
||||||
setMonitors(mappedMonitors);
|
types: ["http", "ping", "docker", "port"],
|
||||||
} else {
|
page: config.page,
|
||||||
// TODO: Check for other errors?
|
rowsPerPage: config.rowsPerPage,
|
||||||
throw new Error("Error fetching monitors");
|
filter: config.filter,
|
||||||
}
|
field: config.sort.field,
|
||||||
|
order: config.sort.order,
|
||||||
|
});
|
||||||
|
const { monitors, summary } = res.data.data;
|
||||||
|
const mappedMonitors = monitors.map((monitor) =>
|
||||||
|
getMonitorWithPercentage(monitor, theme)
|
||||||
|
);
|
||||||
|
setMonitors(mappedMonitors);
|
||||||
|
setMonitorsSummary(summary);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({
|
createToast({
|
||||||
body: "Error fetching monitors",
|
body: "Error fetching monitors",
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
setIsSearching(false);
|
setIsSearching(false);
|
||||||
}
|
}
|
||||||
}, [fetchParams, dispatch, getMonitorWithPercentage, theme]);
|
}, [fetchParams, getMonitorWithPercentage, theme]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(getUptimeSummaryByTeamId(authState.authToken));
|
|
||||||
fetchMonitors();
|
fetchMonitors();
|
||||||
}, [fetchMonitors, monitorUpdateTrigger, authState.authToken, dispatch]);
|
}, [fetchMonitors, monitorUpdateTrigger]);
|
||||||
|
|
||||||
const handleChangePage = (event, newPage) => {
|
const handleChangePage = (event, newPage) => {
|
||||||
setPage(newPage);
|
setPage(newPage);
|
||||||
@@ -131,7 +137,7 @@ const UptimeMonitors = () => {
|
|||||||
const triggerUpdate = () => {
|
const triggerUpdate = () => {
|
||||||
setMonitorUpdateTrigger((prev) => !prev);
|
setMonitorUpdateTrigger((prev) => !prev);
|
||||||
};
|
};
|
||||||
const totalMonitors = monitorsSummary?.monitorCounts?.total;
|
const totalMonitors = monitorsSummary.totalMonitors;
|
||||||
const hasMonitors = totalMonitors > 0;
|
const hasMonitors = totalMonitors > 0;
|
||||||
const canAddMonitor = isAdmin && hasMonitors;
|
const canAddMonitor = isAdmin && hasMonitors;
|
||||||
|
|
||||||
@@ -176,19 +182,20 @@ const UptimeMonitors = () => {
|
|||||||
>
|
>
|
||||||
<StatusBox
|
<StatusBox
|
||||||
title="up"
|
title="up"
|
||||||
value={monitorsSummary?.monitorCounts?.up ?? 0}
|
value={monitorsSummary?.upMonitors ?? 0}
|
||||||
/>
|
/>
|
||||||
<StatusBox
|
<StatusBox
|
||||||
title="down"
|
title="down"
|
||||||
value={monitorsSummary?.monitorCounts?.down ?? 0}
|
value={monitorsSummary?.downMonitors ?? 0}
|
||||||
/>
|
/>
|
||||||
<StatusBox
|
<StatusBox
|
||||||
title="paused"
|
title="paused"
|
||||||
value={monitorsSummary?.monitorCounts?.paused ?? 0}
|
value={monitorsSummary?.pausedMonitors ?? 0}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
<UptimeDataTable
|
<UptimeDataTable
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
|
isLoading={isLoading}
|
||||||
monitors={monitors}
|
monitors={monitors}
|
||||||
monitorCount={totalMonitors}
|
monitorCount={totalMonitors}
|
||||||
sort={sort}
|
sort={sort}
|
||||||
@@ -197,6 +204,7 @@ const UptimeMonitors = () => {
|
|||||||
setSearch={setSearch}
|
setSearch={setSearch}
|
||||||
isSearching={isSearching}
|
isSearching={isSearching}
|
||||||
setIsSearching={setIsSearching}
|
setIsSearching={setIsSearching}
|
||||||
|
setIsLoading={setIsLoading}
|
||||||
triggerUpdate={triggerUpdate}
|
triggerUpdate={triggerUpdate}
|
||||||
/>
|
/>
|
||||||
<Pagination
|
<Pagination
|
||||||
|
|||||||
Reference in new issue
Block a user