-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.ts
More file actions
111 lines (93 loc) · 2.95 KB
/
index.ts
File metadata and controls
111 lines (93 loc) · 2.95 KB
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
import { bind, Variable } from 'astal';
import GTop from 'gi://GTop';
import { FunctionPoller } from 'src/lib/poller/FunctionPoller';
import { CpuServiceCtor } from './types';
/**
* Service for monitoring CPU usage percentage
*/
class CpuUsageService {
private _updateFrequency: Variable<number>;
private _previousCpuData = new GTop.glibtop_cpu();
private _cpuPoller: FunctionPoller<number, []>;
private _isInitialized = false;
private _cpu = Variable(0);
constructor({ frequency }: CpuServiceCtor = {}) {
this._updateFrequency = frequency ?? Variable(2000);
GTop.glibtop_get_cpu(this._previousCpuData);
this._calculateUsage = this._calculateUsage.bind(this);
this._cpuPoller = new FunctionPoller<number, []>(
this.cpu,
[bind(this._updateFrequency)],
bind(this._updateFrequency),
this._calculateUsage,
);
}
/**
* Manually refreshes the CPU usage reading
*/
public refresh(): void {
this._cpu.set(this._calculateUsage());
}
/**
* Gets the CPU usage percentage variable
*
* @returns Variable containing CPU usage percentage (0-100)
*/
public get cpu(): Variable<number> {
return this._cpu;
}
/**
* Calculates the current CPU usage percentage based on CPU time deltas
*
* @returns Current CPU usage percentage
*/
private _calculateUsage(): number {
const currentCpuData = new GTop.glibtop_cpu();
GTop.glibtop_get_cpu(currentCpuData);
const totalDiff = currentCpuData.total - this._previousCpuData.total;
const idleDiff = currentCpuData.idle - this._previousCpuData.idle;
const iowaitDiff = currentCpuData.iowait - this._previousCpuData.iowait;
const inactiveTime = idleDiff + iowaitDiff;
const cpuUsagePercentage = totalDiff > 0 ? ((totalDiff - inactiveTime) / totalDiff) * 100 : 0;
this._previousCpuData = currentCpuData;
return cpuUsagePercentage;
}
/**
* Updates the polling frequency for CPU usage monitoring
*
* @param timerInMs - New polling interval in milliseconds
*/
public updateTimer(timerInMs: number): void {
this._updateFrequency.set(timerInMs);
}
/**
* Initializes the CPU usage monitoring service
*/
public initialize(): void {
if (!this._isInitialized) {
this._cpuPoller.initialize();
this._isInitialized = true;
}
}
/**
* Stops the CPU usage polling
*/
public stopPoller(): void {
this._cpuPoller.stop();
}
/**
* Starts the CPU usage polling
*/
public startPoller(): void {
this._cpuPoller.start();
}
/**
* Cleans up resources and stops monitoring
*/
public destroy(): void {
this._cpuPoller.stop();
this._cpu.drop();
this._updateFrequency.drop();
}
}
export default CpuUsageService;