jan/web/app/_hooks/useGetSystemResources.ts
Louis 5fc1ba7067
feature: @janhq/plugin-core module & plugins update (#321)
* @janhq/plugin-core module

* refactor web to use exported services from module

* refactor data-plugin to provide DAL & move model logics to model management plugin

* model-management in TS

* add ci auto package, increate version, and publish to npm repository

* chore: storage operations

* chore: hybrid data-plugin esm & cjs module

* chore: PouchDB Driver

* chore: documentation

---------

Co-authored-by: Hien To <hien@jan.ai>
Co-authored-by: Service Account <service@jan.ai>
2023-10-14 15:59:28 +07:00

39 lines
1.0 KiB
TypeScript

import { useEffect, useState } from "react";
import { executeSerial } from "../../../electron/core/plugin-manager/execution/extension-manager";
import { SystemMonitoringService } from "@janhq/plugin-core";
export default function useGetSystemResources() {
const [ram, setRam] = useState<number>(0);
const [cpu, setCPU] = useState<number>(0);
const getSystemResources = async () => {
const resourceInfor = await executeSerial(
SystemMonitoringService.GetResourcesInfo
);
const currentLoadInfor = await executeSerial(
SystemMonitoringService.GetCurrentLoad
);
const ram =
(resourceInfor?.mem?.active ?? 0) / (resourceInfor?.mem?.total ?? 1);
setRam(Math.round(ram * 100));
setCPU(Math.round(currentLoadInfor?.currentLoad ?? 0));
};
useEffect(() => {
getSystemResources();
// Fetch interval - every 3s
const intervalId = setInterval(() => {
getSystemResources();
}, 3000);
// clean up
return () => clearInterval(intervalId);
}, []);
return {
ram,
cpu,
};
}