1- const { exec } = require ( 'child_process' ) ;
2- const os = require ( 'os' ) ;
3- const fs = require ( 'fs' ) ;
1+ const { ipcRenderer } = require ( 'electron' ) ;
42
53let cpuUsageHistory = [ ] ;
64let gpuUsageHistory = [ ] ;
7- const maxHistory = 30 ; // For charts
8-
9- function updateCpuTemp ( ) {
10- exec ( 'sensors | grep -A 0 "Tctl:" | cut -c15-22' , ( error , stdout ) => {
11- let temp = 'N/A' ;
12- if ( ! error && stdout ) {
13- temp = parseFloat ( stdout . trim ( ) ) . toFixed ( 1 ) ;
14- } else {
15- try {
16- const raw = fs . readFileSync ( '/sys/class/thermal/thermal_zone0/temp' , 'utf8' ) ;
17- temp = ( parseInt ( raw ) / 1000 ) . toFixed ( 1 ) ;
18- } catch ( e ) { }
19- }
20- document . getElementById ( 'cpu-temp' ) . textContent = temp ;
21- } ) ;
22- }
23-
24- function updateCpuUsage ( ) {
25- exec ( 'top -bn1 | grep "Cpu(s)" | sed "s/.*, *\\([0-9.]*\\)%* id.*/\\1/" | awk \'{print 100 - $1}\'' , ( error , stdout ) => {
26- let usage = 'N/A' ;
27- if ( ! error && stdout ) {
28- usage = parseFloat ( stdout . trim ( ) ) . toFixed ( 1 ) ;
29- cpuUsageHistory . push ( usage ) ;
30- if ( cpuUsageHistory . length > maxHistory ) cpuUsageHistory . shift ( ) ;
31- drawCpuChart ( ) ;
32- }
33- document . getElementById ( 'cpu-usage' ) . textContent = usage ;
34- } ) ;
35- }
36-
37- function updateCpuFreq ( ) {
38- exec ( 'cat /proc/cpuinfo | grep "cpu MHz" | head -1 | awk \'{print $4}\'' , ( error , stdout ) => {
39- let freq = 'N/A' ;
40- if ( ! error && stdout ) {
41- freq = parseFloat ( stdout . trim ( ) ) . toFixed ( 0 ) ;
42- }
43- document . getElementById ( 'cpu-freq' ) . textContent = freq ;
44- } ) ;
45- }
46-
47- function updateCpuFan ( ) {
48- exec ( 'sensors | grep "fan1:" | awk \'{print $2}\'' , ( error , stdout ) => {
49- let fan = 'N/A' ;
50- if ( ! error && stdout ) {
51- fan = stdout . trim ( ) ;
52- }
53- document . getElementById ( 'cpu-fan' ) . textContent = fan ;
54- } ) ;
55- }
56-
57- function updateGpuTemp ( ) {
58- exec ( 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader' , ( error , stdout ) => {
59- let temp = 'N/A' ;
60- if ( ! error && stdout ) {
61- temp = parseFloat ( stdout . trim ( ) ) . toFixed ( 1 ) ;
62- } else {
63- exec ( 'rocm-smi --showtemp | grep "GPU Temp" | awk \'{print $4}\'' , ( err , out ) => {
64- if ( ! err && out ) temp = parseFloat ( out . trim ( ) ) . toFixed ( 1 ) ;
65- } ) ;
66- }
67- document . getElementById ( 'gpu-temp' ) . textContent = temp ;
68- } ) ;
69- }
70-
71- function updateGpuUsage ( ) {
72- exec ( 'nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader' , ( error , stdout ) => {
73- let usage = 'N/A' ;
74- if ( ! error && stdout ) {
75- usage = parseFloat ( stdout . trim ( ) ) . toFixed ( 1 ) ;
76- gpuUsageHistory . push ( usage ) ;
77- if ( gpuUsageHistory . length > maxHistory ) gpuUsageHistory . shift ( ) ;
78- drawGpuChart ( ) ;
79- } else {
80- exec ( 'rocm-smi --showutil | grep "GPU use" | awk \'{print $4}\'' , ( err , out ) => {
81- if ( ! err && out ) usage = parseFloat ( out . trim ( ) ) . toFixed ( 1 ) ;
82- } ) ;
83- }
84- document . getElementById ( 'gpu-usage' ) . textContent = usage ;
85- } ) ;
86- }
87-
88- function updateGpuFan ( ) {
89- exec ( 'nvidia-smi --query-gpu=fan.speed --format=csv,noheader' , ( error , stdout ) => {
90- let fan = 'N/A' ;
91- if ( ! error && stdout ) {
92- fan = parseFloat ( stdout . trim ( ) ) . toFixed ( 1 ) ;
93- } else {
94- exec ( 'rocm-smi --showfan | grep "Fan Level" | awk \'{print $4}\'' , ( err , out ) => {
95- if ( ! err && out ) fan = parseFloat ( out . trim ( ) ) . toFixed ( 1 ) ;
96- } ) ;
97- }
98- document . getElementById ( 'gpu-fan' ) . textContent = fan ;
99- } ) ;
100- }
101-
102- function updateGpuMem ( ) {
103- exec ( 'nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader' , ( error , stdout ) => {
104- let mem = 'N/A' ;
105- if ( ! error && stdout ) {
106- const [ used , total ] = stdout . trim ( ) . split ( ',' ) . map ( s => s . trim ( ) . replace ( ' MiB' , '' ) ) ;
107- mem = `${ used } /${ total } ` ;
108- } else {
109- exec ( 'rocm-smi --showmeminfo vram | grep "Used" | awk \'{print $4 "/" $5}\'' , ( err , out ) => {
110- if ( ! err && out ) mem = out . trim ( ) ;
111- } ) ;
112- }
113- document . getElementById ( 'gpu-mem' ) . textContent = mem ;
114- } ) ;
115- }
116-
117- function updateRamUsage ( ) {
118- const totalMem = os . totalmem ( ) ;
119- const freeMem = os . freemem ( ) ;
120- const usedMem = totalMem - freeMem ;
121- const usage = ( ( usedMem / totalMem ) * 100 ) . toFixed ( 1 ) ;
122- document . getElementById ( 'ram-usage' ) . textContent = usage ;
123- }
124-
125- function updateDiskUsage ( ) {
126- exec ( 'df -h / | tail -1 | awk \'{print $5}\' | sed \'s/%//\'' , ( error , stdout ) => {
127- let usage = 'N/A' ;
128- if ( ! error && stdout ) {
129- usage = stdout . trim ( ) ;
130- }
131- document . getElementById ( 'disk-usage' ) . textContent = usage ;
132- } ) ;
133- }
134-
135- function updateBatteryLevel ( ) {
136- try {
137- const batteryPath = '/sys/class/power_supply/BAT0/capacity' ;
138- if ( fs . existsSync ( batteryPath ) ) {
139- const level = fs . readFileSync ( batteryPath , 'utf8' ) . trim ( ) ;
140- document . getElementById ( 'battery-level' ) . textContent = level ;
141- } else {
142- document . getElementById ( 'battery-level' ) . textContent = 'N/A' ;
143- }
144- } catch ( e ) {
145- document . getElementById ( 'battery-level' ) . textContent = 'N/A' ;
5+ const maxHistory = 30 ;
6+
7+ ipcRenderer . on ( 'update-stats' , ( event , stats ) => {
8+ document . getElementById ( 'cpu-temp' ) . textContent = stats . cpuTemp || 'N/A' ;
9+ document . getElementById ( 'cpu-usage' ) . textContent = stats . cpuUsage || 'N/A' ;
10+ document . getElementById ( 'cpu-freq' ) . textContent = stats . cpuFreq || 'N/A' ;
11+ document . getElementById ( 'cpu-fan' ) . textContent = stats . cpuFan || 'N/A' ;
12+ document . getElementById ( 'gpu-temp' ) . textContent = stats . gpuTemp || 'N/A' ;
13+ document . getElementById ( 'gpu-usage' ) . textContent = stats . gpuUsage || 'N/A' ;
14+ document . getElementById ( 'gpu-fan' ) . textContent = stats . gpuFan || 'N/A' ;
15+ document . getElementById ( 'gpu-mem' ) . textContent = stats . gpuMem || 'N/A' ;
16+ document . getElementById ( 'ram-usage' ) . textContent = stats . ramUsage || 'N/A' ;
17+ document . getElementById ( 'disk-usage' ) . textContent = stats . diskUsage || 'N/A' ;
18+ document . getElementById ( 'battery-level' ) . textContent = stats . batteryLevel || 'N/A' ;
19+ document . getElementById ( 'uptime' ) . textContent = stats . uptime || 'N/A' ;
20+ document . getElementById ( 'net-download' ) . textContent = stats . netDownload || 'N/A' ;
21+ document . getElementById ( 'net-upload' ) . textContent = stats . netUpload || 'N/A' ;
22+ document . getElementById ( 'fps' ) . textContent = stats . fps || 'N/A' ;
23+
24+ if ( stats . cpuUsage !== 'N/A' ) {
25+ cpuUsageHistory . push ( parseFloat ( stats . cpuUsage ) ) ;
26+ if ( cpuUsageHistory . length > maxHistory ) cpuUsageHistory . shift ( ) ;
27+ drawCpuChart ( ) ;
14628 }
147- }
148-
149- function updateUptime ( ) {
150- const uptimeSeconds = os . uptime ( ) ;
151- const days = Math . floor ( uptimeSeconds / ( 3600 * 24 ) ) ;
152- const hours = Math . floor ( ( uptimeSeconds % ( 3600 * 24 ) ) / 3600 ) ;
153- const minutes = Math . floor ( ( uptimeSeconds % 3600 ) / 60 ) ;
154- const seconds = Math . floor ( uptimeSeconds % 60 ) ;
155- document . getElementById ( 'uptime' ) . textContent = `${ days } d ${ hours } h ${ minutes } m ${ seconds } s` ;
156- }
157-
158- function updateNetwork ( ) {
159- // Simple approximation; for real, use speedtest-cli or similar
160- document . getElementById ( 'net-download' ) . textContent = ( Math . random ( ) * 100 ) . toFixed ( 1 ) ;
161- document . getElementById ( 'net-upload' ) . textContent = ( Math . random ( ) * 20 ) . toFixed ( 1 ) ;
162- }
16329
164- function updateFps ( ) {
165- // Placeholder; in real app, integrate with game or use requestAnimationFrame for approx
166- document . getElementById ( 'fps' ) . textContent = Math . floor ( Math . random ( ) * 60 + 30 ) ;
167- }
30+ if ( stats . gpuUsage !== 'N/A' ) {
31+ gpuUsageHistory . push ( parseFloat ( stats . gpuUsage ) ) ;
32+ if ( gpuUsageHistory . length > maxHistory ) gpuUsageHistory . shift ( ) ;
33+ drawGpuChart ( ) ;
34+ }
35+ } ) ;
16836
16937function drawCpuChart ( ) {
17038 const ctx = document . getElementById ( 'cpu-chart' ) . getContext ( '2d' ) ;
@@ -190,37 +58,3 @@ function drawLineChart(ctx, data, color) {
19058 } ) ;
19159 ctx . stroke ( ) ;
19260}
193-
194- // Update every 1 second for smoother updates
195- setInterval ( ( ) => {
196- updateCpuTemp ( ) ;
197- updateCpuUsage ( ) ;
198- updateCpuFreq ( ) ;
199- updateCpuFan ( ) ;
200- updateGpuTemp ( ) ;
201- updateGpuUsage ( ) ;
202- updateGpuFan ( ) ;
203- updateGpuMem ( ) ;
204- updateRamUsage ( ) ;
205- updateDiskUsage ( ) ;
206- updateBatteryLevel ( ) ;
207- updateUptime ( ) ;
208- updateNetwork ( ) ;
209- updateFps ( ) ;
210- } , 1000 ) ;
211-
212- // Initial update
213- updateCpuTemp ( ) ;
214- updateCpuUsage ( ) ;
215- updateCpuFreq ( ) ;
216- updateCpuFan ( ) ;
217- updateGpuTemp ( ) ;
218- updateGpuUsage ( ) ;
219- updateGpuFan ( ) ;
220- updateGpuMem ( ) ;
221- updateRamUsage ( ) ;
222- updateDiskUsage ( ) ;
223- updateBatteryLevel ( ) ;
224- updateUptime ( ) ;
225- updateNetwork ( ) ;
226- updateFps ( ) ;
0 commit comments