2023-10-19 23:22:39 +08:00
|
|
|
import { Button, Card, Select, Space, Spin } from 'antd';
|
2023-10-10 00:09:39 +08:00
|
|
|
import dayjs, { Dayjs } from 'dayjs';
|
|
|
|
import React, { useMemo, useState } from 'react';
|
|
|
|
import { trpc } from '../../api/trpc';
|
|
|
|
import { useCurrentWorkspaceId } from '../../store/user';
|
|
|
|
import { Loading } from '../Loading';
|
2023-10-23 21:39:24 +08:00
|
|
|
import { getMonitorLink, getMonitorProvider } from '../modals/monitor/provider';
|
2023-10-10 00:09:39 +08:00
|
|
|
import { NotFoundTip } from '../NotFoundTip';
|
|
|
|
import { MonitorInfo as MonitorInfoType } from '../../../types';
|
|
|
|
import { Area, AreaConfig } from '@ant-design/charts';
|
2023-10-10 19:47:05 +08:00
|
|
|
import { MonitorHealthBar } from './MonitorHealthBar';
|
2023-10-11 01:21:05 +08:00
|
|
|
import { useSocketSubscribeList } from '../../api/socketio';
|
2023-10-13 00:55:41 +08:00
|
|
|
import { last, uniqBy } from 'lodash-es';
|
|
|
|
import { ErrorTip } from '../ErrorTip';
|
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-23 21:39:24 +08:00
|
|
|
import { MonitorStatsBlock } from './MonitorStatsBlock';
|
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,
|
|
|
|
id: monitorId,
|
|
|
|
});
|
|
|
|
|
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-19 23:22:39 +08:00
|
|
|
<Spin spinning={isLoading}>
|
2023-10-10 00:09:39 +08:00
|
|
|
<Space className="w-full" direction="vertical">
|
2023-10-10 19:47:05 +08:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<Space direction="vertical">
|
|
|
|
<div className="text-2xl">{monitorInfo.name}</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-10 19:47:05 +08:00
|
|
|
<div>
|
2023-10-15 00:51:03 +08:00
|
|
|
<ColorTag label={monitorInfo.type} />
|
2023-10-10 19:47:05 +08:00
|
|
|
<span>
|
|
|
|
{getMonitorLink(monitorInfo as any as MonitorInfoType)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</Space>
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-10 19:47:05 +08:00
|
|
|
<div className="text-black text-opacity-75">
|
|
|
|
Monitored for {dayjs().diff(dayjs(monitorInfo.createdAt), 'days')}{' '}
|
|
|
|
days
|
|
|
|
</div>
|
2023-10-10 00:09:39 +08:00
|
|
|
</div>
|
|
|
|
|
2023-10-19 23:22:39 +08:00
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={() => {
|
|
|
|
navigate(`/monitor/${monitorInfo.id}/edit`);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
|
2023-10-10 19:47:05 +08:00
|
|
|
<Card>
|
|
|
|
<MonitorHealthBar
|
|
|
|
monitorId={monitorId}
|
|
|
|
count={40}
|
|
|
|
size="large"
|
|
|
|
showCurrentStatus={true}
|
2023-10-13 00:55:41 +08:00
|
|
|
onBeatsItemUpdate={(items) => {
|
|
|
|
setCurrentResponse(last(items)?.value ?? 0);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
<MonitorDataMetrics
|
|
|
|
monitorId={monitorId}
|
2023-10-23 21:39:24 +08:00
|
|
|
monitorType={monitorInfo.type}
|
2023-10-13 00:55:41 +08:00
|
|
|
currectResponse={currectResponse}
|
2023-10-10 19:47:05 +08:00
|
|
|
/>
|
|
|
|
</Card>
|
|
|
|
|
2023-10-10 00:09:39 +08:00
|
|
|
<Card>
|
|
|
|
<MonitorDataChart monitorId={monitorId} />
|
|
|
|
</Card>
|
|
|
|
</Space>
|
2023-10-19 23:22:39 +08:00
|
|
|
</Spin>
|
2023-10-10 00:09:39 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
MonitorInfo.displayName = 'MonitorInfo';
|
|
|
|
|
2023-10-13 00:55:41 +08:00
|
|
|
export const MonitorDataMetrics: React.FC<{
|
|
|
|
monitorId: string;
|
2023-10-23 21:39:24 +08:00
|
|
|
monitorType: string;
|
2023-10-13 00:55:41 +08:00
|
|
|
currectResponse: number;
|
|
|
|
}> = React.memo((props) => {
|
|
|
|
const workspaceId = useCurrentWorkspaceId();
|
2023-10-23 21:39:24 +08:00
|
|
|
const { monitorId, monitorType, currectResponse } = props;
|
2023-10-13 00:55:41 +08:00
|
|
|
const { data, isLoading } = trpc.monitor.dataMetrics.useQuery({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
});
|
2023-10-23 21:39:24 +08:00
|
|
|
const providerOverview = useMemo(() => {
|
|
|
|
const provider = getMonitorProvider(monitorType);
|
|
|
|
if (!provider || !provider.overview) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{provider.overview.map((Component) => (
|
|
|
|
<Component monitorId={monitorId} />
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}, [monitorType]);
|
2023-10-13 00:55:41 +08:00
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return <Loading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return <ErrorTip />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex justify-between text-center">
|
2023-10-23 21:39:24 +08:00
|
|
|
<MonitorStatsBlock
|
|
|
|
title="Response"
|
|
|
|
desc="(Current)"
|
|
|
|
text={`${currectResponse} ms`}
|
|
|
|
/>
|
|
|
|
<MonitorStatsBlock
|
|
|
|
title="Avg. Response"
|
|
|
|
desc="(24 hour)"
|
|
|
|
text={`${parseFloat(data.recent1DayAvg.toFixed(0))} ms`}
|
|
|
|
/>
|
|
|
|
<MonitorStatsBlock
|
|
|
|
title="Uptime"
|
|
|
|
desc="(24 hour)"
|
|
|
|
text={`${parseFloat(
|
|
|
|
(
|
|
|
|
(data.recent1DayOnlineCount /
|
|
|
|
(data.recent1DayOnlineCount + data.recent1DayOfflineCount)) *
|
|
|
|
100
|
|
|
|
).toFixed(2)
|
|
|
|
)} %`}
|
|
|
|
/>
|
|
|
|
<MonitorStatsBlock
|
|
|
|
title="Uptime"
|
|
|
|
desc="(30 days)"
|
|
|
|
text={`${parseFloat(
|
|
|
|
(
|
|
|
|
(data.recent30DayOnlineCount /
|
|
|
|
(data.recent30DayOnlineCount + data.recent30DayOfflineCount)) *
|
|
|
|
100
|
|
|
|
).toFixed(2)
|
|
|
|
)} %`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{providerOverview}
|
2023-10-13 00:55:41 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
MonitorDataMetrics.displayName = 'MonitorDataMetrics';
|
|
|
|
|
2023-10-10 00:09:39 +08:00
|
|
|
const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
|
|
|
|
(props) => {
|
|
|
|
const workspaceId = useCurrentWorkspaceId();
|
|
|
|
const { monitorId } = props;
|
|
|
|
const [rangeType, setRangeType] = useState('recent');
|
2023-10-11 01:21:05 +08:00
|
|
|
const newDataList = useSocketSubscribeList('onMonitorReceiveNewData', {
|
|
|
|
filter: (data) => {
|
|
|
|
return data.monitorId === props.monitorId;
|
|
|
|
},
|
|
|
|
});
|
2023-10-10 00:09:39 +08:00
|
|
|
|
|
|
|
const range = useMemo((): [Dayjs, Dayjs] => {
|
|
|
|
if (rangeType === '3h') {
|
|
|
|
return [dayjs().subtract(3, 'hour'), dayjs()];
|
|
|
|
}
|
|
|
|
if (rangeType === '6h') {
|
|
|
|
return [dayjs().subtract(6, 'hour'), dayjs()];
|
|
|
|
}
|
|
|
|
if (rangeType === '24h') {
|
|
|
|
return [dayjs().subtract(24, 'hour'), dayjs()];
|
|
|
|
}
|
|
|
|
if (rangeType === '1w') {
|
|
|
|
return [dayjs().subtract(1, 'week'), dayjs()];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [dayjs().subtract(0.5, 'hour'), dayjs()];
|
|
|
|
}, [rangeType]);
|
|
|
|
|
|
|
|
const { data: _data = [] } = trpc.monitor.data.useQuery({
|
|
|
|
workspaceId,
|
|
|
|
monitorId,
|
|
|
|
startAt: range[0].valueOf(),
|
|
|
|
endAt: range[1].valueOf(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const { data, annotations } = useMemo(() => {
|
|
|
|
const annotations: AreaConfig['annotations'] = [];
|
|
|
|
let start: number | null = null;
|
2023-10-11 01:21:05 +08:00
|
|
|
const data = uniqBy([..._data, ...newDataList], 'createdAt').map(
|
|
|
|
(d, i, arr) => {
|
|
|
|
const value = d.value > 0 ? d.value : null;
|
|
|
|
const time = dayjs(d.createdAt).valueOf();
|
|
|
|
|
|
|
|
if (!value && !start && arr[i - 1]) {
|
|
|
|
start = dayjs(arr[i - 1]['createdAt']).valueOf();
|
|
|
|
} else if (value && start) {
|
|
|
|
annotations.push({
|
|
|
|
type: 'region',
|
|
|
|
start: [start, 'min'],
|
|
|
|
end: [time, 'max'],
|
|
|
|
style: {
|
|
|
|
fill: 'red',
|
|
|
|
fillOpacity: 0.25,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
start = null;
|
|
|
|
}
|
2023-10-10 00:09:39 +08:00
|
|
|
|
2023-10-11 01:21:05 +08:00
|
|
|
return {
|
|
|
|
value,
|
|
|
|
time,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2023-10-10 00:09:39 +08:00
|
|
|
|
|
|
|
return { data, annotations };
|
2023-10-11 01:21:05 +08:00
|
|
|
}, [_data, newDataList]);
|
2023-10-10 00:09:39 +08:00
|
|
|
|
|
|
|
const config = useMemo<AreaConfig>(() => {
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
height: 200,
|
|
|
|
xField: 'time',
|
|
|
|
yField: 'value',
|
|
|
|
smooth: true,
|
|
|
|
meta: {
|
|
|
|
time: {
|
|
|
|
formatter(value) {
|
|
|
|
return dayjs(value).format(
|
|
|
|
rangeType === '1w' ? 'MM-DD HH:mm' : 'HH:mm'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
color: 'rgb(34 197 94 / 0.8)',
|
|
|
|
areaStyle: () => {
|
|
|
|
return {
|
|
|
|
fill: 'l(270) 0:rgb(34 197 94 / 0.2) 0.5:rgb(34 197 94 / 0.5) 1:rgb(34 197 94 / 0.8)',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
annotations,
|
|
|
|
tooltip: {
|
|
|
|
title: (title, datum) => {
|
|
|
|
return dayjs(datum.time).format('YYYY-MM-DD HH:mm');
|
|
|
|
},
|
|
|
|
formatter(datum) {
|
|
|
|
return {
|
|
|
|
name: 'usage',
|
|
|
|
value: datum.value ? datum.value + 'ms' : 'null',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, [data, rangeType]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="mb-4 text-right">
|
|
|
|
<Select
|
|
|
|
className="w-20 text-center"
|
|
|
|
size="small"
|
|
|
|
value={rangeType}
|
|
|
|
onChange={(val) => setRangeType(val)}
|
|
|
|
>
|
|
|
|
<Select.Option value="recent">Recent</Select.Option>
|
|
|
|
<Select.Option value="3h">3h</Select.Option>
|
|
|
|
<Select.Option value="6h">6h</Select.Option>
|
|
|
|
<Select.Option value="24h">24h</Select.Option>
|
|
|
|
<Select.Option value="1w">1w</Select.Option>
|
|
|
|
</Select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Area {...config} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
MonitorDataChart.displayName = 'MonitorDataChart';
|