feat: add create api for monitor
This commit is contained in:
parent
113d49abe9
commit
b18611fa9a
17
src/client/api/model/monitor.ts
Normal file
17
src/client/api/model/monitor.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { getQueryKey } from '@trpc/react-query';
|
||||||
|
import { defaultErrorHandler, defaultSuccessHandler, trpc } from '../trpc';
|
||||||
|
|
||||||
|
export function useMonitorUpsert() {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const mutation = trpc.monitor.upsert.useMutation({
|
||||||
|
onSuccess: (data) => {
|
||||||
|
queryClient.resetQueries(getQueryKey(trpc.monitor.all));
|
||||||
|
|
||||||
|
defaultSuccessHandler();
|
||||||
|
},
|
||||||
|
onError: defaultErrorHandler,
|
||||||
|
});
|
||||||
|
|
||||||
|
return mutation;
|
||||||
|
}
|
@ -29,7 +29,7 @@ export const trpcClient = trpc.createClient({
|
|||||||
* onSuccess: defaultSuccessHandler,
|
* onSuccess: defaultSuccessHandler,
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
export function defaultSuccessHandler(data: any) {
|
export function defaultSuccessHandler() {
|
||||||
message.success('Operate Success');
|
message.success('Operate Success');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ export const NotificationSMTP: React.FC = React.memo(() => {
|
|||||||
<Select.Option value={true}>TLS (465)</Select.Option>
|
<Select.Option value={true}>TLS (465)</Select.Option>
|
||||||
</Select>
|
</Select>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item name={['payload', 'ignoreTLS']}>
|
<Form.Item name={['payload', 'ignoreTLS']} valuePropName="checked">
|
||||||
<Checkbox>Ignore TLS Error</Checkbox>
|
<Checkbox>Ignore TLS Error</Checkbox>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
|
@ -4,11 +4,12 @@ import { Button, Form, Input, InputNumber, Select } from 'antd';
|
|||||||
import { monitorProviders } from './provider';
|
import { monitorProviders } from './provider';
|
||||||
import { useEvent } from '../../../hooks/useEvent';
|
import { useEvent } from '../../../hooks/useEvent';
|
||||||
|
|
||||||
type MonitorInfoEditorValues = Omit<
|
export type MonitorInfoEditorValues = Omit<
|
||||||
Monitor,
|
Monitor,
|
||||||
'id' | 'workspaceId' | 'createdAt'
|
'id' | 'workspaceId' | 'createdAt'
|
||||||
> & {
|
> & {
|
||||||
id?: string;
|
id?: string;
|
||||||
|
payload: Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultValues: Omit<MonitorInfoEditorValues, 'payload'> = {
|
const defaultValues: Omit<MonitorInfoEditorValues, 'payload'> = {
|
||||||
|
@ -1,15 +1,23 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { useNavigate } from 'react-router';
|
||||||
|
import { useMonitorUpsert } from '../../api/model/monitor';
|
||||||
import { MonitorInfoEditor } from '../../components/modals/monitor/MonitorInfoEditor';
|
import { MonitorInfoEditor } from '../../components/modals/monitor/MonitorInfoEditor';
|
||||||
import { useCurrentWorkspaceId } from '../../store/user';
|
import { useCurrentWorkspaceId } from '../../store/user';
|
||||||
|
|
||||||
export const MonitorAdd: React.FC = React.memo(() => {
|
export const MonitorAdd: React.FC = React.memo(() => {
|
||||||
const currentWorkspaceId = useCurrentWorkspaceId();
|
const currentWorkspaceId = useCurrentWorkspaceId()!;
|
||||||
|
const mutation = useMonitorUpsert();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<MonitorInfoEditor
|
<MonitorInfoEditor
|
||||||
onSave={(value) => {
|
onSave={async (value) => {
|
||||||
// console.log(value);
|
await mutation.mutateAsync({
|
||||||
|
...value,
|
||||||
|
workspaceId: currentWorkspaceId,
|
||||||
|
});
|
||||||
|
navigate('/monitor', { replace: true });
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useParams } from 'react-router';
|
import { useNavigate, useParams } from 'react-router';
|
||||||
|
import { useMonitorUpsert } from '../../api/model/monitor';
|
||||||
import { trpc } from '../../api/trpc';
|
import { trpc } from '../../api/trpc';
|
||||||
import { ErrorTip } from '../../components/ErrorTip';
|
import { ErrorTip } from '../../components/ErrorTip';
|
||||||
import { Loading } from '../../components/Loading';
|
import { Loading } from '../../components/Loading';
|
||||||
import { MonitorInfoEditor } from '../../components/modals/monitor/MonitorInfoEditor';
|
import {
|
||||||
|
MonitorInfoEditor,
|
||||||
|
MonitorInfoEditorValues,
|
||||||
|
} from '../../components/modals/monitor/MonitorInfoEditor';
|
||||||
import { useCurrentWorkspaceId } from '../../store/user';
|
import { useCurrentWorkspaceId } from '../../store/user';
|
||||||
|
|
||||||
export const MonitorEdit: React.FC = React.memo(() => {
|
export const MonitorEdit: React.FC = React.memo(() => {
|
||||||
@ -11,8 +15,10 @@ export const MonitorEdit: React.FC = React.memo(() => {
|
|||||||
const currentWorkspaceId = useCurrentWorkspaceId();
|
const currentWorkspaceId = useCurrentWorkspaceId();
|
||||||
const { data: monitor, isLoading } = trpc.monitor.get.useQuery({
|
const { data: monitor, isLoading } = trpc.monitor.get.useQuery({
|
||||||
id: monitorId!,
|
id: monitorId!,
|
||||||
workspaceId: currentWorkspaceId!,
|
workspaceId: currentWorkspaceId,
|
||||||
});
|
});
|
||||||
|
const mutation = useMonitorUpsert();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
@ -25,9 +31,13 @@ export const MonitorEdit: React.FC = React.memo(() => {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<MonitorInfoEditor
|
<MonitorInfoEditor
|
||||||
initialValues={monitor}
|
initialValues={monitor as MonitorInfoEditorValues}
|
||||||
onSave={(value) => {
|
onSave={async (value) => {
|
||||||
console.log(value);
|
await mutation.mutateAsync({
|
||||||
|
...value,
|
||||||
|
workspaceId: currentWorkspaceId,
|
||||||
|
});
|
||||||
|
navigate('/monitor', { replace: true });
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -32,5 +32,9 @@ export function useCurrentWorkspaceId() {
|
|||||||
(state) => state.info?.currentWorkspace?.id
|
(state) => state.info?.currentWorkspace?.id
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!currentWorkspaceId) {
|
||||||
|
throw new Error('No Workspace Id');
|
||||||
|
}
|
||||||
|
|
||||||
return currentWorkspaceId;
|
return currentWorkspaceId;
|
||||||
}
|
}
|
||||||
|
@ -107,11 +107,17 @@ class MonitorRunner {
|
|||||||
|
|
||||||
this.timer = setTimeout(() => {
|
this.timer = setTimeout(() => {
|
||||||
run();
|
run();
|
||||||
}, interval);
|
}, interval * 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
const value = await provider.run(monitor);
|
let value = 0;
|
||||||
|
try {
|
||||||
|
value = await provider.run(monitor);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
value = -1;
|
||||||
|
}
|
||||||
|
|
||||||
// check event update
|
// check event update
|
||||||
if (value < 0 && currentStatus === 'UP') {
|
if (value < 0 && currentStatus === 'UP') {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { MonitorProvider } from './type';
|
import { MonitorProvider } from './type';
|
||||||
import pingUtils from 'ping';
|
import pingUtils from 'ping';
|
||||||
|
import os from 'os';
|
||||||
|
|
||||||
export const ping: MonitorProvider<{
|
export const ping: MonitorProvider<{
|
||||||
hostname: string;
|
hostname: string;
|
||||||
@ -22,13 +23,14 @@ export const ping: MonitorProvider<{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const isWindows = /^win/.test(process.platform);
|
const isWindows = /^win/.test(process.platform);
|
||||||
|
const deadline = ['linux', 'darwin'].includes(os.platform()) ? 10 : undefined;
|
||||||
|
|
||||||
function pingAction(hostname: string, packetSize = 56) {
|
function pingAction(hostname: string, packetSize = 56) {
|
||||||
return new Promise<number | 'unknown'>((resolve, reject) => {
|
return new Promise<number | 'unknown'>((resolve, reject) => {
|
||||||
pingUtils.promise
|
pingUtils.promise
|
||||||
.probe(hostname, {
|
.probe(hostname, {
|
||||||
min_reply: 1,
|
min_reply: 1,
|
||||||
deadline: 10,
|
deadline,
|
||||||
packetSize,
|
packetSize,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user