feat(monitors): 接入统一面板数据与按需进程采样

This commit is contained in:
2026-11-02 22:58:33 +08:00
parent 7df2bbc7c1
commit 604086bbbb
14 changed files with 391 additions and 32 deletions
+21 -2
View File
@@ -6,6 +6,9 @@ final class MetricsStore {
private(set) var snapshot = MetricsSnapshot()
private(set) var history = MetricHistory()
private(set) var isPaused = false
private(set) var isPanelVisible = false
@ObservationIgnored private var generation = 0
@ObservationIgnored private var visibilityGeneration = 0
@ObservationIgnored private var task: Task<Void, Never>?
@ObservationIgnored private let sampler = SystemSampler()
@@ -14,11 +17,16 @@ final class MetricsStore {
func start() {
guard task == nil else { return }
isPaused = false
generation += 1
let currentGeneration = generation
task = Task { [weak self, sampler] in
await sampler.reset()
await sampler.setProcessVisible(self?.isPanelVisible == true)
while !Task.isCancelled {
let next = await sampler.sample()
guard !Task.isCancelled else { return }
let visibleGeneration = self?.visibilityGeneration
var next = await sampler.sample()
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
if self?.isPanelVisible != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
self?.snapshot = next
self?.history.append(next)
SamplingDiagnostics.record(next)
@@ -31,12 +39,23 @@ final class MetricsStore {
func togglePause() {
if isPaused { start() }
else {
generation += 1
task?.cancel()
task = nil
Task { await sampler.setProcessVisible(false) }
isPaused = true
SamplingDiagnostics.record(snapshot, event: "pause")
}
}
func setPanelVisible(_ visible: Bool) {
guard isPanelVisible != visible else { return }
visibilityGeneration += 1
isPanelVisible = visible
if !visible { snapshot.processes = nil }
let active = visible && !isPaused
Task { await sampler.setProcessVisible(active) }
}
deinit { task?.cancel() }
}
@@ -14,6 +14,10 @@ nonisolated enum SamplingDiagnostics {
]
payload["cpu"] = snapshot.cpu?.usage
payload["cores"] = snapshot.cpu?.cores.count
payload["gpuUsage"] = snapshot.gpuUsage
payload["gpuCoreCount"] = snapshot.gpu?.coreCount
payload["processEnumerated"] = snapshot.processes?.enumerated
payload["processReadable"] = snapshot.processes?.readable
payload["memoryUsed"] = snapshot.memory?.used
payload["memoryTotal"] = snapshot.memory?.total
payload["swapUsed"] = snapshot.memory?.swapUsed