Skip to content

Commit

Permalink
chore: refactor watch system resource hook (janhq#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo authored Feb 16, 2024
1 parent 42da19a commit 63cffca
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 38 deletions.
24 changes: 22 additions & 2 deletions web/containers/Layout/BottomBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect } from 'react'

import {
Badge,
Button,
Expand Down Expand Up @@ -31,6 +33,11 @@ import { useMainViewState } from '@/hooks/useMainViewState'

import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import {
cpuUsageAtom,
gpusAtom,
ramUtilitizedAtom,
} from '@/helpers/atoms/SystemBar.atom'

const menuLinks = [
{
Expand All @@ -47,9 +54,12 @@ const menuLinks = [

const BottomBar = () => {
const { activeModel, stateModel } = useActiveModel()
const { ram, cpu, gpus } = useGetSystemResources()
const { watch, stopWatching } = useGetSystemResources()
const progress = useAtomValue(appDownloadProgress)
const downloadedModels = useAtomValue(downloadedModelsAtom)
const gpus = useAtomValue(gpusAtom)
const cpu = useAtomValue(cpuUsageAtom)
const ramUtilitized = useAtomValue(ramUtilitizedAtom)

const { setMainViewState } = useMainViewState()
const downloadStates = useAtomValue(modelDownloadStateAtom)
Expand All @@ -67,6 +77,16 @@ const BottomBar = () => {
return sum
}

useEffect(() => {
// Watch for resource update
watch()

return () => {
stopWatching()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return (
<div className="fixed bottom-0 left-16 z-20 flex h-12 w-[calc(100%-64px)] items-center justify-between border-t border-border bg-background/80 px-3">
<div className="flex flex-shrink-0 items-center gap-x-2">
Expand Down Expand Up @@ -127,7 +147,7 @@ const BottomBar = () => {
<div className="flex items-center gap-x-3">
<div className="flex items-center gap-x-2">
<SystemItem name="CPU:" value={`${cpu}%`} />
<SystemItem name="Mem:" value={`${ram}%`} />
<SystemItem name="Mem:" value={`${ramUtilitized}%`} />
</div>
{gpus.length > 0 && (
<Tooltip>
Expand Down
3 changes: 3 additions & 0 deletions web/containers/Providers/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Fragment, ReactNode } from 'react'

import useAssistants from '@/hooks/useAssistants'
import useGetSystemResources from '@/hooks/useGetSystemResources'
import useModels from '@/hooks/useModels'
import useThreads from '@/hooks/useThreads'

Expand All @@ -14,6 +15,8 @@ const DataLoader: React.FC<Props> = ({ children }) => {
useModels()
useThreads()
useAssistants()
useGetSystemResources()
console.debug('Load Data...')

return <Fragment>{children}</Fragment>
}
Expand Down
2 changes: 0 additions & 2 deletions web/containers/Providers/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,9 @@ export default function EventHandler({ children }: { children: ReactNode }) {
}

useEffect(() => {
console.log('Registering events')
if (window.core?.events) {
events.on(MessageEvent.OnMessageResponse, onNewMessageResponse)
events.on(MessageEvent.OnMessageUpdate, onMessageResponseUpdate)

events.on(ModelEvent.OnModelReady, onModelReady)
events.on(ModelEvent.OnModelFail, onModelInitFailed)
events.on(ModelEvent.OnModelStopped, onModelStopped)
Expand Down
4 changes: 2 additions & 2 deletions web/containers/Providers/EventListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const EventListenerWrapper = ({ children }: PropsWithChildren) => {
)

useEffect(() => {
console.log('EventListenerWrapper: registering event listeners...')
console.debug('EventListenerWrapper: registering event listeners...')

events.on(DownloadEvent.onFileDownloadUpdate, onFileDownloadUpdate)
events.on(DownloadEvent.onFileDownloadError, onFileDownloadError)
events.on(DownloadEvent.onFileDownloadSuccess, onFileDownloadSuccess)

return () => {
console.log('EventListenerWrapper: unregistering event listeners...')
console.debug('EventListenerWrapper: unregistering event listeners...')
events.off(DownloadEvent.onFileDownloadUpdate, onFileDownloadUpdate)
events.off(DownloadEvent.onFileDownloadError, onFileDownloadError)
events.off(DownloadEvent.onFileDownloadSuccess, onFileDownloadSuccess)
Expand Down
4 changes: 3 additions & 1 deletion web/helpers/atoms/SystemBar.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { atom } from 'jotai'

export const totalRamAtom = atom<number>(0)
export const usedRamAtom = atom<number>(0)
export const availableRamAtom = atom<number>(0)

export const cpuUsageAtom = atom<number>(0)
export const ramUtilitizedAtom = atom<number>(0)

export const gpusAtom = atom<Record<string, never>[]>([])

export const nvidiaTotalVramAtom = atom<number>(0)
83 changes: 52 additions & 31 deletions web/hooks/useGetSystemResources.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'

import { ExtensionTypeEnum, MonitoringExtension } from '@janhq/core'

import { useSetAtom } from 'jotai'

import { extensionManager } from '@/extension/ExtensionManager'
import {
availableRamAtom,
cpuUsageAtom,
totalRamAtom,
usedRamAtom,
nvidiaTotalVramAtom,
gpusAtom,
ramUtilitizedAtom,
} from '@/helpers/atoms/SystemBar.atom'

export default function useGetSystemResources() {
const [ram, setRam] = useState<number>(0)
const [cpu, setCPU] = useState<number>(0)
const [intervalId, setIntervalId] = useState<
NodeJS.Timeout | number | undefined
>(undefined)

const [gpus, setGPUs] = useState<Record<string, never>[]>([])
const setTotalRam = useSetAtom(totalRamAtom)
const setGpus = useSetAtom(gpusAtom)
const setUsedRam = useSetAtom(usedRamAtom)
const setAvailableRam = useSetAtom(availableRamAtom)
const setCpuUsage = useSetAtom(cpuUsageAtom)
const setTotalNvidiaVram = useSetAtom(nvidiaTotalVramAtom)
const setRamUtilitized = useSetAtom(ramUtilitizedAtom)

const getSystemResources = async () => {
const getSystemResources = useCallback(async () => {
if (
!extensionManager.get<MonitoringExtension>(
ExtensionTypeEnum.SystemMonitoring
Expand All @@ -38,23 +40,20 @@ export default function useGetSystemResources() {
const resourceInfor = await monitoring?.getResourcesInfo()
const currentLoadInfor = await monitoring?.getCurrentLoad()

const ram =
(resourceInfor?.mem?.usedMemory ?? 0) /
(resourceInfor?.mem?.totalMemory ?? 1)
if (resourceInfor?.mem?.usedMemory) setUsedRam(resourceInfor.mem.usedMemory)
if (resourceInfor?.mem?.totalMemory)
setTotalRam(resourceInfor.mem.totalMemory)

setRam(Math.round(ram * 100))
if (resourceInfor.mem.totalMemory && resourceInfor.mem.usedMemory)
setAvailableRam(
resourceInfor.mem.totalMemory - resourceInfor.mem.usedMemory
)
setCPU(Math.round(currentLoadInfor?.cpu?.usage ?? 0))
const ramUtilitized =
((resourceInfor?.mem?.usedMemory ?? 0) /
(resourceInfor?.mem?.totalMemory ?? 1)) *
100
setRamUtilitized(Math.round(ramUtilitized))

setCpuUsage(Math.round(currentLoadInfor?.cpu?.usage ?? 0))

const gpus = currentLoadInfor?.gpu ?? []
setGPUs(gpus)
setGpus(gpus)

let totalNvidiaVram = 0
if (gpus.length > 0) {
Expand All @@ -65,27 +64,49 @@ export default function useGetSystemResources() {
)
}
setTotalNvidiaVram(totalNvidiaVram)
}
}, [
setUsedRam,
setTotalRam,
setRamUtilitized,
setCpuUsage,
setGpus,
setTotalNvidiaVram,
])

useEffect(() => {
const watch = () => {
getSystemResources()

// Fetch interval - every 2s
// TODO: Will we really need this?
// There is a possibility that this will be removed and replaced by the process event hook?
const intervalId = setInterval(() => {
const itv = setInterval(() => {
getSystemResources()
}, 5000)
}, 2000)
setIntervalId(itv)
}
const stopWatching = useCallback(() => {
if (intervalId) clearInterval(intervalId)
}, [intervalId])

// clean up interval
return () => clearInterval(intervalId)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
getSystemResources()
// Component did unmount
// Stop watching if any
return () => {
stopWatching()
}
}, [getSystemResources, stopWatching])

return {
totalRamAtom,
ram,
cpu,
gpus,
/**
* Fetch resource informations once
*/
getSystemResources,
/**
* Fetch & watch for resource update
*/
watch,
/**
* Stop watching
*/
stopWatching,
}
}

0 comments on commit 63cffca

Please sign in to comment.