Merge pull request #1927 from bluewave-labs/fix/uptime-refactor
fix: uptime refactor
This commit is contained in:
5 files changed
+198
-26
No files matched your search
@@ -58,7 +58,7 @@ const BarChart = ({ checks = [] }) => {
|
|||||||
<>
|
<>
|
||||||
<Typography>
|
<Typography>
|
||||||
{formatDateWithTz(
|
{formatDateWithTz(
|
||||||
check.createdAt,
|
check.updatedAt,
|
||||||
"ddd, MMMM D, YYYY, HH:mm A",
|
"ddd, MMMM D, YYYY, HH:mm A",
|
||||||
uiTimezone
|
uiTimezone
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { networkService } from "../main";
|
||||||
|
import { createToast } from "../Utils/toastUtils";
|
||||||
|
import { useTheme } from "@emotion/react";
|
||||||
|
import { useMonitorUtils } from "./useMonitorUtils";
|
||||||
|
|
||||||
|
export const useFetchMonitorsWithChecks = ({
|
||||||
|
teamId,
|
||||||
|
types,
|
||||||
|
limit,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
filter,
|
||||||
|
field,
|
||||||
|
order,
|
||||||
|
triggerUpdate,
|
||||||
|
}) => {
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [count, setCount] = useState(undefined);
|
||||||
|
const [monitors, setMonitors] = useState(undefined);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
|
||||||
|
const theme = useTheme();
|
||||||
|
const { getMonitorWithPercentage } = useMonitorUtils();
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchMonitors = async () => {
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
const res = await networkService.getMonitorsWithChecksByTeamId({
|
||||||
|
teamId,
|
||||||
|
limit,
|
||||||
|
types,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
filter,
|
||||||
|
field,
|
||||||
|
order,
|
||||||
|
});
|
||||||
|
const { count, monitors } = res?.data?.data ?? {};
|
||||||
|
const mappedMonitors = monitors.map((monitor) =>
|
||||||
|
getMonitorWithPercentage(monitor, theme)
|
||||||
|
);
|
||||||
|
setMonitors(mappedMonitors);
|
||||||
|
setCount(count?.monitorsCount ?? 0);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
setNetworkError(true);
|
||||||
|
createToast({
|
||||||
|
body: error.message,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchMonitors();
|
||||||
|
}, [
|
||||||
|
field,
|
||||||
|
filter,
|
||||||
|
getMonitorWithPercentage,
|
||||||
|
limit,
|
||||||
|
order,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
teamId,
|
||||||
|
theme,
|
||||||
|
types,
|
||||||
|
]);
|
||||||
|
return [monitors, count, isLoading, networkError];
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useFetchMonitorsWithChecks;
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { networkService } from "../main";
|
||||||
|
import { createToast } from "../Utils/toastUtils";
|
||||||
|
|
||||||
|
export const useFetchMonitorsWithSummary = ({ teamId, types }) => {
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [monitors, setMonitors] = useState(undefined);
|
||||||
|
const [monitorsSummary, setMonitorsSummary] = useState(undefined);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchMonitors = async () => {
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
const res = await networkService.getMonitorsWithSummaryByTeamId({
|
||||||
|
teamId,
|
||||||
|
types,
|
||||||
|
});
|
||||||
|
const { monitors, summary } = res?.data?.data ?? {};
|
||||||
|
setMonitors(monitors);
|
||||||
|
setMonitorsSummary(summary);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
setNetworkError(true);
|
||||||
|
createToast({
|
||||||
|
body: error.message,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchMonitors();
|
||||||
|
}, [teamId, types]);
|
||||||
|
return [monitors, monitorsSummary, isLoading, networkError];
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useFetchMonitorsWithSummary;
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
// Required Data
|
||||||
|
// 1. Monitor summary
|
||||||
|
// 2. List of monitors filtered by search term with 25 checks each
|
||||||
|
// 2a.List of monitors must have the total number of monitors that match.
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
||||||
import Greeting from "../../../Utils/greeting";
|
import Greeting from "../../../Utils/greeting";
|
||||||
@@ -23,9 +28,11 @@ import useMonitorsFetch from "./Hooks/useMonitorsFetch";
|
|||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
|
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
import useFetchMonitorsWithSummary from "../../../Hooks/useFetchMonitorsWithSummary";
|
||||||
|
import useFetchMonitorsWithChecks from "../../../Hooks/useFetchMonitorsWithChecks";
|
||||||
|
|
||||||
const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
|
const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
|
||||||
|
const TYPES = ["http", "ping", "docker", "port"];
|
||||||
const CreateMonitorButton = ({ shouldRender }) => {
|
const CreateMonitorButton = ({ shouldRender }) => {
|
||||||
// Utils
|
// Utils
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -89,23 +96,32 @@ const UptimeMonitors = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const teamId = user.teamId;
|
const teamId = user.teamId;
|
||||||
const {
|
|
||||||
monitorsAreLoading,
|
const [monitors, monitorsSummary, monitorsWithSummaryIsLoading, networkError] =
|
||||||
monitors,
|
useFetchMonitorsWithSummary({
|
||||||
filteredMonitors,
|
teamId,
|
||||||
monitorsSummary,
|
types: TYPES,
|
||||||
networkError,
|
});
|
||||||
} = useMonitorsFetch({
|
|
||||||
|
const [
|
||||||
|
monitorsWithChecks,
|
||||||
|
monitorsWithChecksCount,
|
||||||
|
monitorsWithChecksIsLoading,
|
||||||
|
monitorsWithChecksNetworkError,
|
||||||
|
] = useFetchMonitorsWithChecks({
|
||||||
teamId,
|
teamId,
|
||||||
|
types: TYPES,
|
||||||
limit: 25,
|
limit: 25,
|
||||||
page: page,
|
page: page,
|
||||||
rowsPerPage: rowsPerPage,
|
rowsPerPage: rowsPerPage,
|
||||||
filter: search,
|
filter: search,
|
||||||
field: sort?.field,
|
field: sort?.field,
|
||||||
order: sort?.order,
|
order: sort?.order,
|
||||||
triggerUpdate: monitorUpdateTrigger,
|
triggerUpdate,
|
||||||
});
|
});
|
||||||
const totalMonitors = monitorsSummary?.totalMonitors;
|
|
||||||
|
const isLoading = monitorsWithSummaryIsLoading || monitorsWithChecksIsLoading;
|
||||||
|
|
||||||
if (networkError) {
|
if (networkError) {
|
||||||
return (
|
return (
|
||||||
<GenericFallback>
|
<GenericFallback>
|
||||||
@@ -121,8 +137,9 @@ const UptimeMonitors = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
!monitorsAreLoading &&
|
!isLoading &&
|
||||||
(totalMonitors === 0 || typeof totalMonitors === "undefined")
|
(monitorsSummary?.totalMonitors === 0 ||
|
||||||
|
typeof monitorsSummary?.totalMonitors === "undefined")
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
<Fallback
|
<Fallback
|
||||||
@@ -147,19 +164,19 @@ const UptimeMonitors = () => {
|
|||||||
<Breadcrumbs list={BREADCRUMBS} />
|
<Breadcrumbs list={BREADCRUMBS} />
|
||||||
<CreateMonitorHeader
|
<CreateMonitorHeader
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
shouldRender={!monitorsAreLoading}
|
shouldRender={!isLoading}
|
||||||
path="/uptime/create"
|
path="/uptime/create"
|
||||||
/>
|
/>
|
||||||
<Greeting type="uptime" />
|
<Greeting type="uptime" />
|
||||||
<StatusBoxes
|
<StatusBoxes
|
||||||
monitorsSummary={monitorsSummary}
|
monitorsSummary={monitorsSummary}
|
||||||
shouldRender={!monitorsAreLoading}
|
shouldRender={!monitorsWithSummaryIsLoading}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Stack direction={"row"}>
|
<Stack direction={"row"}>
|
||||||
<MonitorCountHeader
|
<MonitorCountHeader
|
||||||
shouldRender={monitors?.length > 0 && !monitorsAreLoading}
|
shouldRender={monitors?.length > 0 && !monitorsWithSummaryIsLoading}
|
||||||
monitorCount={totalMonitors}
|
monitorCount={monitorsSummary?.totalMonitors}
|
||||||
heading={"Uptime monitors"}
|
heading={"Uptime monitors"}
|
||||||
></MonitorCountHeader>
|
></MonitorCountHeader>
|
||||||
<SearchComponent
|
<SearchComponent
|
||||||
@@ -170,19 +187,14 @@ const UptimeMonitors = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
<UptimeDataTable
|
<UptimeDataTable
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
isLoading={monitorsAreLoading}
|
isSearching={isSearching}
|
||||||
filteredMonitors={filteredMonitors}
|
filteredMonitors={monitorsWithChecks}
|
||||||
monitors={monitors}
|
|
||||||
monitorCount={totalMonitors}
|
|
||||||
sort={sort}
|
sort={sort}
|
||||||
setSort={setSort}
|
setSort={setSort}
|
||||||
setSearch={setSearch}
|
monitorsAreLoading={monitorsWithChecksIsLoading}
|
||||||
isSearching={isSearching}
|
|
||||||
monitorsAreLoading={monitorsAreLoading}
|
|
||||||
triggerUpdate={triggerUpdate}
|
|
||||||
/>
|
/>
|
||||||
<Pagination
|
<Pagination
|
||||||
itemCount={totalMonitors}
|
itemCount={monitorsWithChecksCount}
|
||||||
paginationLabel="monitors"
|
paginationLabel="monitors"
|
||||||
page={page}
|
page={page}
|
||||||
rowsPerPage={rowsPerPage}
|
rowsPerPage={rowsPerPage}
|
||||||
|
|||||||
@@ -1033,6 +1033,58 @@ class NetworkService {
|
|||||||
|
|
||||||
return this.axiosInstance.delete(`/status-page/${encodedUrl}`, {});
|
return this.axiosInstance.delete(`/status-page/${encodedUrl}`, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ************************************
|
||||||
|
// Fetch monitors with summary by TeamID
|
||||||
|
// ************************************
|
||||||
|
async getMonitorsWithSummaryByTeamId(config) {
|
||||||
|
const { teamId, types } = config;
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
if (types) {
|
||||||
|
types.forEach((type) => {
|
||||||
|
params.append("type", type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.axiosInstance.get(
|
||||||
|
`/monitors/summary/team/${teamId}?${params.toString()}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ************************************
|
||||||
|
// Fetch monitors with checks by TeamID
|
||||||
|
// ************************************
|
||||||
|
async getMonitorsWithChecksByTeamId(config) {
|
||||||
|
const { teamId, limit, types, page, rowsPerPage, filter, field, order } = config;
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
if (limit) params.append("limit", limit);
|
||||||
|
if (types) {
|
||||||
|
types.forEach((type) => {
|
||||||
|
params.append("type", type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (page) params.append("page", page);
|
||||||
|
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
|
||||||
|
if (filter) params.append("filter", filter);
|
||||||
|
if (field) params.append("field", field);
|
||||||
|
if (order) params.append("order", order);
|
||||||
|
|
||||||
|
return this.axiosInstance.get(
|
||||||
|
`/monitors/team/${teamId}/with-checks?${params.toString()}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NetworkService;
|
export default NetworkService;
|
||||||
|
|||||||
Reference in new issue
Block a user