Files
Netcatty/electron/bridges/sshBridge/sharedStats.cjs
T
27f26d8e69 feat(monitor): 优化磁盘IO展示与系统分区告警 [4030] (#4031)
* feat(monitor): 优化磁盘IO展示与系统分区告警 [4030]

* fix(monitor): 修复磁盘监控评审问题 [4030]

* fix(monitor): align Partitions bars with Memory; hide bare / label

Label-above-bar for partition list; single-root `/` hides the label and
centers the bar like Memory/CPU (unchanged). Multi-disk keeps top align +
system alerts. Disk IO adds a spacer row to track usage labels.

Review crops under .github/review/ for PR discussion (drop before merge OK).

* fix(monitor): keep bare-/ align; multi-disk labels stay left

Address Chris density feedback: keep the single root label hidden and vertically centered with Memory, and restore side-by-side labels for multiple disks. Remove the Disk I/O spacer and update the review crops.

* chore(monitor): drop PR #4031 discussion review crops

---------

Co-authored-by: wendeng.dong <wendeng.dong@sailuntire.com>
Co-authored-by: 陈大猫 <16399091+binaricat@users.noreply.github.com>
2026-10-27 23:54:58 -07:00

186 lines
6.9 KiB
JavaScript

"use strict";
// One in-memory sampler per physical authenticated transport. No persistence/logging.
const { withCachedMonitorGpu, cancelMonitorGpuProbe, monitorGpuUpdatedAt } = require("./monitorGpuStats.cjs");
const { buildEndpointKey } = require("../sshConnectionPool.cjs");
let states = new WeakMap();
const identities = new Map();
const MAX_SAMPLERS = 512;
function prune() {
const now = Date.now();
for (const [key, history] of identities) {
history.points = history.points.filter((p) => now - p.at <= 30 * 60000);
if (now - history.at > 30 * 60000) identities.delete(key);
}
}
const cleanupTimer = setInterval(prune, 60000);
cleanupTimer.unref?.();
function stateFor(conn, endpoint) {
const key = endpoint ? buildEndpointKey(endpoint) : null;
let state = states.get(conn);
const history = (key ? identities.get(key) : null) || state?.history || {
latest: null,
at: 0,
points: [],
latestFull: false,
fullResult: null,
fullAt: 0,
sequence: 0,
};
if (!state) {
state = {
context: { conn: null },
pending: null,
controller: null,
pendingFull: false,
history,
};
states.set(conn, state);
} else if (key && !state.pending && !state.history.latest) {
// A panel may read a snapshot before its first sample supplies the endpoint.
// Attach that empty runtime to the existing history without moving live work.
state.history = history;
}
// Share measurements/history by identity, but in-flight requests, abort
// controllers and CPU/network baselines belong to the physical transport.
// A reconnect must never join (or cancel) work on an older connection.
if (key) {
identities.delete(key);
identities.set(key, state.history);
if (identities.size > MAX_SAMPLERS) {
const oldKey = identities.keys().next().value;
identities.delete(oldKey);
}
}
return state;
}
function appendPoint(state, result, now, freshAfter = 0) {
if (
!result.success ||
!result.stats ||
(now - (state.history.points.at(-1)?.at ?? 0) < 1000 && (state.history.points.at(-1)?.at ?? 0) > freshAfter)
)
return;
const s = result.stats;
state.history.points.push({
at: now,
cpu: s.cpu,
memory:
s.memTotal > 0 && Number.isFinite(s.memUsed)
? (s.memUsed / s.memTotal) * 100
: null,
disk: s.diskPercent,
rx: s.networkReady ? s.netRxSpeed : null,
tx: s.networkReady ? s.netTxSpeed : null,
});
state.history.points = state.history.points
.filter((p) => now - p.at <= 30 * 60000)
.slice(-1800);
}
async function sampleSharedStats(session, collect, now = Date.now()) {
const conn = session.conn || session.moshStatsConn || session.etStatsConn;
if (!conn) {
const result = await collect(session);
const companion = session.moshStatsConn || session.etStatsConn;
if (companion && result.success) {
const state = stateFor(companion);
state.context.prevDiskIo = session.prevDiskIo;
state.context.prevPartitionIo = session.prevPartitionIo;
state.context.prevNetStats = session.prevNetStats;
state.context.prevCpuStats = session.prevCpuStats;
state.history.latest = result;
state.history.at = Date.now();
state.history.latestFull = true;
state.history.fullResult = result;
state.history.fullAt = state.history.at;
}
return result;
}
const state = stateFor(conn, session._reuseEndpoint);
const full = !session._monitorStats;
const cacheMs = full ? 4000 : Math.min(4000, session._monitorIntervalMs ?? 5000);
if (full && state.history.fullResult?.success && now - state.history.fullAt < 4000)
return state.history.fullResult;
if (state.pending) {
if (full) state.terminalJoined = true;
if (!full || state.pendingFull) {
const result = await state.pending;
// A reset can join a terminal query that began before the new boundary.
if (!full && state.history.at > session._monitorFreshAfter)
appendPoint(state, result, state.history.at, session._monitorFreshAfter);
return result;
}
// A terminal needs process and GPU fields omitted from a monitor-only query.
// Finish the shared lightweight query, then upgrade once for all full readers.
await state.pending;
return sampleSharedStats(session, collect);
}
if (
state.history.latest?.success &&
now - state.history.at < cacheMs &&
(full || state.history.at > (session._monitorFreshAfter ?? 0)) &&
(!full || state.history.latestFull)
)
return state.history.latest;
const collectFull = full;
// Keep transport capabilities on the collection context; baseline is independent of a terminal.
const companion = !session.conn;
Object.assign(state.context, {
conn: companion ? null : conn,
moshStatsConn: session.moshStatsConn,
etStatsConn: session.etStatsConn,
type: session.type,
singleChannelSsh: session.singleChannelSsh,
hostname: session.hostname,
_monitorStats: !collectFull,
});
// Starting a replacement retires older samples even if the new attempt fails.
const sequence = ++state.history.sequence;
state.controller = new AbortController();
state.terminalJoined = full;
state.pendingFull = collectFull;
state.context.statsSignal = state.controller.signal;
state.pending = Promise.resolve(collect(state.context))
.then((result) => {
if (!state.controller.signal.aborted && result.success && sequence === state.history.sequence) {
state.history.latest = result;
state.history.latestFull = collectFull;
state.history.at = Date.now();
if (collectFull) {
state.history.fullResult = result;
state.history.fullAt = state.history.at;
}
appendPoint(state, result, state.history.at, session._monitorFreshAfter);
}
return result;
})
.finally(() => {
state.pending = null;
state.controller = null;
// The cache retains measurements and baselines, never a live connection.
state.context.conn = null;
state.context.moshStatsConn = null;
state.context.etStatsConn = null;
});
return state.pending;
}
function readSharedStats(conn) {
const state = stateFor(conn);
if (state.history.points[0] && Date.now() - state.history.points[0].at > 30 * 60000)
state.history.points = state.history.points.filter((p) => Date.now() - p.at <= 30 * 60000);
return { result: state.history.latestFull ? state.history.latest : withCachedMonitorGpu(conn, state.history.latest), at: state.history.at, statsAt: Math.max(state.history.at, monitorGpuUpdatedAt(conn)), points: state.history.points };
}
function cancelSharedStats(conn) {
cancelMonitorGpuProbe(conn);
const state = states.get(conn);
if (state && !state.terminalJoined) state.controller?.abort();
}
module.exports = {
sampleSharedStats,
readSharedStats,
cancelSharedStats,
resetSharedStatsForTests() {
identities.clear();
states = new WeakMap();
},
};