2023-11-15 22:09:10 +08:00
|
|
|
import { Button, Card, Space, Spin } from 'antd';
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import React, { useState } from 'react';
|
2023-10-28 16:01:11 +08:00
|
|
|
import {
|
|
|
|
defaultErrorHandler,
|
|
|
|
defaultSuccessHandler,
|
|
|
|
trpc,
|
|
|
|
} from '../../api/trpc';
|
2023-10-10 00:09:39 +08:00
|
|
|
import { useCurrentWorkspaceId } from '../../store/user';
|
|
|
|
import { Loading } from '../Loading';
|
2023-12-05 20:07:36 +08:00
|
|
|
import { getMonitorLink } from './provider';
|
2023-10-10 00:09:39 +08:00
|
|
|
import { NotFoundTip } from '../NotFoundTip';
|
|
|
|
import { MonitorInfo as MonitorInfoType } from '../../../types';
|
2023-10-10 19:47:05 +08:00
|
|
|
import { MonitorHealthBar } from './MonitorHealthBar';
|
2023-11-15 22:09:10 +08:00
|
|
|
import { last } from 'lodash-es';
|
2023-10-15 00:51:03 +08:00
|
|
|
import { ColorTag } from '../ColorTag';
|
2023-10-19 23:22:39 +08:00
|
|
|
import { useNavigate } from 'react-router';
|
2023-10-25 00:29:47 +08:00
|
|
|
import { MonitorEventList } from './MonitorEventList';
|
2023-10-28 16:01:11 +08:00
|
|
|
import { useEvent } from '../../hooks/useEvent';
|
2023-11-15 22:09:10 +08:00
|
|
|
import { MonitorDataMetrics } from './MonitorDataMetrics';
|
|
|
|
import { MonitorDataChart } from './MonitorDataChart';
|
2023-10-10 00:09:39 +08:00
|
|
|
|
|
|
|
interface MonitorInfoProps {
|
|
|
|
monitorId: string;
|
|
|
|
}
|
|
|
|
export const MonitorInfo: React.FC<MonitorInfoProps> = React.memo((props) => {
|
|
|
|
const workspaceId = useCurrentWorkspaceId();
|
|
|
|
const { monitorId } = props;
|
2023-10-13 00:55:41 +08:00
|
|
|
const [currectResponse, setCurrentResponse] = useState(0);
|
2023-10-19 23:22:39 +08:00
|
|
|
const navigate = useNavigate();
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-19 23:22:39 +08:00
|
|
|
const {
|
|
|
|
data: monitorInfo,
|
|
|
|
isInitialLoading,
|
|
|
|
isLoading,
|
|
|
|
} = trpc.monitor.get.useQuery({
|
2023-10-10 00:09:39 +08:00
|
|
|
workspaceId,
|
2023-10-25 00:17:18 +08:00
|
|
|
monitorId,
|
2023-10-10 00:09:39 +08:00
|
|
|
});
|
2023-10-28 16:01:11 +08:00
|
|
|
const changeActiveMutation = trpc.monitor.changeActive.useMutation({
|
|
|
|
onSuccess: defaultSuccessHandler,
|
|
|
|
onError: defaultErrorHandler,
|
|
|
|
});
|
|
|
|
|
|
|
|
const trpcUtils = trpc.useContext();
|
|
|
|
|
|
|
|
const handleStart = useEvent(async () => {
|
|
|
|
await changeActiveMutation.mutateAsync({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
active: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
trpcUtils.monitor.get.refetch({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
});
|
|
|
|
trpcUtils.monitor.events.refetch({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleStop = useEvent(async () => {
|
|
|
|
await changeActiveMutation.mutateAsync({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
active: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
trpcUtils.monitor.get.refetch({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
});
|
|
|
|
trpcUtils.monitor.events.refetch({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
});
|
|
|
|
});
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-19 23:22:39 +08:00
|
|
|
if (isInitialLoading) {
|
2023-10-10 00:09:39 +08:00
|
|
|
return <Loading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!monitorInfo) {
|
|
|
|
return <NotFoundTip />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-10-25 00:29:47 +08:00
|
|
|
<div className="w-full h-full overflow-auto">
|
|
|
|
<Spin spinning={isLoading}>
|
2023-10-28 16:01:11 +08:00
|
|
|
<Space className="w-full" direction="vertical">
|
2023-10-25 00:29:47 +08:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<Space direction="vertical">
|
2023-10-28 16:01:11 +08:00
|
|
|
<div className="text-2xl flex items-center gap-2">
|
|
|
|
<span>{monitorInfo.name}</span>
|
|
|
|
{monitorInfo.active === false && (
|
|
|
|
<div className="bg-red-500 rounded-full px-2 py-0.5 text-white text-xs">
|
|
|
|
Stoped
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-25 00:29:47 +08:00
|
|
|
<div>
|
|
|
|
<ColorTag label={monitorInfo.type} />
|
|
|
|
<span>
|
|
|
|
{getMonitorLink(monitorInfo as any as MonitorInfoType)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</Space>
|
|
|
|
|
|
|
|
<div className="text-black text-opacity-75">
|
|
|
|
Monitored for {dayjs().diff(dayjs(monitorInfo.createdAt), 'days')}{' '}
|
|
|
|
days
|
2023-10-10 19:47:05 +08:00
|
|
|
</div>
|
2023-10-25 00:29:47 +08:00
|
|
|
</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-28 16:01:11 +08:00
|
|
|
<div className="flex gap-2">
|
2023-10-25 00:29:47 +08:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={() => {
|
|
|
|
navigate(`/monitor/${monitorInfo.id}/edit`);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</Button>
|
2023-10-28 16:01:11 +08:00
|
|
|
|
|
|
|
{monitorInfo.active ? (
|
|
|
|
<Button
|
|
|
|
loading={changeActiveMutation.isLoading}
|
|
|
|
onClick={handleStop}
|
|
|
|
>
|
|
|
|
Stop
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
loading={changeActiveMutation.isLoading}
|
|
|
|
onClick={handleStart}
|
|
|
|
>
|
|
|
|
Start
|
|
|
|
</Button>
|
|
|
|
)}
|
2023-10-10 19:47:05 +08:00
|
|
|
</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-25 00:29:47 +08:00
|
|
|
<Card>
|
|
|
|
<MonitorHealthBar
|
|
|
|
monitorId={monitorId}
|
|
|
|
count={40}
|
|
|
|
size="large"
|
|
|
|
showCurrentStatus={true}
|
|
|
|
onBeatsItemUpdate={(items) => {
|
|
|
|
setCurrentResponse(last(items)?.value ?? 0);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Card>
|
2023-10-19 23:22:39 +08:00
|
|
|
|
2023-10-25 00:29:47 +08:00
|
|
|
<Card>
|
|
|
|
<MonitorDataMetrics
|
|
|
|
monitorId={monitorId}
|
|
|
|
monitorType={monitorInfo.type}
|
|
|
|
currectResponse={currectResponse}
|
|
|
|
/>
|
|
|
|
</Card>
|
2023-10-13 00:55:41 +08:00
|
|
|
|
2023-10-25 00:29:47 +08:00
|
|
|
<Card>
|
|
|
|
<MonitorDataChart monitorId={monitorId} />
|
|
|
|
</Card>
|
2023-10-10 19:47:05 +08:00
|
|
|
|
2023-10-25 00:29:47 +08:00
|
|
|
<MonitorEventList monitorId={monitorId} />
|
|
|
|
</Space>
|
|
|
|
</Spin>
|
|
|
|
</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
MonitorInfo.displayName = 'MonitorInfo';
|