1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import * as Uebersicht from "uebersicht";
import * as DataWidget from "./data-widget.jsx";
import * as DataWidgetLoader from "./data-widget-loader.jsx";
import Graph from "./graph.jsx";
import * as Icons from "../icons/icons.jsx";
import useWidgetRefresh from "../../hooks/use-widget-refresh.js";
import useServerSocket from "../../hooks/use-server-socket.js";
import { useSimpleBarContext } from "../simple-bar-context.jsx";
import * as Utils from "../../utils.js";
export { gpuStyles as styles } from "../../styles/components/data/gpu.js";
const { React } = Uebersicht;
const DEFAULT_REFRESH_FREQUENCY = 2000;
const GRAPH_LENGTH = 40;
/**
* GPU Widget component
* @returns {JSX.Element|null} The GPU widget component
*/
export const Widget = React.memo(() => {
const { displayIndex, settings } = useSimpleBarContext();
const { widgets, gpuWidgetOptions } = settings;
const { gpuWidget } = widgets;
const {
refreshFrequency,
showOnDisplay,
displayAsGraph,
gpuMacmonBinaryPath,
showIcon,
} = gpuWidgetOptions;
const isDisabled = React.useRef(false);
// Calculate the refresh frequency for the widget
const refresh = React.useMemo(
() =>
Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY),
[refreshFrequency],
);
// Determine if the widget should be visible
const visible =
Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && gpuWidget;
const [graph, setGraph] = React.useState([]);
const [state, setState] = React.useState();
const [loading, setLoading] = React.useState(visible);
/**
* Reset the widget state
*/
const resetWidget = () => {
setState(undefined);
setLoading(false);
setGraph([]);
};
/**
* Fetch GPU data and update the widget state
*/
const getGpu = React.useCallback(async () => {
if (!visible) return;
try {
const result = await Utils.cachedRun(
`${gpuMacmonBinaryPath} pipe -s 1`,
refresh,
);
if (!visible || isDisabled.current) {
return;
}
const json = JSON.parse(result);
const { gpu_usage } = json;
const formattedUsage = { usage: Math.round(gpu_usage[1] * 100) };
setState(formattedUsage);
if (displayAsGraph) {
Utils.addToGraphHistory(formattedUsage, setGraph, GRAPH_LENGTH);
}
setLoading(false);
} catch {
setTimeout(getGpu, 1000);
}
}, [displayAsGraph, gpuMacmonBinaryPath, visible, refresh]);
// Update the disabled state based on visibility
React.useEffect(() => {
isDisabled.current = !visible;
}, [visible]);
// Use server socket to fetch GPU data
useServerSocket("gpu", visible, getGpu, resetWidget, setLoading);
// Use widget refresh hook to periodically refresh the widget
useWidgetRefresh(visible, getGpu, refresh);
if (loading) return <DataWidgetLoader.Widget className="cpu" />;
if (!state) return null;
const { usage } = state;
if (displayAsGraph) {
return (
<DataWidget.Widget classes="gpu gpu--graph" disableSlider>
<Graph
className="gpu__graph"
caption={{
usage: {
value: `${usage}%`,
icon: showIcon ? Icons.CPU : null,
color: "var(--cyan)",
},
}}
values={graph}
maxLength={GRAPH_LENGTH}
maxValue={100}
/>
</DataWidget.Widget>
);
}
return (
<DataWidget.Widget classes="cpu" Icon={showIcon ? Icons.CPU : null}>
<span className="cpu__usage">{usage}%</span>
</DataWidget.Widget>
);
});
Widget.displayName = "Gpu";
|