jan/web/hooks/useGetSystemResources.ts
Louis 96dba2690d feat: class-based plugin manager
chore: add facades

refactor: core module export

refactor: inference plugin - deprecate function registering (#537)

* refactor: revamp inference plugin as class - deprecate function registering

* refactor: monitoring plugin - deprecate service registering (#538)

refactor: revamp inference plugin as class - deprecate function registering

chore: update import

refactor: plugin revamp - model management

chore: update build steps and remove experimental plugins

refactor: remove pluggable electron

chore: add sorting for conversations

chore: build plugins for testing

chore: consistent plugin directory name

chore: docs

chore: fix CI

chore: update conversation prefix
2023-11-06 13:46:01 +07:00

48 lines
1.4 KiB
TypeScript

import { useEffect, useState } from 'react'
import { PluginType } from '@janhq/core'
import { useSetAtom } from 'jotai'
import { totalRamAtom } from '@helpers/atoms/SystemBar.atom'
import { pluginManager } from '@plugin/PluginManager'
import { MonitoringPlugin } from '@janhq/core/lib/plugins'
export default function useGetSystemResources() {
const [ram, setRam] = useState<number>(0)
const [cpu, setCPU] = useState<number>(0)
const setTotalRam = useSetAtom(totalRamAtom)
const getSystemResources = async () => {
if (!pluginManager.get<MonitoringPlugin>(PluginType.SystemMonitoring)) {
return
}
const monitoring = pluginManager.get<MonitoringPlugin>(
PluginType.SystemMonitoring
)
const resourceInfor = await monitoring?.getResourcesInfo()
const currentLoadInfor = await monitoring?.getCurrentLoad()
const ram =
(resourceInfor?.mem?.active ?? 0) / (resourceInfor?.mem?.total ?? 1)
if (resourceInfor?.mem?.total) setTotalRam(resourceInfor.mem.total)
setRam(Math.round(ram * 100))
setCPU(Math.round(currentLoadInfor?.currentLoad ?? 0))
}
useEffect(() => {
getSystemResources()
// Fetch interval - every 3s
const intervalId = setInterval(() => {
getSystemResources()
}, 5000)
// clean up
return () => clearInterval(intervalId)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return {
ram,
cpu,
}
}