chore: cluster system load service

This commit is contained in:
jialin
2026-01-12 12:24:34 +08:00
parent 2e5b075d1b
commit 05ad296b43
7 changed files with 105 additions and 9 deletions
@@ -0,0 +1,73 @@
import { createAxiosToken } from '@/hooks/use-chunk-request';
import { useRequest } from 'ahooks';
import { CancelTokenSource } from 'axios';
import { useEffect, useRef, useState } from 'react';
import { queryClusterDetail } from '../apis';
export const useClusterSystemLoad = () => {
const [systemLoad, setSystemLoad] = useState<{
current: {
cpu: number;
ram: number;
gpu: number;
vram: number;
};
history: Record<string, { timestamp: number; value: number }[]>;
}>({
current: {
cpu: 0,
ram: 0,
gpu: 0,
vram: 0
},
history: {}
});
const axiosTokenRef = useRef<CancelTokenSource | null>(null);
const {
run: fetchClusterSystemLoad,
loading,
cancel
} = useRequest(
async (params) => {
axiosTokenRef.current?.cancel();
axiosTokenRef.current = createAxiosToken();
return await queryClusterDetail(params, {
token: axiosTokenRef.current.token
});
},
{
manual: true,
onSuccess: (response) => {
setSystemLoad({
current: response.system_load?.current,
history: response.system_load?.history
});
},
onError: () => {
setSystemLoad({
current: {
cpu: 0,
ram: 0,
gpu: 0,
vram: 0
},
history: {}
});
}
}
);
useEffect(() => {
return () => {
cancel();
axiosTokenRef.current?.cancel();
};
}, []);
return {
systemLoad,
loading,
fetchClusterSystemLoad
};
};