import Icon from '@ant-design/icons' import * as echarts from 'echarts/core' import { useUpdatedEffect } from '@/util/hooks' import { getTimesBetweenTwoTimes } from '@/util/datetime' import { r_sys_statistics_online } from '@/services/system' import FlexBox from '@/components/common/FlexBox' import { getTooltipTimeFormatter, lineEChartsBaseOption } from '@/pages/System/Statistics/shared' import { CommonCard } from '@/pages/System/Statistics' const OnlineInfo = () => { const onlineInfoDivRef = useRef(null) const onlineInfoEChartsRef = useRef(null) const [isLoading, setIsLoading] = useState(false) const [currentOnlineCount, setCurrentOnlineCount] = useState(-1) const [scope, setScope] = useState('WEAK') useUpdatedEffect(() => { const chartResizeObserver = new ResizeObserver(() => { onlineInfoEChartsRef.current?.resize() }) onlineInfoDivRef.current && chartResizeObserver.observe(onlineInfoDivRef.current) return () => { onlineInfoDivRef.current && chartResizeObserver.unobserve(onlineInfoDivRef.current) } }, [isLoading]) useUpdatedEffect(() => { getOnlineInfo() }, []) const handleOnScopeChange = (value: string) => { setScope(value) getOnlineInfo(value) } const handleOnRefresh = () => { getOnlineInfo() } const getOnlineInfo = (_scope: string = scope) => { if (isLoading) { return } setIsLoading(true) setCurrentOnlineCount(-1) void r_sys_statistics_online({ scope: _scope }).then((res) => { const response = res.data if (response.success) { const data = response.data if (data) { setIsLoading(false) setCurrentOnlineCount(data.current) setTimeout(() => { const dataList = getTimesBetweenTwoTimes( data.history[0].time, data.history[data.history.length - 1].time, 'minute' ).map((time) => [ time, data.history.find( (value) => value.time.substring(0, 16) === time.substring(0, 16) )?.record ?? 0 ]) onlineInfoEChartsRef.current = echarts.init( onlineInfoDivRef.current, null, { renderer: 'svg' } ) onlineInfoEChartsRef.current?.setOption({ ...lineEChartsBaseOption, tooltip: { ...lineEChartsBaseOption.tooltip, formatter: getTooltipTimeFormatter('yyyy-MM-DD HH:mm') }, xAxis: { ...lineEChartsBaseOption.xAxis }, series: [ { name: '在线人数', type: 'line', smooth: true, symbol: 'none', areaStyle: {}, data: dataList } ] }) }) } } }) } return ( 在线用户 {currentOnlineCount === -1 ? '获取中...' : `当前 ${currentOnlineCount}`} } loading={isLoading} expand={ <> 今天 最近7天 最近30天 最近3个月 最近12个月 最近2年 最近3年 最近5年 全部 } >
) } export default OnlineInfo