opencode-system-metrics-tui 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tui.js +31 -11
- package/package.json +1 -1
package/dist/tui.js
CHANGED
|
@@ -40,6 +40,7 @@ import { execFile } from "node:child_process";
|
|
|
40
40
|
import { promisify } from "node:util";
|
|
41
41
|
import si from "systeminformation";
|
|
42
42
|
var execFileAsync = promisify(execFile);
|
|
43
|
+
var WINDOWS_NETWORK_TIMEOUT_MS = 1500;
|
|
43
44
|
function parseMonitorTemperature(value) {
|
|
44
45
|
if (value === undefined)
|
|
45
46
|
return;
|
|
@@ -77,7 +78,7 @@ async function readWindowsNetworkSample() {
|
|
|
77
78
|
"-NonInteractive",
|
|
78
79
|
"-Command",
|
|
79
80
|
"Get-NetAdapterStatistics | Select-Object Name,ReceivedBytes,SentBytes | ConvertTo-Json -Compress"
|
|
80
|
-
], { windowsHide: true, timeout:
|
|
81
|
+
], { windowsHide: true, timeout: WINDOWS_NETWORK_TIMEOUT_MS, maxBuffer: 1024 * 1024 });
|
|
81
82
|
return parseWindowsNetworkOutput(stdout);
|
|
82
83
|
}
|
|
83
84
|
var previousNetwork;
|
|
@@ -114,6 +115,7 @@ async function withTimeout(promise, timeoutMs) {
|
|
|
114
115
|
}
|
|
115
116
|
var SLOW_METRIC_CACHE_MS = 1e4;
|
|
116
117
|
var TEMPERATURE_CACHE_MS = 1e4;
|
|
118
|
+
var OPTIONAL_SOURCE_BACKOFF_MS = 30000;
|
|
117
119
|
var isWindows = process.platform === "win32";
|
|
118
120
|
var NETWORK_CACHE_MS = 2000;
|
|
119
121
|
function selectGpuMetrics(graphics) {
|
|
@@ -154,16 +156,23 @@ function selectGpuMetrics(graphics) {
|
|
|
154
156
|
}
|
|
155
157
|
return result;
|
|
156
158
|
}
|
|
157
|
-
function readCachedMetric(state, reader, cacheMs = SLOW_METRIC_CACHE_MS, now = Date.now()) {
|
|
159
|
+
function readCachedMetric(state, reader, cacheMs = SLOW_METRIC_CACHE_MS, now = Date.now(), unavailableCacheMs = cacheMs) {
|
|
158
160
|
if (state.inFlight !== undefined)
|
|
159
161
|
return state.inFlight;
|
|
162
|
+
if (state.lastFailureAt !== undefined && now - state.lastFailureAt < unavailableCacheMs) {
|
|
163
|
+
return Promise.resolve(state.value);
|
|
164
|
+
}
|
|
160
165
|
if (now - state.lastAttemptAt < cacheMs)
|
|
161
166
|
return Promise.resolve(state.value);
|
|
162
167
|
state.lastAttemptAt = now;
|
|
163
168
|
state.inFlight = reader().then((value) => {
|
|
164
169
|
state.value = value;
|
|
170
|
+
state.lastFailureAt = undefined;
|
|
165
171
|
return value;
|
|
166
|
-
}).catch(() =>
|
|
172
|
+
}).catch(() => {
|
|
173
|
+
state.lastFailureAt = now;
|
|
174
|
+
return state.value;
|
|
175
|
+
}).finally(() => {
|
|
167
176
|
state.inFlight = undefined;
|
|
168
177
|
});
|
|
169
178
|
return state.inFlight;
|
|
@@ -173,6 +182,11 @@ var cachedCpuTemperature = {
|
|
|
173
182
|
lastAttemptAt: 0,
|
|
174
183
|
inFlight: undefined
|
|
175
184
|
};
|
|
185
|
+
var cachedLibreHardwareMonitorTemperature = {
|
|
186
|
+
value: undefined,
|
|
187
|
+
lastAttemptAt: 0,
|
|
188
|
+
inFlight: undefined
|
|
189
|
+
};
|
|
176
190
|
var cachedGraphics = {
|
|
177
191
|
value: undefined,
|
|
178
192
|
lastAttemptAt: 0,
|
|
@@ -200,15 +214,21 @@ async function readWindowsNetworkMetrics() {
|
|
|
200
214
|
return result;
|
|
201
215
|
}
|
|
202
216
|
async function readWindowsCpuTemperature() {
|
|
203
|
-
let value;
|
|
204
217
|
try {
|
|
205
|
-
|
|
218
|
+
const native = await si.cpuTemperature();
|
|
219
|
+
const value = finite(native.main) ?? finite(native.max) ?? undefined;
|
|
220
|
+
if (value !== undefined)
|
|
221
|
+
return { main: value };
|
|
206
222
|
} catch {}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
223
|
+
const now = Date.now();
|
|
224
|
+
const value = await readCachedMetric(cachedLibreHardwareMonitorTemperature, async () => {
|
|
225
|
+
const temperature = await readLibreHardwareMonitorTemperature();
|
|
226
|
+
if (temperature === undefined)
|
|
227
|
+
throw new Error("LibreHardwareMonitor temperature unavailable");
|
|
228
|
+
return temperature;
|
|
229
|
+
}, TEMPERATURE_CACHE_MS, now, OPTIONAL_SOURCE_BACKOFF_MS);
|
|
230
|
+
if (cachedLibreHardwareMonitorTemperature.lastFailureAt !== undefined && now - cachedLibreHardwareMonitorTemperature.lastFailureAt < OPTIONAL_SOURCE_BACKOFF_MS) {
|
|
231
|
+
return;
|
|
212
232
|
}
|
|
213
233
|
return value === undefined ? undefined : { main: value };
|
|
214
234
|
}
|
|
@@ -220,7 +240,7 @@ async function readMetrics() {
|
|
|
220
240
|
const [temperature, graphics, network] = await Promise.allSettled([
|
|
221
241
|
withTimeout(readCachedMetric(cachedCpuTemperature, isWindows ? readWindowsCpuTemperature : () => si.cpuTemperature(), TEMPERATURE_CACHE_MS), 2500),
|
|
222
242
|
withTimeout(readCachedMetric(cachedGraphics, () => si.graphics(), SLOW_METRIC_CACHE_MS), 2500),
|
|
223
|
-
withTimeout(readCachedMetric(cachedNetwork, isWindows ? readWindowsNetworkMetrics : () => si.networkStats("*"), isWindows ? NETWORK_CACHE_MS : 2000), 1500)
|
|
243
|
+
withTimeout(readCachedMetric(cachedNetwork, isWindows ? readWindowsNetworkMetrics : () => si.networkStats("*"), isWindows ? NETWORK_CACHE_MS : 2000, Date.now(), OPTIONAL_SOURCE_BACKOFF_MS), 1500)
|
|
224
244
|
]);
|
|
225
245
|
const cpuPercent = load.status === "fulfilled" ? finite(load.value.currentLoad) : null;
|
|
226
246
|
const memoryTotalBytes = memory.status === "fulfilled" ? finite(memory.value.total) : null;
|