jan/web/hooks/useHardwareManagement.ts
Faisal Amir daa7c0ca21
feat: better hardware setting (#4471)
* feat: better hardware setting

* chore: update layout

* feat: better hardware setting

* chore: fix title section

* chore: added hardware engine management

* chore: integrate gpus and enable set gpu activate

* chore: update calculate ram and vram

* chore: update calulate vram and ram used

* fix: set active gpus

* chore: fix progress bar spacing

* chore: always update cache vram gpu

* chore: update cpu usage percentage

* chore: fix type usage cpu

* chore: update ram cpus usage getsystemmonitor from new api harware engine management system

* test: update test case data using hardware management extension

* chore: resolve conflict lock json

* chore: cleanup app services

* chore: update type OperationSystemInfo

* chore: update app service

* chore: show list gpus on system monitor

* chore: remove monitoring extension

* chore: update test case app service

* chore: remove unused hooks useGpusSetting

* chore: remove monitor from shource index

* chore: fix test core

* chore: update gpu and cpu info on engine management ext

* chore: fix app service test

* chore: update test appService include cpu info

* chore: filter gpus show or hide on system monitor based activated gpu

* chore: remove unused run_mode

* chore: remove tensort

* chore: update check gpu run_mode

* chore: handle undefined gpus

* chore: cleanup PR

* chore: cleanup process node error

* chore: fix type
2025-02-03 22:01:08 +07:00

100 lines
2.4 KiB
TypeScript

import { useMemo } from 'react'
import { ExtensionTypeEnum, HardwareManagementExtension } from '@janhq/core'
import { useSetAtom } from 'jotai'
import useSWR from 'swr'
import { extensionManager } from '@/extension/ExtensionManager'
import {
cpuUsageAtom,
ramUtilitizedAtom,
totalRamAtom,
usedRamAtom,
} from '@/helpers/atoms/SystemBar.atom'
// fetcher function
async function fetchExtensionData<T>(
extension: HardwareManagementExtension | null,
method: (extension: HardwareManagementExtension) => Promise<T>
): Promise<T> {
if (!extension) {
throw new Error('Extension not found')
}
return method(extension)
}
const getExtension = () =>
extensionManager.get<HardwareManagementExtension>(
ExtensionTypeEnum.Hardware
) ?? null
/**
* @returns A Promise that resolves to an object of list engines.
*/
export function useGetHardwareInfo() {
const setCpuUsage = useSetAtom(cpuUsageAtom)
const setUsedRam = useSetAtom(usedRamAtom)
const setTotalRam = useSetAtom(totalRamAtom)
const setRamUtilitized = useSetAtom(ramUtilitizedAtom)
const extension = useMemo(
() =>
extensionManager.get<HardwareManagementExtension>(
ExtensionTypeEnum.Hardware
) ?? null,
[]
)
const {
data: hardware,
error,
mutate,
} = useSWR(
extension ? 'hardware' : null,
() => fetchExtensionData(extension, (ext) => ext.getHardware()),
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
refreshInterval: 2000,
}
)
const usedMemory =
Number(hardware?.ram.total) - Number(hardware?.ram.available)
if (hardware?.ram?.total && hardware?.ram?.available)
setUsedRam(Number(usedMemory))
if (hardware?.ram?.total) setTotalRam(hardware.ram.total)
const ramUtilitized =
((Number(usedMemory) ?? 0) / (hardware?.ram.total ?? 1)) * 100
setRamUtilitized(Math.round(ramUtilitized))
setCpuUsage(Math.round(hardware?.cpu.usage ?? 0))
return { hardware, error, mutate }
}
/**
* set gpus activate
* @returns A Promise that resolves set gpus activate.
*/
export const setActiveGpus = async (data: { gpus: number[] }) => {
const extension = getExtension()
if (!extension) {
throw new Error('Extension is not available')
}
try {
const response = await extension.setAvtiveGpu(data)
return response
} catch (error) {
console.error('Failed to install engine variant:', error)
throw error
}
}