monitor import/export

This commit is contained in:
Alex Holliday committed 2025-11-20 23:27:30 +00:00
1 parent af6fede1b3
commit 6881be7555
12 files changed
+381 -154

No files matched your search

@@ -42,6 +42,11 @@ export const SettingsSwitch = () => {
orgPermissions.includes("invite.write") ||
orgPermissions.includes("*");
const hasMonitorRead =
orgPermissions.includes("monitors.*") ||
orgPermissions.includes("monitors.read") ||
orgPermissions.includes("*");
return (
<>
<Tooltip title="Settings" placement="top">
@@ -69,6 +74,11 @@ export const SettingsSwitch = () => {
horizontal: "right",
}}
>
{hasMonitorRead && (
<MenuItem onClick={() => handleClick("export")}>
<Typography>Import | Export</Typography>
</MenuItem>
)}
<MenuItem onClick={() => handleClick("profile")}>
<Typography>Profile</Typography>
</MenuItem>
+38
View File
@@ -182,3 +182,41 @@ export const useDelete = <R = any,>() => {
return { deleteFn, loading, error };
};
export const useGetOnDemand = <R = any>() => {
const currentTeamId = useAppSelector((state) => state.auth.selectedTeamId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError } = useToast();
const getFn = async (
endpoint: string,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await get<ApiResponse<R>>(endpoint, {
...config,
headers: {
...config?.headers,
"x-team-id": currentTeamId,
},
});
return res.data;
} catch (err: any) {
console.error(err);
const errMsg =
err?.response?.data?.message || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { get: getFn, loading, error };
};
+153
View File
@@ -0,0 +1,153 @@
import { BasePage, ConfigBox } from "@/components/design-elements/index.js";
import Stack from "@mui/material/Stack";
import Box from "@mui/material/Box";
import { Button } from "@/components/inputs";
import { Typography } from "@mui/material";
import { useGetOnDemand, usePost } from "@/hooks/UseApi";
import { useTheme } from "@mui/material/styles";
import { useRef } from "react";
const ExportPage = () => {
const fileInputRef = useRef<HTMLInputElement | null>(null);
const theme = useTheme();
const { get, loading } = useGetOnDemand();
const { post, loading: isPosting } = usePost();
const handleExport = async () => {
const res = await get("/monitors/export");
if (!res?.data) {
return;
}
const blob = new Blob([JSON.stringify(res.data, null, 2)], {
type: "application/json",
});
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "monitors.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
const handleImport = async () => {
if (!fileInputRef.current) {
return;
}
const input = fileInputRef.current;
const handleFileChange = async () => {
if (!input.files || input.files.length === 0) {
return;
}
const file = input.files[0];
try {
const text = await file.text();
const json = JSON.parse(text);
const res = await post("/monitors/import", { monitors: json });
console.log(res);
} catch (error) {
console.error("Failed to parse JSON", error);
} finally {
input.value = "";
input.removeEventListener("change", handleFileChange);
}
};
input.addEventListener("change", handleFileChange);
input.click();
};
return (
<BasePage>
<ConfigBox
title="Export current team's monitors"
subtitle="This will only export the basic monitor settings, not the check history, incidents history, or notifications"
rightContent={
<Stack gap={theme.spacing(4)}>
<Typography>Click here to export your monitors to JSON</Typography>
<Box>
<Button
loading={loading}
variant="contained"
color="accent"
onClick={handleExport}
>
Export to JSON
</Button>
</Box>
</Stack>
}
/>
<ConfigBox
title="Import monitors"
subtitle="Import monitors from a JSON file. Sample:"
leftContent={
<Box
component={"pre"}
sx={{
fontFamily: "monospace",
p: 2,
borderRadius: 1,
overflow: "auto",
}}
>
<code>
{JSON.stringify(
[
{
name: "Metrics",
url: "http://localhost:59232/api/v1/metrics",
type: "infrastructure",
interval: 60000,
n: 3,
},
{
name: "Yahoo",
url: "https://www.yahoo.com",
type: "pagespeed",
interval: 180000,
n: 3,
},
],
null,
2
)}
</code>
</Box>
}
rightContent={
<Stack gap={theme.spacing(4)}>
<Typography>
Click here to import your monitors from JSON
</Typography>
<Box>
<Button
loading={isPosting}
variant="contained"
color="accent"
onClick={handleImport}
>
Import from JSON
</Button>
<input
ref={fileInputRef}
type="file"
accept="application/json"
style={{ display: "none" }}
/>
</Box>
</Stack>
}
/>
</BasePage>
);
};
export default ExportPage;
+24 -20
View File
@@ -291,29 +291,33 @@ export const SettingsForm = ({
</Stack>
)}
/>
<Stack
gap={theme.spacing(4)}
direction="row"
justifyContent="flex-end"
>
<Button
loading={loading}
variant="contained"
color="accent"
onClick={() => {
onTest(formValues);
}}
>
Test transport config
</Button>
<Button
loading={loading}
type="submit"
variant="contained"
color="accent"
>
Save e-mail settings
</Button>
</Stack>
</Stack>
}
/>
<Stack gap={theme.spacing(4)} direction="row" justifyContent="flex-end">
<Button
loading={loading}
variant="contained"
color="accent"
onClick={() => {
onTest(formValues);
}}
>
Test transport config
</Button>
<Button
loading={loading}
type="submit"
variant="contained"
color="accent"
>
{t("settingsSave")}
</Button>
</Stack>
</Stack>
);
};
+3
View File
@@ -45,6 +45,8 @@ import ResetPage from "@/pages/recovery/Reset";
import ProfilePage from "@/pages/profile/Profile";
import ExportPage from "@/pages/export/Export";
import RootLayout from "@/components/layouts/RootLayout";
import { ProtectedRoute } from "@/components/protected-route";
@@ -132,6 +134,7 @@ const Router = () => {
<Route path="diagnostics" element={<DiagnosticPage />} />
<Route path="profile" element={<ProfilePage />} />
<Route path="export" element={<ExportPage />} />
</Route>
</Routes>
);