import { Select } from 'antd'; import dayjs, { Dayjs } from 'dayjs'; import { get, takeRight, uniqBy } from 'lodash-es'; import React, { useState, useMemo } from 'react'; import { useSocketSubscribeList } from '../../api/socketio'; import { trpc } from '../../api/trpc'; import { useCurrentWorkspaceId } from '../../store/user'; import { getMonitorProvider, getProviderDisplay } from './provider'; import { useTranslation } from '@i18next-toolkit/react'; import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, } from '../ui/chart'; import { Area, AreaChart, CartesianGrid, Customized, Rectangle, XAxis, YAxis, } from 'recharts'; import { useTheme } from '@/hooks/useTheme'; import { CustomizedErrorArea } from './CustomizedErrorArea'; const chartConfig = { value: { label: Result, }, } satisfies ChartConfig; export const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo( (props) => { const { t } = useTranslation(); const workspaceId = useCurrentWorkspaceId(); const { monitorId } = props; const [rangeType, setRangeType] = useState('recent'); const { colors } = useTheme(); const subscribedDataList = useSocketSubscribeList( 'onMonitorReceiveNewData', { filter: (data) => { return data.monitorId === props.monitorId; }, } ); 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: monitorInfo } = trpc.monitor.get.useQuery({ workspaceId, monitorId, }); const { data: _recentData = [] } = trpc.monitor.recentData.useQuery({ workspaceId, monitorId, take: 40, }); const { data: _data = [] } = trpc.monitor.data.useQuery({ workspaceId, monitorId, startAt: range[0].valueOf(), endAt: range[1].valueOf(), }); const providerInfo = getMonitorProvider(monitorInfo?.type ?? ''); const { data } = useMemo(() => { let fetchedData = rangeType === 'recent' ? _recentData : _data; const data = takeRight( uniqBy([...fetchedData, ...subscribedDataList], 'createdAt'), fetchedData.length ).map((d, i, arr) => { const value = d.value > 0 ? d.value : null; const time = dayjs(d.createdAt).valueOf(); return { value, time, }; }); return { data }; }, [_recentData, _data, subscribedDataList]); const isTrendingMode = monitorInfo?.trendingMode ?? false; // if true, y axis not start from 0 return (
dayjs(date).format(rangeType === '1w' ? 'MM-DD HH:mm' : 'HH:mm') } /> dayjs(get(payload, [0, 'payload', 'time'])).format( 'YYYY-MM-DD HH:mm:ss' ) } formatter={(value, defaultText, item, index, payload) => { if (typeof value !== 'number') { return defaultText; } const { name, text } = getProviderDisplay( Number(value), providerInfo ); return (
{name}: {text}
); }} content={} />
); } ); MonitorDataChart.displayName = 'MonitorDataChart';