-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from shm11C3/udpate_hardware-service
update: ハードウェア使用率更新処理の修正
- Loading branch information
Showing
6 changed files
with
122 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { atom } from "jotai"; | ||
|
||
export const cpuUsageHistoryAtom = atom<number[]>([]); | ||
export const memoryUsageHistoryAtom = atom<number[]>([]); | ||
export const graphicUsageHistoryAtom = atom<number[]>([]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import type { Settings } from "@/types/settingsType"; | ||
import { atom } from "jotai"; | ||
|
||
export const settingsAtom = atom<Settings | null>(null); | ||
export const settingsAtom = atom<Settings>({ | ||
language: "en", | ||
theme: "light", | ||
display_targets: [], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const chartConfig = { | ||
/** | ||
* グラフの履歴の長さ(秒) | ||
*/ | ||
historyLengthSec: 60, | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
cpuUsageHistoryAtom, | ||
graphicUsageHistoryAtom, | ||
memoryUsageHistoryAtom, | ||
} from "@/atom/chart"; | ||
import { chartConfig } from "@/consts/chart"; | ||
import { | ||
getCpuUsage, | ||
getGpuUsage, | ||
getMemoryUsage, | ||
} from "@/services/hardwareService"; | ||
import type { ChartDataType } from "@/types/chartType"; | ||
import { type PrimitiveAtom, useSetAtom } from "jotai"; | ||
import { useEffect } from "react"; | ||
|
||
type AtomActionMapping = { | ||
atom: PrimitiveAtom<number[]>; | ||
action: () => Promise<number>; | ||
}; | ||
|
||
/** | ||
* ハードウェア使用率の履歴を更新する | ||
*/ | ||
export const useUsageUpdater = (dataType: ChartDataType) => { | ||
const mapping: Record<ChartDataType, AtomActionMapping> = { | ||
cpu: { | ||
atom: cpuUsageHistoryAtom, | ||
action: getCpuUsage, | ||
}, | ||
memory: { | ||
atom: memoryUsageHistoryAtom, | ||
action: getMemoryUsage, | ||
}, | ||
gpu: { | ||
atom: graphicUsageHistoryAtom, | ||
action: getGpuUsage, | ||
}, | ||
}; | ||
|
||
const setHistory = useSetAtom(mapping[dataType].atom); | ||
const getUsage = mapping[dataType].action; | ||
|
||
useEffect(() => { | ||
const intervalId = setInterval(async () => { | ||
const usage = await getUsage(); | ||
setHistory((prev) => { | ||
const newHistory = [...prev, usage]; | ||
|
||
// 履歴保持数に満たない場合は0で埋める | ||
const paddedHistory = Array( | ||
Math.max(chartConfig.historyLengthSec - newHistory.length, 0), | ||
) | ||
.fill(null) | ||
.concat(newHistory); | ||
return paddedHistory.slice(-chartConfig.historyLengthSec); | ||
}); | ||
}, 1000); | ||
|
||
return () => clearInterval(intervalId); | ||
}, [setHistory, getUsage]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import type { ChartDataType } from "./chartType"; | ||
|
||
export type Settings = { | ||
language: string; | ||
theme: "light" | "dark"; | ||
display_targets: "cpu" | "memory" | "gpu"; | ||
display_targets: Array<ChartDataType>; | ||
}; |